Files
pihkaal-me/app/stores/app.ts

58 lines
1.2 KiB
TypeScript

import { z } from "zod/mini";
const STORAGE_ID = "app_settings";
const settingsSchema = z.object({
color: z.object({
col: z.number(),
row: z.number(),
}),
});
type Settings = z.infer<typeof settingsSchema>;
const defaultSettings = (): Settings => ({
color: { col: 0, row: 0 },
});
const loadSettings = (): Settings => {
const stored = localStorage.getItem(STORAGE_ID);
try {
const result = settingsSchema.safeParse(JSON.parse(stored ?? ""));
if (result.success) {
return result.data;
}
} catch {
// JSON.parse failed
}
const settings = defaultSettings();
saveSettings(settings);
return settings;
};
const saveSettings = (settings: Settings) => {
localStorage.setItem(STORAGE_ID, JSON.stringify(settings));
};
export const useAppStore = defineStore("app", {
state: () => ({
booted: false,
settings: loadSettings(),
}),
actions: {
setColor(col: number, row: number) {
this.settings.color = { col, row };
},
},
getters: {
color: (state) => ({
col: state.settings.color.col,
row: state.settings.color.row,
hex: APP_COLORS[state.settings.color.row]![state.settings.color.col]!,
}),
},
});