Refactor duplication of hard coded soft delete filter logic (#10058)

This PR adds a useCheckIsSoftDeleteFilter hook instead of the
undocumented in-place logic to retrieve the soft delete filter.

Also took the opportunity to refactor a recent change of @prastoin with
it.

Split VariantFilterChip into SoftDeleteFilterChip and RecordFilterChip
to separate concerns about this soft delete filtering.
This commit is contained in:
Lucas Bordeau
2025-02-07 11:03:13 +01:00
committed by GitHub
parent e081d8ab5e
commit 30e4fdbd06
11 changed files with 138 additions and 85 deletions

View File

@ -0,0 +1,35 @@
import { useFilterableFieldMetadataItems } from '@/object-record/record-filter/hooks/useFilterableFieldMetadataItems';
import { RecordFilter } from '@/object-record/record-filter/types/RecordFilter';
import { RecordFilterOperand } from '@/object-record/record-filter/types/RecordFilterOperand';
import { isSoftDeleteFilterActiveComponentState } from '@/object-record/record-table/states/isSoftDeleteFilterActiveComponentState';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { isDefined } from 'twenty-shared';
export const useCheckIsSoftDeleteFilter = () => {
const { filterableFieldMetadataItems } = useFilterableFieldMetadataItems();
const isSoftDeleteFilterActive = useRecoilComponentValueV2(
isSoftDeleteFilterActiveComponentState,
);
const checkIsSoftDeleteFilter = (recordFilter: RecordFilter) => {
const foundFieldMetadataItem = filterableFieldMetadataItems.find(
(fieldMetadataItem) =>
fieldMetadataItem.id === recordFilter.fieldMetadataId,
);
if (!isDefined(foundFieldMetadataItem)) {
throw new Error(
`Field metadata item not found for field metadata id: ${recordFilter.fieldMetadataId}`,
);
}
return (
foundFieldMetadataItem.name === 'deletedAt' &&
isSoftDeleteFilterActive &&
recordFilter.operand === RecordFilterOperand.IsNotEmpty
);
};
return { checkIsSoftDeleteFilter };
};

View File

@ -4,7 +4,6 @@ import { ViewFilterOperand } from '@/views/types/ViewFilterOperand';
export type RecordFilter = {
id: string;
variant?: 'default' | 'danger';
fieldMetadataId: string;
value: string;
displayValue: string;

View File

@ -59,10 +59,11 @@ export const useHandleToggleTrashColumnFilter = ({
const newFilter: RecordFilter = {
id: v4(),
variant: 'danger',
fieldMetadataId: trashFieldMetadata.id,
operand: ViewFilterOperand.IsNotEmpty,
displayValue: '',
type: filterType,
label: `Deleted`,
definition: {
label: `Deleted`,
iconName: 'IconTrash',

View File

@ -1,6 +1,7 @@
import { IconFilterOff } from 'twenty-ui';
import { useObjectLabel } from '@/object-metadata/hooks/useObjectLabel';
import { useCheckIsSoftDeleteFilter } from '@/object-record/record-filter/hooks/useCheckIsSoftDeleteFilter';
import { useRemoveRecordFilter } from '@/object-record/record-filter/hooks/useRemoveRecordFilter';
import { useHandleToggleTrashColumnFilter } from '@/object-record/record-index/hooks/useHandleToggleTrashColumnFilter';
import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
@ -8,6 +9,7 @@ import { RecordTableEmptyStateDisplay } from '@/object-record/record-table/empty
import { tableFiltersComponentState } from '@/object-record/record-table/states/tableFiltersComponentState';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useDeleteCombinedViewFilters } from '@/views/hooks/useDeleteCombinedViewFilters';
import { isDefined } from 'twenty-shared';
export const RecordTableEmptyStateSoftDelete = () => {
const { objectMetadataItem, objectNameSingular, recordTableId } =
@ -28,12 +30,12 @@ export const RecordTableEmptyStateSoftDelete = () => {
const { removeRecordFilter } = useRemoveRecordFilter();
const handleButtonClick = async () => {
const deletedFilter = tableFilters.find(
(filter) => filter.label === 'Deleted' && filter.operand === 'isNotEmpty',
);
const { checkIsSoftDeleteFilter } = useCheckIsSoftDeleteFilter();
if (!deletedFilter) {
const handleButtonClick = async () => {
const deletedFilter = tableFilters.find(checkIsSoftDeleteFilter);
if (!isDefined(deletedFilter)) {
throw new Error('Deleted filter not found');
}

View File

@ -1,9 +1,9 @@
import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
import { RecordFiltersComponentInstanceContext } from '@/object-record/record-filter/states/context/RecordFiltersComponentInstanceContext';
import { createComponentStateV2 } from '@/ui/utilities/state/component-state/utils/createComponentStateV2';
export const isSoftDeleteFilterActiveComponentState =
createComponentStateV2<boolean>({
key: 'isSoftDeleteFilterActiveComponentState',
defaultValue: false,
componentInstanceContext: RecordTableComponentInstanceContext,
componentInstanceContext: RecordFiltersComponentInstanceContext,
});