Files
cinny/src/app/state/spaceRooms.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

60 lines
1.4 KiB
TypeScript

import { atom } from 'jotai';
import { produce } from 'immer';
import {
atomWithLocalStorage,
getLocalStorageItem,
setLocalStorageItem,
} from './utils/atomWithLocalStorage';
const SPACE_ROOMS = 'spaceRooms';
const baseSpaceRoomsAtom = atomWithLocalStorage<Set<string>>(
SPACE_ROOMS,
(key) => {
const arrayValue = getLocalStorageItem<string[]>(key, []);
return new Set(arrayValue);
},
(key, value) => {
const arrayValue = Array.from(value);
setLocalStorageItem(key, arrayValue);
},
);
type SpaceRoomsAction =
| {
type: 'PUT';
roomIds: string[];
}
| {
type: 'DELETE';
roomIds: string[];
};
export const spaceRoomsAtom = atom<Set<string>, [SpaceRoomsAction], undefined>(
(get) => get(baseSpaceRoomsAtom),
(get, set, action) => {
const current = get(baseSpaceRoomsAtom);
const { type, roomIds } = action;
if (type === 'DELETE' && roomIds.find((roomId) => current.has(roomId))) {
set(
baseSpaceRoomsAtom,
produce(current, (draft) => {
roomIds.forEach((roomId) => draft.delete(roomId));
}),
);
return;
}
if (type === 'PUT') {
const newEntries = roomIds.filter((roomId) => !current.has(roomId));
if (newEntries.length > 0)
set(
baseSpaceRoomsAtom,
produce(current, (draft) => {
newEntries.forEach((roomId) => draft.add(roomId));
}),
);
}
},
);