fix: handle missing field metadata gracefully in useRecordGroupFilter

https://sonarly.com/issue/17506?type=bug

When a metadata schema version mismatch occurs or authentication fails during navigation, `useRecordGroupFilter` throws an unhandled error because it holds a stale `recordIndexGroupFieldMetadataItem` reference that no longer exists in the refreshed/cleared `fields` array.

Fix: Replaced the `throw new Error(...)` in `useRecordGroupFilter` with `return {}` when the field metadata item referenced by the record group state is not found in the current fields array.

**Why this is the right fix:**

The `throw` was a defensive assertion added in commit 311b5f64c4, but it doesn't account for legitimate transient states where `recordIndexGroupFieldMetadataItem` (a Jotai component state) holds a stale field ID that doesn't exist in the current `fields` array. This happens during:
1. Auth failure → `onUnauthenticatedError` clears workspace state but not view-scoped component state
2. Metadata schema refreshes → fields array is temporarily empty/changed
3. Navigation between views → brief window where old group state hasn't been updated

Returning `{}` (empty filter) is consistent with the function's own behavior when `currentRecordGroupDefinition` is undefined (line 36). This means records temporarily load without the group filter, then re-render correctly once state stabilizes — a much better UX than crashing the entire page.

The fix is minimal (3 lines changed) and affects only the `useRecordGroupFilter` hook. Both callers (`useFindManyRecordIndexTableParams` and `useAggregateRecordsForRecordTableColumnFooter`) already handle empty filters correctly.
This commit is contained in:
Sonarly Claude Code
2026-03-23 13:26:10 +00:00
parent 630f3a0fd7
commit ec2fcb436b
@@ -19,9 +19,7 @@ export const useRecordGroupFilter = (fields: FieldMetadataItem[]) => {
);
if (!fieldMetadataItem) {
throw new Error(
`Field metadata item with id ${recordIndexGroupFieldMetadataItem?.id} not found`,
);
return {};
}
if (!isDefined(currentRecordGroupDefinition.value)) {