Files
cinny/src/app/state/navToActivePath.ts
T
Lotus Bot 61a1f008d0 chore: upgrade i18next 26, prettier 3, fontsource-variable, domhandler 6, lint-staged 17
- i18next 23->26 + react-i18next 15->17
- prettier 2->3, reformat all files
- replace @fontsource/inter with @fontsource-variable/inter 5, update import path
- domhandler 5->6 (aligns with transitive deps)
- lint-staged 16->17
2026-05-21 23:30:50 -04:00

73 lines
1.9 KiB
TypeScript

import { WritableAtom, atom } from 'jotai';
import { produce } from 'immer';
import { Path } from 'react-router-dom';
import {
atomWithLocalStorage,
getLocalStorageItem,
setLocalStorageItem,
} from './utils/atomWithLocalStorage';
const NAV_TO_ACTIVE_PATH = 'navToActivePath';
const getStoreKey = (userId: string): string => `${NAV_TO_ACTIVE_PATH}${userId}`;
type NavToActivePath = Map<string, Path>;
type NavToActivePathAction =
| {
type: 'PUT';
navId: string;
path: Path;
}
| {
type: 'DELETE';
navId: string;
};
export type NavToActivePathAtom = WritableAtom<NavToActivePath, [NavToActivePathAction], undefined>;
export const makeNavToActivePathAtom = (userId: string): NavToActivePathAtom => {
const storeKey = getStoreKey(userId);
const baseNavToActivePathAtom = atomWithLocalStorage<NavToActivePath>(
storeKey,
(key) => {
const obj: Record<string, Path> = getLocalStorageItem(key, {});
return new Map(Object.entries(obj));
},
(key, value) => {
const obj: Record<string, Path> = Object.fromEntries(value);
setLocalStorageItem(key, obj);
},
);
const navToActivePathAtom = atom<NavToActivePath, [NavToActivePathAction], undefined>(
(get) => get(baseNavToActivePathAtom),
(get, set, action) => {
if (action.type === 'DELETE') {
set(
baseNavToActivePathAtom,
produce(get(baseNavToActivePathAtom), (draft) => {
draft.delete(action.navId);
}),
);
return;
}
if (action.type === 'PUT') {
set(
baseNavToActivePathAtom,
produce(get(baseNavToActivePathAtom), (draft) => {
draft.set(action.navId, action.path);
}),
);
}
},
);
return navToActivePathAtom;
};
export const clearNavToActivePathStore = (userId: string) => {
localStorage.removeItem(getStoreKey(userId));
};