mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 01:39:34 +08:00
update js deps and add package-lock.json (#192)
- replaces use-is-in-viewport package with lib/use-intersection-observer.ts due to npm dependency conflict
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -11,4 +11,3 @@ dist
|
||||
beszel/cmd/hub/hub
|
||||
beszel/cmd/agent/agent
|
||||
node_modules
|
||||
package-lock.json
|
||||
|
Binary file not shown.
4216
beszel/site/package-lock.json
generated
Normal file
4216
beszel/site/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "site",
|
||||
"name": "beszel",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
@@ -23,7 +23,7 @@
|
||||
"@radix-ui/react-toast": "^1.2.1",
|
||||
"@radix-ui/react-tooltip": "^1.1.2",
|
||||
"@tanstack/react-table": "^8.20.5",
|
||||
"@vitejs/plugin-react": "^4.3.1",
|
||||
"@vitejs/plugin-react": "^4.3.2",
|
||||
"class-variance-authority": "^0.7.0",
|
||||
"clsx": "^2.1.1",
|
||||
"cmdk": "^1.0.0",
|
||||
@@ -34,20 +34,19 @@
|
||||
"pocketbase": "^0.21.5",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
"recharts": "^2.13.0-alpha.4",
|
||||
"recharts": "^2.13.0-alpha.5",
|
||||
"tailwind-merge": "^2.5.2",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
"use-is-in-viewport": "^1.0.9",
|
||||
"valibot": "^0.36.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "^1.1.8",
|
||||
"@types/react": "^18.3.5",
|
||||
"@types/bun": "^1.1.10",
|
||||
"@types/react": "^18.3.10",
|
||||
"@types/react-dom": "^18.3.0",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"postcss": "^8.4.44",
|
||||
"tailwindcss": "^3.4.10",
|
||||
"typescript": "^5.5.4",
|
||||
"vite": "^5.4.2"
|
||||
"postcss": "^8.4.47",
|
||||
"tailwindcss": "^3.4.13",
|
||||
"typescript": "^5.6.2",
|
||||
"vite": "^5.4.8"
|
||||
}
|
||||
}
|
||||
|
@@ -6,19 +6,14 @@ import { useStore } from '@nanostores/react'
|
||||
import Spinner from '../spinner'
|
||||
import { ClockArrowUp, CpuIcon, GlobeIcon, LayoutGridIcon, MonitorIcon, XIcon } from 'lucide-react'
|
||||
import ChartTimeSelect from '../charts/chart-time-select'
|
||||
import {
|
||||
chartTimeData,
|
||||
cn,
|
||||
getPbTimestamp,
|
||||
useClampedIsInViewport,
|
||||
useLocalStorage,
|
||||
} from '@/lib/utils'
|
||||
import { chartTimeData, cn, getPbTimestamp, useLocalStorage } from '@/lib/utils'
|
||||
import { Separator } from '../ui/separator'
|
||||
import { scaleTime } from 'd3-scale'
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/tooltip'
|
||||
import { Button, buttonVariants } from '../ui/button'
|
||||
import { Input } from '../ui/input'
|
||||
import { Rows, TuxIcon } from '../ui/icons'
|
||||
import { useIntersectionObserver } from '@/lib/use-intersection-observer'
|
||||
|
||||
const CpuChart = lazy(() => import('../charts/cpu-chart'))
|
||||
const ContainerCpuChart = lazy(() => import('../charts/container-cpu-chart'))
|
||||
@@ -504,13 +499,12 @@ function ChartCard({
|
||||
grid?: boolean
|
||||
isContainerChart?: boolean
|
||||
}) {
|
||||
const target = useRef<HTMLDivElement>(null)
|
||||
const [isInViewport, wrappedTargetRef] = useClampedIsInViewport({ target: target })
|
||||
const { isIntersecting, ref } = useIntersectionObserver()
|
||||
|
||||
return (
|
||||
<Card
|
||||
className={cn('pb-2 sm:pb-4 odd:last-of-type:col-span-full', { 'col-span-full': !grid })}
|
||||
ref={wrappedTargetRef}
|
||||
ref={ref}
|
||||
>
|
||||
<CardHeader className="pb-5 pt-4 relative space-y-1 max-sm:py-3 max-sm:px-4">
|
||||
<CardTitle className="text-xl sm:text-2xl">{title}</CardTitle>
|
||||
@@ -519,7 +513,7 @@ function ChartCard({
|
||||
</CardHeader>
|
||||
<CardContent className="pl-0 w-[calc(100%-1.6em)] h-52 relative">
|
||||
{<Spinner />}
|
||||
{isInViewport && <Suspense>{children}</Suspense>}
|
||||
{isIntersecting && <Suspense>{children}</Suspense>}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
|
169
beszel/site/src/lib/use-intersection-observer.ts
Normal file
169
beszel/site/src/lib/use-intersection-observer.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
|
||||
// adapted from usehooks-ts/use-intersection-observer
|
||||
|
||||
/** The hook internal state. */
|
||||
type State = {
|
||||
/** A boolean indicating if the element is intersecting. */
|
||||
isIntersecting: boolean
|
||||
/** The intersection observer entry. */
|
||||
entry?: IntersectionObserverEntry
|
||||
}
|
||||
|
||||
/** Represents the options for configuring the Intersection Observer. */
|
||||
type UseIntersectionObserverOptions = {
|
||||
/**
|
||||
* The element that is used as the viewport for checking visibility of the target.
|
||||
* @default null
|
||||
*/
|
||||
root?: Element | Document | null
|
||||
/**
|
||||
* A margin around the root.
|
||||
* @default '0%'
|
||||
*/
|
||||
rootMargin?: string
|
||||
/**
|
||||
* A threshold indicating the percentage of the target's visibility needed to trigger the callback.
|
||||
* @default 0
|
||||
*/
|
||||
threshold?: number | number[]
|
||||
/**
|
||||
* If true, freezes the intersection state once the element becomes visible.
|
||||
* @default true
|
||||
*/
|
||||
freeze?: boolean
|
||||
/**
|
||||
* A callback function to be invoked when the intersection state changes.
|
||||
* @param {boolean} isIntersecting - A boolean indicating if the element is intersecting.
|
||||
* @param {IntersectionObserverEntry} entry - The intersection observer Entry.
|
||||
* @default undefined
|
||||
*/
|
||||
onChange?: (isIntersecting: boolean, entry: IntersectionObserverEntry) => void
|
||||
/**
|
||||
* The initial state of the intersection.
|
||||
* @default false
|
||||
*/
|
||||
initialIsIntersecting?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* The return type of the useIntersectionObserver hook.
|
||||
*
|
||||
* Supports both tuple and object destructing.
|
||||
* @param {(node: Element | null) => void} ref - The ref callback function.
|
||||
* @param {boolean} isIntersecting - A boolean indicating if the element is intersecting.
|
||||
* @param {IntersectionObserverEntry | undefined} entry - The intersection observer Entry.
|
||||
*/
|
||||
type IntersectionReturn = {
|
||||
ref: (node?: Element | null) => void
|
||||
isIntersecting: boolean
|
||||
entry?: IntersectionObserverEntry
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom hook that tracks the intersection of a DOM element with its containing element or the viewport using the [`Intersection Observer API`](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
|
||||
* @param {UseIntersectionObserverOptions} options - The options for the Intersection Observer.
|
||||
* @returns {IntersectionReturn} The ref callback, a boolean indicating if the element is intersecting, and the intersection observer entry.
|
||||
* @example
|
||||
* ```tsx
|
||||
* const { ref, isIntersecting, entry } = useIntersectionObserver({ threshold: 0.5 });
|
||||
* ```
|
||||
*/
|
||||
export function useIntersectionObserver({
|
||||
threshold = 0,
|
||||
root = null,
|
||||
rootMargin = '0%',
|
||||
freeze = true,
|
||||
initialIsIntersecting = false,
|
||||
onChange,
|
||||
}: UseIntersectionObserverOptions = {}): IntersectionReturn {
|
||||
const [ref, setRef] = useState<Element | null>(null)
|
||||
|
||||
const [state, setState] = useState<State>(() => ({
|
||||
isIntersecting: initialIsIntersecting,
|
||||
entry: undefined,
|
||||
}))
|
||||
|
||||
const callbackRef = useRef<UseIntersectionObserverOptions['onChange']>()
|
||||
|
||||
callbackRef.current = onChange
|
||||
|
||||
const frozen = state.entry?.isIntersecting && freeze
|
||||
|
||||
useEffect(() => {
|
||||
// Ensure we have a ref to observe
|
||||
if (!ref) return
|
||||
|
||||
// Ensure the browser supports the Intersection Observer API
|
||||
if (!('IntersectionObserver' in window)) return
|
||||
|
||||
// Skip if frozen
|
||||
if (frozen) return
|
||||
|
||||
let unobserve: (() => void) | undefined
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
(entries: IntersectionObserverEntry[]): void => {
|
||||
const thresholds = Array.isArray(observer.thresholds)
|
||||
? observer.thresholds
|
||||
: [observer.thresholds]
|
||||
|
||||
entries.forEach((entry) => {
|
||||
const isIntersecting =
|
||||
entry.isIntersecting &&
|
||||
thresholds.some((threshold) => entry.intersectionRatio >= threshold)
|
||||
|
||||
setState({ isIntersecting, entry })
|
||||
|
||||
if (callbackRef.current) {
|
||||
callbackRef.current(isIntersecting, entry)
|
||||
}
|
||||
|
||||
if (isIntersecting && freeze && unobserve) {
|
||||
unobserve()
|
||||
unobserve = undefined
|
||||
}
|
||||
})
|
||||
},
|
||||
{ threshold, root, rootMargin }
|
||||
)
|
||||
|
||||
observer.observe(ref)
|
||||
|
||||
return () => {
|
||||
observer.disconnect()
|
||||
}
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [
|
||||
ref,
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
JSON.stringify(threshold),
|
||||
root,
|
||||
rootMargin,
|
||||
frozen,
|
||||
freeze,
|
||||
])
|
||||
|
||||
// ensures that if the observed element changes, the intersection observer is reinitialized
|
||||
const prevRef = useRef<Element | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
!ref &&
|
||||
state.entry?.target &&
|
||||
!freeze &&
|
||||
!frozen &&
|
||||
prevRef.current !== state.entry.target
|
||||
) {
|
||||
prevRef.current = state.entry.target
|
||||
setState({ isIntersecting: initialIsIntersecting, entry: undefined })
|
||||
}
|
||||
}, [ref, state.entry, freeze, frozen, initialIsIntersecting])
|
||||
|
||||
return {
|
||||
ref: setRef,
|
||||
isIntersecting: !!state.isIntersecting,
|
||||
entry: state.entry,
|
||||
} as IntersectionReturn
|
||||
}
|
@@ -7,7 +7,6 @@ import { RecordModel, RecordSubscription } from 'pocketbase'
|
||||
import { WritableAtom } from 'nanostores'
|
||||
import { timeDay, timeHour } from 'd3-time'
|
||||
import { useEffect, useState } from 'react'
|
||||
import useIsInViewport, { CallbackRef, HookOptions } from 'use-is-in-viewport'
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
@@ -214,24 +213,6 @@ export function useYAxisWidth() {
|
||||
return { yAxisWidth, updateYAxisWidth }
|
||||
}
|
||||
|
||||
export function useClampedIsInViewport(options: HookOptions): [boolean | null, CallbackRef] {
|
||||
const [isInViewport, wrappedTargetRef] = useIsInViewport(options)
|
||||
const [wasInViewportAtleastOnce, setWasInViewportAtleastOnce] = useState(isInViewport)
|
||||
|
||||
useEffect(() => {
|
||||
setWasInViewportAtleastOnce((prev) => {
|
||||
// this will clamp it to the first true
|
||||
// received from useIsInViewport
|
||||
if (!prev) {
|
||||
return isInViewport
|
||||
}
|
||||
return prev
|
||||
})
|
||||
}, [isInViewport])
|
||||
|
||||
return [wasInViewportAtleastOnce, wrappedTargetRef]
|
||||
}
|
||||
|
||||
export function toFixedWithoutTrailingZeros(num: number, digits: number) {
|
||||
return parseFloat(num.toFixed(digits)).toString()
|
||||
}
|
||||
|
Reference in New Issue
Block a user