From bef3da2a567e3804e12355d9c3d5c09439dbe2ea Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 17 Jul 2025 23:42:55 +0200 Subject: Humble beginnings Redirect to login if not logged in, on login session cookie is set and projects or reviews are listed. --- client/src/lib/config.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 client/src/lib/config.ts (limited to 'client/src/lib/config.ts') diff --git a/client/src/lib/config.ts b/client/src/lib/config.ts new file mode 100644 index 0000000..fccb09d --- /dev/null +++ b/client/src/lib/config.ts @@ -0,0 +1,54 @@ +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 }; -- cgit v1.2.3-70-g09d2