-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAppsPanel.tsx
More file actions
342 lines (318 loc) · 17.3 KB
/
Copy pathAppsPanel.tsx
File metadata and controls
342 lines (318 loc) · 17.3 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { useState, useEffect, useMemo } from 'react';
import { useShallow } from 'zustand/shallow';
import { AlertTriangle, Box, ChevronRight, Cpu, Database, FileCode, Network, Settings, Zap } from 'lucide-react';
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { useAppStore } from '@/lib/store';
import {
RESOURCE_TABS,
renderResourceTabContent,
isResourceTabId,
SCRIPTS_TAB,
type ResourceTabId,
} from '@/components/ResourceTabs';
import { EntityStatusControl } from '@/components/EntityStatusControl';
import type { ComponentTopic, Operation, Fault } from '@/lib/types';
type AppTab = 'overview' | ResourceTabId;
interface TabConfig {
id: AppTab;
label: string;
icon: typeof Database;
}
const BASE_APP_TABS: TabConfig[] = [{ id: 'overview', label: 'Overview', icon: Cpu }, ...RESOURCE_TABS];
interface AppsPanelProps {
appId: string;
appName?: string;
fqn?: string;
nodeName?: string;
namespace?: string;
componentId?: string;
path: string;
onNavigate?: (path: string) => void;
}
/**
* Apps Panel - displays app (ROS 2 node) entity details
*
* Apps are individual ROS 2 nodes in SOVD. They have:
* - Data (topics they publish/subscribe to)
* - Operations (services/actions they provide)
* - Configurations (parameters)
* - Faults (diagnostic trouble codes)
*/
export function AppsPanel({ appId, appName, fqn, nodeName, namespace, componentId, path, onNavigate }: AppsPanelProps) {
const [activeTab, setActiveTab] = useState<AppTab>('overview');
const [topics, setTopics] = useState<ComponentTopic[]>([]);
const [operations, setOperations] = useState<Operation[]>([]);
const [faults, setFaults] = useState<Fault[]>([]);
const [isLoading, setIsLoading] = useState(false);
const { selectEntity, configurations, fetchEntityData, fetchEntityOperations, listEntityFaults, scriptsSupported } =
useAppStore(
useShallow((state) => ({
selectEntity: state.selectEntity,
configurations: state.configurations,
fetchEntityData: state.fetchEntityData,
fetchEntityOperations: state.fetchEntityOperations,
listEntityFaults: state.listEntityFaults,
scriptsSupported: state.scriptsSupported,
}))
);
const appTabs = useMemo(
() => (scriptsSupported ? [...BASE_APP_TABS, SCRIPTS_TAB] : BASE_APP_TABS),
[scriptsSupported]
);
// Fall back to the default tab when the Scripts tab disappears (e.g. the
// gateway capability flips off) while it is the active tab.
useEffect(() => {
if (!scriptsSupported && activeTab === 'scripts') setActiveTab('overview');
}, [scriptsSupported, activeTab]);
// Load app resources on mount (configurations are loaded by ConfigurationPanel)
useEffect(() => {
const loadAppData = async () => {
setIsLoading(true);
try {
// Load resources in parallel (configurations handled by ConfigurationPanel)
const [topicsData, opsData, faultsData] = await Promise.all([
fetchEntityData('apps', appId).catch(() => [] as ComponentTopic[]),
fetchEntityOperations('apps', appId).catch(() => [] as Operation[]),
listEntityFaults('apps', appId).catch(() => ({ items: [] as Fault[], count: 0 })),
]);
setTopics(topicsData);
setOperations(opsData);
setFaults(faultsData.items);
} catch (error) {
console.error('Failed to load app data:', error);
} finally {
setIsLoading(false);
}
};
loadAppData();
}, [fetchEntityData, fetchEntityOperations, listEntityFaults, appId]);
const handleResourceClick = (resourcePath: string) => {
if (onNavigate) {
onNavigate(resourcePath);
} else {
selectEntity(resourcePath);
}
};
// Count resources for badges
const publishTopics = topics.filter((t) => t.isPublisher);
const subscribeTopics = topics.filter((t) => t.isSubscriber);
const activeFaults = faults.filter((f) => f.status === 'active');
return (
<div className="space-y-6">
{/* App Header */}
<Card>
<CardHeader>
<div className="flex items-center gap-3">
<div className="p-2 rounded-lg bg-emerald-100 dark:bg-emerald-900">
<Cpu className="w-5 h-5 text-emerald-600 dark:text-emerald-400" />
</div>
<div className="min-w-0 flex-1">
<CardTitle className="text-lg truncate">{appName || nodeName || appId}</CardTitle>
<CardDescription className="flex items-center gap-2 flex-wrap">
<Badge variant="outline" className="text-emerald-600 border-emerald-300">
app
</Badge>
{componentId && (
<>
<span className="text-muted-foreground">•</span>
<Button
variant="link"
className="h-auto p-0 text-xs text-muted-foreground hover:text-primary"
onClick={() => {
const areaSegment =
namespace && namespace.trim().length > 0
? namespace.split('/').filter(Boolean)[0] || 'root'
: 'root';
handleResourceClick(`/${areaSegment}/${componentId}`);
}}
>
<Box className="w-3 h-3 mr-1" />
{componentId}
</Button>
</>
)}
</CardDescription>
</div>
</div>
{/* Lifecycle status control (gateway 0.6.0 lifecycle API) */}
<div className="mt-4">
<EntityStatusControl entityType="apps" entityId={appId} />
</div>
</CardHeader>
{/* Tab Navigation */}
<div className="px-6 pb-4">
<div className="flex gap-1 p-1 bg-muted rounded-lg overflow-x-auto">
{appTabs.map((tab) => {
const TabIcon = tab.icon;
const isActive = activeTab === tab.id;
let count = 0;
if (tab.id === 'data') count = topics.length;
if (tab.id === 'operations') count = operations.length;
if (tab.id === 'configurations') count = configurations.get(appId)?.length || 0;
if (tab.id === 'faults') count = activeFaults.length;
return (
<button
key={tab.id}
onClick={() => setActiveTab(tab.id)}
className={`flex items-center justify-center gap-1.5 px-3 py-2 rounded-md text-sm font-medium transition-colors whitespace-nowrap ${
isActive
? 'bg-background text-foreground shadow-sm'
: 'text-muted-foreground hover:text-foreground hover:bg-background/50'
}`}
>
<TabIcon className="w-4 h-4" />
{tab.label}
{count > 0 && (
<Badge
variant={isActive ? 'default' : 'secondary'}
className={`ml-1 h-5 px-1.5 ${tab.id === 'faults' && count > 0 ? 'bg-red-500 text-white' : ''}`}
>
{count}
</Badge>
)}
</button>
);
})}
</div>
</div>
</Card>
{/* Tab Content */}
{activeTab === 'overview' && (
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base">Node Information</CardTitle>
</CardHeader>
<CardContent>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="p-3 rounded-lg bg-muted/50">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<FileCode className="w-4 h-4" />
<span>Node Name</span>
</div>
<p className="font-mono text-sm mt-1">{nodeName || appId}</p>
</div>
<div className="p-3 rounded-lg bg-muted/50">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Network className="w-4 h-4" />
<span>Namespace</span>
</div>
<p className="font-mono text-sm mt-1">{namespace || '/'}</p>
</div>
<div className="p-3 rounded-lg bg-muted/50 md:col-span-2">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Cpu className="w-4 h-4" />
<span>Fully Qualified Name</span>
</div>
<p className="font-mono text-sm mt-1">
{fqn || `${namespace || '/'}${nodeName || appId}`}
</p>
</div>
</div>
{/* Resource Summary */}
<div className="mt-6 grid grid-cols-2 md:grid-cols-4 gap-3">
<button
onClick={() => setActiveTab('data')}
className="p-3 rounded-lg border hover:bg-accent/50 transition-colors text-left"
>
<Database className="w-4 h-4 text-blue-500 mb-1" />
<div className="text-2xl font-semibold">{topics.length}</div>
<div className="text-xs text-muted-foreground">Topics</div>
</button>
<button
onClick={() => setActiveTab('operations')}
className="p-3 rounded-lg border hover:bg-accent/50 transition-colors text-left"
>
<Zap className="w-4 h-4 text-amber-500 mb-1" />
<div className="text-2xl font-semibold">{operations.length}</div>
<div className="text-xs text-muted-foreground">Operations</div>
</button>
<button
onClick={() => setActiveTab('configurations')}
className="p-3 rounded-lg border hover:bg-accent/50 transition-colors text-left"
>
<Settings className="w-4 h-4 text-purple-500 mb-1" />
<div className="text-2xl font-semibold">{configurations.get(appId)?.length || 0}</div>
<div className="text-xs text-muted-foreground">Parameters</div>
</button>
<button
onClick={() => setActiveTab('faults')}
className="p-3 rounded-lg border hover:bg-accent/50 transition-colors text-left"
>
<AlertTriangle
className={`w-4 h-4 mb-1 ${activeFaults.length > 0 ? 'text-red-500' : 'text-muted-foreground'}`}
/>
<div
className={`text-2xl font-semibold ${activeFaults.length > 0 ? 'text-red-500' : ''}`}
>
{activeFaults.length}
</div>
<div className="text-xs text-muted-foreground">Active Faults</div>
</button>
</div>
</CardContent>
</Card>
)}
{activeTab === 'data' && (
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base flex items-center gap-2">
<Database className="w-4 h-4 text-blue-500" />
Topics
</CardTitle>
<CardDescription>
{publishTopics.length} published, {subscribeTopics.length} subscribed
</CardDescription>
</CardHeader>
<CardContent>
{topics.length === 0 ? (
<div className="text-center text-muted-foreground py-4">
No topics available for this app.
</div>
) : (
<div className="space-y-2">
{topics.map((topic, idx) => {
const cleanName = topic.topic.startsWith('/') ? topic.topic.slice(1) : topic.topic;
const encodedName = encodeURIComponent(cleanName);
const topicPath = `${path}/data/${encodedName}`;
return (
<div
key={topic.uniqueKey || `${topic.topic}-${idx}`}
className="flex items-center gap-3 p-2 rounded-lg hover:bg-accent/50 cursor-pointer group"
onClick={() => handleResourceClick(topicPath)}
>
<Badge
variant={topic.isPublisher ? 'default' : 'secondary'}
className={
topic.isPublisher
? 'bg-green-500/10 text-green-600 border-green-300'
: 'bg-blue-500/10 text-blue-600 border-blue-300'
}
>
{topic.isPublisher ? 'pub' : 'sub'}
</Badge>
<span className="font-mono text-sm truncate flex-1">{topic.topic}</span>
{topic.type && (
<span className="text-xs text-muted-foreground truncate max-w-[200px]">
{topic.type}
</span>
)}
<ChevronRight className="w-4 h-4 text-muted-foreground opacity-0 group-hover:opacity-100" />
</div>
);
})}
</div>
)}
</CardContent>
</Card>
)}
{/* Operations / Configurations / Faults / Logs delegated to the shared helper */}
{activeTab !== 'overview' &&
activeTab !== 'data' &&
isResourceTabId(activeTab) &&
renderResourceTabContent(activeTab, appId, 'apps')}
{isLoading && <div className="text-center text-muted-foreground py-4">Loading app resources...</div>}
</div>
);
}