forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDashboardData.ts
More file actions
218 lines (196 loc) · 7.04 KB
/
useDashboardData.ts
File metadata and controls
218 lines (196 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// src/hooks/useDashboardData.ts
"use client"
import { keepPreviousData, useQuery, useQueryClient } from "@tanstack/react-query"
import { useCallback, useMemo, useState } from "react"
import { usePollingIntervals } from "@/hooks/usePollingIntervals"
import { useUpdateCheck } from "@/hooks/useUpdateCheck"
import type { DashboardAlert } from "@/lib/dashboard"
import {
computeAlerts,
computeSystemAlerts,
detectRankChanges,
fetchDismissedKeys,
postDismissAlert as persistDismiss,
} from "@/lib/dashboard"
import { clientQueryOptions, trackerQueryOptions } from "@/lib/query-options"
import type {
DayRange,
SafeDownloadClient,
Snapshot,
TodayAtAGlance,
TrackerSummary,
} from "@/types/api"
export function selectActiveTrackers(trackers: TrackerSummary[]) {
return trackers.filter((t) => t.isActive)
}
export function selectClientsForAlerts(clients: SafeDownloadClient[]) {
return clients.map(({ id, name, enabled, lastError }) => ({ id, name, enabled, lastError }))
}
interface DashboardData {
trackers: TrackerSummary[]
snapshotMap: Map<number, Snapshot[]>
loading: boolean
alerts: DashboardAlert[]
dayRange: DayRange
setDayRange: (range: DayRange) => void
todayData: TodayAtAGlance | null
todayLoading: boolean
dismissAlert: (key: string, type: string) => void
dismissAllAlerts: () => void
refresh: () => Promise<void>
}
interface UseDashboardDataOptions {
initialTrackers?: TrackerSummary[]
snapshotRetentionDays?: number | null
}
function useDashboardData(options?: UseDashboardDataOptions): DashboardData {
const [dayRange, setDayRange] = useState<DayRange>(30)
const queryClient = useQueryClient()
const updateCheck = useUpdateCheck()
const intervals = usePollingIntervals()
const dismissedQuery = useQuery({
queryKey: ["dismissed-alert-keys"],
queryFn: fetchDismissedKeys,
staleTime: Infinity,
})
const [localDismissedKeys, setLocalDismissedKeys] = useState<Set<string>>(new Set())
// Merge server-fetched dismissed keys with locally-dismissed keys
const dismissedKeys = useMemo(() => {
const merged = new Set(dismissedQuery.data ?? [])
for (const key of localDismissedKeys) {
merged.add(key)
}
return merged
}, [dismissedQuery.data, localDismissedKeys])
// Tracker list query
const trackersQuery = useQuery({
...trackerQueryOptions,
refetchInterval: intervals.trackerRefetchMs,
select: selectActiveTrackers,
initialData: options?.initialTrackers,
initialDataUpdatedAt: options?.initialTrackers ? Date.now() : undefined,
})
const trackers = trackersQuery.data ?? []
// Fleet snapshot query (all trackers in one request)
const fleetSnapshotsQuery = useQuery({
queryKey: ["tracker-snapshots-fleet", dayRange],
queryFn: async ({ signal }) => {
const url =
dayRange === 0
? "/api/trackers/snapshots/fleet"
: `/api/trackers/snapshots/fleet?days=${dayRange}`
const res = await fetch(url, { signal })
if (!res.ok) throw new Error(`Fleet snapshot fetch failed: ${res.status}`)
return res.json() as Promise<Record<string, Snapshot[]>>
},
placeholderData: keepPreviousData,
refetchInterval: intervals.trackerRefetchMs,
})
const todayQuery = useQuery({
queryKey: ["dashboard-today"],
queryFn: async ({ signal }): Promise<TodayAtAGlance> => {
const res = await fetch("/api/dashboard/today", { signal })
if (!res.ok) throw new Error("Failed to fetch today data")
return res.json()
},
staleTime: intervals.trackerRefetchMs,
refetchInterval: intervals.trackerRefetchMs,
})
// Secondary queries for alert computation (sidebar's 10s poll drives fetches)
const clientsQuery = useQuery({
...clientQueryOptions,
select: selectClientsForAlerts,
})
const backupQuery = useQuery({
queryKey: ["backup-history-for-alerts"],
queryFn: async ({ signal }) => {
const res = await fetch("/api/settings/backup/history", { signal })
if (!res.ok) throw new Error(`Backup history fetch failed: ${res.status}`)
return res.json() as Promise<{ createdAt: string; status: string }[]>
},
refetchInterval: intervals.clientRefetchMs,
})
// Derived: snapshotMap (built from fleet response keyed by tracker ID)
const snapshotMap = useMemo(() => {
const map = new Map<number, Snapshot[]>()
const data = fleetSnapshotsQuery.data
if (data) {
for (const [key, snapshots] of Object.entries(data)) {
map.set(Number(key), snapshots)
}
}
return map
}, [fleetSnapshotsQuery.data])
// Derived: alerts
// System alerts depend on client-only queries (update check, clients, backups).
// Only include them once those queries have fetched to avoid SSR/client mismatch.
const clientQueriesReady =
!updateCheck.loading &&
!clientsQuery.isLoading &&
!backupQuery.isLoading &&
!clientsQuery.isError &&
!backupQuery.isError
const visibleAlerts = useMemo(() => {
const trackerAlerts = computeAlerts(trackers)
const rankAlerts = detectRankChanges(trackers, snapshotMap, 7)
const systemAlerts = clientQueriesReady
? computeSystemAlerts({
latestVersion: updateCheck.latestVersion ?? undefined,
currentVersion: process.env.NEXT_PUBLIC_APP_VERSION ?? "0.0.0",
failedBackups: (backupQuery.data ?? []).filter((b) => b.status === "failed"),
clients: clientsQuery.data ?? [],
snapshotRetentionDays: options?.snapshotRetentionDays ?? null,
})
: []
const combined = [...trackerAlerts, ...rankAlerts, ...systemAlerts]
return combined.filter((a) => !dismissedKeys.has(a.key))
}, [
trackers,
snapshotMap,
clientQueriesReady,
updateCheck.latestVersion,
backupQuery.data,
clientsQuery.data,
dismissedKeys,
options?.snapshotRetentionDays,
])
// Dismiss handlers
const dismissAlert = useCallback((key: string, type: string) => {
persistDismiss(key, type)
setLocalDismissedKeys((prev) => new Set([...prev, key]))
}, [])
const dismissAllAlerts = useCallback(() => {
const dismissible = visibleAlerts.filter((a) => a.dismissible !== false)
for (const a of dismissible) {
persistDismiss(a.key, a.type)
}
setLocalDismissedKeys((prev) => {
const next = new Set(prev)
for (const a of dismissible) {
next.add(a.key)
}
return next
})
}, [visibleAlerts])
const refresh = useCallback(async () => {
await queryClient.refetchQueries({ queryKey: trackerQueryOptions.queryKey })
queryClient.invalidateQueries({ queryKey: ["tracker-snapshots-fleet"] })
queryClient.invalidateQueries({ queryKey: ["download-client-snapshots"] })
queryClient.invalidateQueries({ queryKey: ["dashboard-today"] })
}, [queryClient])
return {
trackers,
snapshotMap,
todayData: todayQuery.data ?? null,
todayLoading: todayQuery.isLoading,
loading: trackersQuery.isLoading,
alerts: visibleAlerts,
dayRange,
setDayRange,
dismissAlert,
dismissAllAlerts,
refresh,
}
}
export type { DashboardData, UseDashboardDataOptions }
export { useDashboardData }