Implemented record filter group initialization from view (#10527)

This PR implements the initialization of current record filter groups
state from view.

It also implements mapRecordFilterGroupToViewFilterGroup,
mapRecordFilterGroupLogicalOperatorToViewFilterGroupLogicalOperator and
mapViewFilterGroupLogicalOperatorToRecordFilterGroupLogicalOperator with
their corresponding unit tests.

Some unused states not caught by ESLint are also removed.
This commit is contained in:
Lucas Bordeau
2025-02-27 10:25:59 +01:00
committed by GitHub
parent 2f048201ad
commit 5a39903d42
14 changed files with 427 additions and 32 deletions

View File

@ -18,6 +18,7 @@ import { FiltersHotkeyScope } from '@/object-record/object-filter-dropdown/types
import { VIEW_SORT_DROPDOWN_ID } from '@/object-record/object-sort-dropdown/constants/ViewSortDropdownId';
import { ObjectSortDropdownComponentInstanceContext } from '@/object-record/object-sort-dropdown/states/context/ObjectSortDropdownComponentInstanceContext';
import { ViewBarRecordFilterEffect } from '@/views/components/ViewBarRecordFilterEffect';
import { ViewBarRecordFilterGroupEffect } from '@/views/components/ViewBarRecordFilterGroupEffect';
import { ViewBarRecordSortEffect } from '@/views/components/ViewBarRecordSortEffect';
import { UpdateViewButtonGroup } from './UpdateViewButtonGroup';
import { ViewBarDetails } from './ViewBarDetails';
@ -47,6 +48,7 @@ export const ViewBar = ({
<ObjectSortDropdownComponentInstanceContext.Provider
value={{ instanceId: VIEW_SORT_DROPDOWN_ID }}
>
<ViewBarRecordFilterGroupEffect />
<ViewBarRecordFilterEffect />
<ViewBarRecordSortEffect />
<ViewBarFilterEffect filterDropdownId={filterDropdownId} />

View File

@ -41,10 +41,6 @@ export const ViewBarRecordFilterEffect = () => {
currentRecordFiltersComponentState,
);
const currentRecordFilters = useRecoilComponentValueV2(
currentRecordFiltersComponentState,
);
const { filterableFieldMetadataItems } = useFilterableFieldMetadataItems(
contextStoreCurrentObjectMetadataItem?.id,
);
@ -65,6 +61,7 @@ export const ViewBarRecordFilterEffect = () => {
filterableFieldMetadataItems,
),
);
setHasInitializedCurrentRecordFilters(true);
}
}
@ -72,7 +69,6 @@ export const ViewBarRecordFilterEffect = () => {
currentViewId,
setCurrentRecordFilters,
filterableFieldMetadataItems,
currentRecordFilters,
hasInitializedCurrentRecordFilters,
setHasInitializedCurrentRecordFilters,
contextStoreCurrentObjectMetadataItem?.id,

View File

@ -0,0 +1,72 @@
import { contextStoreCurrentObjectMetadataItemComponentState } from '@/context-store/states/contextStoreCurrentObjectMetadataItemComponentState';
import { contextStoreCurrentViewIdComponentState } from '@/context-store/states/contextStoreCurrentViewIdComponentState';
import { currentRecordFilterGroupsComponentState } from '@/object-record/record-filter-group/states/currentRecordFilterGroupsComponentState';
import { prefetchViewFromViewIdFamilySelector } from '@/prefetch/states/selector/prefetchViewFromViewIdFamilySelector';
import { useRecoilComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentFamilyStateV2';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
import { hasInitializedCurrentRecordFilterGroupsComponentFamilyState } from '@/views/states/hasInitializedCurrentRecordFilterGroupsComponentFamilyState';
import { mapViewFilterGroupsToRecordFilterGroups } from '@/views/utils/mapViewFilterGroupsToRecordFilterGroups';
import { useEffect } from 'react';
import { useRecoilValue } from 'recoil';
import { isDefined } from 'twenty-shared';
export const ViewBarRecordFilterGroupEffect = () => {
const currentViewId = useRecoilComponentValueV2(
contextStoreCurrentViewIdComponentState,
);
const contextStoreCurrentObjectMetadataItem = useRecoilComponentValueV2(
contextStoreCurrentObjectMetadataItemComponentState,
);
const currentView = useRecoilValue(
prefetchViewFromViewIdFamilySelector({
viewId: currentViewId ?? '',
}),
);
const [
hasInitializedCurrentRecordFilterGroups,
setHasInitializedCurrentRecordFilterGroups,
] = useRecoilComponentFamilyStateV2(
hasInitializedCurrentRecordFilterGroupsComponentFamilyState,
{
viewId: currentViewId ?? undefined,
},
);
const setCurrentRecordFilterGroups = useSetRecoilComponentStateV2(
currentRecordFilterGroupsComponentState,
);
useEffect(() => {
if (isDefined(currentView) && !hasInitializedCurrentRecordFilterGroups) {
if (
currentView.objectMetadataId !==
contextStoreCurrentObjectMetadataItem?.id
) {
return;
}
if (isDefined(currentView)) {
setCurrentRecordFilterGroups(
mapViewFilterGroupsToRecordFilterGroups(
currentView.viewFilterGroups ?? [],
),
);
setHasInitializedCurrentRecordFilterGroups(true);
}
}
}, [
currentViewId,
setCurrentRecordFilterGroups,
hasInitializedCurrentRecordFilterGroups,
setHasInitializedCurrentRecordFilterGroups,
contextStoreCurrentObjectMetadataItem?.id,
currentView,
]);
return null;
};

View File

@ -1,6 +1,5 @@
import { contextStoreCurrentObjectMetadataItemComponentState } from '@/context-store/states/contextStoreCurrentObjectMetadataItemComponentState';
import { contextStoreCurrentViewIdComponentState } from '@/context-store/states/contextStoreCurrentViewIdComponentState';
import { availableFieldMetadataItemsForSortFamilySelector } from '@/object-metadata/states/availableFieldMetadataItemsForSortFamilySelector';
import { currentRecordSortsComponentState } from '@/object-record/record-sort/states/currentRecordSortsComponentState';
import { prefetchViewFromViewIdFamilySelector } from '@/prefetch/states/selector/prefetchViewFromViewIdFamilySelector';
import { useRecoilComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentFamilyStateV2';
@ -43,12 +42,6 @@ export const ViewBarRecordSortEffect = () => {
currentRecordSortsComponentState,
);
const sortableFieldMetadataItems = useRecoilValue(
availableFieldMetadataItemsForSortFamilySelector({
objectMetadataItemId: contextStoreCurrentObjectMetadataItem?.id,
}),
);
useEffect(() => {
if (isDefined(currentView) && !hasInitializedCurrentRecordSorts) {
if (
@ -66,7 +59,6 @@ export const ViewBarRecordSortEffect = () => {
}, [
hasInitializedCurrentRecordSorts,
currentView,
sortableFieldMetadataItems,
setCurrentRecordSorts,
contextStoreCurrentObjectMetadataItem,
setHasInitializedCurrentRecordSorts,