Fix useFindManyRecords withSoftDeleterFilter (#11056)

Fixes #11038

# Fix useFindManyRecords withSoftDeleterFilter

The error came from a faulty implementation of the `withSoftDeleted`
parameter inside `useFindManyRecords` and from the fact that
`withSoftDeleted: true` was added to access deleted records actions.
However, this parameter was always set in
`useFindManyRecordsSelectedInContextStore` instead of considering
whether the filter was active or not.

```
const withSoftDeleterFilter = {
  or: [{ deletedAt: { is: 'NULL' } }, { deletedAt: { is: 'NOT_NULL' } }],
};
```

The final filter was incorrectly doing an `or` operation between the
base filter and `withSoftDeleterFilter` when it should have been an
`and`:

```
filter: {
  ...filter,
  ...(withSoftDeleted ? withSoftDeleterFilter : {}),
}
```

The correct implementation should be:

```
filter:
  filter || withSoftDeleted
    ? {
        and: [
          ...(filter ? [filter] : []),
          ...(withSoftDeleted ? [withSoftDeleterFilter] : []),
        ],
      }
    : undefined,
```

# Fix useFindManyRecordsSelectedInContextStore

- Check if the soft deleted filter is active before using the
`withSoftDeleterFilter` parameter
This commit is contained in:
Raphaël Bosi
2025-03-20 16:27:55 +01:00
committed by GitHub
parent 295f153abe
commit 7dac60cfee
3 changed files with 29 additions and 8 deletions

View File

@ -1,5 +1,6 @@
import { CommandMenuContextRecordsChip } from '@/command-menu/components/CommandMenuContextRecordsChip';
import { CommandMenuItem } from '@/command-menu/components/CommandMenuItem';
import { COMMAND_MENU_PREVIOUS_COMPONENT_INSTANCE_ID } from '@/command-menu/constants/CommandMenuPreviousComponentInstanceId';
import { RESET_CONTEXT_TO_SELECTION } from '@/command-menu/constants/ResetContextToSelection';
import { useResetPreviousCommandMenuContext } from '@/command-menu/hooks/useResetPreviousCommandMenuContext';
import { contextStoreCurrentObjectMetadataItemIdComponentState } from '@/context-store/states/contextStoreCurrentObjectMetadataItemIdComponentState';
@ -48,7 +49,7 @@ export const ResetContextToSelectionCommandButton = () => {
RightComponent={
<CommandMenuContextRecordsChip
objectMetadataItemId={objectMetadataItem.id}
instanceId="command-menu-previous"
instanceId={COMMAND_MENU_PREVIOUS_COMPONENT_INSTANCE_ID}
/>
}
onClick={resetPreviousCommandMenuContext}

View File

@ -3,9 +3,12 @@ import { contextStoreFiltersComponentState } from '@/context-store/states/contex
import { contextStoreTargetedRecordsRuleComponentState } from '@/context-store/states/contextStoreTargetedRecordsRuleComponentState';
import { computeContextStoreFilters } from '@/context-store/utils/computeContextStoreFilters';
import { useObjectMetadataItemById } from '@/object-metadata/hooks/useObjectMetadataItemById';
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { useFilterValueDependencies } from '@/object-record/record-filter/hooks/useFilterValueDependencies';
import { RecordFilterOperand } from '@/object-record/record-filter/types/RecordFilterOperand';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useRecoilValue } from 'recoil';
export const useFindManyRecordsSelectedInContextStore = ({
instanceId,
@ -35,6 +38,23 @@ export const useFindManyRecordsSelectedInContextStore = ({
const { filterValueDependencies } = useFilterValueDependencies();
const objectMetadataItems = useRecoilValue(objectMetadataItemsState);
const allFieldMetadataItems = objectMetadataItems.flatMap(
(objectMetadataItem) => objectMetadataItem.fields,
);
const isSoftDeleteFilterActive = contextStoreFilters.some((filter) => {
const foundFieldMetadataItem = allFieldMetadataItems.find(
(fieldMetadataItem) => fieldMetadataItem.id === filter.fieldMetadataId,
);
return (
foundFieldMetadataItem?.name === 'deletedAt' &&
filter.operand === RecordFilterOperand.IsNotEmpty
);
});
const queryFilter = computeContextStoreFilters(
contextStoreTargetedRecordsRule,
contextStoreFilters,
@ -46,7 +66,7 @@ export const useFindManyRecordsSelectedInContextStore = ({
const { records, loading, totalCount } = useFindManyRecords({
objectNameSingular: objectMetadataItem?.nameSingular ?? '',
filter: queryFilter,
withSoftDeleted: true,
withSoftDeleted: isSoftDeleteFilterActive,
orderBy: [
{
position: 'AscNullsFirst',

View File

@ -72,14 +72,14 @@ export const useFindManyRecords = <T extends ObjectRecord = ObjectRecord>({
useQuery<RecordGqlOperationFindManyResult>(findManyRecordsQuery, {
skip: skip || !objectMetadataItem,
variables: {
...(filter || withSoftDeleted
filter: withSoftDeleted
? {
filter: {
...filter,
...(withSoftDeleted ? withSoftDeleterFilter : {}),
},
and: [
...(filter ? [filter] : []),
...(withSoftDeleted ? [withSoftDeleterFilter] : []),
],
}
: {}),
: filter,
orderBy,
lastCursor: cursorFilter?.cursor ?? undefined,
limit,