feature: the package installs the Router Manager, which is no longer a package of its own.
Release / release (push) Failing after 4s
Release / release (push) Failing after 4s
This commit is contained in:
1 parent
6d90441bee
commit
8efe21bfa6
129 files changed
+17381
-104
No files matched your search
@@ -0,0 +1,71 @@
|
||||
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, and whether the WireGuard configuration dialog leaves out the lines a client fills in (wireguardPrefilled).
|
||||
*/
|
||||
export type BrowserSettings = { routerRefresh: number; metricsRefresh: number; wireguardPrefilled: boolean }
|
||||
|
||||
/** The localStorage key of the settings. */
|
||||
const KEY = 'vpn-router-manager.settings'
|
||||
|
||||
/** The event raised on window when the settings change in this tab. */
|
||||
const CHANGED = 'vpn-router-manager-settings'
|
||||
|
||||
export const DEFAULT_SETTINGS: BrowserSettings = { routerRefresh: 10, metricsRefresh: 60, wireguardPrefilled: 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,
|
||||
}
|
||||
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))
|
||||
}
|
||||
Reference in new issue
Block a user