-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlowViewer.tsx
More file actions
311 lines (291 loc) · 11.9 KB
/
FlowViewer.tsx
File metadata and controls
311 lines (291 loc) · 11.9 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { useEffect, useState } from 'react';
import { useParams } from '@tanstack/react-router';
import { useClient } from '@objectstack/client-react';
import { useScopedClient } from '@/hooks/useObjectStackClient';
import type { MetadataViewerProps } from '@/plugins/types';
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Skeleton } from '@/components/ui/skeleton';
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs';
import {
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
} from '@/components/ui/table';
import { Workflow, Zap, Calendar, MousePointerClick, Globe, Database, CheckCircle2 } from 'lucide-react';
import { CopyButton, JsonTree } from './MetadataInspector';
import { FlowTestRunner } from './FlowTestRunner';
import { FlowRunsPanel } from './FlowRunsPanel';
function resolveLabel(val: unknown): string {
if (typeof val === 'string') return val;
if (val && typeof val === 'object' && 'defaultValue' in val) return String((val as any).defaultValue);
if (val && typeof val === 'object' && 'key' in val) return String((val as any).key);
return '';
}
const TRIGGER_ICONS: Record<string, any> = {
autolaunched: Zap,
record_change: Database,
schedule: Calendar,
screen: MousePointerClick,
api: Globe,
};
const TRIGGER_LABELS: Record<string, string> = {
autolaunched: 'Autolaunched',
record_change: 'Record Change',
schedule: 'Schedule',
screen: 'Screen',
api: 'API',
};
export function FlowViewer({ metadataName, data, packageId }: MetadataViewerProps) {
const params = useParams({ strict: false }) as { projectId?: string };
const unscoped = useClient();
const scoped = useScopedClient(params.projectId);
const client: any = scoped ?? unscoped;
const [flow, setFlow] = useState<any>(data ?? null);
const [loading, setLoading] = useState(!data);
const [error, setError] = useState<string | null>(null);
const [refreshKey, setRefreshKey] = useState(0);
useEffect(() => {
if (data) { setFlow(data); setLoading(false); return; }
if (!client?.meta?.getItem) return;
let mounted = true;
setLoading(true);
setError(null);
(async () => {
try {
const res: any = await client.meta.getItem('flow', metadataName, packageId ? { packageId } : undefined);
if (mounted) setFlow(res?.item ?? res);
} catch (e: any) {
if (mounted) setError(e?.message ?? String(e));
} finally {
if (mounted) setLoading(false);
}
})();
return () => { mounted = false; };
}, [client, metadataName, packageId, data]);
if (loading) {
return (
<div className="space-y-4 p-4">
<Card>
<CardHeader><Skeleton className="h-6 w-48" /></CardHeader>
<CardContent className="space-y-3">
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-full" />
</CardContent>
</Card>
</div>
);
}
if (error || !flow) {
return (
<div className="p-4">
<Card>
<CardContent className="py-12 text-center text-muted-foreground">
{error
? <span className="text-red-500 font-mono text-sm break-all">{error}</span>
: <>Flow definition not found: <code className="font-mono">{metadataName}</code></>
}
</CardContent>
</Card>
</div>
);
}
const label = resolveLabel(flow.label) || flow.name || metadataName;
const name = flow.name || metadataName;
const description = resolveLabel(flow.description);
const status: string | undefined = flow.status;
const isActive = status === 'active';
const triggerType: string = flow.type || 'autolaunched';
const TriggerIcon = TRIGGER_ICONS[triggerType] || Workflow;
const triggerLabel = TRIGGER_LABELS[triggerType] || triggerType;
const variables: any[] = Array.isArray(flow.variables) ? flow.variables : [];
const nodes: any[] = Array.isArray(flow.nodes) ? flow.nodes : [];
const edges: any[] = Array.isArray(flow.edges) ? flow.edges : [];
return (
<div className="space-y-4">
{/* Header */}
<Card>
<CardHeader className="pb-3">
<div className="flex items-center justify-between gap-3">
<div className="space-y-1 min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<Workflow className="h-5 w-5 text-muted-foreground" />
<CardTitle className="text-xl truncate">{label}</CardTitle>
<Badge variant="outline" className="text-xs">Flow</Badge>
{typeof flow.version === 'number' && (
<Badge variant="secondary" className="text-[10px] font-mono">v{flow.version}</Badge>
)}
{isActive ? (
<Badge variant="outline" className="text-[10px] font-mono text-emerald-600 border-emerald-300 inline-flex items-center gap-1">
<CheckCircle2 className="h-3 w-3" /> Enabled
</Badge>
) : status ? (
<Badge variant="outline" className="text-[10px] font-mono text-muted-foreground">{status}</Badge>
) : null}
</div>
<CardDescription className="flex items-center gap-2 flex-wrap">
<code className="font-mono text-xs bg-muted px-1.5 py-0.5 rounded">{name}</code>
<CopyButton text={name} />
{description && (
<>
<span>·</span>
<span className="text-xs">{description}</span>
</>
)}
</CardDescription>
</div>
</div>
</CardHeader>
</Card>
{/* Tabs */}
<Tabs defaultValue="overview" className="w-full">
<TabsList>
<TabsTrigger value="overview">Overview</TabsTrigger>
<TabsTrigger value="test">Test Run</TabsTrigger>
<TabsTrigger value="runs">Runs</TabsTrigger>
<TabsTrigger value="raw">Raw JSON</TabsTrigger>
</TabsList>
{/* Overview */}
<TabsContent value="overview" className="space-y-4 mt-4">
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base flex items-center gap-2">
<TriggerIcon className="h-4 w-4 text-muted-foreground" />
Trigger
</CardTitle>
</CardHeader>
<CardContent className="text-sm space-y-1">
<div className="flex items-center gap-2">
<span className="text-muted-foreground">Type:</span>
<Badge variant="outline" className="text-[10px] font-mono">{triggerLabel}</Badge>
</div>
{flow.trigger && (
<div className="mt-2">
<JsonTree data={flow.trigger} />
</div>
)}
</CardContent>
</Card>
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base">Variables ({variables.length})</CardTitle>
</CardHeader>
<CardContent>
{variables.length === 0 ? (
<p className="text-sm text-muted-foreground">No variables defined.</p>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead>Name</TableHead>
<TableHead>Type</TableHead>
<TableHead className="text-center">Input</TableHead>
<TableHead className="text-center">Output</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{variables.map((v, i) => (
<TableRow key={`${v.name}-${i}`}>
<TableCell className="font-mono text-xs">{v.name}</TableCell>
<TableCell>
<Badge variant="outline" className="text-[10px] font-mono">{v.type}</Badge>
</TableCell>
<TableCell className="text-center text-xs">{v.isInput ? '✓' : '—'}</TableCell>
<TableCell className="text-center text-xs">{v.isOutput ? '✓' : '—'}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</CardContent>
</Card>
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base">Nodes ({nodes.length})</CardTitle>
</CardHeader>
<CardContent>
{nodes.length === 0 ? (
<p className="text-sm text-muted-foreground">No nodes defined.</p>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead>ID</TableHead>
<TableHead>Type</TableHead>
<TableHead>Label</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{nodes.map((n, i) => (
<TableRow key={`${n.id}-${i}`}>
<TableCell className="font-mono text-xs">{n.id}</TableCell>
<TableCell>
<Badge variant="outline" className="text-[10px] font-mono">{n.type}</Badge>
</TableCell>
<TableCell className="text-xs">{resolveLabel(n.label) || '—'}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</CardContent>
</Card>
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base">Edges ({edges.length})</CardTitle>
</CardHeader>
<CardContent>
{edges.length === 0 ? (
<p className="text-sm text-muted-foreground">No edges defined.</p>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead>From → To</TableHead>
<TableHead>Condition</TableHead>
<TableHead>Default</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{edges.map((e, i) => (
<TableRow key={`${e.id ?? i}`}>
<TableCell className="font-mono text-xs">
{e.source} <span className="text-muted-foreground">→</span> {e.target}
</TableCell>
<TableCell className="font-mono text-xs text-muted-foreground break-all">
{e.condition ?? '—'}
</TableCell>
<TableCell className="text-xs">{e.isDefault ? '✓' : '—'}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</CardContent>
</Card>
</TabsContent>
{/* Test Run */}
<TabsContent value="test" className="mt-4">
<FlowTestRunner
flow={flow}
onExecuted={() => setRefreshKey(k => k + 1)}
/>
</TabsContent>
{/* Runs */}
<TabsContent value="runs" className="mt-4">
<FlowRunsPanel flowName={name} refreshKey={refreshKey} />
</TabsContent>
{/* Raw JSON */}
<TabsContent value="raw" className="mt-4">
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base">Raw Flow Definition</CardTitle>
</CardHeader>
<CardContent>
<JsonTree data={flow} />
</CardContent>
</Card>
</TabsContent>
</Tabs>
</div>
);
}