74 lines
3.1 KiB
TypeScript
74 lines
3.1 KiB
TypeScript
import { useSyncExternalStore } from 'react'
|
|
|
|
/**
|
|
* The settings of this browser: the seconds between the automatic reads of the Overview and the Metrics tab, 0 for
|
|
* manual, whether the WireGuard configuration dialog leaves out the lines a client fills in (wireguardPrefilled), and
|
|
* whether the page menu opens the Shell page in a new tab (shellNewTab).
|
|
*/
|
|
export type BrowserSettings = { routerRefresh: number; metricsRefresh: number; wireguardPrefilled: boolean; shellNewTab: boolean }
|
|
|
|
/** The localStorage key of the settings. */
|
|
const KEY = 'linux-manager-ui.settings'
|
|
|
|
/** The event raised on window when the settings change in this tab. */
|
|
const CHANGED = 'linux-manager-ui-settings'
|
|
|
|
export const DEFAULT_SETTINGS: BrowserSettings = { routerRefresh: 10, metricsRefresh: 60, wireguardPrefilled: false, shellNewTab: false }
|
|
|
|
/** The refresh choices of both tabs, in seconds; 0 is manual. */
|
|
export const REFRESH_CHOICES = [0, 5, 10, 30, 60, 300, 900]
|
|
|
|
/** A refresh choice in words. */
|
|
export function refreshLabel(seconds: number) {
|
|
return seconds === 0 ? 'Manual' : seconds < 60 ? `Every ${seconds} s` : `Every ${seconds / 60} min`
|
|
}
|
|
|
|
let cached: { text: string | null; settings: BrowserSettings } | null = null
|
|
|
|
/** The settings in localStorage, each that is missing or not one of its choices replaced by its default. */
|
|
function snapshot(): BrowserSettings {
|
|
const text = window.localStorage.getItem(KEY)
|
|
if (cached?.text === text) {
|
|
return cached.settings
|
|
}
|
|
let stored: Record<string, unknown> = {}
|
|
try {
|
|
const value: unknown = JSON.parse(text ?? 'null')
|
|
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
stored = value as Record<string, unknown>
|
|
}
|
|
} catch {
|
|
// Settings that cannot be read are replaced by the defaults.
|
|
}
|
|
const choice = (value: unknown, choices: number[], fallback: number) =>
|
|
(typeof value === 'number' && choices.includes(value) ? value : fallback)
|
|
const settings = {
|
|
routerRefresh: choice(stored.routerRefresh, REFRESH_CHOICES, DEFAULT_SETTINGS.routerRefresh),
|
|
metricsRefresh: choice(stored.metricsRefresh, REFRESH_CHOICES, DEFAULT_SETTINGS.metricsRefresh),
|
|
wireguardPrefilled: typeof stored.wireguardPrefilled === 'boolean' ? stored.wireguardPrefilled : DEFAULT_SETTINGS.wireguardPrefilled,
|
|
shellNewTab: typeof stored.shellNewTab === 'boolean' ? stored.shellNewTab : DEFAULT_SETTINGS.shellNewTab,
|
|
}
|
|
cached = { text, settings }
|
|
return settings
|
|
}
|
|
|
|
function subscribe(onChange: () => void) {
|
|
window.addEventListener(CHANGED, onChange)
|
|
window.addEventListener('storage', onChange)
|
|
return () => {
|
|
window.removeEventListener(CHANGED, onChange)
|
|
window.removeEventListener('storage', onChange)
|
|
}
|
|
}
|
|
|
|
/** The settings of this browser, followed when they change here or in another tab. */
|
|
export function useBrowserSettings(): BrowserSettings {
|
|
return useSyncExternalStore(subscribe, snapshot)
|
|
}
|
|
|
|
/** Keeps the settings in localStorage and tells every user of useBrowserSettings. */
|
|
export function saveBrowserSettings(settings: BrowserSettings) {
|
|
window.localStorage.setItem(KEY, JSON.stringify(settings))
|
|
window.dispatchEvent(new Event(CHANGED))
|
|
}
|