diff options
author | metamuffin <metamuffin@disroot.org> | 2022-09-11 09:52:29 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-09-11 09:52:29 +0200 |
commit | 38dd237bf0dd21ad83e2b127a0f4054b065d7d49 (patch) | |
tree | 044149c4d1538669674d8cc22e84d22f05c0c4cf /client-web/source/preferences | |
parent | a47a937c8058eb88d95ff4a006d159ae63b08e9e (diff) | |
download | keks-meet-38dd237bf0dd21ad83e2b127a0f4054b065d7d49.tar keks-meet-38dd237bf0dd21ad83e2b127a0f4054b065d7d49.tar.bz2 keks-meet-38dd237bf0dd21ad83e2b127a0f4054b065d7d49.tar.zst |
prefs in localStorage
Diffstat (limited to 'client-web/source/preferences')
-rw-r--r-- | client-web/source/preferences/decl.ts | 4 | ||||
-rw-r--r-- | client-web/source/preferences/mod.ts | 54 | ||||
-rw-r--r-- | client-web/source/preferences/ui.ts | 10 |
3 files changed, 42 insertions, 26 deletions
diff --git a/client-web/source/preferences/decl.ts b/client-web/source/preferences/decl.ts index cb46f3f..d641915 100644 --- a/client-web/source/preferences/decl.ts +++ b/client-web/source/preferences/decl.ts @@ -10,8 +10,8 @@ const string = "", bool = false, number = 0; // example types for ts const optional = <T>(a: T): T | undefined => a export const PREF_DECLS = { - username: { type: string, default: "guest-" + hex_id(), description: "Username" }, - warn_redirect: { type: bool, hidden: true, default: false, description: "Internal option that is set by a server redirect." }, + username: { type: string, default: "guest-" + hex_id(), description: "Username", allow_url: true }, + warn_redirect: { type: bool, hidden: true, default: false, description: "Internal option that is set by a server redirect.", allow_url: true }, image_view_popup: { type: bool, default: true, description: "Open image in popup instead of new tab" }, /* MEDIA */ diff --git a/client-web/source/preferences/mod.ts b/client-web/source/preferences/mod.ts index 4a2d8b9..08ecaea 100644 --- a/client-web/source/preferences/mod.ts +++ b/client-web/source/preferences/mod.ts @@ -9,6 +9,7 @@ export interface PrefDecl<T> { possible_values?: T[] optional?: boolean, hidden?: boolean + allow_url?: boolean } type Type = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"; @@ -36,7 +37,15 @@ export function register_prefs<T extends Record<string, PrefDecl<unknown>>>(ds: for (const key in ds) { const d = ds[key]; const type = typeof d.type; + let value = get_param(type, key) + if (value !== undefined && !d.allow_url) setTimeout(() => { // defer log call because this is executed early + log({ scope: "*", warn: true }, `pref key ${JSON.stringify(key)} is not allowed in url`) + }) + if (!d.allow_url) value = undefined + const j = localStorage.getItem(key) + if (j) value ??= JSON.parse(j) + if (value !== undefined) explicit[key] = value value ??= d.default; if (d.possible_values) if (!d.possible_values.includes(value)) value = d.default @@ -45,6 +54,7 @@ export function register_prefs<T extends Record<string, PrefDecl<unknown>>>(ds: return { prefs, explicit } } +window["change_pref" as "onbeforeprint"] = change_pref as () => void // TODO ugly export function change_pref<T extends keyof typeof PREFS>(key: T, value: typeof PREFS[T]) { log("*", `pref changed: ${key}`) PREFS[key] = value @@ -52,8 +62,17 @@ export function change_pref<T extends keyof typeof PREFS>(key: T, value: typeof PREFS_EXPLICIT[key] = value else delete PREFS_EXPLICIT[key] pref_change_handlers.get(key)?.forEach(h => h()) - window.location.hash = "#" + generate_section() + // window.location.hash = "#" + generate_section() + localStorage.setItem(key, JSON.stringify(value)) +} + +function param_to_string<T>(p: T): string { + if (typeof p == "string") return p + else if (typeof p == "boolean") return JSON.stringify(p) + else if (typeof p == "number") return JSON.stringify(p) + throw new Error("impossible"); } + export function generate_section(): string { const section = [] for (const key in PREFS_EXPLICIT) { @@ -76,27 +95,20 @@ export function load_params(): { raw_params: { [key: string]: string }, rname: s return { raw_params, rname } } -function param_to_string<T>(p: T): string { - if (typeof p == "string") return p - else if (typeof p == "boolean") return JSON.stringify(p) - else if (typeof p == "number") return JSON.stringify(p) - throw new Error("impossible"); -} - function get_param<T>(ty: string, key: string): T | undefined { const v = load_params().raw_params[key] - if (v == undefined) return undefined - if (ty == "string") return v as unknown as T - else if (ty == "number") { - const n = parseInt(v) - if (!Number.isNaN(n)) return n as unknown as T - console.warn("invalid number parameter"); - } else if (ty == "boolean") { - if (v == "0" || v == "false" || v == "no") return false as unknown as T - if (v == "1" || v == "true" || v == "yes") return true as unknown as T - console.warn("invalid boolean parameter"); - } else { - throw new Error("invalid param type"); + if (v !== undefined) { + if (ty == "string") return v as unknown as T + else if (ty == "number") { + const n = parseInt(v) + if (!Number.isNaN(n)) return n as unknown as T + console.warn("invalid number parameter"); + } else if (ty == "boolean") { + if (v == "0" || v == "false" || v == "no") return false as unknown as T + if (v == "1" || v == "true" || v == "yes") return true as unknown as T + console.warn("invalid boolean parameter"); + } else { + throw new Error("invalid param type"); + } } - return undefined } diff --git a/client-web/source/preferences/ui.ts b/client-web/source/preferences/ui.ts index 72d7792..634d945 100644 --- a/client-web/source/preferences/ui.ts +++ b/client-web/source/preferences/ui.ts @@ -4,8 +4,6 @@ import { change_pref, on_pref_changed, PrefDecl, PREFS } from "./mod.ts"; export class PrefUi extends OverlayUi { constructor() { - console.log(PREFS); - const rows = Object.entries(PREF_DECLS as Record<string, PrefDecl<unknown>>).filter(e => !e[1].hidden).map(([key_, decl]) => { const key = key_ as keyof typeof PREF_DECLS const id = `pref-${key}` @@ -77,14 +75,20 @@ export class PrefUi extends OverlayUi { const label = elabel(decl.description ?? `[${key}]`, { id }) return etr({ class: "pref" }, etd({}, label), etd({}, use_opt_ ?? ""), etd({}, prim_control ?? "")) }) + const notification_perm = Notification.permission == "granted" ? ediv() : ediv({}, espan("For keks-meet to send notifications, it needs you to grant permission: "), ebutton("Grant", { onclick: () => Notification.requestPermission() }), ) + const reset = ediv({}, + espan("Want to clear all settings? Use this:"), + ebutton("RESET", { onclick: () => { if (confirm("really clear all preferences?")) { localStorage.clear(); window.location.reload() } } }), + ) + const table = document.createElement("table") table.append(...rows) - super(ediv({ class: "prefs-overlay" }, notification_perm, ebr(), table)) + super(ediv({ class: "prefs-overlay" }, notification_perm, reset, ebr(), table)) } } |