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
This commit is contained in:
+8
@@ -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;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user