-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataPanel.tsx
More file actions
299 lines (273 loc) · 12.6 KB
/
DataPanel.tsx
File metadata and controls
299 lines (273 loc) · 12.6 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { useState } from 'react';
import { Radio, RefreshCw, Copy, AlertTriangle, CheckCircle2, XCircle } from 'lucide-react';
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible';
import { JsonFormViewer } from '@/components/JsonFormViewer';
import { TopicPublishForm } from '@/components/TopicPublishForm';
import type { ComponentTopic, TopicEndpoint, QosProfile, SovdResourceEntityType } from '@/lib/types';
import { cn } from '@/lib/utils';
import { useAppStore } from '@/lib/store';
interface DataPanelProps {
/** Data item from the API */
topic: ComponentTopic;
/** Entity ID for publishing */
entityId: string;
/** Entity type for API endpoint */
entityType?: SovdResourceEntityType;
/** Whether a refresh is in progress */
isRefreshing?: boolean;
/** Callback when refresh is requested */
onRefresh?: () => void;
}
/**
* Format QoS profile for display
*/
function formatQos(qos: QosProfile): string {
const parts = [
qos.reliability !== 'unknown' ? qos.reliability : null,
qos.durability !== 'volatile' ? qos.durability : null,
qos.history === 'keep_last' ? `depth=${qos.depth}` : qos.history,
].filter(Boolean);
return parts.length > 0 ? parts.join(', ') : 'default';
}
/**
* Check if QoS profiles are compatible between publishers and subscribers
*/
function checkQosCompatibility(
publishers: TopicEndpoint[],
subscribers: TopicEndpoint[]
): {
compatible: boolean;
warning?: string;
} {
if (publishers.length === 0 || subscribers.length === 0) {
return { compatible: true };
}
// Check reliability mismatch (RELIABLE sub needs RELIABLE pub)
const reliableSubs = subscribers.filter((s) => s.qos.reliability === 'reliable');
const bestEffortPubs = publishers.filter((p) => p.qos.reliability === 'best_effort');
if (reliableSubs.length > 0 && bestEffortPubs.length > 0) {
return {
compatible: false,
warning: 'QoS mismatch: Reliable subscribers cannot receive from best_effort publishers',
};
}
// Check durability mismatch (TRANSIENT_LOCAL sub may not get late-joining data from VOLATILE pub)
const transientSubs = subscribers.filter((s) => s.qos.durability === 'transient_local');
const volatilePubs = publishers.filter((p) => p.qos.durability === 'volatile');
if (transientSubs.length > 0 && volatilePubs.length > 0) {
return {
compatible: true,
warning: 'Transient local subscribers may miss late-joining data from volatile publishers',
};
}
return { compatible: true };
}
/**
* Connection Status Section
*/
function ConnectionStatus({ topic }: { topic: ComponentTopic }) {
const pubCount = topic.publisher_count ?? topic.publishers?.length ?? 0;
const subCount = topic.subscriber_count ?? topic.subscribers?.length ?? 0;
const hasData = topic.status === 'data' && topic.data !== null && topic.data !== undefined;
const qosCheck = checkQosCompatibility(topic.publishers || [], topic.subscribers || []);
const statusIcon = hasData ? (
<CheckCircle2 className="w-4 h-4 text-green-500" />
) : pubCount > 0 ? (
<AlertTriangle className="w-4 h-4 text-amber-500" />
) : (
<XCircle className="w-4 h-4 text-muted-foreground" />
);
const statusText = hasData ? 'Active' : pubCount > 0 ? 'Waiting for data' : 'No publishers';
return (
<div className="rounded-lg border bg-muted/30 p-3 space-y-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
{statusIcon}
<span className="text-sm font-medium">{statusText}</span>
</div>
<div className="flex items-center gap-4 text-sm">
<span className="flex items-center gap-1">
<Badge variant="outline" className="text-xs">
{pubCount} pub
</Badge>
</span>
<span className="flex items-center gap-1">
<Badge variant="outline" className="text-xs">
{subCount} sub
</Badge>
</span>
</div>
</div>
{/* QoS Warning */}
{qosCheck.warning && (
<div
className={cn(
'flex items-start gap-2 text-xs p-2 rounded',
qosCheck.compatible ? 'bg-amber-500/10 text-amber-600' : 'bg-destructive/10 text-destructive'
)}
>
<AlertTriangle className="w-3.5 h-3.5 mt-0.5 shrink-0" />
<span>{qosCheck.warning}</span>
</div>
)}
</div>
);
}
/**
* QoS Details Section
*/
function QosDetails({ publishers, subscribers }: { publishers?: TopicEndpoint[]; subscribers?: TopicEndpoint[] }) {
const [isOpen, setIsOpen] = useState(false);
if ((!publishers || publishers.length === 0) && (!subscribers || subscribers.length === 0)) {
return null;
}
return (
<Collapsible open={isOpen} onOpenChange={setIsOpen}>
<CollapsibleTrigger asChild>
<Button variant="ghost" size="sm" className="w-full justify-between h-8 px-2">
<span className="text-xs font-medium">QoS Details</span>
<span className="text-xs text-muted-foreground">{isOpen ? 'Hide' : 'Show'}</span>
</Button>
</CollapsibleTrigger>
<CollapsibleContent className="space-y-3 pt-2">
{publishers && publishers.length > 0 && (
<div>
<div className="text-xs font-medium text-muted-foreground mb-1">Publishers</div>
<div className="space-y-1">
{publishers.map((pub, idx) => (
<div
key={idx}
className="flex items-center justify-between text-xs p-1.5 rounded bg-muted/50"
>
<span className="font-mono truncate">{pub.fqn}</span>
<span className="text-muted-foreground">{formatQos(pub.qos)}</span>
</div>
))}
</div>
</div>
)}
{subscribers && subscribers.length > 0 && (
<div>
<div className="text-xs font-medium text-muted-foreground mb-1">Subscribers</div>
<div className="space-y-1">
{subscribers.map((sub, idx) => (
<div
key={idx}
className="flex items-center justify-between text-xs p-1.5 rounded bg-muted/50"
>
<span className="font-mono truncate">{sub.fqn}</span>
<span className="text-muted-foreground">{formatQos(sub.qos)}</span>
</div>
))}
</div>
</div>
)}
</CollapsibleContent>
</Collapsible>
);
}
/**
* DataPanel - Full diagnostic view for a data item
*/
export function DataPanel({
topic,
entityId,
entityType = 'components',
isRefreshing = false,
onRefresh,
}: DataPanelProps) {
const [publishValue, setPublishValue] = useState<unknown>(topic.type_info?.default_value || topic.data || {});
const isConnected = useAppStore((state) => state.isConnected);
const hasData = topic.status === 'data' && topic.data !== null && topic.data !== undefined;
// `access` is the explicit per-item write capability; when present it
// overrides the legacy "any typed topic is publishable" heuristic so a
// read-only data item never surfaces a write form.
const canWrite = isConnected && topic.access !== 'read' && !!(topic.type || topic.type_info || topic.data);
// Use "Write Value" when the gateway told us this is a writable scalar
// (access === 'write' / 'readwrite'); fall back to "Publish Message" for
// streaming topics where the operation really is a publish.
const writeSectionLabel =
topic.access === 'write' || topic.access === 'readwrite' ? 'Write Value' : 'Publish Message';
const handleCopyFromLast = () => {
if (topic.data) {
setPublishValue(JSON.parse(JSON.stringify(topic.data)));
}
};
return (
<Card>
<CardHeader>
<div className="flex items-start justify-between">
<div className="flex items-center gap-3">
<Radio className={cn('w-5 h-5 shrink-0', hasData ? 'text-primary' : 'text-muted-foreground')} />
<div>
<div className="flex items-center gap-2 flex-wrap">
<CardTitle className="text-base">{topic.topic}</CardTitle>
{topic.type && (
<span className="text-xs text-muted-foreground font-mono bg-muted px-1.5 py-0.5 rounded">
{topic.type}
</span>
)}
</div>
<CardDescription className="text-xs mt-1">Data diagnostics and access</CardDescription>
</div>
</div>
<Button variant="outline" size="sm" onClick={onRefresh} disabled={isRefreshing}>
<RefreshCw className={cn('w-4 h-4 mr-2', isRefreshing && 'animate-spin')} />
Refresh
</Button>
</div>
</CardHeader>
<CardContent className="space-y-4">
{/* Connection Status */}
<ConnectionStatus topic={topic} />
{/* QoS Details (collapsible) */}
<QosDetails publishers={topic.publishers} subscribers={topic.subscribers} />
{/* Last Received Value */}
<div className="space-y-2">
<div className="flex items-center justify-between">
<span className="text-sm font-medium">Last Received Value</span>
{hasData && (
<Button variant="ghost" size="sm" onClick={handleCopyFromLast} className="h-7 text-xs">
<Copy className="w-3 h-3 mr-1" />
Copy to Publish
</Button>
)}
</div>
{hasData ? (
<JsonFormViewer
data={topic.data}
schema={topic.type_info?.schema}
editable={false}
timestamp={topic.timestamp}
/>
) : (
<div className="rounded-lg border bg-muted/30 p-4 text-center">
<p className="text-sm text-muted-foreground">
{topic.status === 'metadata_only'
? 'No data received yet. Schema available for publishing.'
: 'Topic exists but is not publishing messages.'}
</p>
</div>
)}
</div>
{/* Write/Publish Section */}
{canWrite && (
<div className="border-t pt-4 space-y-2">
<span className="text-sm font-medium">{writeSectionLabel}</span>
<TopicPublishForm
topic={topic}
entityId={entityId}
entityType={entityType}
initialValue={publishValue}
onValueChange={setPublishValue}
/>
</div>
)}
</CardContent>
</Card>
);
}
export default DataPanel;