2024-05-31 19:49:46 +05:30
|
|
|
import { WritableAtom, atom } from 'jotai';
|
2026-05-21 22:23:19 -04:00
|
|
|
import { produce } from 'immer';
|
2024-05-31 19:49:46 +05:30
|
|
|
import {
|
|
|
|
|
atomWithLocalStorage,
|
|
|
|
|
getLocalStorageItem,
|
|
|
|
|
setLocalStorageItem,
|
|
|
|
|
} from './utils/atomWithLocalStorage';
|
|
|
|
|
|
|
|
|
|
const OPENED_SIDEBAR_FOLDER = 'openedSidebarFolder';
|
|
|
|
|
|
|
|
|
|
type OpenedSidebarFolderAction =
|
|
|
|
|
| {
|
|
|
|
|
type: 'PUT';
|
|
|
|
|
id: string;
|
|
|
|
|
}
|
|
|
|
|
| {
|
|
|
|
|
type: 'DELETE';
|
|
|
|
|
id: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type OpenedSidebarFolderAtom = WritableAtom<
|
|
|
|
|
Set<string>,
|
|
|
|
|
[OpenedSidebarFolderAction],
|
|
|
|
|
undefined
|
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
export const makeOpenedSidebarFolderAtom = (userId: string): OpenedSidebarFolderAtom => {
|
|
|
|
|
const storeKey = `${OPENED_SIDEBAR_FOLDER}${userId}`;
|
|
|
|
|
|
|
|
|
|
const baseOpenedSidebarFolderAtom = atomWithLocalStorage<Set<string>>(
|
|
|
|
|
storeKey,
|
|
|
|
|
(key) => {
|
|
|
|
|
const arrayValue = getLocalStorageItem<string[]>(key, []);
|
|
|
|
|
return new Set(arrayValue);
|
|
|
|
|
},
|
|
|
|
|
(key, value) => {
|
|
|
|
|
const arrayValue = Array.from(value);
|
|
|
|
|
setLocalStorageItem(key, arrayValue);
|
2026-05-21 23:30:50 -04:00
|
|
|
},
|
2024-05-31 19:49:46 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const openedSidebarFolderAtom = atom<Set<string>, [OpenedSidebarFolderAction], undefined>(
|
|
|
|
|
(get) => get(baseOpenedSidebarFolderAtom),
|
|
|
|
|
(get, set, action) => {
|
|
|
|
|
if (action.type === 'DELETE') {
|
|
|
|
|
set(
|
|
|
|
|
baseOpenedSidebarFolderAtom,
|
|
|
|
|
produce(get(baseOpenedSidebarFolderAtom), (draft) => {
|
|
|
|
|
draft.delete(action.id);
|
2026-05-21 23:30:50 -04:00
|
|
|
}),
|
2024-05-31 19:49:46 +05:30
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (action.type === 'PUT') {
|
|
|
|
|
set(
|
|
|
|
|
baseOpenedSidebarFolderAtom,
|
|
|
|
|
produce(get(baseOpenedSidebarFolderAtom), (draft) => {
|
|
|
|
|
draft.add(action.id);
|
2026-05-21 23:30:50 -04:00
|
|
|
}),
|
2024-05-31 19:49:46 +05:30
|
|
|
);
|
|
|
|
|
}
|
2026-05-21 23:30:50 -04:00
|
|
|
},
|
2024-05-31 19:49:46 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return openedSidebarFolderAtom;
|
|
|
|
|
};
|