Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/raystack/components/data-table/components/ordering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ import {
export interface OrderingProps {
columnList: ColumnData[];
onChange: (columnId: string, order: SortOrdersValues) => void;
value: DataTableSort;
value?: DataTableSort;
}

export function Ordering({ columnList, onChange, value }: OrderingProps) {
function handleColumnChange(columnId: string) {
onChange(columnId, value.order);
onChange(columnId, value?.order ?? SortOrders.ASC);
}

function handleOrderChange() {
if (!value) return;
const newOrder =
value.order === SortOrders.ASC ? SortOrders.DESC : SortOrders.ASC;
onChange(value.name, newOrder);
Expand All @@ -48,7 +49,7 @@ export function Ordering({ columnList, onChange, value }: OrderingProps) {
>
<Select
onValueChange={handleColumnChange}
value={value.name}
value={value?.name}
disabled={columnList.length === 0}
>
<Select.Trigger
Expand All @@ -70,12 +71,12 @@ export function Ordering({ columnList, onChange, value }: OrderingProps) {
size={4}
disabled={columnList.length === 0}
>
{value.order === SortOrders?.ASC ? (
{value?.order === SortOrders?.ASC ? (
<TextAlignTopIcon className={styles['display-popover-sort-icon']} />
) : (
<TextAlignBottomIcon
className={styles['display-popover-sort-icon']}
/>
) : (
<TextAlignTopIcon className={styles['display-popover-sort-icon']} />
)}
Comment on lines +74 to 80

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Confirm SortOrders is a non-nullable const/enum (no `?.` warranted)
fd -e tsx -e ts | xargs rg -nP '\b(SortOrders)\b\s*=' 2>/dev/null
ast-grep run --pattern 'export const SortOrders = $_' --lang typescript $(fd -e ts -e tsx) 2>/dev/null
rg -nP 'SortOrders\s*:' $(fd -e ts -e tsx) 2>/dev/null | head

Repository: raystack/apsara

Length of output: 430


Default state renders the wrong icon and uses unnecessary optional chaining

When value is undefined, the condition value?.order === SortOrders?.ASC evaluates to false, causing the component to render the descending (TextAlignBottomIcon) icon. However, the logical default for a new sort is ascending. Additionally, SortOrders is a non-nullable constant, making the optional chaining (?.) on SortOrders?.ASC redundant.

Update the logic to treat the absent state as ASC and remove the redundant optional chaining:

🔧 Proposed fix
-          {value?.order === SortOrders?.ASC ? (
+          {(value?.order ?? SortOrders.ASC) === SortOrders.ASC ? (
             <TextAlignTopIcon className={styles['display-popover-sort-icon']} />
           ) : (
             <TextAlignBottomIcon
               className={styles['display-popover-sort-icon']}
             />
           )}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{value?.order === SortOrders?.ASC ? (
<TextAlignTopIcon className={styles['display-popover-sort-icon']} />
) : (
<TextAlignBottomIcon
className={styles['display-popover-sort-icon']}
/>
) : (
<TextAlignTopIcon className={styles['display-popover-sort-icon']} />
)}
{(value?.order ?? SortOrders.ASC) === SortOrders.ASC ? (
<TextAlignTopIcon className={styles['display-popover-sort-icon']} />
) : (
<TextAlignBottomIcon
className={styles['display-popover-sort-icon']}
/>
)}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/raystack/components/data-table/components/ordering.tsx` around lines
74 - 80, The sort icon logic in ordering.tsx should treat an undefined value as
ascending instead of falling through to the descending icon. Update the
conditional around value.order in the ordering render so the default branch maps
to SortOrders.ASC, and remove the unnecessary optional chaining on SortOrders
since it is a non-nullable constant. Keep the fix localized to the icon
selection in this component.

</IconButton>
</Flex>
Expand Down
2 changes: 1 addition & 1 deletion packages/raystack/components/data-table/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function DataTableRoot<TData, TValue>({
setTableQuery(prev => ({
...prev,
...defaultTableQuery,
sort: [defaultSort],
sort: defaultSort ? [defaultSort] : [],
group_by: [defaultGroupOption.id]
}));
handleColumnVisibilityChange(initialColumnVisibility);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export type TableContextType<TData, TValue> = {
isLoading?: boolean;
loadMoreData: () => void;
mode: DataTableMode;
defaultSort: DataTableSort;
defaultSort?: DataTableSort;
tableQuery?: InternalQuery;
totalRowCount?: number;
loadingRowCount?: number;
Expand Down
4 changes: 2 additions & 2 deletions packages/raystack/components/data-table/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const isSearchChanged = (oldSearch?: string, newSearch?: string): boolean => {
*/
export const hasActiveQuery = (
tableQuery: InternalQuery,
defaultSort: DataTableSort
defaultSort?: DataTableSort
): boolean => {
const hasFilters =
(tableQuery?.filters && tableQuery.filters.length > 0) || false;
Expand Down Expand Up @@ -345,7 +345,7 @@ export function hasActiveTableFiltering<T>(table: Table<T>): boolean {
}

export function getDefaultTableQuery(
defaultSort: DataTableSort,
defaultSort?: DataTableSort,
oldQuery: DataTableQuery = {}
): InternalQuery {
// Convert DataTableQuery to InternalQuery
Expand Down
Loading