perf(ui): trim hidden session and bootstrap work
This commit is contained in:
48
packages/ui/src/lib/ui-bootstrap-cache.ts
Normal file
48
packages/ui/src/lib/ui-bootstrap-cache.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
export type UiBootstrapTheme = "light" | "dark" | "system"
|
||||
|
||||
export interface UiBootstrapCacheSnapshot {
|
||||
theme?: UiBootstrapTheme
|
||||
locale?: string
|
||||
}
|
||||
|
||||
const UI_BOOTSTRAP_CACHE_KEY = "codenomad:ui-bootstrap"
|
||||
|
||||
export function readUiBootstrapCache(): UiBootstrapCacheSnapshot {
|
||||
if (typeof window === "undefined" || typeof window.localStorage === "undefined") {
|
||||
return {}
|
||||
}
|
||||
|
||||
try {
|
||||
const raw = window.localStorage.getItem(UI_BOOTSTRAP_CACHE_KEY)
|
||||
if (!raw) {
|
||||
return {}
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(raw) as UiBootstrapCacheSnapshot
|
||||
if (!parsed || typeof parsed !== "object") {
|
||||
return {}
|
||||
}
|
||||
|
||||
return {
|
||||
theme:
|
||||
parsed.theme === "light" || parsed.theme === "dark" || parsed.theme === "system"
|
||||
? parsed.theme
|
||||
: undefined,
|
||||
locale: typeof parsed.locale === "string" ? parsed.locale : undefined,
|
||||
}
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
export function writeUiBootstrapCache(snapshot: UiBootstrapCacheSnapshot) {
|
||||
if (typeof window === "undefined" || typeof window.localStorage === "undefined") {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
window.localStorage.setItem(UI_BOOTSTRAP_CACHE_KEY, JSON.stringify(snapshot))
|
||||
} catch {
|
||||
/* noop */
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user