Files
Sable/src/app/state/closedNavCategories.ts
T
2026-04-17 00:37:15 -05:00

73 lines
1.9 KiB
TypeScript

import type { WritableAtom } from 'jotai';
import { atom } from 'jotai';
import { produce } from 'immer';
import {
atomWithLocalStorage,
getLocalStorageItem,
setLocalStorageItem,
} from './utils/atomWithLocalStorage';
const CLOSED_NAV_CATEGORY = 'closedNavCategories';
type ClosedNavCategoriesAction =
| {
type: 'PUT';
categoryId: string;
}
| {
type: 'DELETE';
categoryId: string;
};
export type ClosedNavCategoriesAtom = WritableAtom<
Set<string>,
[ClosedNavCategoriesAction],
undefined
>;
export const makeClosedNavCategoriesAtom = (userId: string): ClosedNavCategoriesAtom => {
const storeKey = `${CLOSED_NAV_CATEGORY}${userId}`;
const baseClosedNavCategoriesAtom = atomWithLocalStorage<Set<string>>(
storeKey,
(key) => {
const arrayValue = getLocalStorageItem<string[]>(key, []);
return new Set(arrayValue);
},
(key, value) => {
const arrayValue = Array.from(value);
setLocalStorageItem(key, arrayValue);
}
);
const closedNavCategoriesAtom = atom<Set<string>, [ClosedNavCategoriesAction], undefined>(
(get) => get(baseClosedNavCategoriesAtom),
(get, set, action) => {
if (action.type === 'DELETE') {
set(
baseClosedNavCategoriesAtom,
produce(get(baseClosedNavCategoriesAtom), (draft) => {
draft.delete(action.categoryId);
})
);
return;
}
if (action.type === 'PUT') {
set(
baseClosedNavCategoriesAtom,
produce(get(baseClosedNavCategoriesAtom), (draft) => {
draft.add(action.categoryId);
})
);
}
}
);
return closedNavCategoriesAtom;
};
export const makeNavCategoryId = (...args: string[]): string => args.join('|');
export const getNavCategoryIdParts = (categoryId: string): [string, string] =>
categoryId.split('|') as [string, string];