diff options
Diffstat (limited to 'src/stores')
| -rw-r--r-- | src/stores/aprilFools.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/stores/aprilFools.ts b/src/stores/aprilFools.ts new file mode 100644 index 00000000..d86829c1 --- /dev/null +++ b/src/stores/aprilFools.ts @@ -0,0 +1,37 @@ +import { browser } from "$app/environment"; +import { writable } from "svelte/store"; + +const storageKey = (date = new Date()) => + `aprilFoolsExecutiveMode:${date.getFullYear()}`; + +export const isAprilFoolsDay = (date = new Date()) => + date.getMonth() === 3 && date.getDate() === 1; + +let hydrated = !browser; + +const store = writable(false); + +export const hydrateAprilFools = () => { + if (!browser || hydrated) return; + + const storedValue = localStorage.getItem(storageKey()); + + store.set(storedValue === null ? isAprilFoolsDay() : storedValue === "true"); + hydrated = true; +}; + +store.subscribe((value) => { + if (!browser || !hydrated) return; + + localStorage.setItem(storageKey(), String(value)); +}); + +export const aprilFoolsVisible = () => isAprilFoolsDay(); + +const aprilFools = { + subscribe: store.subscribe, + set: store.set, + toggle: () => store.update((value) => !value), +}; + +export default aprilFools; |