Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 96022d8122 View with hidden groups crashes when mainGroupByFieldMetadataId is null
https://sonarly.com/issue/11500?type=bug

The `useGetViewGroupsFilters` hook throws an unhandled error when a view has hidden viewGroups but its `mainGroupByFieldMetadataId` field is null/undefined, crashing the companies page.

Fix: Replaced the hard `throw new Error('mainGroupByFieldMetadataId is required')` inside the `.map()` callback with an early return of `[]` at the top of the hook, before accessing `viewGroups`.

**What changed:** When `currentView` is undefined or its `mainGroupByFieldMetadataId` is null/undefined, the hook now returns an empty filter array instead of crashing. This is the correct semantic — if no group-by field is configured, there are no group-visibility filters to exclude.

**Why this is safe:** The filters produced by this hook are spread into the record filters array in `useGetRecordIndexTotalCount`. An empty array simply means "no additional group visibility filters" — the query works fine without them, it just won't exclude hidden groups from the count (which is the expected behavior when grouping isn't configured).

This also matches the pattern used in `mapViewGroupsToRecordGroupDefinitions` which returns `[]` when the field metadata is missing rather than throwing.

The `.filter(isDefined)` after `.map()` was also removed since the map callback always returns a concrete object (never `undefined`) — this was dead code.
2026-03-09 08:51:23 +00:00
@@ -6,14 +6,14 @@ import { getFilterTypeFromFieldType, isDefined } from 'twenty-shared/utils';
export const useGetViewGroupsFilters = (): RecordFilter[] => {
const { currentView } = useGetCurrentViewOnly();
if (!isDefined(currentView?.mainGroupByFieldMetadataId)) {
return [];
}
return (
currentView?.viewGroups
currentView.viewGroups
.filter((recordGroup) => !recordGroup.isVisible)
.map((recordGroup) => {
if (!isDefined(currentView.mainGroupByFieldMetadataId)) {
throw new Error('mainGroupByFieldMetadataId is required');
}
return {
id: recordGroup.id,
fieldMetadataId: currentView.mainGroupByFieldMetadataId,
@@ -23,7 +23,6 @@ export const useGetViewGroupsFilters = (): RecordFilter[] => {
type: getFilterTypeFromFieldType(FieldMetadataType.SELECT),
label: '',
};
})
.filter(isDefined) || []
}) || []
);
};