Fix collectable collection selecting initial items lazily

This commit is contained in:
schroda
2026-03-20 01:30:01 +01:00
parent 33c55f8b85
commit b63c8a548e
3 changed files with 13 additions and 6 deletions

View File

@@ -6,7 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { useCallback, useMemo, useRef, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
export type SelectableCollectionReturnType<Id extends number | string, Key extends string = string> = {
selectedItemIds: Id[];
@@ -134,6 +134,10 @@ export const useSelectableCollection = <Id extends number | string, Key extends
setKeyToSelectedItemIds(initialState);
}, [clearSelection, initialState]);
useEffect(() => {
setKeyToSelectedItemIds(initialState);
}, [initialState]);
return {
selectedItemIds,
keySelectedItemIds,

View File

@@ -50,7 +50,7 @@ export function CheckboxListSetting<Item>({
const { selectedItemIds, handleSelection, handleSelectAll, reset } = useSelectableCollection(items.length, {
currentKey: 'default',
itemIds,
initialState: { default: currentSelectedItemIds },
initialState: useMemo(() => ({ default: currentSelectedItemIds }), [currentSelectedItemIds]),
});
const handleCancel = () => {

View File

@@ -126,10 +126,13 @@ export function CategorySelect(props: CategorySelectProps) {
'categoriesToAdd' | 'categoriesToRemove'
>(allCategories.length, {
currentKey: 'categoriesToAdd',
initialState: {
initialState: useMemo(
() => ({
categoriesToAdd: [...mangaCategoryIds, ...defaultCategoryIds],
categoriesToRemove: [],
},
}),
[mangaCategoryIds, defaultCategoryIds],
),
});
useEffect(() => {