mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-26 03:17:49 +02:00
fix(site): handle missing default chart time (#2397)
Co-authored-by: henrygd <hank@henrygd.me>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { atom, computed, listenKeys, map, type ReadableAtom } from "nanostores"
|
||||
import { atom, computed, map, type ReadableAtom } from "nanostores"
|
||||
import type { AlertMap, ChartTimes, SystemRecord, UpdateInfo, UserSettings } from "@/types"
|
||||
import { pb } from "./api"
|
||||
import { Unit } from "./enums"
|
||||
@@ -31,8 +31,11 @@ export const $publicKey = atom("")
|
||||
/** New version info if an update is available, otherwise undefined */
|
||||
export const $newVersion = atom<UpdateInfo | undefined>()
|
||||
|
||||
/** Chart time period used when user settings don't provide one */
|
||||
export const defaultChartTime: ChartTimes = "1h"
|
||||
|
||||
/** Chart time period */
|
||||
export const $chartTime = atom<ChartTimes>("1h")
|
||||
export const $chartTime = atom<ChartTimes>(defaultChartTime)
|
||||
|
||||
/** Whether to display average or max chart values */
|
||||
export const $maxValues = atom(false)
|
||||
@@ -50,13 +53,25 @@ export const $maxValues = atom(false)
|
||||
|
||||
/** User settings */
|
||||
export const $userSettings = map<UserSettings>({
|
||||
chartTime: "1h",
|
||||
chartTime: defaultChartTime,
|
||||
emails: [pb.authStore.record?.email || ""],
|
||||
unitNet: Unit.Bytes,
|
||||
unitTemp: Unit.Celsius,
|
||||
})
|
||||
// update chart time on change
|
||||
listenKeys($userSettings, ["chartTime"], ({ chartTime }) => $chartTime.set(chartTime))
|
||||
|
||||
/** Chart time period stored in user settings, or the default if it's missing */
|
||||
export function getUserChartTime(settings: UserSettings = $userSettings.get()): ChartTimes {
|
||||
return settings.chartTime || defaultChartTime
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply settings loaded from the database, including the default chart time.
|
||||
* Other settings writes don't touch $chartTime so they can't reset the active chart range.
|
||||
*/
|
||||
export function hydrateUserSettings(settings: UserSettings) {
|
||||
$userSettings.set(settings)
|
||||
$chartTime.set(getUserChartTime(settings))
|
||||
}
|
||||
|
||||
/** Container chart filter */
|
||||
export const $containerFilter = atom("")
|
||||
|
||||
Reference in New Issue
Block a user