diff --git a/src/ts/EnvHandler.ts b/src/ts/EnvHandler.ts index 55de446..80844cf 100644 --- a/src/ts/EnvHandler.ts +++ b/src/ts/EnvHandler.ts @@ -1,42 +1,54 @@ // used to deal with .env things, but with this instead because nix +// Drafted manually, then fed through chatgpt +// Idk what is going on anymore, but it isn't really an important aspect of the code, since this is just for changing from productiong to dev environment + +type Env = { [K in keyof typeof env]: (typeof env)[K] } & Record< + string, + unknown +>; export let env = { - POCKETBASE_URL: "https://pocket.deprived.dev/", - DEV_POCKETBASE_URL: "https://dev.pocket.deprived.dev/", -}; + POCKETBASE_URL: "https://pocket.deprived.dev", +} as const; export default env; -// Check if ?debug=1 or debug in localstorage is 1 or true -export function checkIfDev(): boolean { - if (typeof window === "undefined") return false; +let initialized = false; - try { - const params = new URL(window.location.href).searchParams; - const urlDebug = params.get("debug"); +// Load overrides from localstorage or url params +const isOverride = (k: string) => k.startsWith("-E"); +const norm = (k: string) => k.slice(2); // strip "-E" - const ls = window.localStorage?.getItem("debug") ?? ""; - const lsDebug = ls.toLowerCase(); - - const isUrlDebug = urlDebug === "1"; - const isLocalDebug = lsDebug === "1" || lsDebug === "true"; - - return isUrlDebug || isLocalDebug; - } catch { - return false; +function apply(from: Record, tag: string) { + for (const [rawK, rawV] of Object.entries(from)) { + env[rawK] = rawV; } } -// Load overrides from localstorage -function loadOverrides() { - for (const [name, val] of Object.entries(env)) { - const envOverride = localStorage.getItem(name); - if (envOverride) { - env[name] = envOverride; - console.log("Env var +: " + name + "=" + val); - } else { - console.log("Env var loaded: " + name + "=" + val); - } - } -} +export function initEnv(): void { + if (initialized) return; + initialized = true; + window.env = env!; -loadOverrides(); + // localStorage overrides: only keys starting with "-E" + const ls: Record = {}; + for (const [k, v] of Object.entries(env)) { + const val = localStorage.getItem(k); + if (val != null) ls[k] = val; + } + apply(ls, "localStorage"); + + // URL param overrides: ?-EKEY=value + const params = new URLSearchParams(location.search); + const url: Record = {}; + for (const [k, v] of params.entries()) url[k] = v; + apply(url, "url"); + + // (optional) persist URL overrides in localStorage with the "-E" prefix + for (const [k, v] of Object.entries(url)) { + if (isOverride(k)) localStorage.setItem(norm(k), String(v)); + } + + // Object.assign(env as any, current); + // for (const [k, v] of Object.entries(env)) + // console.log(`[env final] ${k}=${String(v)}`); +} diff --git a/src/ts/Redaction/Redactor.ts b/src/ts/Redaction/Redactor.ts index 9642fbb..5305ca7 100644 --- a/src/ts/Redaction/Redactor.ts +++ b/src/ts/Redaction/Redactor.ts @@ -1,5 +1,6 @@ import { parseProfile, type Profile } from "./Profile"; import env from "@ts/EnvHandler"; +import { initEnv } from "@ts/EnvHandler"; class Redactor { public unredactedProfile: Profile | undefined = undefined; @@ -24,7 +25,8 @@ class Redactor { const unredactHash = hashResJson.response; console.log('Trying unredact hash: "' + unredactHash + '"'); - const url = `https://${env.POCKETBASE_URL}/api/files/redacted_content/${unredactHash}/redacted_cv_info_ha08bbn520.json`; + initEnv(); + const url = `${env.POCKETBASE_URL}/api/files/redacted_content/${unredactHash}/redacted_cv_info_ha08bbn520.json`; const res = await fetch(url, { method: "GET",