@shipfox/client-workflows 4.0.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,443 @@
1
+ import type {DefinitionDto, DefinitionSyncSummaryDto} from '@shipfox/api-definitions-dto';
2
+ import {ApiError} from '@shipfox/client-api';
3
+ import {SourceStrip, useDefinitionsInfiniteQuery, useProjectQuery} from '@shipfox/client-projects';
4
+ import {QueryLoadError} from '@shipfox/client-ui';
5
+ import {Button} from '@shipfox/react-ui/button';
6
+ import {Callout} from '@shipfox/react-ui/callout';
7
+ import {EmptyState} from '@shipfox/react-ui/empty-state';
8
+ import {Icon, type IconName} from '@shipfox/react-ui/icon';
9
+ import {LoadErrorState} from '@shipfox/react-ui/load-error-state';
10
+ import {RelativeTime, RelativeTimeProvider} from '@shipfox/react-ui/relative-time';
11
+ import {
12
+ Sheet,
13
+ SheetBody,
14
+ SheetContent,
15
+ SheetDescription,
16
+ SheetHeader,
17
+ SheetTitle,
18
+ } from '@shipfox/react-ui/sheet';
19
+ import {Skeleton} from '@shipfox/react-ui/skeleton';
20
+ import {
21
+ Table,
22
+ TableBody,
23
+ TableCell,
24
+ TableHead,
25
+ TableHeader,
26
+ TableRow,
27
+ } from '@shipfox/react-ui/table';
28
+ import {toast} from '@shipfox/react-ui/toast';
29
+ import {Code, Header, Text} from '@shipfox/react-ui/typography';
30
+ import {useState} from 'react';
31
+ import {useFireManualWorkflowMutation} from '#hooks/api/workflow-runs.js';
32
+
33
+ export function ProjectWorkflowsPage({projectId}: {projectId: string}) {
34
+ return (
35
+ <RelativeTimeProvider>
36
+ <ProjectWorkflowsPageInner projectId={projectId} />
37
+ </RelativeTimeProvider>
38
+ );
39
+ }
40
+
41
+ function ProjectWorkflowsPageInner({projectId}: {projectId: string}) {
42
+ const projectQuery = useProjectQuery(projectId);
43
+ const definitionsQuery = useDefinitionsInfiniteQuery(projectId);
44
+ const fireManual = useFireManualWorkflowMutation();
45
+ const [selectedDefinition, setSelectedDefinition] = useState<DefinitionDto | null>(null);
46
+ const [runError, setRunError] = useState<{definitionId: string; message: string} | null>(null);
47
+ const definitions = definitionsQuery.data?.pages.flatMap((page) => page.definitions) ?? [];
48
+ const sync = definitionsQuery.data?.pages[0]?.sync;
49
+
50
+ async function handleRun(definition: DefinitionDto) {
51
+ setRunError(null);
52
+ if (!definition.manual_trigger) return;
53
+ try {
54
+ await fireManual.mutateAsync({projectId, definitionId: definition.id});
55
+ toast.success('Run queued');
56
+ } catch (error) {
57
+ const message = errorMessage(error, 'Could not queue run.');
58
+ setRunError({definitionId: definition.id, message});
59
+ toast.error(message);
60
+ }
61
+ }
62
+
63
+ return (
64
+ <div className="flex w-full flex-col gap-24">
65
+ {projectQuery.isPending ? (
66
+ <div className="flex flex-col gap-12">
67
+ <Skeleton className="h-28 w-1/3" />
68
+ <Skeleton className="h-18 w-1/2" />
69
+ </div>
70
+ ) : null}
71
+
72
+ {projectQuery.isError && projectQuery.data === undefined ? (
73
+ projectQuery.error instanceof ApiError && projectQuery.error.status === 404 ? (
74
+ <EmptyState
75
+ icon="errorWarningLine"
76
+ title="Project not found"
77
+ description="This project doesn't exist, or you don't have access to it."
78
+ />
79
+ ) : (
80
+ <QueryLoadError query={projectQuery} subject="project" />
81
+ )
82
+ ) : null}
83
+
84
+ {projectQuery.data ? (
85
+ <>
86
+ <header className="flex flex-col gap-4">
87
+ <Header variant="h2">Workflows</Header>
88
+ <Text size="sm" className="text-foreground-neutral-muted">
89
+ Synced workflow definitions for this project source.
90
+ </Text>
91
+ </header>
92
+
93
+ <SourceStrip
94
+ connectionId={projectQuery.data.source.connection_id}
95
+ externalRepositoryId={projectQuery.data.source.external_repository_id}
96
+ sync={sync}
97
+ isPending={definitionsQuery.isPending}
98
+ />
99
+
100
+ <WorkflowSyncAlert sync={sync} />
101
+
102
+ <WorkflowDefinitionsList
103
+ definitions={definitions}
104
+ isPending={definitionsQuery.isPending}
105
+ isError={definitionsQuery.isError}
106
+ sync={sync ?? null}
107
+ runError={runError}
108
+ runningDefinitionId={
109
+ fireManual.isPending && fireManual.variables
110
+ ? fireManual.variables.definitionId
111
+ : null
112
+ }
113
+ hasNextPage={definitionsQuery.hasNextPage}
114
+ isFetchingNextPage={definitionsQuery.isFetchingNextPage}
115
+ isFetchNextPageError={definitionsQuery.isFetchNextPageError}
116
+ onRetry={() => definitionsQuery.refetch()}
117
+ onLoadMore={() => definitionsQuery.fetchNextPage()}
118
+ onOpenDefinition={setSelectedDefinition}
119
+ onRun={(definition) => {
120
+ void handleRun(definition);
121
+ }}
122
+ />
123
+ </>
124
+ ) : null}
125
+
126
+ <DefinitionSheet
127
+ definition={selectedDefinition}
128
+ onOpenChange={(open) => {
129
+ if (!open) setSelectedDefinition(null);
130
+ }}
131
+ />
132
+ </div>
133
+ );
134
+ }
135
+
136
+ function WorkflowDefinitionsList({
137
+ definitions,
138
+ isPending,
139
+ isError,
140
+ sync,
141
+ runError,
142
+ runningDefinitionId,
143
+ hasNextPage,
144
+ isFetchingNextPage,
145
+ isFetchNextPageError,
146
+ onRetry,
147
+ onLoadMore,
148
+ onOpenDefinition,
149
+ onRun,
150
+ }: {
151
+ definitions: DefinitionDto[];
152
+ isPending: boolean;
153
+ isError: boolean;
154
+ sync: DefinitionSyncSummaryDto | null;
155
+ runError: {definitionId: string; message: string} | null;
156
+ runningDefinitionId: string | null;
157
+ hasNextPage: boolean;
158
+ isFetchingNextPage: boolean;
159
+ isFetchNextPageError: boolean;
160
+ onRetry: () => void;
161
+ onLoadMore: () => void;
162
+ onOpenDefinition: (definition: DefinitionDto) => void;
163
+ onRun: (definition: DefinitionDto) => void;
164
+ }) {
165
+ if (isPending) {
166
+ return (
167
+ <div className="flex flex-col gap-8">
168
+ <Skeleton className="h-40 w-full" />
169
+ <Skeleton className="h-40 w-full" />
170
+ <Skeleton className="h-40 w-full" />
171
+ </div>
172
+ );
173
+ }
174
+
175
+ if (isError && definitions.length === 0) {
176
+ return (
177
+ <LoadErrorState
178
+ title="Couldn't load workflows"
179
+ description="Definitions could not be loaded. Source metadata remains visible."
180
+ onRetry={onRetry}
181
+ retryLabel="Retry loading workflows"
182
+ />
183
+ );
184
+ }
185
+
186
+ if (definitions.length === 0) {
187
+ return <WorkflowEmptyState sync={sync} />;
188
+ }
189
+
190
+ return (
191
+ <>
192
+ <div className="hidden rounded-8 border border-border-neutral-base md:block">
193
+ <Table>
194
+ <TableHeader>
195
+ <TableRow>
196
+ <TableHead className="w-40"></TableHead>
197
+ <TableHead>Workflow</TableHead>
198
+ <TableHead className="w-180">Updated</TableHead>
199
+ <TableHead className="w-80 text-right"></TableHead>
200
+ </TableRow>
201
+ </TableHeader>
202
+ <TableBody>
203
+ {definitions.map((definition) => {
204
+ const runErrorMessage =
205
+ runError?.definitionId === definition.id ? runError.message : null;
206
+ const isRunning = runningDefinitionId === definition.id;
207
+
208
+ return (
209
+ // The workflow-name cell holds a `<button>` so the row is
210
+ // keyboard-reachable (Tab focuses, Enter/Space activates
211
+ // via native button semantics). The TableRow itself is no
212
+ // longer clickable — a row-level onClick would be invisible
213
+ // to keyboard users and require custom keydown handling.
214
+ // The `group` class on the row still drives the Run button
215
+ // reveal on hover or focus-within.
216
+ <TableRow key={definition.id} className="group">
217
+ <TableCell>
218
+ <Icon
219
+ name={sourceIcon(definition.source)}
220
+ className="size-16 text-foreground-neutral-muted"
221
+ aria-hidden="true"
222
+ />
223
+ </TableCell>
224
+ <TableCell className="max-w-260">
225
+ <div className="flex min-w-0 flex-col gap-2">
226
+ <button
227
+ type="button"
228
+ onClick={() => onOpenDefinition(definition)}
229
+ className="flex min-w-0 flex-col gap-2 text-left outline-none focus-visible:shadow-border-interactive-with-active rounded-4"
230
+ >
231
+ <Text size="sm" bold className="truncate">
232
+ {definition.name}
233
+ </Text>
234
+ <Code className="truncate text-foreground-neutral-muted">
235
+ {definition.config_path ?? 'Manual definition'}
236
+ </Code>
237
+ </button>
238
+ {runErrorMessage ? (
239
+ <Text size="xs" className="text-tag-error-text">
240
+ {runErrorMessage}
241
+ </Text>
242
+ ) : null}
243
+ </div>
244
+ </TableCell>
245
+ <TableCell className="text-foreground-neutral-muted">
246
+ <RelativeTime value={definition.updated_at} />
247
+ </TableCell>
248
+ <TableCell>
249
+ {definition.manual_trigger ? (
250
+ <div className="flex justify-end opacity-0 transition-opacity group-hover:opacity-100 group-focus-within:opacity-100">
251
+ <Button size="xs" isLoading={isRunning} onClick={() => onRun(definition)}>
252
+ Run
253
+ </Button>
254
+ </div>
255
+ ) : null}
256
+ </TableCell>
257
+ </TableRow>
258
+ );
259
+ })}
260
+ </TableBody>
261
+ </Table>
262
+ </div>
263
+
264
+ <div className="flex flex-col rounded-8 border border-border-neutral-base md:hidden">
265
+ {definitions.map((definition) => {
266
+ const runErrorMessage =
267
+ runError?.definitionId === definition.id ? runError.message : null;
268
+ const isRunning = runningDefinitionId === definition.id;
269
+
270
+ return (
271
+ <div
272
+ key={definition.id}
273
+ className="flex flex-col gap-10 border-b border-border-neutral-base p-12 last:border-b-0"
274
+ >
275
+ <button
276
+ type="button"
277
+ className="flex min-w-0 items-start gap-10 text-left"
278
+ onClick={() => onOpenDefinition(definition)}
279
+ >
280
+ <Icon
281
+ name={sourceIcon(definition.source)}
282
+ className="size-16 shrink-0 text-foreground-neutral-muted"
283
+ aria-hidden="true"
284
+ />
285
+ <div className="flex min-w-0 flex-col gap-4">
286
+ <Text size="sm" bold className="break-words">
287
+ {definition.name}
288
+ </Text>
289
+ <Code className="break-words text-foreground-neutral-muted">
290
+ {definition.config_path ?? 'Manual definition'}
291
+ </Code>
292
+ </div>
293
+ </button>
294
+ <div className="flex items-center justify-between gap-8">
295
+ <Text size="xs" className="text-foreground-neutral-muted">
296
+ Updated <RelativeTime value={definition.updated_at} />
297
+ </Text>
298
+ {definition.manual_trigger ? (
299
+ <Button size="sm" isLoading={isRunning} onClick={() => onRun(definition)}>
300
+ Run
301
+ </Button>
302
+ ) : null}
303
+ </div>
304
+ {runErrorMessage ? (
305
+ <Text size="xs" className="text-tag-error-text">
306
+ {runErrorMessage}
307
+ </Text>
308
+ ) : null}
309
+ </div>
310
+ );
311
+ })}
312
+ </div>
313
+
314
+ {isFetchNextPageError ? (
315
+ <Callout role="alert" type="error">
316
+ <div className="flex items-center justify-between gap-12">
317
+ <Text size="sm">Could not load more workflows.</Text>
318
+ <Button size="sm" variant="secondary" onClick={onLoadMore}>
319
+ Retry
320
+ </Button>
321
+ </div>
322
+ </Callout>
323
+ ) : null}
324
+
325
+ {hasNextPage ? (
326
+ <div className="flex justify-center">
327
+ <Button size="sm" variant="secondary" isLoading={isFetchingNextPage} onClick={onLoadMore}>
328
+ Load more
329
+ </Button>
330
+ </div>
331
+ ) : null}
332
+ </>
333
+ );
334
+ }
335
+
336
+ function sourceIcon(source: 'manual' | 'vcs'): IconName {
337
+ return source === 'vcs' ? ('gitBranchLine' as IconName) : ('terminalLine' as IconName);
338
+ }
339
+
340
+ function WorkflowEmptyState({sync}: {sync: DefinitionSyncSummaryDto | null}) {
341
+ const message =
342
+ sync?.status === 'failed' && sync.last_error_code === 'no-workflow-files'
343
+ ? 'No workflow files found under .shipfox/workflows/.'
344
+ : sync?.status === 'failed'
345
+ ? (sync.last_error_message ?? 'Workflow definitions could not be synced.')
346
+ : sync?.status === 'syncing'
347
+ ? 'Workflow definitions are being discovered.'
348
+ : sync?.status === 'succeeded'
349
+ ? 'No workflow definitions found.'
350
+ : 'Workflow sync has not reported yet.';
351
+
352
+ return <EmptyState icon="flowChart" title="No workflows" description={message} />;
353
+ }
354
+
355
+ function WorkflowSyncAlert({sync}: {sync: DefinitionSyncSummaryDto | null | undefined}) {
356
+ if (sync?.status !== 'failed') return null;
357
+
358
+ return (
359
+ <Callout role="alert" type="error">
360
+ <div className="flex flex-col gap-4">
361
+ <Text size="sm" bold>
362
+ Workflow sync failed
363
+ </Text>
364
+ <Text size="sm">
365
+ {sync.last_error_message ?? 'The latest workflow sync failed before definitions updated.'}
366
+ </Text>
367
+ </div>
368
+ </Callout>
369
+ );
370
+ }
371
+
372
+ function DefinitionSheet({
373
+ definition,
374
+ onOpenChange,
375
+ }: {
376
+ definition: DefinitionDto | null;
377
+ onOpenChange: (open: boolean) => void;
378
+ }) {
379
+ const normalizedJson = definition
380
+ ? JSON.stringify(
381
+ {
382
+ workflow_document: definition.workflow_document,
383
+ workflow_model: definition.workflow_model,
384
+ },
385
+ null,
386
+ 2,
387
+ )
388
+ : '';
389
+
390
+ return (
391
+ <Sheet open={Boolean(definition)} onOpenChange={onOpenChange}>
392
+ <SheetContent className="w-full sm:max-w-[560px]">
393
+ {definition ? (
394
+ <>
395
+ <SheetHeader>
396
+ <SheetTitle>{definition.name}</SheetTitle>
397
+ <SheetDescription>
398
+ {definition.config_path ?? 'Manual workflow definition'}
399
+ </SheetDescription>
400
+ </SheetHeader>
401
+ <SheetBody className="gap-18">
402
+ <div className="grid w-full gap-10">
403
+ <Metadata label="Definition id" value={definition.id} />
404
+ <Metadata label="Source" value={definition.source} />
405
+ <Metadata label="Ref" value={definition.ref ?? 'Not set'} />
406
+ <Metadata label="SHA" value={definition.sha ?? 'Not set'} />
407
+ </div>
408
+ <div className="flex w-full flex-col gap-8">
409
+ <Text size="sm" bold>
410
+ Normalized definition
411
+ </Text>
412
+ <pre className="max-h-[52vh] w-full overflow-auto rounded-8 border border-border-neutral-base bg-background-neutral-subtle p-12 scrollbar">
413
+ <Code as="code" className="whitespace-pre text-foreground-neutral-base">
414
+ {normalizedJson}
415
+ </Code>
416
+ </pre>
417
+ </div>
418
+ </SheetBody>
419
+ </>
420
+ ) : null}
421
+ </SheetContent>
422
+ </Sheet>
423
+ );
424
+ }
425
+
426
+ function Metadata({label, value}: {label: string; value: string}) {
427
+ return (
428
+ <div className="min-w-0 py-12 first:pt-0 last:pb-0">
429
+ <Text size="xs" className="text-foreground-neutral-muted">
430
+ {label}
431
+ </Text>
432
+ <Text size="sm" className="break-words">
433
+ {value}
434
+ </Text>
435
+ </div>
436
+ );
437
+ }
438
+
439
+ function errorMessage(error: unknown, fallback: string) {
440
+ if (error instanceof ApiError && error.message) return error.message;
441
+ if (error instanceof Error && error.message) return error.message;
442
+ return fallback;
443
+ }
@@ -0,0 +1,10 @@
1
+ import {defineRoute} from '@shipfox/client-shell/runtime';
2
+ import {useParams} from '@tanstack/react-router';
3
+ import {ProjectWorkflowsPage} from '#pages/project-workflows-page.js';
4
+
5
+ export default defineRoute({
6
+ component: () => {
7
+ const {pid} = useParams({strict: false}) as {pid: string};
8
+ return <ProjectWorkflowsPage projectId={pid} />;
9
+ },
10
+ });
package/test/pages.tsx CHANGED
@@ -1,4 +1,6 @@
1
1
  import {configureApiClient} from '@shipfox/client-api';
2
+ import {type AuthState, authStateAtom} from '@shipfox/client-shell/runtime';
3
+ import {Toaster} from '@shipfox/react-ui/toast';
2
4
  import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
3
5
  import {
4
6
  createMemoryHistory,
@@ -10,6 +12,7 @@ import {
10
12
  useParams,
11
13
  } from '@tanstack/react-router';
12
14
  import {type RenderResult, render} from '@testing-library/react';
15
+ import {createStore, Provider as JotaiProvider} from 'jotai';
13
16
  import type {ReactElement} from 'react';
14
17
 
15
18
  // The workflow run page navigates with the router (run rows are links and the page redirects
@@ -19,6 +22,15 @@ import type {ReactElement} from 'react';
19
22
  // the real route wiring so a redirect re-renders the page with the run it landed on.
20
23
  export const PROJECT_TEST_WID = '11111111-1111-4111-8111-111111111111';
21
24
 
25
+ // Pages that render components depending on `useActiveWorkspace()` (e.g. the project source
26
+ // strip) need an authenticated workspace matching `$wid` in the atom `client-shell/runtime`
27
+ // reads. A fixed membership id is fine here: nothing in these tests asserts on it.
28
+ const authState: AuthState = {
29
+ status: 'authenticated',
30
+ token: 'token',
31
+ workspaces: [{id: PROJECT_TEST_WID, name: 'Acme', membershipId: 'm-1'}],
32
+ };
33
+
22
34
  export function jsonResponse(body: unknown, init: ResponseInit = {}) {
23
35
  return new Response(JSON.stringify(body), {
24
36
  status: 200,
@@ -45,6 +57,11 @@ function createTestRouter(
45
57
  return renderPage({workflowRunId});
46
58
  },
47
59
  });
60
+ const workflowsRoute = createRoute({
61
+ getParentRoute: () => rootRoute,
62
+ path: '/workspaces/$wid/projects/$pid/workflows',
63
+ component: () => renderPage({}),
64
+ });
48
65
  const modelProviderSettingsRoute = createRoute({
49
66
  getParentRoute: () => rootRoute,
50
67
  path: '/workspaces/$wid/settings/agents',
@@ -53,7 +70,12 @@ function createTestRouter(
53
70
 
54
71
  return createRouter({
55
72
  history: createMemoryHistory({initialEntries: [path]}),
56
- routeTree: rootRoute.addChildren([runsRoute, runDetailRoute, modelProviderSettingsRoute]),
73
+ routeTree: rootRoute.addChildren([
74
+ runsRoute,
75
+ runDetailRoute,
76
+ workflowsRoute,
77
+ modelProviderSettingsRoute,
78
+ ]),
57
79
  });
58
80
  }
59
81
 
@@ -66,12 +88,17 @@ export function renderProjectPage(
66
88
  } {
67
89
  const queryClient = new QueryClient({defaultOptions: {queries: {retry: false}}});
68
90
  const router = createTestRouter(path, renderPage);
91
+ const store = createStore();
92
+ store.set(authStateAtom, authState);
69
93
 
70
94
  configureApiClient({baseUrl: 'https://api.example.test'});
71
95
 
72
96
  const result = render(
73
97
  <QueryClientProvider client={queryClient}>
74
- <RouterProvider router={router} />
98
+ <JotaiProvider store={store}>
99
+ <RouterProvider router={router} />
100
+ <Toaster />
101
+ </JotaiProvider>
75
102
  </QueryClientProvider>,
76
103
  );
77
104