mirror of
https://github.com/henrygd/beszel.git
synced 2025-12-17 18:56:17 +01:00
Adds display unit preference (#938)
* Adds temperature unit preference * add unit preferences for networking * adds options for MB/s and bps. * supports disk throughput unit preferences
This commit is contained in:
@@ -28,6 +28,9 @@ export const $maxValues = atom(false)
|
||||
export const $userSettings = map<UserSettings>({
|
||||
chartTime: "1h",
|
||||
emails: [pb.authStore.record?.email || ""],
|
||||
temperatureUnit: "celsius",
|
||||
networkUnit: "mbps",
|
||||
diskUnit: "mbps",
|
||||
})
|
||||
// update local storage on change
|
||||
$userSettings.subscribe((value) => {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { toast } from "@/components/ui/use-toast"
|
||||
import { type ClassValue, clsx } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { $alerts, $copyContent, $systems, $userSettings, pb } from "./stores"
|
||||
import { AlertInfo, AlertRecord, ChartTimeData, ChartTimes, FingerprintRecord, SystemRecord } from "@/types"
|
||||
import { AlertInfo, AlertRecord, ChartTimeData, ChartTimes, FingerprintRecord, SystemRecord, TemperatureUnit, TemperatureConversion, SpeedUnit, SpeedConversion } from "@/types"
|
||||
import { RecordModel, RecordSubscription } from "pocketbase"
|
||||
import { WritableAtom } from "nanostores"
|
||||
import { timeDay, timeHour } from "d3-time"
|
||||
@@ -266,6 +266,109 @@ export function useLocalStorage<T>(key: string, defaultValue: T) {
|
||||
return [value, setValue]
|
||||
}
|
||||
|
||||
/** Convert temperature from Celsius to the specified unit */
|
||||
export function convertTemperature(
|
||||
celsius: number,
|
||||
unit: TemperatureUnit = "celsius"
|
||||
): TemperatureConversion {
|
||||
switch (unit) {
|
||||
case "fahrenheit":
|
||||
return { value: (celsius * 9) / 5 + 32, symbol: "°F" }
|
||||
default:
|
||||
return { value: celsius, symbol: "°C" }
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert network speed from MB/s to the specified unit */
|
||||
export function convertNetworkSpeed(
|
||||
mbps: number,
|
||||
unit: SpeedUnit = "mbps"
|
||||
): SpeedConversion {
|
||||
switch (unit) {
|
||||
case "bps": {
|
||||
const bps = mbps * 8 * 1_000_000 // Convert MB/s to bits per second
|
||||
|
||||
// Format large numbers appropriately
|
||||
if (bps >= 1_000_000_000) {
|
||||
return {
|
||||
value: bps / 1_000_000_000,
|
||||
symbol: " Gbps",
|
||||
display: `${decimalString(bps / 1_000_000_000, bps >= 10_000_000_000 ? 0 : 1)} Gbps`,
|
||||
}
|
||||
} else if (bps >= 1_000_000) {
|
||||
return {
|
||||
value: bps / 1_000_000,
|
||||
symbol: " Mbps",
|
||||
display: `${decimalString(bps / 1_000_000, bps >= 10_000_000 ? 0 : 1)} Mbps`,
|
||||
}
|
||||
} else if (bps >= 1_000) {
|
||||
return {
|
||||
value: bps / 1_000,
|
||||
symbol: " Kbps",
|
||||
display: `${decimalString(bps / 1_000, bps >= 10_000 ? 0 : 1)} Kbps`,
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
value: bps,
|
||||
symbol: " bps",
|
||||
display: `${Math.round(bps)} bps`,
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
return {
|
||||
value: mbps,
|
||||
symbol: " MB/s",
|
||||
display: `${decimalString(mbps, mbps >= 100 ? 1 : 2)} MB/s`,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert disk speed from MB/s to the specified unit */
|
||||
export function convertDiskSpeed(
|
||||
mbps: number,
|
||||
unit: SpeedUnit = "mbps"
|
||||
): SpeedConversion {
|
||||
switch (unit) {
|
||||
case "bps": {
|
||||
const bps = mbps * 8 * 1_000_000 // Convert MB/s to bits per second
|
||||
|
||||
// Format large numbers appropriately
|
||||
if (bps >= 1_000_000_000) {
|
||||
return {
|
||||
value: bps / 1_000_000_000,
|
||||
symbol: " Gbps",
|
||||
display: `${decimalString(bps / 1_000_000_000, bps >= 10_000_000_000 ? 0 : 1)} Gbps`,
|
||||
}
|
||||
} else if (bps >= 1_000_000) {
|
||||
return {
|
||||
value: bps / 1_000_000,
|
||||
symbol: " Mbps",
|
||||
display: `${decimalString(bps / 1_000_000, bps >= 10_000_000 ? 0 : 1)} Mbps`,
|
||||
}
|
||||
} else if (bps >= 1_000) {
|
||||
return {
|
||||
value: bps / 1_000,
|
||||
symbol: " Kbps",
|
||||
display: `${decimalString(bps / 1_000, bps >= 10_000 ? 0 : 1)} Kbps`,
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
value: bps,
|
||||
symbol: " bps",
|
||||
display: `${Math.round(bps)} bps`,
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
return {
|
||||
value: mbps,
|
||||
symbol: " MB/s",
|
||||
display: `${decimalString(mbps, mbps >= 100 ? 1 : 2)} MB/s`,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateUserSettings() {
|
||||
try {
|
||||
const req = await pb.collection("user_settings").getFirstListItem("", { fields: "settings" })
|
||||
|
||||
Reference in New Issue
Block a user