mirror of
https://github.com/Buriburizaem0n/nezha-dash-v1.git
synced 2026-02-04 12:40:10 +00:00
feat: chart history on browser
This commit is contained in:
26
src/hooks/use-chart-history.ts
Normal file
26
src/hooks/use-chart-history.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { NezhaWebsocketResponse } from "@/types/nezha-api"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export function useChartHistory<T>(
|
||||
messageHistory: { data: string }[],
|
||||
serverId: number,
|
||||
formatFn: (wsData: NezhaWebsocketResponse, serverId: number) => T | null,
|
||||
) {
|
||||
const [data, setData] = useState<T[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
if (messageHistory.length > 0 && data.length === 0) {
|
||||
const historyData = messageHistory
|
||||
.map((msg) => {
|
||||
const wsData = JSON.parse(msg.data) as NezhaWebsocketResponse
|
||||
return formatFn(wsData, serverId)
|
||||
})
|
||||
.filter((item): item is T => item !== null)
|
||||
.reverse()
|
||||
|
||||
setData(historyData)
|
||||
}
|
||||
}, [messageHistory])
|
||||
|
||||
return data
|
||||
}
|
||||
Reference in New Issue
Block a user