From 84319479eee1606df269615139dfd63503b13bb3 Mon Sep 17 00:00:00 2001 From: Sonarly Claude Code Date: Fri, 6 Mar 2026 15:02:07 +0000 Subject: [PATCH] Unchecked findIndex(-1) in board field visibility toggle causes TypeError https://sonarly.com/issue/7338?type=bug When toggling field visibility on a Kanban board, `Array.findIndex()` returns -1 because the field exists in `currentRecordFields` but not in `recordIndexFieldDefinitions`. Accessing `array[-1]` yields `undefined`, and setting `.isVisible` on it throws the TypeError. Fix: Added bounds checks for `findIndex()` returning `-1` in two places in `useObjectOptionsForBoard.ts`: 1. **`handleReorderBoardFields`** (line 117): Guards against `findIndex` returning `-1` when reordering board fields. If the field isn't found in `recordIndexFieldDefinitions`, the `produce` callback returns early (immer returns the original array unchanged). 2. **`handleBoardFieldVisibilityChange`** (line 229): Guards against `findIndex` returning `-1` when toggling field visibility. Same early-return pattern. Both `produce` callbacks now safely no-op when the field exists in `currentRecordFields` but not in `recordIndexFieldDefinitions`. This is correct because: - The primary state updates (`updateRecordField`, `saveViewFields`) have already executed by this point - `recordIndexFieldDefinitions` is explicitly transitional state (marked with `// TODO: remove after refactor`) - Skipping the `recordIndexFieldDefinitions` update when the field isn't found is strictly better than crashing --- .../hooks/useObjectOptionsForBoard.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/twenty-front/src/modules/object-record/object-options-dropdown/hooks/useObjectOptionsForBoard.ts b/packages/twenty-front/src/modules/object-record/object-options-dropdown/hooks/useObjectOptionsForBoard.ts index 442f1fc2d6c..dab65f7601b 100644 --- a/packages/twenty-front/src/modules/object-record/object-options-dropdown/hooks/useObjectOptionsForBoard.ts +++ b/packages/twenty-front/src/modules/object-record/object-options-dropdown/hooks/useObjectOptionsForBoard.ts @@ -114,6 +114,10 @@ export const useObjectOptionsForBoard = ({ updatedRecordField.fieldMetadataItemId, ); + if (indexToModify === -1) { + return; + } + draftRecordIndexFieldDefinitions[indexToModify].position = updatedRecordField.position; }, @@ -222,6 +226,10 @@ export const useObjectOptionsForBoard = ({ updatedRecordField.fieldMetadataItemId, ); + if (indexToModify === -1) { + return; + } + draftRecordIndexFieldDefinitions[indexToModify].isVisible = shouldShowFieldMetadataItem; },