Skip to content

Commit

Permalink
improve useeffects for favicon and system subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
henrygd committed Oct 19, 2024
1 parent b5c158d commit 6de5dce
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion beszel/site/src/components/routes/system.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default function SystemDetail({ name }: { name: string }) {
return () => {
pb.collection('systems').unsubscribe(system.id)
}
}, [system])
}, [system.id])

const chartData: ChartData = useMemo(() => {
const lastCreated = Math.max(
Expand Down
6 changes: 3 additions & 3 deletions beszel/site/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { timeDay, timeHour } from 'd3-time'
import { useEffect, useState } from 'react'
import { CpuIcon, HardDriveIcon, MemoryStickIcon, ServerIcon } from 'lucide-react'
import { EthernetIcon, ThermometerIcon } from '@/components/ui/icons'
import { newQueue, Queue } from '@henrygd/queue'

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand Down Expand Up @@ -103,8 +102,9 @@ export const formatDay = (timestamp: string) => {
return dayFormatter.format(new Date(timestamp))
}

export const updateFavicon = (newIcon: string) =>
((document.querySelector("link[rel='icon']") as HTMLLinkElement).href = `/static/${newIcon}`)
export const updateFavicon = (newIcon: string) => {
;(document.querySelector("link[rel='icon']") as HTMLLinkElement).href = `/static/${newIcon}`
}

export const isAdmin = () => pb.authStore.model?.role === 'admin'
export const isReadOnlyUser = () => pb.authStore.model?.role === 'readonly'
Expand Down
12 changes: 5 additions & 7 deletions beszel/site/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,27 @@ const App = () => {
updateUserSettings()
// get alerts after system list is loaded
updateSystemList().then(updateAlerts)

return () => updateFavicon('favicon.svg')
}, [])

// update favicon
useEffect(() => {
if (!authenticated || !systems.length) {
if (!systems.length || !authenticated) {
updateFavicon('favicon.svg')
} else {
let up = false
for (const system of systems) {
if (system.status === 'down') {
updateFavicon('favicon-red.svg')
return () => updateFavicon('favicon.svg')
return
} else if (system.status === 'up') {
up = true
}
}
updateFavicon(up ? 'favicon-green.svg' : 'favicon.svg')
return () => updateFavicon('favicon.svg')
}
return () => {
updateFavicon('favicon.svg')
}
}, [authenticated, systems])
}, [systems])

if (!page) {
return <h1 className="text-3xl text-center my-14">404</h1>
Expand Down

0 comments on commit 6de5dce

Please sign in to comment.