mirror of
https://github.com/array-in-a-matrix/xinny.git
synced 2024-06-12 17:37:13 -04:00
50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
|
import { atom } from 'jotai';
|
||
|
|
||
|
const STORAGE_KEY = 'settings';
|
||
|
export interface Settings {
|
||
|
themeIndex: number;
|
||
|
useSystemTheme: boolean;
|
||
|
isMarkdown: boolean;
|
||
|
editorToolbar: boolean;
|
||
|
isPeopleDrawer: boolean;
|
||
|
|
||
|
hideMembershipEvents: boolean;
|
||
|
hideNickAvatarEvents: boolean;
|
||
|
|
||
|
showNotifications: boolean;
|
||
|
isNotificationSounds: boolean;
|
||
|
}
|
||
|
|
||
|
const defaultSettings: Settings = {
|
||
|
themeIndex: 0,
|
||
|
useSystemTheme: true,
|
||
|
isMarkdown: true,
|
||
|
editorToolbar: false,
|
||
|
isPeopleDrawer: true,
|
||
|
|
||
|
hideMembershipEvents: false,
|
||
|
hideNickAvatarEvents: true,
|
||
|
|
||
|
showNotifications: true,
|
||
|
isNotificationSounds: true,
|
||
|
};
|
||
|
|
||
|
export const getSettings = () => {
|
||
|
const settings = localStorage.getItem(STORAGE_KEY);
|
||
|
if (settings === null) return defaultSettings;
|
||
|
return JSON.parse(settings) as Settings;
|
||
|
};
|
||
|
|
||
|
export const setSettings = (settings: Settings) => {
|
||
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
|
||
|
};
|
||
|
|
||
|
const baseSettings = atom<Settings>(getSettings());
|
||
|
export const settingsAtom = atom<Settings, Settings>(
|
||
|
(get) => get(baseSettings),
|
||
|
(get, set, update) => {
|
||
|
set(baseSettings, update);
|
||
|
setSettings(update);
|
||
|
}
|
||
|
);
|