Files
cinny/src/app/state/settings.ts
T

67 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-06-12 21:15:23 +10:00
import { atom } from 'jotai';
const STORAGE_KEY = 'settings';
2023-10-06 13:44:06 +11:00
export type MessageSpacing = '0' | '100' | '200' | '300' | '400' | '500';
export type MessageLayout = 0 | 1 | 2;
2023-06-12 21:15:23 +10:00
export interface Settings {
themeIndex: number;
useSystemTheme: boolean;
isMarkdown: boolean;
editorToolbar: boolean;
isPeopleDrawer: boolean;
2023-10-06 13:44:06 +11:00
useSystemEmoji: boolean;
2023-06-12 21:15:23 +10:00
2023-10-18 13:15:30 +11:00
enterForNewline: boolean;
2023-10-06 13:44:06 +11:00
messageLayout: MessageLayout;
messageSpacing: MessageSpacing;
2023-06-12 21:15:23 +10:00
hideMembershipEvents: boolean;
hideNickAvatarEvents: boolean;
2023-10-06 13:44:06 +11:00
mediaAutoLoad: boolean;
showHiddenEvents: boolean;
2023-06-12 21:15:23 +10:00
showNotifications: boolean;
isNotificationSounds: boolean;
}
const defaultSettings: Settings = {
themeIndex: 0,
useSystemTheme: true,
isMarkdown: true,
editorToolbar: false,
isPeopleDrawer: true,
2023-10-06 13:44:06 +11:00
useSystemEmoji: false,
2023-06-12 21:15:23 +10:00
2023-10-18 13:15:30 +11:00
enterForNewline: false,
2023-10-06 13:44:06 +11:00
messageLayout: 0,
messageSpacing: '400',
2023-06-12 21:15:23 +10:00
hideMembershipEvents: false,
hideNickAvatarEvents: true,
2023-10-06 13:44:06 +11:00
mediaAutoLoad: true,
showHiddenEvents: false,
2023-06-12 21:15:23 +10:00
showNotifications: true,
isNotificationSounds: true,
};
export const getSettings = () => {
const settings = localStorage.getItem(STORAGE_KEY);
if (settings === null) return defaultSettings;
2023-10-06 13:44:06 +11:00
return {
...defaultSettings,
...(JSON.parse(settings) as Settings),
};
2023-06-12 21:15:23 +10:00
};
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);
}
);