forked from Lumina-etwork/Lumina-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacilityDashboard.tsx
More file actions
241 lines (225 loc) · 9.03 KB
/
Copy pathFacilityDashboard.tsx
File metadata and controls
241 lines (225 loc) · 9.03 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
'use client'
import { useEffect, useState } from 'react'
import { create } from 'zustand'
import { NodeList } from '@/src/components/network/NodeList'
import { AlertFeed } from '@/src/components/dashboard/AlertFeed'
import { ActivityLogList } from '@/src/components/activity/ActivityLogList'
import { SkeletonCard } from '@/src/components/skeleton/SkeletonCard'
import { SkeletonChart } from '@/src/components/skeleton/SkeletonChart'
import { SolarBatteryGauge } from '@/src/components/node/SolarBatteryGauge'
import { useSkeletonTiming } from '@/src/hooks/useSkeletonTiming'
import { useActivityLogSubscription } from '@/src/hooks/useActivityLogSubscription'
import type { NodePosition } from '@/src/types/network'
import { getNodePowerSource } from '@/src/hooks/useNodeStatus'
interface DashboardStore {
nodesReady: boolean
alertsReady: boolean
metricsReady: boolean
setNodesReady: () => void
setAlertsReady: () => void
setMetricsReady: () => void
}
const useDashboardStore = create<DashboardStore>((set) => ({
nodesReady: false,
alertsReady: false,
metricsReady: false,
setNodesReady: () => set({ nodesReady: true }),
setAlertsReady: () => set({ alertsReady: true }),
setMetricsReady: () => set({ metricsReady: true }),
}))
const MOCK_NODES: NodePosition[] = Array.from({ length: 12 }, (_, i) => ({
id: `node-${i + 1}`,
x: Math.random() * 800,
y: Math.random() * 600,
z: Math.random() * 100,
label: `Validator ${String.fromCharCode(65 + i)}`,
color: ['#0f766e', '#9a3412', '#2563eb', '#7c3aed', '#db2777', '#ca8a04'][i % 6],
metadata: {
description: `Network node ${i + 1}`,
location: ['us-east', 'eu-west', 'ap-southeast', 'us-west', 'eu-central', 'ap-northeast'][i % 6],
ownerName: `Org ${String.fromCharCode(65 + i)}`,
firmwareVersion: `v${Math.floor(Math.random() * 5)}.${Math.floor(Math.random() * 10)}`,
hardwareModel: ['X1', 'P2', 'Z3', 'Q4'][i % 4],
ipAddress: `10.0.${Math.floor(i / 4)}.${(i % 4) * 64 + 1}`,
uptime: `${Math.floor(Math.random() * 365)}d ${Math.floor(Math.random() * 24)}h`,
powerSource: (i % 4 === 0 ? 'grid' : i % 3 === 0 ? 'battery' : 'solar'),
},
}))
function NodeSectionSkeleton() {
return (
<div className="rounded-lg border border-[#d8d0c1] bg-white">
<div className="flex items-center justify-between border-b border-[#d8d0c1] px-5 py-3">
<div>
<div className="skeleton skeleton-text skeleton-text--lg" style={{ width: 60, height: 16, margin: 0 }} />
<div className="skeleton skeleton-text skeleton-text--sm" style={{ width: 100, height: 12, margin: '4px 0 0' }} />
</div>
</div>
<div className="space-y-2 p-3">
{Array.from({ length: 4 }, (_, i) => (
<SkeletonCard key={i} variant="node" />
))}
</div>
</div>
)
}
function AlertSectionSkeleton() {
return (
<div>
<div className="flex items-center gap-2 mb-3">
<div className="skeleton skeleton-text skeleton-text--md" style={{ width: 80, height: 16, margin: 0 }} />
<div className="skeleton" style={{ width: 24, height: 20, borderRadius: 9999 }} />
</div>
{Array.from({ length: 3 }, (_, i) => (
<div key={i} className="mb-3">
<SkeletonCard variant="alert" />
</div>
))}
</div>
)
}
export function FacilityDashboard() {
const { events: activityEvents } = useActivityLogSubscription(10_000)
const [nodesData, setNodesData] = useState<NodePosition[] | null>(null)
const setNodesReady = useDashboardStore((s) => s.setNodesReady)
const setAlertsReady = useDashboardStore((s) => s.setAlertsReady)
const setMetricsReady = useDashboardStore((s) => s.setMetricsReady)
const solarNodes = (nodesData ?? []).filter((node) => getNodePowerSource(node) !== 'grid')
const nodesSkeleton = useSkeletonTiming(
() => useDashboardStore.getState().nodesReady,
useDashboardStore.subscribe,
)
const alertsSkeleton = useSkeletonTiming(
() => useDashboardStore.getState().alertsReady,
useDashboardStore.subscribe,
)
const metricsSkeleton = useSkeletonTiming(
() => useDashboardStore.getState().metricsReady,
useDashboardStore.subscribe,
)
useEffect(() => {
const nodesTimer = setTimeout(() => {
setNodesData(MOCK_NODES)
setNodesReady()
}, 800)
const alertsTimer = setTimeout(() => {
setAlertsReady()
}, 1200)
const metricsTimer = setTimeout(() => {
setMetricsReady()
}, 2000)
return () => {
clearTimeout(nodesTimer)
clearTimeout(alertsTimer)
clearTimeout(metricsTimer)
}
}, [setNodesReady, setAlertsReady, setMetricsReady])
return (
<main className="min-h-screen bg-[#f7f4ee] text-[#171512]">
<div className="mx-auto flex min-h-screen w-full max-w-7xl flex-col px-5 py-5 sm:px-8 lg:px-10">
<header className="flex flex-col gap-4 border-b border-[#d8d0c1] pb-5 sm:flex-row sm:items-center sm:justify-between">
<div>
<p className="text-sm font-semibold uppercase tracking-[0.22em] text-[#6f5f48]">
Facility Monitor
</p>
<h1 className="mt-2 text-3xl font-semibold text-[#171512] sm:text-4xl">
Network Dashboard
</h1>
</div>
</header>
<div className="grid grid-cols-1 gap-6 py-6 lg:grid-cols-3">
<section className="lg:col-span-2">
<h2 className="sr-only">Node List</h2>
{nodesSkeleton.showSkeleton ? (
<>
{nodesSkeleton.timedOut && (
<div className="skeleton-timeout">
<span>Taking longer than expected</span>
</div>
)}
<NodeSectionSkeleton />
</>
) : (
<div className="skeleton-fade-active">
<NodeList nodes={nodesData ?? []} maxDisplay={10} />
</div>
)}
</section>
<section className="lg:col-span-1">
<h2 className="sr-only">Alerts</h2>
{alertsSkeleton.showSkeleton ? (
<>
{alertsSkeleton.timedOut && (
<div className="skeleton-timeout">
<span>Taking longer than expected</span>
</div>
)}
<AlertSectionSkeleton />
</>
) : (
<div className="skeleton-fade-active">
<AlertFeed />
</div>
)}
</section>
</div>
{solarNodes.length > 0 && (
<section className="mb-6">
<h2 className="text-lg font-semibold text-[#171512] mb-4">Solar Battery Forecasts</h2>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
{solarNodes.slice(0, 3).map((node) => (
<SolarBatteryGauge
key={node.id}
facilityId={String(node.metadata?.location ?? 'us-east')}
nodeLabel={node.label ?? node.id}
/>
))}
</div>
</section>
)}
<section>
<h2 className="text-lg font-semibold text-[#171512] mb-4">Network Metrics</h2>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
{['Latency', 'Throughput', 'Packet Loss', 'Uptime'].map((metric) => (
<div key={metric}>
{metricsSkeleton.showSkeleton ? (
<SkeletonCard variant="metric" />
) : (
<div className="skeleton-fade-active rounded-lg border border-[#d8d0c1] bg-white p-4">
<p className="text-xs font-semibold uppercase tracking-wider text-[#6f5f48]">
{metric}
</p>
<p className="mt-2 text-2xl font-semibold text-[#171512]">
{metric === 'Latency' && '24ms'}
{metric === 'Throughput' && '1,247/s'}
{metric === 'Packet Loss' && '0.02%'}
{metric === 'Uptime' && '99.97%'}
</p>
<p className="mt-1 text-xs text-[#6f5f48]">
{metric === 'Latency' && '12ms avg'}
{metric === 'Throughput' && 'Peak: 2,100/s'}
{metric === 'Packet Loss' && 'Last 24h'}
{metric === 'Uptime' && '30d rolling'}
</p>
</div>
)}
</div>
))}
</div>
<div className="mt-6">
<ActivityLogList events={activityEvents} />
</div>
<div className="mt-6">
{metricsSkeleton.showSkeleton ? (
<SkeletonChart bars={16} height={220} />
) : (
<div className="skeleton-fade-active rounded-lg border border-[#d8d0c1] bg-white p-4">
<h3 className="text-sm font-semibold text-[#171512] mb-4">Throughput Over Time</h3>
<SkeletonChart bars={16} height={220} />
</div>
)}
</div>
</section>
</div>
</main>
)
}