import { browser } from '$app/environment'; import type { Infer } from 'superstruct'; import { assert, object, optional, number, string } from 'superstruct'; const Config = object({ active_project: optional(string()) }); type Config = Infer; const CachedConfig = object({ config: Config, expires: number() }); type CachedConfig = Infer; const CACHE_TTL = 10 * 60 * 60 * 1000; async function get_config(): Promise { // Cache, might be outdated but saves on call to server if (browser) { try { // TODO: Use async localStorage const stored = localStorage.getItem('config'); if (stored !== null) { const cached: CachedConfig = JSON.parse(stored); assert(cached, CachedConfig); if (cached.expires < Date.now()) { // TODO: Should we update expires here? // If page is in use we probably don't need to sync for a while. return cached.config; } } } catch { // ignore errors } } // Default config // eslint-disable-next-line prefer-const let config: Config = { active_project: undefined }; // TODO: Fetch config if (browser) { const cached: CachedConfig = { config: config, expires: Date.now() + CACHE_TTL }; // TODO: Use async localStorage localStorage.setItem('config', JSON.stringify(cached)); } return config; } export { type Config, get_config };