The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(front): narrow TableSortValue to the direction the table implements (#24536)
Félix Malfaitdf6b7dc1ef5b
diff --git a/packages/twenty-front/src/modules/object-core/workflows/hooks/useCoreWorkflows.ts b/packages/twenty-front/src/modules/object-core/workflows/hooks/useCoreWorkflows.tsindex 27fa1d5c6bd..61d31f70327 100644--- a/packages/twenty-front/src/modules/object-core/workflows/hooks/useCoreWorkflows.ts+++ b/packages/twenty-front/src/modules/object-core/workflows/hooks/useCoreWorkflows.ts@@ -17,7 +17,7 @@ export const CORE_WORKFLOWS_PAGE_SIZE = 60;export const CORE_WORKFLOWS_INITIAL_SORT: TableSortValue = {fieldName: 'updatedAt',- orderBy: 'DescNullsLast',+ direction: 'desc',};const ORDER_BY_FIELD_BY_FIELD_NAME: Record<string, CoreWorkflowOrderByField> = {@@ -38,9 +38,10 @@ export const useCoreWorkflows = () => {const orderBy =ORDER_BY_FIELD_BY_FIELD_NAME[sortValue.fieldName] ??CoreWorkflowOrderByField.UPDATED_AT;- const orderByDirection = sortValue.orderBy.startsWith('Asc')- ? CoreWorkflowOrderByDirection.ASC- : CoreWorkflowOrderByDirection.DESC;+ const orderByDirection =+ sortValue.direction === 'asc'+ ? CoreWorkflowOrderByDirection.ASC+ : CoreWorkflowOrderByDirection.DESC;const [isFetchingMore, setIsFetchingMore] = useState(false);diff --git a/packages/twenty-front/src/modules/settings/admin-panel/chats/components/SettingsAdminChatsTable.tsx b/packages/twenty-front/src/modules/settings/admin-panel/chats/components/SettingsAdminChatsTable.tsxindex 07f5e2cf4c0..12919deb604 100644--- a/packages/twenty-front/src/modules/settings/admin-panel/chats/components/SettingsAdminChatsTable.tsx+++ b/packages/twenty-front/src/modules/settings/admin-panel/chats/components/SettingsAdminChatsTable.tsx@@ -44,7 +44,7 @@ export const SettingsAdminChatsTable = ({align="right"initialSort={{fieldName: AdminChatThreadSortField.CREATED_AT,- orderBy: 'DescNullsLast',+ direction: 'desc',}}/></TableRow>diff --git a/packages/twenty-front/src/modules/settings/admin-panel/chats/utils/__tests__/getAdminChatsSortVariables.test.ts b/packages/twenty-front/src/modules/settings/admin-panel/chats/utils/__tests__/getAdminChatsSortVariables.test.tsindex d54d4fb79ad..bc3f106e96a 100644--- a/packages/twenty-front/src/modules/settings/admin-panel/chats/utils/__tests__/getAdminChatsSortVariables.test.ts+++ b/packages/twenty-front/src/modules/settings/admin-panel/chats/utils/__tests__/getAdminChatsSortVariables.test.ts@@ -16,7 +16,7 @@ describe('getAdminChatsSortVariables', () => {expect(getAdminChatsSortVariables({fieldName: 'unknownField',- orderBy: 'AscNullsLast',+ direction: 'asc',}),).toEqual({sortBy: AdminChatThreadSortField.CREATED_AT,@@ -28,7 +28,7 @@ describe('getAdminChatsSortVariables', () => {expect(getAdminChatsSortVariables({fieldName: AdminChatThreadSortField.MESSAGE_COUNT,- orderBy: 'AscNullsLast',+ direction: 'asc',}),).toEqual({sortBy: AdminChatThreadSortField.MESSAGE_COUNT,@@ -40,7 +40,7 @@ describe('getAdminChatsSortVariables', () => {expect(getAdminChatsSortVariables({fieldName: AdminChatThreadSortField.UPDATED_AT,- orderBy: 'DescNullsLast',+ direction: 'desc',}),).toEqual({sortBy: AdminChatThreadSortField.UPDATED_AT,diff --git a/packages/twenty-front/src/modules/settings/admin-panel/chats/utils/getAdminChatsSortVariables.ts b/packages/twenty-front/src/modules/settings/admin-panel/chats/utils/getAdminChatsSortVariables.tsindex 28389cb1334..035c5a976a2 100644--- a/packages/twenty-front/src/modules/settings/admin-panel/chats/utils/getAdminChatsSortVariables.ts+++ b/packages/twenty-front/src/modules/settings/admin-panel/chats/utils/getAdminChatsSortVariables.ts@@ -33,8 +33,9 @@ export const getAdminChatsSortVariables = (return {sortBy: sortValue.fieldName,- sortDirection: sortValue.orderBy.startsWith('Asc')- ? AdminChatThreadSortDirection.ASC- : AdminChatThreadSortDirection.DESC,+ sortDirection:+ sortValue.direction === 'asc'+ ? AdminChatThreadSortDirection.ASC+ : AdminChatThreadSortDirection.DESC,};};diff --git a/packages/twenty-front/src/modules/settings/data-model/object-details/components/SettingsObjectRelationsTable.tsx b/packages/twenty-front/src/modules/settings/data-model/object-details/components/SettingsObjectRelationsTable.tsxindex f6dd7806077..f85e8a0acd5 100644--- a/packages/twenty-front/src/modules/settings/data-model/object-details/components/SettingsObjectRelationsTable.tsx+++ b/packages/twenty-front/src/modules/settings/data-model/object-details/components/SettingsObjectRelationsTable.tsx@@ -69,7 +69,7 @@ const SETTINGS_OBJECT_RELATION_TABLE_METADATA: TableMetadata<FieldMetadataItem>],initialSort: {fieldName: 'label',- orderBy: 'AscNullsLast',+ direction: 'asc',},};diff --git a/packages/twenty-front/src/modules/settings/roles/role-permissions/object-level-permissions/field-permissions/components/SettingsRolePermissionsObjectLevelObjectFieldPermissionTable.tsx b/packages/twenty-front/src/modules/settings/roles/role-permissions/object-level-permissions/field-permissions/components/SettingsRolePermissionsObjectLevelObjectFieldPermissionTable.tsxindex f8d8f3d9af9..e125f3a23ac 100644--- a/packages/twenty-front/src/modules/settings/roles/role-permissions/object-level-permissions/field-permissions/components/SettingsRolePermissionsObjectLevelObjectFieldPermissionTable.tsx+++ b/packages/twenty-front/src/modules/settings/roles/role-permissions/object-level-permissions/field-permissions/components/SettingsRolePermissionsObjectLevelObjectFieldPermissionTable.tsx@@ -8,7 +8,6 @@ import {} from '@/settings/roles/role-permissions/object-level-permissions/field-permissions/components/SettingsRolePermissionsObjectLevelObjectFieldPermissionTableRow';import { useObjectPermissionDerivedStates } from '@/settings/roles/role-permissions/object-level-permissions/field-permissions/hooks/useObjectPermissionDerivedStates';import { settingsDraftRoleFamilyState } from '@/settings/roles/states/settingsDraftRoleFamilyState';-import { type OrderBy } from 'twenty-shared/types';import { SettingsTextInput } from '@/ui/input/components/SettingsTextInput';import { SortableTableHeader } from '@/ui/layout/table/components/SortableTableHeader';import { Table } from '@/ui/layout/table/components/Table';@@ -25,7 +24,6 @@ import { H2Title } from 'twenty-ui/typography';import { Section } from 'twenty-ui/layout';import { themeCssVariables } from 'twenty-ui/theme-constants';import { sortByProperty } from '~/utils/array/sortByProperty';-import { turnOrderByIntoSort } from '~/utils/turnOrderByIntoSort';export const SETTINGS_ROLE_PERMISSION_OBJECT_LEVEL_FIELD_PERMISSION_TABLE_ID ='settings-role-permissions-object-level-object-field-permission';@@ -63,14 +61,7 @@ export const SettingsRolePermissionsObjectLevelObjectFieldPermissionTable = ({const restrictableFieldMetadataItems = [...searchedFields.filter(filterUserFacingFieldMetadataItems),- ].sort(- sortByProperty(- 'label',- turnOrderByIntoSort(- sortedFieldByTable?.orderBy ?? ('AscNullsFirst' satisfies OrderBy),- ),- ),- );+ ].sort(sortByProperty('label', sortedFieldByTable?.direction ?? 'asc'));const settingsDraftRole = useAtomFamilyStateValue(settingsDraftRoleFamilyState,@@ -120,7 +111,7 @@ export const SettingsRolePermissionsObjectLevelObjectFieldPermissionTable = ({tableId={SETTINGS_ROLE_PERMISSION_OBJECT_LEVEL_FIELD_PERMISSION_TABLE_ID}- initialSort={{ fieldName: 'label', orderBy: 'AscNullsFirst' }}+ initialSort={{ fieldName: 'label', direction: 'asc' }}/><TableHeader><TableHeaderText>{t`Data type`}</TableHeaderText>diff --git a/packages/twenty-front/src/modules/ui/layout/table/components/SortableTableHeader.tsx b/packages/twenty-front/src/modules/ui/layout/table/components/SortableTableHeader.tsxindex 30772202501..e782b3908b4 100644--- a/packages/twenty-front/src/modules/ui/layout/table/components/SortableTableHeader.tsx+++ b/packages/twenty-front/src/modules/ui/layout/table/components/SortableTableHeader.tsx@@ -44,23 +44,15 @@ export const SortableTableHeader = ({const isSortOnThisField = sortValue?.fieldName === fieldName;- const sortDirection = isSortOnThisField ? sortValue.orderBy : null;+ const isAsc = isSortOnThisField && sortValue.direction === 'asc';+ const isDesc = isSortOnThisField && sortValue.direction === 'desc';- const isAsc =- sortDirection === 'AscNullsLast' || sortDirection === 'AscNullsFirst';- const isDesc =- sortDirection === 'DescNullsLast' || sortDirection === 'DescNullsFirst';-- const isSortActive = isAsc || isDesc;+ const isSortActive = isSortOnThisField;const handleClick = () => {setSortedFieldByTable({fieldName,- orderBy: isSortOnThisField- ? sortValue.orderBy === 'AscNullsLast'- ? 'DescNullsLast'- : 'AscNullsLast'- : 'DescNullsLast',+ direction: isDesc ? 'asc' : 'desc',});};diff --git a/packages/twenty-front/src/modules/ui/layout/table/components/__tests__/SortableTableHeader.test.tsx b/packages/twenty-front/src/modules/ui/layout/table/components/__tests__/SortableTableHeader.test.tsxnew file mode 100644index 00000000000..72e0e24f37d--- /dev/null+++ b/packages/twenty-front/src/modules/ui/layout/table/components/__tests__/SortableTableHeader.test.tsx@@ -0,0 +1,79 @@+import { render, screen } from '@testing-library/react';+import userEvent from '@testing-library/user-event';+import { createStore, Provider } from 'jotai';+import { type ReactNode } from 'react';+import { isDefined } from 'twenty-shared/utils';++import { SortableTableHeader } from '@/ui/layout/table/components/SortableTableHeader';+import { type TableSortValue } from '@/ui/layout/table/types/TableSortValue';++const renderHeader = (initialSort: TableSortValue) => {+ const store = createStore();++ const Wrapper = ({ children }: { children: ReactNode }) => (+ <Provider store={store}>{children}</Provider>+ );++ const { container } = render(+ <SortableTableHeader+ tableId="test-table"+ fieldName="label"+ label="Name"+ initialSort={initialSort}+ />,+ { wrapper: Wrapper },+ );++ return {+ click: () => userEvent.click(screen.getByText('Name')),+ getArrowDirection: () => {+ if (isDefined(container.querySelector('.tabler-icon-arrow-up'))) {+ return 'asc';+ }++ return isDefined(container.querySelector('.tabler-icon-arrow-down'))+ ? 'desc'+ : 'none';+ },+ };+};++describe('SortableTableHeader', () => {+ it('reverses the arrow on the first click of an initially ascending column', async () => {+ const { click, getArrowDirection } = renderHeader({+ fieldName: 'label',+ direction: 'asc',+ });++ expect(getArrowDirection()).toBe('asc');++ await click();++ expect(getArrowDirection()).toBe('desc');+ });++ it('toggles the arrow back to ascending on the second click', async () => {+ const { click, getArrowDirection } = renderHeader({+ fieldName: 'label',+ direction: 'asc',+ });++ await click();+ await click();++ expect(getArrowDirection()).toBe('asc');+ });++ it('shows a descending arrow on the first click of an unsorted column', async () => {+ const { click, getArrowDirection } = renderHeader({+ fieldName: 'otherField',+ direction: 'asc',+ });++ expect(getArrowDirection()).toBe('none');++ await click();++ expect(getArrowDirection()).toBe('desc');+ });+});diff --git a/packages/twenty-front/src/modules/ui/layout/table/hooks/__tests__/useSortedArray.test.tsx b/packages/twenty-front/src/modules/ui/layout/table/hooks/__tests__/useSortedArray.test.tsxindex 223b47339cc..6353e6b3881 100644--- a/packages/twenty-front/src/modules/ui/layout/table/hooks/__tests__/useSortedArray.test.tsx+++ b/packages/twenty-front/src/modules/ui/layout/table/hooks/__tests__/useSortedArray.test.tsx@@ -12,14 +12,14 @@ import {tableDataSortedBylabelInDescendingOrder,} from '~/testing/mock-data/tableData';-import { type OrderBy } from 'twenty-shared/types';import { sortedFieldByTableFamilyState } from '@/ui/layout/table/states/sortedFieldByTableFamilyState';+import { type ArraySortDirection } from 'twenty-shared/types';import { useSortedArray } from '@/ui/layout/table/hooks/useSortedArray';const createSortedWrapper = (fieldName: keyof MockedTableType,- orderBy: OrderBy,+ direction: ArraySortDirection,) => {const store = createStore();store.set(@@ -28,7 +28,7 @@ const createSortedWrapper = (}),{fieldName: fieldName as string,- orderBy,+ direction,},);const Wrapper = ({ children }: { children: ReactNode }) => (@@ -42,7 +42,7 @@ describe('useSortedArray hook', () => {const { result } = renderHook(() => useSortedArray(tableData, mockedTableMetadata),{- wrapper: createSortedWrapper('labelPlural', 'AscNullsLast'),+ wrapper: createSortedWrapper('labelPlural', 'asc'),},);@@ -55,7 +55,7 @@ describe('useSortedArray hook', () => {const { result } = renderHook(() => useSortedArray(tableData, mockedTableMetadata),{- wrapper: createSortedWrapper('labelPlural', 'DescNullsLast'),+ wrapper: createSortedWrapper('labelPlural', 'desc'),},);@@ -68,7 +68,7 @@ describe('useSortedArray hook', () => {const { result } = renderHook(() => useSortedArray(tableData, mockedTableMetadata),{- wrapper: createSortedWrapper('fieldsCount', 'AscNullsLast'),+ wrapper: createSortedWrapper('fieldsCount', 'asc'),},);@@ -81,7 +81,7 @@ describe('useSortedArray hook', () => {const { result } = renderHook(() => useSortedArray(tableData, mockedTableMetadata),{- wrapper: createSortedWrapper('fieldsCount', 'DescNullsLast'),+ wrapper: createSortedWrapper('fieldsCount', 'desc'),},);diff --git a/packages/twenty-front/src/modules/ui/layout/table/hooks/useSortedArray.ts b/packages/twenty-front/src/modules/ui/layout/table/hooks/useSortedArray.tsindex a9ecdb214ab..844e74668b3 100644--- a/packages/twenty-front/src/modules/ui/layout/table/hooks/useSortedArray.ts+++ b/packages/twenty-front/src/modules/ui/layout/table/hooks/useSortedArray.ts@@ -30,11 +30,11 @@ export const useSortedArray = <T>(const sortFieldType = tableMetadata.fields.find((field) => field.fieldName === sortFieldName,)?.fieldType;- const sortOrder = sortValueToUse.orderBy;+ const sortDirection = sortValueToUse.direction;return [...arrayToSort].sort((a: T, b: T) => {if (sortFieldType === 'string') {- return sortOrder === 'AscNullsLast' || sortOrder === 'AscNullsFirst'+ return sortDirection === 'asc'? (a[sortFieldName] as string)?.localeCompare(b[sortFieldName] as string,)@@ -42,7 +42,7 @@ export const useSortedArray = <T>(a[sortFieldName] as string,);} else if (sortFieldType === 'number') {- return sortOrder === 'AscNullsLast' || sortOrder === 'AscNullsFirst'+ return sortDirection === 'asc'? (a[sortFieldName] as number) - (b[sortFieldName] as number): (b[sortFieldName] as number) - (a[sortFieldName] as number);} else {diff --git a/packages/twenty-front/src/modules/ui/layout/table/types/TableSortValue.ts b/packages/twenty-front/src/modules/ui/layout/table/types/TableSortValue.tsindex ce319ee1085..79bd2b22882 100644--- a/packages/twenty-front/src/modules/ui/layout/table/types/TableSortValue.ts+++ b/packages/twenty-front/src/modules/ui/layout/table/types/TableSortValue.ts@@ -1,6 +1,6 @@-import { type OrderBy } from 'twenty-shared/types';+import { type ArraySortDirection } from 'twenty-shared/types';export type TableSortValue = {fieldName: string;- orderBy: OrderBy;+ direction: ArraySortDirection;};diff --git a/packages/twenty-front/src/pages/settings/ai/constants/SettingsAiAgentTableMetadata.ts b/packages/twenty-front/src/pages/settings/ai/constants/SettingsAiAgentTableMetadata.tsindex 1ea6cd58cc4..b76811a6f13 100644--- a/packages/twenty-front/src/pages/settings/ai/constants/SettingsAiAgentTableMetadata.ts+++ b/packages/twenty-front/src/pages/settings/ai/constants/SettingsAiAgentTableMetadata.ts@@ -20,6 +20,6 @@ export const SETTINGS_AI_AGENT_TABLE_METADATA: TableMetadata<Agent> = {],initialSort: {fieldName: 'name',- orderBy: 'AscNullsLast',+ direction: 'asc',},};diff --git a/packages/twenty-front/src/pages/settings/ai/constants/SettingsSkillTableMetadata.ts b/packages/twenty-front/src/pages/settings/ai/constants/SettingsSkillTableMetadata.tsindex 68609eab4f0..80c5b0a9d50 100644--- a/packages/twenty-front/src/pages/settings/ai/constants/SettingsSkillTableMetadata.ts+++ b/packages/twenty-front/src/pages/settings/ai/constants/SettingsSkillTableMetadata.ts@@ -21,6 +21,6 @@ export const SETTINGS_SKILL_TABLE_METADATA: TableMetadata<Skill> = {],initialSort: {fieldName: 'name',- orderBy: 'AscNullsLast',+ direction: 'asc',},};diff --git a/packages/twenty-front/src/pages/settings/data-model/SettingsObjectFieldTable.tsx b/packages/twenty-front/src/pages/settings/data-model/SettingsObjectFieldTable.tsxindex ae3cf136997..96f755202cb 100644--- a/packages/twenty-front/src/pages/settings/data-model/SettingsObjectFieldTable.tsx+++ b/packages/twenty-front/src/pages/settings/data-model/SettingsObjectFieldTable.tsx@@ -63,7 +63,7 @@ const SETTINGS_OBJECT_FIELD_TABLE_METADATA: TableMetadata<SettingsObjectDetailTa],initialSort: {fieldName: 'label',- orderBy: 'AscNullsLast',+ direction: 'asc',},};diff --git a/packages/twenty-front/src/pages/settings/data-model/SettingsObjectIndexTable.tsx b/packages/twenty-front/src/pages/settings/data-model/SettingsObjectIndexTable.tsxindex 16a68e8cab4..095ab6851a8 100644--- a/packages/twenty-front/src/pages/settings/data-model/SettingsObjectIndexTable.tsx+++ b/packages/twenty-front/src/pages/settings/data-model/SettingsObjectIndexTable.tsx@@ -60,7 +60,7 @@ const TABLE_METADATA: TableMetadata<SettingsObjectIndexesTableItem> = {fields: TABLE_FIELDS,initialSort: {fieldName: 'indexFields',- orderBy: 'AscNullsLast',+ direction: 'asc',},};diff --git a/packages/twenty-front/src/pages/settings/data-model/constants/SettingsObjectTableMetadata.ts b/packages/twenty-front/src/pages/settings/data-model/constants/SettingsObjectTableMetadata.tsindex 1770a451a53..4b4c91cde5f 100644--- a/packages/twenty-front/src/pages/settings/data-model/constants/SettingsObjectTableMetadata.ts+++ b/packages/twenty-front/src/pages/settings/data-model/constants/SettingsObjectTableMetadata.ts@@ -33,6 +33,6 @@ export const GET_SETTINGS_OBJECT_TABLE_METADATA: TableMetadata<SettingsObjectTab],initialSort: {fieldName: 'labelPlural',- orderBy: 'AscNullsLast',+ direction: 'asc',},};diff --git a/packages/twenty-front/src/utils/__tests__/turnOrderByIntoSort.test.ts b/packages/twenty-front/src/utils/__tests__/turnOrderByIntoSort.test.tsdeleted file mode 100644index 1d5ddc4d34c..00000000000--- a/packages/twenty-front/src/utils/__tests__/turnOrderByIntoSort.test.ts+++ /dev/null@@ -1,19 +0,0 @@-import { turnOrderByIntoSort } from '~/utils/turnOrderByIntoSort';--describe('turnOrderByIntoSort', () => {- it('should be ok for "AscNullsFirst"', () => {- expect(turnOrderByIntoSort('AscNullsFirst')).toBe('asc');- });-- it('should be ok for "AscNullsLast"', () => {- expect(turnOrderByIntoSort('AscNullsLast')).toBe('asc');- });-- it('should be ok for "DescNullsFirst"', () => {- expect(turnOrderByIntoSort('DescNullsFirst')).toBe('desc');- });-- it('should be ok for "DescNullsLast"', () => {- expect(turnOrderByIntoSort('DescNullsLast')).toBe('desc');- });-});diff --git a/packages/twenty-front/src/utils/array/sortByProperty.ts b/packages/twenty-front/src/utils/array/sortByProperty.tsindex 7cdb9b3b488..2f2021b0d82 100644--- a/packages/twenty-front/src/utils/array/sortByProperty.ts+++ b/packages/twenty-front/src/utils/array/sortByProperty.ts@@ -1,5 +1,7 @@+import { type ArraySortDirection } from 'twenty-shared/types';+export const sortByProperty =- <T, K extends keyof T>(propertyName: K, sortBy: 'asc' | 'desc' = 'asc') =>+ <T, K extends keyof T>(propertyName: K, sortBy: ArraySortDirection = 'asc') =>(objectA: T, objectB: T) => {const a = sortBy === 'asc' ? objectA : objectB;const b = sortBy === 'asc' ? objectB : objectA;diff --git a/packages/twenty-front/src/utils/turnOrderByIntoSort.ts b/packages/twenty-front/src/utils/turnOrderByIntoSort.tsdeleted file mode 100644index d538095a0c9..00000000000--- a/packages/twenty-front/src/utils/turnOrderByIntoSort.ts+++ /dev/null@@ -1,12 +0,0 @@-import { type OrderBy } from 'twenty-shared/types';-import { assertUnreachable } from 'twenty-shared/utils';--export const turnOrderByIntoSort = (orderBy: OrderBy): 'asc' | 'desc' => {- if (orderBy === 'AscNullsFirst' || orderBy === 'AscNullsLast') {- return 'asc';- } else if (orderBy === 'DescNullsFirst' || orderBy === 'DescNullsLast') {- return 'desc';- } else {- assertUnreachable(orderBy);- }-};