The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Show empty list groups again once they have records (#27198)
Félix Malfait936e13e7e96e
diff --git a/packages/twenty-front/src/modules/object-record/record-group/states/selectors/__tests__/visibleRecordGroupIdsComponentFamilySelector.test.ts b/packages/twenty-front/src/modules/object-record/record-group/states/selectors/__tests__/visibleRecordGroupIdsComponentFamilySelector.test.tsnew file mode 100644index 00000000000..c4d1b82c68d--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/record-group/states/selectors/__tests__/visibleRecordGroupIdsComponentFamilySelector.test.ts@@ -0,0 +1,93 @@+import { recordGroupDefinitionFamilyState } from '@/object-record/record-group/states/recordGroupDefinitionFamilyState';+import { recordGroupIdsComponentState } from '@/object-record/record-group/states/recordGroupIdsComponentState';+import { visibleRecordGroupIdsComponentFamilySelector } from '@/object-record/record-group/states/selectors/visibleRecordGroupIdsComponentFamilySelector';+import { visibleRecordGroupIdsIncludingEmptyComponentSelector } from '@/object-record/record-group/states/selectors/visibleRecordGroupIdsIncludingEmptyComponentSelector';+import { RecordGroupDefinitionType } from '@/object-record/record-group/types/RecordGroupDefinition';+import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';+import { recordIndexShouldHideEmptyRecordGroupsComponentState } from '@/object-record/record-index/states/recordIndexShouldHideEmptyRecordGroupsComponentState';+import {+ jotaiStore,+ resetJotaiStore,+} from '@/ui/utilities/state/jotai/jotaiStore';+import { ViewType } from '@/views/types/ViewType';++const instanceId = 'record-index-id';++const getVisibleRecordGroupIds = () =>+ jotaiStore.get(+ visibleRecordGroupIdsComponentFamilySelector.selectorFamily({+ instanceId,+ familyKey: ViewType.LIST,+ }),+ );++const getVisibleRecordGroupIdsIncludingEmpty = () =>+ jotaiStore.get(+ visibleRecordGroupIdsIncludingEmptyComponentSelector.selectorFamily({+ instanceId,+ }),+ );++describe('visible record group ids selectors', () => {+ beforeEach(() => {+ resetJotaiStore();++ jotaiStore.set(recordGroupIdsComponentState.atomFamily({ instanceId }), [+ 'empty-group',+ 'hidden-group',+ 'second-group',+ 'first-group',+ ]);++ for (const [recordGroupId, position, isVisible, recordIds] of [+ ['first-group', 0, true, ['a']],+ ['empty-group', 0.5, true, []],+ ['hidden-group', 0.7, false, ['b']],+ ['second-group', 1, true, ['c']],+ ] as const) {+ jotaiStore.set(+ recordGroupDefinitionFamilyState.atomFamily(recordGroupId),+ {+ id: recordGroupId,+ type: RecordGroupDefinitionType.Value,+ title: recordGroupId,+ value: recordGroupId,+ color: 'transparent',+ position,+ isVisible,+ },+ );+ jotaiStore.set(+ recordIndexRecordIdsByGroupComponentFamilyState.atomFamily({+ instanceId,+ familyKey: recordGroupId,+ }),+ [...recordIds],+ );+ }+ });++ it('should list visible groups in order, empty ones included', () => {+ expect(getVisibleRecordGroupIds()).toEqual([+ 'first-group',+ 'empty-group',+ 'second-group',+ ]);+ });++ it('should leave empty groups out when they are hidden, except in the including-empty list', () => {+ jotaiStore.set(+ recordIndexShouldHideEmptyRecordGroupsComponentState.atomFamily({+ instanceId,+ }),+ true,+ );++ expect(getVisibleRecordGroupIds()).toEqual(['first-group', 'second-group']);+ expect(getVisibleRecordGroupIdsIncludingEmpty()).toEqual([+ 'first-group',+ 'empty-group',+ 'second-group',+ ]);+ });+});diff --git a/packages/twenty-front/src/modules/object-record/record-group/states/selectors/visibleRecordGroupIdsComponentFamilySelector.ts b/packages/twenty-front/src/modules/object-record/record-group/states/selectors/visibleRecordGroupIdsComponentFamilySelector.tsindex 5adcd92ef47..0788b2602dd 100644--- a/packages/twenty-front/src/modules/object-record/record-group/states/selectors/visibleRecordGroupIdsComponentFamilySelector.ts+++ b/packages/twenty-front/src/modules/object-record/record-group/states/selectors/visibleRecordGroupIdsComponentFamilySelector.ts@@ -1,15 +1,10 @@-import { recordGroupDefinitionFamilyState } from '@/object-record/record-group/states/recordGroupDefinitionFamilyState';-import { recordGroupIdsComponentState } from '@/object-record/record-group/states/recordGroupIdsComponentState';+import { visibleRecordGroupIdsIncludingEmptyComponentSelector } from '@/object-record/record-group/states/selectors/visibleRecordGroupIdsIncludingEmptyComponentSelector';import { type RecordGroupDefinition } from '@/object-record/record-group/types/RecordGroupDefinition';-import { RecordGroupSort } from '@/object-record/record-group/types/RecordGroupSort';-import { recordGroupSortedInsert } from '@/object-record/record-group/utils/recordGroupSortedInsert';-import { recordIndexRecordGroupSortComponentState } from '@/object-record/record-index/states/recordIndexRecordGroupSortComponentState';import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';import { recordIndexShouldHideEmptyRecordGroupsComponentState } from '@/object-record/record-index/states/recordIndexShouldHideEmptyRecordGroupsComponentState';import { createAtomComponentFamilySelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilySelector';import { ViewComponentInstanceContext } from '@/views/states/contexts/ViewComponentInstanceContext';import { type ViewType } from '@/views/types/ViewType';-import { isDefined } from 'twenty-shared/utils';export const visibleRecordGroupIdsComponentFamilySelector =createAtomComponentFamilySelector<RecordGroupDefinition['id'][], ViewType>({@@ -18,63 +13,26 @@ export const visibleRecordGroupIdsComponentFamilySelector =get:({ instanceId, familyKey: _viewType }) =>({ get }) => {- const recordGroupSort = get(recordIndexRecordGroupSortComponentState, {- instanceId,- });-- const recordGroupIds = get(recordGroupIdsComponentState, {- instanceId,- });+ const visibleRecordGroupIds = get(+ visibleRecordGroupIdsIncludingEmptyComponentSelector,+ { instanceId },+ );const shouldHideEmptyRecordGroups = get(recordIndexShouldHideEmptyRecordGroupsComponentState,{ instanceId },);- const result: RecordGroupDefinition[] = [];-- const comparator = (- a: RecordGroupDefinition,- b: RecordGroupDefinition,- ) => {- switch (recordGroupSort) {- case RecordGroupSort.Alphabetical:- return a.title.localeCompare(b.title);- case RecordGroupSort.ReverseAlphabetical:- return b.title.localeCompare(a.title);- case RecordGroupSort.Manual:- default:- return a.position - b.position;- }- };-- for (const recordGroupId of recordGroupIds) {- const recordGroupDefinition = get(- recordGroupDefinitionFamilyState,- recordGroupId,- );-- if (!isDefined(recordGroupDefinition)) {- continue;- }-- if (!recordGroupDefinition.isVisible) {- continue;- }-- if (shouldHideEmptyRecordGroups) {- const rowIds = get(- recordIndexRecordIdsByGroupComponentFamilyState,- { instanceId, familyKey: recordGroupId },- );- if (rowIds.length === 0) {- continue;- }- }-- recordGroupSortedInsert(result, recordGroupDefinition, comparator);+ if (!shouldHideEmptyRecordGroups) {+ return visibleRecordGroupIds;}- return result.map(({ id }) => id);+ return visibleRecordGroupIds.filter(+ (recordGroupId) =>+ get(recordIndexRecordIdsByGroupComponentFamilyState, {+ instanceId,+ familyKey: recordGroupId,+ }).length > 0,+ );},});diff --git a/packages/twenty-front/src/modules/object-record/record-group/states/selectors/visibleRecordGroupIdsIncludingEmptyComponentSelector.ts b/packages/twenty-front/src/modules/object-record/record-group/states/selectors/visibleRecordGroupIdsIncludingEmptyComponentSelector.tsnew file mode 100644index 00000000000..87306d433c1--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/record-group/states/selectors/visibleRecordGroupIdsIncludingEmptyComponentSelector.ts@@ -0,0 +1,62 @@+import { recordGroupDefinitionFamilyState } from '@/object-record/record-group/states/recordGroupDefinitionFamilyState';+import { recordGroupIdsComponentState } from '@/object-record/record-group/states/recordGroupIdsComponentState';+import { type RecordGroupDefinition } from '@/object-record/record-group/types/RecordGroupDefinition';+import { RecordGroupSort } from '@/object-record/record-group/types/RecordGroupSort';+import { recordGroupSortedInsert } from '@/object-record/record-group/utils/recordGroupSortedInsert';+import { recordIndexRecordGroupSortComponentState } from '@/object-record/record-index/states/recordIndexRecordGroupSortComponentState';+import { createAtomComponentSelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentSelector';+import { ViewComponentInstanceContext } from '@/views/states/contexts/ViewComponentInstanceContext';+import { isDefined } from 'twenty-shared/utils';++export const visibleRecordGroupIdsIncludingEmptyComponentSelector =+ createAtomComponentSelector<RecordGroupDefinition['id'][]>({+ key: 'visibleRecordGroupIdsIncludingEmptyComponentSelector',+ componentInstanceContext: ViewComponentInstanceContext,+ get:+ ({ instanceId }) =>+ ({ get }) => {+ const recordGroupSort = get(recordIndexRecordGroupSortComponentState, {+ instanceId,+ });++ const recordGroupIds = get(recordGroupIdsComponentState, {+ instanceId,+ });++ const result: RecordGroupDefinition[] = [];++ const comparator = (+ a: RecordGroupDefinition,+ b: RecordGroupDefinition,+ ) => {+ switch (recordGroupSort) {+ case RecordGroupSort.Alphabetical:+ return a.title.localeCompare(b.title);+ case RecordGroupSort.ReverseAlphabetical:+ return b.title.localeCompare(a.title);+ case RecordGroupSort.Manual:+ default:+ return a.position - b.position;+ }+ };++ for (const recordGroupId of recordGroupIds) {+ const recordGroupDefinition = get(+ recordGroupDefinitionFamilyState,+ recordGroupId,+ );++ if (!isDefined(recordGroupDefinition)) {+ continue;+ }++ if (!recordGroupDefinition.isVisible) {+ continue;+ }++ recordGroupSortedInsert(result, recordGroupDefinition, comparator);+ }++ return result.map(({ id }) => id);+ },+ });diff --git a/packages/twenty-front/src/modules/object-record/record-list/components/RecordListRecordGroupsBody.tsx b/packages/twenty-front/src/modules/object-record/record-list/components/RecordListRecordGroupsBody.tsxindex 4e64806ea80..35e7e6e7e79 100644--- a/packages/twenty-front/src/modules/object-record/record-list/components/RecordListRecordGroupsBody.tsx+++ b/packages/twenty-front/src/modules/object-record/record-list/components/RecordListRecordGroupsBody.tsx@@ -1,14 +1,14 @@import { RecordGroupContext } from '@/object-record/record-group/states/context/RecordGroupContext';-import { visibleRecordGroupIdsComponentFamilySelector } from '@/object-record/record-group/states/selectors/visibleRecordGroupIdsComponentFamilySelector';+import { visibleRecordGroupIdsIncludingEmptyComponentSelector } from '@/object-record/record-group/states/selectors/visibleRecordGroupIdsIncludingEmptyComponentSelector';import { RecordIndexGroupAggregatesDataLoader } from '@/object-record/record-index/components/RecordIndexGroupAggregatesDataLoader';import { RecordListRecordGroup } from '@/object-record/record-list/components/RecordListRecordGroup';-import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';-import { ViewType } from '@/views/types/ViewType';+import { useAtomComponentSelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorValue';+// Each group loads its own records, so empty groups stay mounted and hide+// themselves, and show again once records come backexport const RecordListRecordGroupsBody = () => {- const visibleRecordGroupIds = useAtomComponentFamilySelectorValue(- visibleRecordGroupIdsComponentFamilySelector,- ViewType.LIST,+ const visibleRecordGroupIds = useAtomComponentSelectorValue(+ visibleRecordGroupIdsIncludingEmptyComponentSelector,);return (diff --git a/packages/twenty-front/src/modules/object-record/record-list/components/__tests__/RecordListRecordGroupsBody.test.tsx b/packages/twenty-front/src/modules/object-record/record-list/components/__tests__/RecordListRecordGroupsBody.test.tsxnew file mode 100644index 00000000000..423a2109e05--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/record-list/components/__tests__/RecordListRecordGroupsBody.test.tsx@@ -0,0 +1,160 @@+import { render, screen } from '@testing-library/react';+import { Provider as JotaiProvider } from 'jotai';++import { recordGroupDefinitionFamilyState } from '@/object-record/record-group/states/recordGroupDefinitionFamilyState';+import { recordGroupIdsComponentState } from '@/object-record/record-group/states/recordGroupIdsComponentState';+import { RecordGroupDefinitionType } from '@/object-record/record-group/types/RecordGroupDefinition';+import { recordIndexShouldHideEmptyRecordGroupsComponentState } from '@/object-record/record-index/states/recordIndexShouldHideEmptyRecordGroupsComponentState';+import { RecordListRecordGroupsBody } from '@/object-record/record-list/components/RecordListRecordGroupsBody';+import { type ObjectRecord } from '@/object-record/types/ObjectRecord';+import {+ jotaiStore,+ resetJotaiStore,+} from '@/ui/utilities/state/jotai/jotaiStore';+import { ViewComponentInstanceContext } from '@/views/states/contexts/ViewComponentInstanceContext';++const instanceId = 'record-index-id';++const firstRecord: ObjectRecord = { id: 'record-1', __typename: 'Opportunity' };+const secondRecord: ObjectRecord = {+ id: 'record-2',+ __typename: 'Opportunity',+};++let mockRecordsByGroupId: Record<string, ObjectRecord[]> = {};++jest.mock(+ '@/object-record/record-index/hooks/useRecordIndexTableQuery',+ () => ({+ useRecordIndexTableQuery: () => {+ const { useCurrentRecordGroupId } = jest.requireActual(+ '@/object-record/record-group/hooks/useCurrentRecordGroupId',+ );++ return {+ records: mockRecordsByGroupId[useCurrentRecordGroupId()] ?? [],+ loading: false,+ hasNextPage: false,+ fetchMoreRecords: jest.fn(),+ };+ },+ }),+);++jest.mock('@/object-record/record-index/contexts/RecordIndexContext', () => ({+ useRecordIndexContextOrThrow: () => ({+ objectNameSingular: 'opportunity',+ }),+}));++jest.mock(+ '@/object-record/record-list/components/RecordListRecordGroupSection',+ () => ({+ RecordListRecordGroupSection: () => {+ const { useCurrentRecordGroupId } = jest.requireActual(+ '@/object-record/record-group/hooks/useCurrentRecordGroupId',+ );++ return <div>{`Group ${useCurrentRecordGroupId()}`}</div>;+ },+ }),+);++jest.mock('@/object-record/record-list/components/RecordListRecords', () => ({+ RecordListRecords: () => null,+}));++jest.mock(+ '@/object-record/record-index/components/RecordIndexGroupAggregatesDataLoader',+ () => ({+ RecordIndexGroupAggregatesDataLoader: () => null,+ }),+);++const getGroupsBody = () => (+ <JotaiProvider store={jotaiStore}>+ <ViewComponentInstanceContext.Provider value={{ instanceId }}>+ <RecordListRecordGroupsBody />+ </ViewComponentInstanceContext.Provider>+ </JotaiProvider>+);++describe('RecordListRecordGroupsBody', () => {+ beforeEach(() => {+ resetJotaiStore();++ jotaiStore.set(recordGroupIdsComponentState.atomFamily({ instanceId }), [+ 'new',+ 'meeting',+ ]);++ for (const [recordGroupId, position] of [+ ['new', 0],+ ['meeting', 1],+ ] as const) {+ jotaiStore.set(+ recordGroupDefinitionFamilyState.atomFamily(recordGroupId),+ {+ id: recordGroupId,+ type: RecordGroupDefinitionType.Value,+ title: recordGroupId,+ value: recordGroupId,+ color: 'transparent',+ position,+ isVisible: true,+ },+ );+ }++ jotaiStore.set(+ recordIndexShouldHideEmptyRecordGroupsComponentState.atomFamily({+ instanceId,+ }),+ true,+ );+ });++ it('should show a hidden empty group again once its records come back', () => {+ mockRecordsByGroupId = {+ new: [firstRecord],+ meeting: [secondRecord],+ };++ const { rerender } = render(getGroupsBody());++ expect(screen.getByText('Group new')).toBeInTheDocument();+ expect(screen.getByText('Group meeting')).toBeInTheDocument();++ mockRecordsByGroupId = {+ new: [firstRecord],+ meeting: [],+ };+ rerender(getGroupsBody());++ expect(screen.queryByText('Group meeting')).not.toBeInTheDocument();++ mockRecordsByGroupId = {+ new: [firstRecord],+ meeting: [secondRecord],+ };+ rerender(getGroupsBody());++ expect(screen.getByText('Group meeting')).toBeInTheDocument();+ });++ it('should show groups that start empty once their records load', () => {+ mockRecordsByGroupId = {};++ const { rerender } = render(getGroupsBody());++ expect(screen.queryByText('Group new')).not.toBeInTheDocument();++ mockRecordsByGroupId = {+ new: [firstRecord],+ };+ rerender(getGroupsBody());++ expect(screen.getByText('Group new')).toBeInTheDocument();+ expect(screen.queryByText('Group meeting')).not.toBeInTheDocument();+ });+});