Skip to content

Commit

Permalink
Refactored empty filter (#9532)
Browse files Browse the repository at this point in the history
Refactored empty filter function and removed it from useFilterDropdown
hook
  • Loading branch information
lucasbordeau authored Jan 10, 2025
1 parent 654a0bb commit c716a30
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { IconFilterOff, MenuItem } from 'twenty-ui';

import { useFilterDropdown } from '@/object-record/object-filter-dropdown/hooks/useFilterDropdown';
import { useEmptyRecordFilter } from '@/object-record/object-filter-dropdown/hooks/useEmptyRecordFilter';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';

export const ObjectFilterDropdownRecordRemoveFilterMenuItem = () => {
const { emptyFilterButKeepDefinition } = useFilterDropdown();
const { emptyRecordFilter } = useEmptyRecordFilter();

const { closeDropdown } = useDropdown();

const handleRemoveFilter = () => {
emptyFilterButKeepDefinition();
emptyRecordFilter();
closeDropdown();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRecoilValue } from 'recoil';
import { v4 } from 'uuid';

import { useApplyRecordFilter } from '@/object-record/object-filter-dropdown/hooks/useApplyRecordFilter';
import { useEmptyRecordFilter } from '@/object-record/object-filter-dropdown/hooks/useEmptyRecordFilter';
import { useFilterDropdown } from '@/object-record/object-filter-dropdown/hooks/useFilterDropdown';
import { getActorSourceMultiSelectOptions } from '@/object-record/object-filter-dropdown/utils/getActorSourceMultiSelectOptions';
import { RelationPickerHotkeyScope } from '@/object-record/relation-picker/types/RelationPickerHotkeyScope';
Expand Down Expand Up @@ -30,7 +31,6 @@ export const ObjectFilterDropdownSourceSelect = ({
selectedFilterState,
setObjectFilterDropdownSelectedRecordIds,
objectFilterDropdownSelectedRecordIdsState,
emptyFilterButKeepDefinition,
} = useFilterDropdown();

const { applyRecordFilter } = useApplyRecordFilter(viewComponentId);
Expand Down Expand Up @@ -65,6 +65,8 @@ export const ObjectFilterDropdownSourceSelect = ({
objectFilterDropdownSelectedRecordIds.includes(option.id),
);

const { emptyRecordFilter } = useEmptyRecordFilter();

const handleMultipleItemSelectChange = (
itemToSelect: SelectableItem,
newSelectedValue: boolean,
Expand All @@ -76,7 +78,7 @@ export const ObjectFilterDropdownSourceSelect = ({
);

if (newSelectedItemIds.length === 0) {
emptyFilterButKeepDefinition();
emptyRecordFilter();
deleteCombinedViewFilter(fieldId);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { objectFilterDropdownSearchInputComponentState } from '@/object-record/object-filter-dropdown/states/objectFilterDropdownSearchInputComponentState';
import { objectFilterDropdownSelectedRecordIdsComponentState } from '@/object-record/object-filter-dropdown/states/objectFilterDropdownSelectedRecordIdsComponentState';
import { selectedFilterComponentState } from '@/object-record/object-filter-dropdown/states/selectedFilterComponentState';
import { useRecoilComponentCallbackStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentCallbackStateV2';
import { useRecoilCallback } from 'recoil';

export const useEmptyRecordFilter = (componentInstanceId?: string) => {
const objectFilterDropdownSearchInputCallbackState =
useRecoilComponentCallbackStateV2(
objectFilterDropdownSearchInputComponentState,
componentInstanceId,
);

const objectFilterDropdownSelectedRecordIdsCallbackState =
useRecoilComponentCallbackStateV2(
objectFilterDropdownSelectedRecordIdsComponentState,
componentInstanceId,
);

const selectedFilterCallbackState = useRecoilComponentCallbackStateV2(
selectedFilterComponentState,
componentInstanceId,
);

const emptyRecordFilter = useRecoilCallback(
({ set }) =>
() => {
set(objectFilterDropdownSearchInputCallbackState, '');
set(objectFilterDropdownSelectedRecordIdsCallbackState, []);
set(selectedFilterCallbackState, undefined);
},
[
objectFilterDropdownSearchInputCallbackState,
objectFilterDropdownSelectedRecordIdsCallbackState,
selectedFilterCallbackState,
],
);

return {
emptyRecordFilter,
};
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRecoilCallback, useSetRecoilState } from 'recoil';
import { useSetRecoilState } from 'recoil';

import { useFilterDropdownStates } from '@/object-record/object-filter-dropdown/hooks/useFilterDropdownStates';

Expand Down Expand Up @@ -27,20 +27,6 @@ export const useFilterDropdown = (props?: UseFilterDropdownProps) => {
advancedFilterViewFilterIdState,
} = useFilterDropdownStates(componentInstanceId);

const emptyFilterButKeepDefinition = useRecoilCallback(
({ set }) =>
() => {
set(objectFilterDropdownSearchInputState, '');
set(objectFilterDropdownSelectedRecordIdsState, []);
set(selectedFilterState, undefined);
},
[
objectFilterDropdownSearchInputState,
objectFilterDropdownSelectedRecordIdsState,
selectedFilterState,
],
);

const setSelectedFilter = useSetRecoilState(selectedFilterState);
const setSelectedOperandInDropdown = useSetRecoilState(
selectedOperandInDropdownState,
Expand Down Expand Up @@ -77,7 +63,6 @@ export const useFilterDropdown = (props?: UseFilterDropdownProps) => {
setOnFilterSelect,
setAdvancedFilterViewFilterGroupId,
setAdvancedFilterViewFilterId,
emptyFilterButKeepDefinition,
filterDefinitionUsedInDropdownState,
objectFilterDropdownSearchInputState,
objectFilterDropdownSelectedRecordIdsState,
Expand Down

0 comments on commit c716a30

Please sign in to comment.