From dcbf5a1899c415905be93b428faf93a4a87f7d0b Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 21 Jan 2024 22:31:43 +0100 Subject: [PATCH] Fix/library manga selection type error (#566) * Prevent TypeError when deselecting items from a specific key This was noticed when changing the categories of a manga to a category without any selected mangas, in this case, when trying to deselect a manga after changing the categories, a TypeError was thrown which resulted in a white screen * Remove duplicates from the list of all selected ids * Update state via setter * Clear selection after disabling the library selection mode * Remove console log --- .../collection/useSelectableCollection.ts | 15 ++++++++++++--- src/components/manga/MangaActionMenuItems.tsx | 1 - src/screens/Library.tsx | 4 ++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/components/collection/useSelectableCollection.ts b/src/components/collection/useSelectableCollection.ts index 0bcffb8d..bebf0090 100644 --- a/src/components/collection/useSelectableCollection.ts +++ b/src/components/collection/useSelectableCollection.ts @@ -19,6 +19,7 @@ export type SelectableCollectionReturnType void; setSelectionForKey: (key: Key, itemIds: Id[]) => void; getSelectionForKey: (key: Key) => Id[]; + clearSelection: () => void; }; export const useSelectableCollection = ( @@ -35,7 +36,7 @@ export const useSelectableCollection = => { const [keyToSelectedItemIds, setKeyToSelectedItemIds] = useState>(initialState); - const selectedItemIds = Object.values(keyToSelectedItemIds).flat(); + const selectedItemIds = [...new Set(Object.values(keyToSelectedItemIds).flat())]; const areAllItemsSelected = selectedItemIds.length === totalCount; const areNoItemsSelected = !selectedItemIds.length; @@ -48,7 +49,7 @@ export const useSelectableCollection = ({ ...prevState, - [key]: prevState[key].filter((selectedItemId) => selectedItemId !== id), + [key]: prevState[key]?.filter((selectedItemId) => selectedItemId !== id) ?? [], })); return; } @@ -79,11 +80,18 @@ export const useSelectableCollection = { - keyToSelectedItemIds[key] = itemIds; + setKeyToSelectedItemIds((prevState) => ({ + ...prevState, + [key]: [...itemIds], + })); }; const getSelectionForKey = (key: Key) => keyToSelectedItemIds[key]; + const clearSelection = () => { + setKeyToSelectedItemIds({}); + }; + return { selectedItemIds, keySelectedItemIds, @@ -95,5 +103,6 @@ export const useSelectableCollection = { - console.log('MangaActionMenuItem', manga); Mangas.performAction(action, manga ? [manga.id] : Mangas.getIds(mangas), { wasManuallyMarkedAsRead: true, }).catch(defaultPromiseErrorHandler(`MangaActionMenuItems:performAction(${action})`)); diff --git a/src/screens/Library.tsx b/src/screens/Library.tsx index d66004dd..46cdfd2b 100644 --- a/src/screens/Library.tsx +++ b/src/screens/Library.tsx @@ -75,6 +75,7 @@ export function Library() { selectedItemIds, handleSelectAll, handleSelection, + clearSelection, } = useSelectableCollection(mangas.length, { currentKey: activeTab?.id.toString() }); const handleSelect = (id: number, selected: boolean) => { @@ -108,6 +109,9 @@ export function Library() { onClose={(selectionModeState) => { handleClose(); setIsSelectModeActive(selectionModeState); + if (!selectionModeState) { + clearSelection(); + } }} setHideMenu={setHideMenu} />