Fix bug where aggregate resets record filters (#10136)

This PR fixes a bug where setting an aggregate in the footer of a record
table makes the currentRecordFilters state being overwritten by the
view, which we don't want.
This commit is contained in:
Lucas Bordeau
2025-02-11 19:14:18 +01:00
committed by GitHub
parent 252922b522
commit b757a37d07
2 changed files with 30 additions and 1 deletions

View File

@ -3,9 +3,11 @@ import { useFilterableFieldMetadataItemsInRecordIndexContext } from '@/object-re
import { currentRecordFiltersComponentState } from '@/object-record/record-filter/states/currentRecordFiltersComponentState'; import { currentRecordFiltersComponentState } from '@/object-record/record-filter/states/currentRecordFiltersComponentState';
import { usePrefetchedData } from '@/prefetch/hooks/usePrefetchedData'; import { usePrefetchedData } from '@/prefetch/hooks/usePrefetchedData';
import { PrefetchKey } from '@/prefetch/types/PrefetchKey'; import { PrefetchKey } from '@/prefetch/types/PrefetchKey';
import { useRecoilComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentFamilyStateV2';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2'; import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2'; import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
import { currentViewIdComponentState } from '@/views/states/currentViewIdComponentState'; import { currentViewIdComponentState } from '@/views/states/currentViewIdComponentState';
import { hasInitializedCurrentRecordFiltersComponentFamilyState } from '@/views/states/hasInitializedCurrentRecordFiltersComponentFamilyState';
import { View } from '@/views/types/View'; import { View } from '@/views/types/View';
import { mapViewFiltersToFilters } from '@/views/utils/mapViewFiltersToFilters'; import { mapViewFiltersToFilters } from '@/views/utils/mapViewFiltersToFilters';
import { useEffect } from 'react'; import { useEffect } from 'react';
@ -18,15 +20,29 @@ export const ViewBarRecordFilterEffect = () => {
const currentViewId = useRecoilComponentValueV2(currentViewIdComponentState); const currentViewId = useRecoilComponentValueV2(currentViewIdComponentState);
const [
hasInitializedCurrentRecordFilters,
setHasInitializedCurrentRecordFilters,
] = useRecoilComponentFamilyStateV2(
hasInitializedCurrentRecordFiltersComponentFamilyState,
{
viewId: currentViewId,
},
);
const setCurrentRecordFilters = useSetRecoilComponentStateV2( const setCurrentRecordFilters = useSetRecoilComponentStateV2(
currentRecordFiltersComponentState, currentRecordFiltersComponentState,
); );
const currentRecordFilters = useRecoilComponentValueV2(
currentRecordFiltersComponentState,
);
const { filterableFieldMetadataItems } = const { filterableFieldMetadataItems } =
useFilterableFieldMetadataItemsInRecordIndexContext(); useFilterableFieldMetadataItemsInRecordIndexContext();
useEffect(() => { useEffect(() => {
if (isDataPrefetched) { if (isDataPrefetched && !hasInitializedCurrentRecordFilters) {
const currentView = views.find((view) => view.id === currentViewId); const currentView = views.find((view) => view.id === currentViewId);
const filterDefinitions = filterableFieldMetadataItems.map( const filterDefinitions = filterableFieldMetadataItems.map(
@ -40,6 +56,7 @@ export const ViewBarRecordFilterEffect = () => {
setCurrentRecordFilters( setCurrentRecordFilters(
mapViewFiltersToFilters(currentView.viewFilters, filterDefinitions), mapViewFiltersToFilters(currentView.viewFilters, filterDefinitions),
); );
setHasInitializedCurrentRecordFilters(true);
} }
} }
}, [ }, [
@ -48,6 +65,9 @@ export const ViewBarRecordFilterEffect = () => {
currentViewId, currentViewId,
setCurrentRecordFilters, setCurrentRecordFilters,
filterableFieldMetadataItems, filterableFieldMetadataItems,
currentRecordFilters,
hasInitializedCurrentRecordFilters,
setHasInitializedCurrentRecordFilters,
]); ]);
return null; return null;

View File

@ -0,0 +1,9 @@
import { RecordFiltersComponentInstanceContext } from '@/object-record/record-filter/states/context/RecordFiltersComponentInstanceContext';
import { createComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/utils/createComponentFamilyStateV2';
export const hasInitializedCurrentRecordFiltersComponentFamilyState =
createComponentFamilyStateV2<boolean, { viewId?: string }>({
key: 'hasInitializedCurrentRecordFiltersComponentFamilyState',
defaultValue: false,
componentInstanceContext: RecordFiltersComponentInstanceContext,
});