From 6404895a475b3228c742c0be480985dc4c6cfa41 Mon Sep 17 00:00:00 2001 From: henrygd Date: Sat, 22 Feb 2025 15:28:07 -0500 Subject: [PATCH] refactor: centralize chart max/avg value state to global store --- beszel/site/src/components/routes/system.tsx | 43 ++++++++------------ beszel/site/src/lib/stores.ts | 7 +++- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/beszel/site/src/components/routes/system.tsx b/beszel/site/src/components/routes/system.tsx index cdbabe9..38b1ae2 100644 --- a/beszel/site/src/components/routes/system.tsx +++ b/beszel/site/src/components/routes/system.tsx @@ -1,6 +1,6 @@ -import { $systems, pb, $chartTime, $containerFilter, $userSettings, $direction } from "@/lib/stores" +import { $systems, pb, $chartTime, $containerFilter, $userSettings, $direction, $maxValues } from "@/lib/stores" import { ChartData, ChartTimes, ContainerStatsRecord, GPUData, SystemRecord, SystemStatsRecord } from "@/types" -import React, { lazy, useCallback, useEffect, useMemo, useRef, useState } from "react" +import React, { lazy, memo, useCallback, useEffect, useMemo, useRef, useState } from "react" import { Card, CardHeader, CardTitle, CardDescription } from "../ui/card" import { useStore } from "@nanostores/react" import Spinner from "../spinner" @@ -98,10 +98,7 @@ export default function SystemDetail({ name }: { name: string }) { const { _ } = useLingui() const systems = useStore($systems) const chartTime = useStore($chartTime) - /** Max CPU toggle value */ - const cpuMaxStore = useState(false) - const bandwidthMaxStore = useState(false) - const diskIoMaxStore = useState(false) + const maxValues = useStore($maxValues) const [grid, setGrid] = useLocalStorage("grid", true) const [system, setSystem] = useState({} as SystemRecord) const [systemStats, setSystemStats] = useState([] as SystemStatsRecord[]) @@ -121,9 +118,6 @@ export default function SystemDetail({ name }: { name: string }) { setContainerData([]) setContainerFilterBar(null) $containerFilter.set("") - cpuMaxStore[1](false) - bandwidthMaxStore[1](false) - diskIoMaxStore[1](false) } }, [name]) @@ -291,6 +285,9 @@ export default function SystemDetail({ name }: { name: string }) { return null } + // select field for switching between avg and max values + const maxValSelect = isLongerChart ? : null + // if no data, show empty message const dataEmpty = !chartLoading && chartData.systemStats.length === 0 const lastGpuVals = Object.values(systemStats.at(-1)?.stats.g ?? {}) @@ -385,9 +382,9 @@ export default function SystemDetail({ name }: { name: string }) { grid={grid} title={_(t`CPU Usage`)} description={t`Average system-wide CPU utilization`} - cornerEl={isLongerChart ? : null} + cornerEl={maxValSelect} > - + {containerFilterBar && ( @@ -432,19 +429,19 @@ export default function SystemDetail({ name }: { name: string }) { grid={grid} title={t`Disk I/O`} description={t`Throughput of root filesystem`} - cornerEl={isLongerChart ? : null} + cornerEl={maxValSelect} > - + : null} + cornerEl={maxValSelect} description={t`Network traffic of public interfaces`} > - + {containerFilterBar && containerData.length > 0 && ( @@ -564,13 +561,9 @@ export default function SystemDetail({ name }: { name: string }) { grid={grid} title={`${extraFsName} I/O`} description={t`Throughput of ${extraFsName}`} - cornerEl={isLongerChart ? : null} + cornerEl={maxValSelect} > - + ) @@ -612,12 +605,10 @@ function ContainerFilterBar() { ) } -function SelectAvgMax({ store }: { store: [boolean, React.Dispatch>] }) { - const [max, setMax] = store +const SelectAvgMax = memo(({ max }: { max: boolean }) => { const Icon = max ? ChartMax : ChartAverage - return ( - $maxValues.set(e === "max")}> @@ -632,7 +623,7 @@ function SelectAvgMax({ store }: { store: [boolean, React.Dispatch ) -} +}) function ChartCard({ title, diff --git a/beszel/site/src/lib/stores.ts b/beszel/site/src/lib/stores.ts index 4fdce7b..9b24c1f 100644 --- a/beszel/site/src/lib/stores.ts +++ b/beszel/site/src/lib/stores.ts @@ -1,5 +1,5 @@ import PocketBase from "pocketbase" -import { atom, map, WritableAtom } from "nanostores" +import { atom, map, PreinitializedWritableAtom } from "nanostores" import { AlertRecord, ChartTimes, SystemRecord, UserSettings } from "@/types" import { basePath } from "@/components/router" @@ -22,7 +22,10 @@ export const $publicKey = atom("") export const $hubVersion = atom("") /** Chart time period */ -export const $chartTime = atom("1h") as WritableAtom +export const $chartTime = atom("1h") as PreinitializedWritableAtom + +/** Whether to display average or max chart values */ +export const $maxValues = atom(false) /** User settings */ export const $userSettings = map({