Skip to content

Commit

Permalink
Fix react-hooks/exhaustive-deps error on useSelected.tsx (#4258)
Browse files Browse the repository at this point in the history
* Fix react-hooks/exhaustive-deps error on useSelected.tsx

* Release notes
  • Loading branch information
joel-jeremy authored Feb 19, 2025
1 parent 7c9a499 commit 81db739
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 0 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,6 @@ export default [
'packages/desktop-client/src/hooks/useCategories.ts',
'packages/desktop-client/src/hooks/usePayees.ts',
'packages/desktop-client/src/hooks/useProperFocus.tsx',
'packages/desktop-client/src/hooks/useSelected.tsx',
'packages/loot-core/src/client/query-hooks.tsx',
],

rules: {
Expand Down
24 changes: 11 additions & 13 deletions packages/desktop-client/src/hooks/useSelected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function useSelected<T extends Item>(
const prevState = undo.getUndoState('selectedItems');
undo.setUndoState('selectedItems', { name, items: state.selectedItems });
return () => undo.setUndoState('selectedItems', prevState);
}, [state.selectedItems]);
}, [name, state.selectedItems]);

useEffect(() => {
function onUndo({ messages, undoTag }: UndoState) {
Expand Down Expand Up @@ -233,7 +233,7 @@ export function useSelected<T extends Item>(
}

return listen('undo-event', onUndo);
}, []);
}, [name]);

return {
items: state.selectedItems,
Expand Down Expand Up @@ -263,37 +263,35 @@ export function SelectedProvider<T extends Item>({
fetchAllIds,
children,
}: SelectedProviderProps<T>) {
const latestItems = useRef(null);

useEffect(() => {
latestItems.current = instance.items;
}, [instance.items]);
const { items: instanceItems, dispatch: instanceDispatch } = instance;
const latestItems = useRef(instanceItems);
latestItems.current = instanceItems;

const dispatch = useCallback(
async (action: Actions) => {
if (action.type === 'select-all') {
if (latestItems.current && latestItems.current.size > 0) {
return instance.dispatch({
return instanceDispatch({
type: 'select-none',
isRangeSelect: action.isRangeSelect,
});
} else {
if (fetchAllIds) {
return instance.dispatch({
return instanceDispatch({
type: 'select-all',
ids: await fetchAllIds(),
isRangeSelect: action.isRangeSelect,
});
}
return instance.dispatch({
return instanceDispatch({
type: 'select-all',
isRangeSelect: action.isRangeSelect,
});
}
}
return instance.dispatch(action);
return instanceDispatch(action);
},
[instance.dispatch, fetchAllIds],
[instanceDispatch, fetchAllIds],
);

return (
Expand Down Expand Up @@ -335,7 +333,7 @@ export function SelectedProviderWithItems<T extends Item>({

useEffect(() => {
registerDispatch?.(selected.dispatch);
}, [registerDispatch]);
}, [registerDispatch, selected.dispatch]);

return (
<SelectedProvider<T> instance={selected} fetchAllIds={fetchAllIds}>
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4258.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [joel-jeremy]
---

Fix react-hooks/exhaustive-deps error on useSelected.tsx

0 comments on commit 81db739

Please sign in to comment.