mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 01:39:34 +08:00
refactor: replace useLocalStorage with useBrowserStorage
This commit is contained in:
@@ -14,7 +14,7 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
|
|||||||
import { Input } from "@/components/ui/input"
|
import { Input } from "@/components/ui/input"
|
||||||
import { Label } from "@/components/ui/label"
|
import { Label } from "@/components/ui/label"
|
||||||
import { $publicKey } from "@/lib/stores"
|
import { $publicKey } from "@/lib/stores"
|
||||||
import { cn, generateToken, tokenMap, useLocalStorage } from "@/lib/utils"
|
import { cn, generateToken, tokenMap, useBrowserStorage } from "@/lib/utils"
|
||||||
import { pb, isReadOnlyUser } from "@/lib/api"
|
import { pb, isReadOnlyUser } from "@/lib/api"
|
||||||
import { useStore } from "@nanostores/react"
|
import { useStore } from "@nanostores/react"
|
||||||
import { ChevronDownIcon, ExternalLinkIcon, PlusIcon } from "lucide-react"
|
import { ChevronDownIcon, ExternalLinkIcon, PlusIcon } from "lucide-react"
|
||||||
@@ -77,7 +77,7 @@ export const SystemDialog = ({ setOpen, system }: { setOpen: (open: boolean) =>
|
|||||||
const port = useRef<HTMLInputElement>(null)
|
const port = useRef<HTMLInputElement>(null)
|
||||||
const [hostValue, setHostValue] = useState(system?.host ?? "")
|
const [hostValue, setHostValue] = useState(system?.host ?? "")
|
||||||
const isUnixSocket = hostValue.startsWith("/")
|
const isUnixSocket = hostValue.startsWith("/")
|
||||||
const [tab, setTab] = useLocalStorage("as-tab", "docker")
|
const [tab, setTab] = useBrowserStorage("as-tab", "docker")
|
||||||
const [token, setToken] = useState(system?.token ?? "")
|
const [token, setToken] = useState(system?.token ?? "")
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@@ -26,7 +26,7 @@ import {
|
|||||||
listen,
|
listen,
|
||||||
parseSemVer,
|
parseSemVer,
|
||||||
toFixedFloat,
|
toFixedFloat,
|
||||||
useLocalStorage,
|
useBrowserStorage,
|
||||||
} from "@/lib/utils"
|
} from "@/lib/utils"
|
||||||
import { getPbTimestamp, pb } from "@/lib/api"
|
import { getPbTimestamp, pb } from "@/lib/api"
|
||||||
import { Separator } from "../ui/separator"
|
import { Separator } from "../ui/separator"
|
||||||
@@ -123,7 +123,7 @@ export default function SystemDetail({ name }: { name: string }) {
|
|||||||
const systems = useStore($systems)
|
const systems = useStore($systems)
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
const maxValues = useStore($maxValues)
|
const maxValues = useStore($maxValues)
|
||||||
const [grid, setGrid] = useLocalStorage("grid", true)
|
const [grid, setGrid] = useBrowserStorage("grid", true)
|
||||||
const [system, setSystem] = useState({} as SystemRecord)
|
const [system, setSystem] = useState({} as SystemRecord)
|
||||||
const [systemStats, setSystemStats] = useState([] as SystemStatsRecord[])
|
const [systemStats, setSystemStats] = useState([] as SystemStatsRecord[])
|
||||||
const [containerData, setContainerData] = useState([] as ChartData["containerData"])
|
const [containerData, setContainerData] = useState([] as ChartData["containerData"])
|
||||||
|
@@ -59,14 +59,13 @@ export default function SystemsTable() {
|
|||||||
const { i18n, t } = useLingui()
|
const { i18n, t } = useLingui()
|
||||||
const [filter, setFilter] = useState<string>()
|
const [filter, setFilter] = useState<string>()
|
||||||
const [statusFilter, setStatusFilter] = useState<StatusFilter>("all")
|
const [statusFilter, setStatusFilter] = useState<StatusFilter>("all")
|
||||||
const [sorting, setSorting] = useLocalStorage<SortingState>("sortMode",[{ id: "system", desc: false }])
|
const [sorting, setSorting] = useBrowserStorage<SortingState>(
|
||||||
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([])
|
"sortMode",
|
||||||
const [columnVisibility, setColumnVisibility] = useLocalStorage<VisibilityState>("cols", {})
|
[{ id: "system", desc: false }],
|
||||||
const [viewMode, setViewMode] = useLocalStorage<ViewMode>(
|
sessionStorage
|
||||||
"viewMode",
|
|
||||||
// show grid view on mobile if there are less than 200 systems (looks better but table is more efficient)
|
|
||||||
window.innerWidth < 1024 && data.length < 200 ? "grid" : "table"
|
|
||||||
)
|
)
|
||||||
|
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([])
|
||||||
|
const [columnVisibility, setColumnVisibility] = useBrowserStorage<VisibilityState>("cols", {})
|
||||||
|
|
||||||
const locale = i18n.locale
|
const locale = i18n.locale
|
||||||
|
|
||||||
@@ -78,13 +77,11 @@ export default function SystemsTable() {
|
|||||||
return data.filter((system) => system.status === statusFilter)
|
return data.filter((system) => system.status === statusFilter)
|
||||||
}, [data, statusFilter])
|
}, [data, statusFilter])
|
||||||
|
|
||||||
const runningRecords = useMemo(() => {
|
const [viewMode, setViewMode] = useBrowserStorage<ViewMode>(
|
||||||
return data.filter((record) => record.status === "up").length
|
"viewMode",
|
||||||
}, [data])
|
// show grid view on mobile if there are less than 200 systems (looks better but table is more efficient)
|
||||||
|
window.innerWidth < 1024 && filteredData.length < 200 ? "grid" : "table"
|
||||||
const totalRecords = useMemo(() => {
|
)
|
||||||
return data.length
|
|
||||||
}, [data])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (filter !== undefined) {
|
if (filter !== undefined) {
|
||||||
|
@@ -152,20 +152,20 @@ export function decimalString(num: number, digits = 2) {
|
|||||||
return formatter.format(num)
|
return formatter.format(num)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get value from local storage */
|
/** Get value from local or session storage */
|
||||||
function getStorageValue(key: string, defaultValue: any) {
|
function getStorageValue(key: string, defaultValue: any, storageInterface: Storage = localStorage) {
|
||||||
const saved = localStorage?.getItem(key)
|
const saved = storageInterface?.getItem(key)
|
||||||
return saved ? JSON.parse(saved) : defaultValue
|
return saved ? JSON.parse(saved) : defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Hook to sync value in local storage */
|
/** Hook to sync value in local or session storage */
|
||||||
export function useLocalStorage<T>(key: string, defaultValue: T) {
|
export function useBrowserStorage<T>(key: string, defaultValue: T, storageInterface: Storage = localStorage) {
|
||||||
key = `besz-${key}`
|
key = `besz-${key}`
|
||||||
const [value, setValue] = useState(() => {
|
const [value, setValue] = useState(() => {
|
||||||
return getStorageValue(key, defaultValue)
|
return getStorageValue(key, defaultValue, storageInterface)
|
||||||
})
|
})
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
localStorage?.setItem(key, JSON.stringify(value))
|
storageInterface?.setItem(key, JSON.stringify(value))
|
||||||
}, [key, value])
|
}, [key, value])
|
||||||
|
|
||||||
return [value, setValue]
|
return [value, setValue]
|
||||||
|
Reference in New Issue
Block a user