@asteby/metacore-runtime-react 53.4.1 → 53.6.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.
- package/CHANGELOG.md +12 -0
- package/README.md +73 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/setup/addon-config-surface.d.ts +119 -0
- package/dist/setup/addon-config-surface.d.ts.map +1 -0
- package/dist/setup/addon-config-surface.js +662 -0
- package/dist/setup/connector-blocks-model.d.ts +79 -0
- package/dist/setup/connector-blocks-model.d.ts.map +1 -0
- package/dist/setup/connector-blocks-model.js +96 -0
- package/dist/setup/connector-blocks.d.ts +48 -0
- package/dist/setup/connector-blocks.d.ts.map +1 -0
- package/dist/setup/connector-blocks.js +132 -0
- package/dist/setup/connector-form-model.d.ts +40 -0
- package/dist/setup/connector-form-model.d.ts.map +1 -0
- package/dist/setup/connector-form-model.js +76 -0
- package/dist/setup/flow-model.d.ts +49 -0
- package/dist/setup/flow-model.d.ts.map +1 -0
- package/dist/setup/flow-model.js +124 -0
- package/dist/setup/health-card.d.ts +25 -0
- package/dist/setup/health-card.d.ts.map +1 -0
- package/dist/setup/health-card.js +21 -0
- package/dist/setup/index.d.ts +13 -0
- package/dist/setup/index.d.ts.map +1 -0
- package/dist/setup/index.js +15 -0
- package/dist/setup/messages.d.ts +15 -0
- package/dist/setup/messages.d.ts.map +1 -0
- package/dist/setup/messages.js +320 -0
- package/dist/setup/records-table.d.ts +24 -0
- package/dist/setup/records-table.d.ts.map +1 -0
- package/dist/setup/records-table.js +80 -0
- package/dist/setup/setup-flow.d.ts +30 -0
- package/dist/setup/setup-flow.d.ts.map +1 -0
- package/dist/setup/setup-flow.js +277 -0
- package/dist/setup/setup-form-step.d.ts +20 -0
- package/dist/setup/setup-form-step.d.ts.map +1 -0
- package/dist/setup/setup-form-step.js +173 -0
- package/dist/setup/step-status-badge.d.ts +12 -0
- package/dist/setup/step-status-badge.d.ts.map +1 -0
- package/dist/setup/step-status-badge.js +21 -0
- package/dist/setup/tone.d.ts +8 -0
- package/dist/setup/tone.d.ts.map +1 -0
- package/dist/setup/tone.js +33 -0
- package/dist/setup/types.d.ts +130 -0
- package/dist/setup/types.d.ts.map +1 -0
- package/dist/setup/types.js +1 -0
- package/package.json +9 -5
- package/src/__tests__/addon-config-surface.test.tsx +260 -0
- package/src/__tests__/connector-blocks.test.tsx +302 -0
- package/src/__tests__/setup-components.test.tsx +147 -0
- package/src/__tests__/setup-flow-model.test.ts +117 -0
- package/src/__tests__/setup-flow.test.tsx +424 -0
- package/src/index.ts +29 -0
- package/src/setup/addon-config-surface.tsx +1426 -0
- package/src/setup/connector-blocks-model.ts +163 -0
- package/src/setup/connector-blocks.tsx +341 -0
- package/src/setup/connector-form-model.ts +115 -0
- package/src/setup/flow-model.ts +156 -0
- package/src/setup/health-card.tsx +131 -0
- package/src/setup/index.ts +35 -0
- package/src/setup/messages.ts +334 -0
- package/src/setup/records-table.tsx +186 -0
- package/src/setup/setup-flow.tsx +562 -0
- package/src/setup/setup-form-step.tsx +342 -0
- package/src/setup/step-status-badge.tsx +47 -0
- package/src/setup/tone.ts +44 -0
- package/src/setup/types.ts +147 -0
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import { Loader2, RefreshCw } from 'lucide-react'
|
|
3
|
+
import { Button } from '@asteby/metacore-ui/primitives'
|
|
4
|
+
import { cn } from '@asteby/metacore-ui/lib'
|
|
5
|
+
import { Slot } from '../slot'
|
|
6
|
+
import { useSetupT } from './messages'
|
|
7
|
+
import {
|
|
8
|
+
effectiveStatus,
|
|
9
|
+
flowProgress,
|
|
10
|
+
isSatisfied,
|
|
11
|
+
mergeAnswer,
|
|
12
|
+
nextPollDelayMs,
|
|
13
|
+
normalizeState,
|
|
14
|
+
pickActiveStep,
|
|
15
|
+
safeUrl,
|
|
16
|
+
unmetDependencies,
|
|
17
|
+
} from './flow-model'
|
|
18
|
+
import { RecordsTable } from './records-table'
|
|
19
|
+
import { SetupFormStep } from './setup-form-step'
|
|
20
|
+
import { StepStatusBadge } from './step-status-badge'
|
|
21
|
+
import { TONE_TEXT } from './tone'
|
|
22
|
+
import type {
|
|
23
|
+
RunStepInput,
|
|
24
|
+
SetupDiagnostic,
|
|
25
|
+
SetupFix,
|
|
26
|
+
SetupFlowClient,
|
|
27
|
+
SetupFlowDef,
|
|
28
|
+
SetupFlowState,
|
|
29
|
+
SetupStepDef,
|
|
30
|
+
SetupStepState,
|
|
31
|
+
} from './types'
|
|
32
|
+
|
|
33
|
+
export interface SetupCustomStepProps {
|
|
34
|
+
step: SetupStepDef
|
|
35
|
+
state?: SetupStepState
|
|
36
|
+
/** Runs the step on the server (and updates the flow). */
|
|
37
|
+
run: (input?: RunStepInput) => Promise<void>
|
|
38
|
+
refresh: () => Promise<void>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface SetupFlowProps {
|
|
42
|
+
flow: SetupFlowDef
|
|
43
|
+
client: SetupFlowClient
|
|
44
|
+
/** State already known (e.g. from SSR/cache); the client is still read on mount. */
|
|
45
|
+
initialState?: SetupFlowState
|
|
46
|
+
/** Renders kind=custom. Default: the federated slot `step.slot ?? setup.<flow.key>.<step.key>`. */
|
|
47
|
+
renderCustom?: (p: SetupCustomStepProps) => React.ReactNode
|
|
48
|
+
/** Opens `fix.url`. Default: relative paths via location, absolute in a new tab. */
|
|
49
|
+
onNavigate?: (url: string) => void
|
|
50
|
+
onComplete?: () => void
|
|
51
|
+
/** Automatic re-reads while something runs before asking for a manual refresh. */
|
|
52
|
+
maxPollAttempts?: number
|
|
53
|
+
className?: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const errorMessage = (e: unknown, fallback: string) => {
|
|
57
|
+
const r = (e as { response?: { data?: { message?: unknown } } })?.response?.data?.message
|
|
58
|
+
if (typeof r === 'string' && r.trim()) return r
|
|
59
|
+
const m = (e as Error)?.message
|
|
60
|
+
return typeof m === 'string' && m.trim() ? m : fallback
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Resumable setup flow (`contributions.setup`). The server owns the state
|
|
65
|
+
* (`client.getState`); the UI renders each step by `kind`, honours
|
|
66
|
+
* `depends_on` / `optional`, polls with backoff while something runs
|
|
67
|
+
* (`retry_after_s` wins), and offers diagnostics with a fix button.
|
|
68
|
+
*/
|
|
69
|
+
export function SetupFlow({
|
|
70
|
+
flow,
|
|
71
|
+
client,
|
|
72
|
+
initialState,
|
|
73
|
+
renderCustom,
|
|
74
|
+
onNavigate,
|
|
75
|
+
onComplete,
|
|
76
|
+
maxPollAttempts = 40,
|
|
77
|
+
className,
|
|
78
|
+
}: SetupFlowProps) {
|
|
79
|
+
const t = useSetupT()
|
|
80
|
+
const [states, setStates] = useState<Record<string, SetupStepState>>(() => normalizeState(initialState))
|
|
81
|
+
const [serverCompleted, setServerCompleted] = useState<boolean | undefined>(initialState?.completed)
|
|
82
|
+
const [loading, setLoading] = useState(!initialState)
|
|
83
|
+
const [loadError, setLoadError] = useState<string | null>(null)
|
|
84
|
+
const [selected, setSelected] = useState<string | null>(null)
|
|
85
|
+
const [busyCount, setBusyCount] = useState<Record<string, number>>({})
|
|
86
|
+
const [refreshing, setRefreshing] = useState(false)
|
|
87
|
+
const [stepErrors, setStepErrors] = useState<Record<string, string>>({})
|
|
88
|
+
const [pollStopped, setPollStopped] = useState(false)
|
|
89
|
+
const [tick, setTick] = useState(0)
|
|
90
|
+
const [announce, setAnnounce] = useState('')
|
|
91
|
+
const attempts = useRef(0)
|
|
92
|
+
const sig = useRef('')
|
|
93
|
+
const prevStatuses = useRef<Record<string, string>>({})
|
|
94
|
+
const wasIncomplete = useRef(false)
|
|
95
|
+
const clientRef = useRef(client)
|
|
96
|
+
clientRef.current = client
|
|
97
|
+
const onCompleteRef = useRef(onComplete)
|
|
98
|
+
onCompleteRef.current = onComplete
|
|
99
|
+
const hasSubscribe = !!client.subscribe
|
|
100
|
+
const inflight = useRef<Promise<void> | null>(null)
|
|
101
|
+
const alive = useRef(true)
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
alive.current = true
|
|
104
|
+
return () => {
|
|
105
|
+
alive.current = false
|
|
106
|
+
}
|
|
107
|
+
}, [])
|
|
108
|
+
|
|
109
|
+
const apply = useCallback((s: SetupFlowState) => {
|
|
110
|
+
setStates(normalizeState(s))
|
|
111
|
+
setServerCompleted(s.completed)
|
|
112
|
+
}, [])
|
|
113
|
+
|
|
114
|
+
// `client` / `subscribe` live in refs: a host passing an inline client object
|
|
115
|
+
// (new identity every render) must not restart the fetch/subscribe effects.
|
|
116
|
+
const refresh = useCallback((): Promise<void> => {
|
|
117
|
+
if (inflight.current) return inflight.current
|
|
118
|
+
const run = (async () => {
|
|
119
|
+
if (alive.current) setRefreshing(true)
|
|
120
|
+
try {
|
|
121
|
+
const s = await clientRef.current.getState()
|
|
122
|
+
if (!alive.current) return
|
|
123
|
+
apply(s)
|
|
124
|
+
setLoadError(null)
|
|
125
|
+
} catch (e) {
|
|
126
|
+
if (alive.current) setLoadError(errorMessage(e, t('flow.load_error')))
|
|
127
|
+
} finally {
|
|
128
|
+
inflight.current = null
|
|
129
|
+
if (alive.current) {
|
|
130
|
+
setRefreshing(false)
|
|
131
|
+
setLoading(false)
|
|
132
|
+
setTick((n) => n + 1)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
})()
|
|
136
|
+
inflight.current = run
|
|
137
|
+
return run
|
|
138
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
139
|
+
}, [apply])
|
|
140
|
+
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
void refresh()
|
|
143
|
+
}, [refresh])
|
|
144
|
+
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
if (!hasSubscribe) return
|
|
147
|
+
return clientRef.current.subscribe?.((s) => {
|
|
148
|
+
if (alive.current) apply(s)
|
|
149
|
+
})
|
|
150
|
+
}, [hasSubscribe, apply])
|
|
151
|
+
|
|
152
|
+
const defs = flow.steps
|
|
153
|
+
const progress = flowProgress(defs, states, serverCompleted)
|
|
154
|
+
const activeKey = pickActiveStep(defs, states)
|
|
155
|
+
const currentKey = selected && defs.some((d) => d.key === selected) ? selected : activeKey
|
|
156
|
+
const current = defs.find((d) => d.key === currentKey)
|
|
157
|
+
|
|
158
|
+
// Polling with backoff while anything runs or the server asked to come back.
|
|
159
|
+
const watching = defs.filter((d) => {
|
|
160
|
+
const st = states[d.key]
|
|
161
|
+
return !!st && !st.skipped && (st.status === 'running' || ((st.retry_after_s ?? 0) > 0 && st.status !== 'ok'))
|
|
162
|
+
})
|
|
163
|
+
const retryHints = watching.map((d) => states[d.key].retry_after_s ?? 0).filter((n) => n > 0)
|
|
164
|
+
const retryHint = retryHints.length ? Math.min(...retryHints) : undefined
|
|
165
|
+
const signature = watching.map((d) => `${d.key}:${states[d.key].status}`).join('|') + `#${retryHint ?? ''}`
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
if (watching.length === 0) {
|
|
168
|
+
attempts.current = 0
|
|
169
|
+
sig.current = ''
|
|
170
|
+
setPollStopped(false)
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
if (sig.current !== signature) {
|
|
174
|
+
sig.current = signature
|
|
175
|
+
attempts.current = 0
|
|
176
|
+
setPollStopped(false)
|
|
177
|
+
}
|
|
178
|
+
if (attempts.current >= maxPollAttempts) {
|
|
179
|
+
setPollStopped(true)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
const id = setTimeout(() => {
|
|
183
|
+
attempts.current += 1
|
|
184
|
+
void refresh()
|
|
185
|
+
}, nextPollDelayMs(attempts.current, retryHint))
|
|
186
|
+
return () => clearTimeout(id)
|
|
187
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
188
|
+
}, [signature, tick, maxPollAttempts])
|
|
189
|
+
|
|
190
|
+
// Screen-reader announcement when a step changes status.
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
const prev = prevStatuses.current
|
|
193
|
+
const msgs: string[] = []
|
|
194
|
+
for (const d of defs) {
|
|
195
|
+
const eff = effectiveStatus(d, states)
|
|
196
|
+
if (prev[d.key] && prev[d.key] !== eff) {
|
|
197
|
+
msgs.push(t('flow.status_changed', { step: d.title, status: t(`status.${eff}`) }))
|
|
198
|
+
}
|
|
199
|
+
prev[d.key] = eff
|
|
200
|
+
}
|
|
201
|
+
if (msgs.length) setAnnounce(msgs.join('. '))
|
|
202
|
+
}, [states, defs, t])
|
|
203
|
+
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
// Fires on the incomplete -> complete transition only: a flow that was
|
|
206
|
+
// already complete when it mounted does not re-announce itself.
|
|
207
|
+
if (loading) return
|
|
208
|
+
if (!progress.complete) {
|
|
209
|
+
wasIncomplete.current = true
|
|
210
|
+
} else if (wasIncomplete.current) {
|
|
211
|
+
wasIncomplete.current = false
|
|
212
|
+
onCompleteRef.current?.()
|
|
213
|
+
}
|
|
214
|
+
}, [progress.complete, loading])
|
|
215
|
+
|
|
216
|
+
const settle = useCallback(
|
|
217
|
+
async (key: string, call: () => Promise<SetupStepState | SetupFlowState | void>, optimistic: boolean) => {
|
|
218
|
+
// Functional updates only: polling/subscribe may have written newer
|
|
219
|
+
// state while the call was in flight, and `busy` is tracked per step.
|
|
220
|
+
const mark = (d: number) =>
|
|
221
|
+
setBusyCount((p) => {
|
|
222
|
+
const n = (p[key] ?? 0) + d
|
|
223
|
+
const { [key]: _d, ...rest } = p
|
|
224
|
+
return n > 0 ? { ...rest, [key]: n } : rest
|
|
225
|
+
})
|
|
226
|
+
mark(1)
|
|
227
|
+
setStepErrors(({ [key]: _d, ...rest }) => rest)
|
|
228
|
+
let marker: SetupStepState | null = null
|
|
229
|
+
let prev: SetupStepState | undefined
|
|
230
|
+
if (optimistic) {
|
|
231
|
+
setStates((p) => {
|
|
232
|
+
prev = p[key]
|
|
233
|
+
marker = { ...(p[key] ?? { key, status: 'pending' }), key, status: 'running' }
|
|
234
|
+
return { ...p, [key]: marker }
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
const answer = await call()
|
|
239
|
+
if (!alive.current) return
|
|
240
|
+
if (answer && 'steps' in answer) setServerCompleted(answer.completed)
|
|
241
|
+
setStates((p) => mergeAnswer(p, key, answer).next)
|
|
242
|
+
if (!answer) await refresh()
|
|
243
|
+
else setTick((n) => n + 1)
|
|
244
|
+
} catch (e) {
|
|
245
|
+
if (!alive.current) return
|
|
246
|
+
if (marker) {
|
|
247
|
+
const m = marker
|
|
248
|
+
// Undo only our optimistic marker, never a newer server state.
|
|
249
|
+
setStates((p) => {
|
|
250
|
+
if (p[key] !== m) return p
|
|
251
|
+
const { [key]: _d, ...rest } = p
|
|
252
|
+
return prev ? { ...rest, [key]: prev } : rest
|
|
253
|
+
})
|
|
254
|
+
}
|
|
255
|
+
setStepErrors((p) => ({ ...p, [key]: errorMessage(e, t('flow.step_failed')) }))
|
|
256
|
+
} finally {
|
|
257
|
+
if (alive.current) mark(-1)
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
[refresh, t],
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
const run = useCallback(
|
|
264
|
+
(key: string, input?: RunStepInput) => settle(key, () => client.runStep(key, input), true),
|
|
265
|
+
[client, settle],
|
|
266
|
+
)
|
|
267
|
+
const skip = useCallback((key: string) => settle(key, () => client.skipStep(key), false), [client, settle])
|
|
268
|
+
|
|
269
|
+
const onFix = (fix: SetupFix, stepKey: string) => {
|
|
270
|
+
if (fix.step) {
|
|
271
|
+
setSelected(fix.step)
|
|
272
|
+
return
|
|
273
|
+
}
|
|
274
|
+
if (fix.url) {
|
|
275
|
+
const url = safeUrl(fix.url)
|
|
276
|
+
if (!url) return
|
|
277
|
+
if (onNavigate) onNavigate(url)
|
|
278
|
+
else if (url.startsWith('/')) window.location.assign(url)
|
|
279
|
+
else window.open(url, '_blank', 'noopener,noreferrer')
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
if (fix.action) void run(stepKey, { action: fix.action })
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (loading) {
|
|
286
|
+
return (
|
|
287
|
+
<div className={cn('flex items-center gap-2 py-8 text-sm text-muted-foreground', className)} role='status'>
|
|
288
|
+
<Loader2 className='h-4 w-4 animate-spin motion-reduce:animate-none' aria-hidden='true' />
|
|
289
|
+
{t('flow.loading')}
|
|
290
|
+
</div>
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
if (loadError && Object.keys(states).length === 0) {
|
|
294
|
+
return (
|
|
295
|
+
<div className={cn('flex flex-col items-start gap-3 py-6', className)}>
|
|
296
|
+
<p role='alert' className='text-sm text-destructive'>
|
|
297
|
+
{loadError}
|
|
298
|
+
</p>
|
|
299
|
+
<Button type='button' variant='outline' size='sm' onClick={() => void refresh()}>
|
|
300
|
+
{t('flow.retry')}
|
|
301
|
+
</Button>
|
|
302
|
+
</div>
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return (
|
|
307
|
+
<div className={cn('flex flex-col gap-4', className)} data-setup-flow={flow.key}>
|
|
308
|
+
<header className='flex flex-col gap-2'>
|
|
309
|
+
{flow.title ? <h2 className='text-lg font-semibold'>{flow.title}</h2> : null}
|
|
310
|
+
{flow.description ? <p className='text-sm text-muted-foreground'>{flow.description}</p> : null}
|
|
311
|
+
<div className='flex items-center gap-3'>
|
|
312
|
+
<div
|
|
313
|
+
role='progressbar'
|
|
314
|
+
aria-label={t('flow.progress')}
|
|
315
|
+
aria-valuemin={0}
|
|
316
|
+
aria-valuemax={100}
|
|
317
|
+
aria-valuenow={progress.percent}
|
|
318
|
+
className='h-2 flex-1 overflow-hidden rounded-full bg-muted'
|
|
319
|
+
>
|
|
320
|
+
<div className='h-full rounded-full bg-primary transition-all motion-reduce:transition-none' style={{ width: `${progress.percent}%` }} />
|
|
321
|
+
</div>
|
|
322
|
+
<span className='shrink-0 text-xs tabular-nums text-muted-foreground'>
|
|
323
|
+
{t('health.progress', { done: progress.done, total: progress.total })}
|
|
324
|
+
</span>
|
|
325
|
+
</div>
|
|
326
|
+
{progress.complete ? (
|
|
327
|
+
<p className={cn('text-sm font-medium', TONE_TEXT.good)} role='status'>
|
|
328
|
+
{t('flow.complete')}
|
|
329
|
+
</p>
|
|
330
|
+
) : null}
|
|
331
|
+
{loadError ? (
|
|
332
|
+
<p role='alert' className='text-xs text-destructive'>
|
|
333
|
+
{loadError}
|
|
334
|
+
</p>
|
|
335
|
+
) : null}
|
|
336
|
+
</header>
|
|
337
|
+
|
|
338
|
+
<div className='grid gap-4 md:grid-cols-[16rem_minmax(0,1fr)]'>
|
|
339
|
+
<nav aria-label={t('flow.steps')}>
|
|
340
|
+
<ol className='flex gap-2 overflow-x-auto pb-1 md:flex-col md:overflow-visible md:pb-0'>
|
|
341
|
+
{defs.map((d, i) => {
|
|
342
|
+
const eff = effectiveStatus(d, states)
|
|
343
|
+
const isCurrent = d.key === currentKey
|
|
344
|
+
return (
|
|
345
|
+
<li key={d.key} className='shrink-0 md:shrink'>
|
|
346
|
+
<button
|
|
347
|
+
type='button'
|
|
348
|
+
onClick={() => setSelected(d.key)}
|
|
349
|
+
aria-current={isCurrent ? 'step' : undefined}
|
|
350
|
+
className={cn(
|
|
351
|
+
'flex w-full min-w-[10rem] flex-col items-start gap-1 rounded-md border px-3 py-2 text-left text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring md:min-w-0',
|
|
352
|
+
isCurrent ? 'border-primary bg-primary/5' : 'hover:bg-muted/50',
|
|
353
|
+
)}
|
|
354
|
+
>
|
|
355
|
+
<span className='font-medium'>
|
|
356
|
+
<span className='tabular-nums text-muted-foreground'>{i + 1}. </span>
|
|
357
|
+
{d.title}
|
|
358
|
+
</span>
|
|
359
|
+
<span className='flex flex-wrap items-center gap-1'>
|
|
360
|
+
<StepStatusBadge status={eff} />
|
|
361
|
+
{d.optional ? <span className='text-xs text-muted-foreground'>{t('flow.optional')}</span> : null}
|
|
362
|
+
</span>
|
|
363
|
+
</button>
|
|
364
|
+
</li>
|
|
365
|
+
)
|
|
366
|
+
})}
|
|
367
|
+
</ol>
|
|
368
|
+
</nav>
|
|
369
|
+
|
|
370
|
+
{current ? (
|
|
371
|
+
<StepPanel
|
|
372
|
+
key={current.key}
|
|
373
|
+
flowKey={flow.key}
|
|
374
|
+
step={current}
|
|
375
|
+
index={defs.indexOf(current)}
|
|
376
|
+
total={defs.length}
|
|
377
|
+
defs={defs}
|
|
378
|
+
states={states}
|
|
379
|
+
busy={(busyCount[current.key] ?? 0) > 0}
|
|
380
|
+
refreshing={refreshing}
|
|
381
|
+
error={stepErrors[current.key]}
|
|
382
|
+
pollStopped={pollStopped && watching.some((w) => w.key === current.key)}
|
|
383
|
+
client={client}
|
|
384
|
+
renderCustom={renderCustom}
|
|
385
|
+
onRun={(input) => run(current.key, input)}
|
|
386
|
+
onSkip={() => skip(current.key)}
|
|
387
|
+
onRefresh={refresh}
|
|
388
|
+
onFix={(fix) => onFix(fix, current.key)}
|
|
389
|
+
/>
|
|
390
|
+
) : null}
|
|
391
|
+
</div>
|
|
392
|
+
|
|
393
|
+
<p className='sr-only' role='status' aria-live='polite'>
|
|
394
|
+
{announce}
|
|
395
|
+
</p>
|
|
396
|
+
</div>
|
|
397
|
+
)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function StepPanel({
|
|
401
|
+
flowKey,
|
|
402
|
+
step,
|
|
403
|
+
index,
|
|
404
|
+
total,
|
|
405
|
+
defs,
|
|
406
|
+
states,
|
|
407
|
+
busy,
|
|
408
|
+
refreshing,
|
|
409
|
+
error,
|
|
410
|
+
pollStopped,
|
|
411
|
+
client,
|
|
412
|
+
renderCustom,
|
|
413
|
+
onRun,
|
|
414
|
+
onSkip,
|
|
415
|
+
onRefresh,
|
|
416
|
+
onFix,
|
|
417
|
+
}: {
|
|
418
|
+
flowKey?: string
|
|
419
|
+
step: SetupStepDef
|
|
420
|
+
index: number
|
|
421
|
+
total: number
|
|
422
|
+
defs: SetupStepDef[]
|
|
423
|
+
states: Record<string, SetupStepState>
|
|
424
|
+
busy: boolean
|
|
425
|
+
refreshing: boolean
|
|
426
|
+
error?: string
|
|
427
|
+
pollStopped: boolean
|
|
428
|
+
client: SetupFlowClient
|
|
429
|
+
renderCustom?: SetupFlowProps['renderCustom']
|
|
430
|
+
onRun: (input?: RunStepInput) => Promise<void>
|
|
431
|
+
onSkip: () => Promise<void>
|
|
432
|
+
onRefresh: () => Promise<void>
|
|
433
|
+
onFix: (fix: SetupFix) => void
|
|
434
|
+
}) {
|
|
435
|
+
const t = useSetupT()
|
|
436
|
+
const state = states[step.key]
|
|
437
|
+
const eff = effectiveStatus(step, states)
|
|
438
|
+
const unmet = unmetDependencies(step, states)
|
|
439
|
+
const blocked = eff === 'blocked'
|
|
440
|
+
const running = eff === 'running'
|
|
441
|
+
const titleOf = (k: string) => defs.find((d) => d.key === k)?.title ?? k
|
|
442
|
+
const diagnostics: SetupDiagnostic[] = state?.diagnostics ?? []
|
|
443
|
+
|
|
444
|
+
const mainLabel =
|
|
445
|
+
step.action_label ??
|
|
446
|
+
(step.kind === 'action' ? t('flow.run') : state && state.status !== 'pending' ? t('flow.recheck') : t('flow.check'))
|
|
447
|
+
|
|
448
|
+
let body: React.ReactNode = null
|
|
449
|
+
if (step.kind === 'form') {
|
|
450
|
+
body = (
|
|
451
|
+
<SetupFormStep
|
|
452
|
+
step={step}
|
|
453
|
+
state={state}
|
|
454
|
+
busy={busy || running}
|
|
455
|
+
onAutosave={client.saveStep ? (values) => client.saveStep!(step.key, values) : undefined}
|
|
456
|
+
onSubmit={(values) => onRun({ values })}
|
|
457
|
+
/>
|
|
458
|
+
)
|
|
459
|
+
} else if (step.kind === 'records') {
|
|
460
|
+
body = (
|
|
461
|
+
<RecordsTable
|
|
462
|
+
records={state?.records ?? []}
|
|
463
|
+
caption={step.title}
|
|
464
|
+
rechecking={busy || running}
|
|
465
|
+
onRecheck={blocked ? undefined : () => void onRun()}
|
|
466
|
+
onFix={(fix) => onFix(fix)}
|
|
467
|
+
/>
|
|
468
|
+
)
|
|
469
|
+
} else if (step.kind === 'custom') {
|
|
470
|
+
const props: SetupCustomStepProps = { step, state, run: onRun, refresh: onRefresh }
|
|
471
|
+
body = renderCustom ? (
|
|
472
|
+
renderCustom(props)
|
|
473
|
+
) : (
|
|
474
|
+
<Slot
|
|
475
|
+
name={step.slot ?? `setup.${flowKey ?? 'flow'}.${step.key}`}
|
|
476
|
+
props={props as unknown as Record<string, unknown>}
|
|
477
|
+
fallback={<p className='text-sm text-muted-foreground'>{t('flow.custom_missing')}</p>}
|
|
478
|
+
/>
|
|
479
|
+
)
|
|
480
|
+
} else {
|
|
481
|
+
body = (
|
|
482
|
+
<div>
|
|
483
|
+
<Button type='button' onClick={() => void onRun()} disabled={busy || running || blocked}>
|
|
484
|
+
{busy || running ? <Loader2 className='mr-2 h-4 w-4 animate-spin motion-reduce:animate-none' aria-hidden='true' /> : null}
|
|
485
|
+
{mainLabel}
|
|
486
|
+
</Button>
|
|
487
|
+
</div>
|
|
488
|
+
)
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return (
|
|
492
|
+
<section className='flex min-w-0 flex-col gap-4 rounded-lg border p-4' aria-labelledby={`setup-step-${step.key}-title`}>
|
|
493
|
+
<header className='flex flex-col gap-1'>
|
|
494
|
+
<p className='text-xs text-muted-foreground'>{t('flow.step_of', { n: index + 1, total })}</p>
|
|
495
|
+
<div className='flex flex-wrap items-center gap-2'>
|
|
496
|
+
<h3 id={`setup-step-${step.key}-title`} className='text-base font-semibold'>
|
|
497
|
+
{step.title}
|
|
498
|
+
</h3>
|
|
499
|
+
<StepStatusBadge status={eff} />
|
|
500
|
+
{step.optional ? <span className='text-xs text-muted-foreground'>{t('flow.optional')}</span> : null}
|
|
501
|
+
</div>
|
|
502
|
+
{step.description ? <p className='text-sm text-muted-foreground'>{step.description}</p> : null}
|
|
503
|
+
{state?.message ? <p className='text-sm'>{state.message}</p> : null}
|
|
504
|
+
</header>
|
|
505
|
+
|
|
506
|
+
{blocked && unmet.length > 0 ? (
|
|
507
|
+
<div className='flex flex-wrap items-center gap-2 text-sm text-muted-foreground'>
|
|
508
|
+
<span>{t('flow.waiting_for', { steps: unmet.map(titleOf).join(', ') })}</span>
|
|
509
|
+
<Button type='button' size='sm' variant='outline' className='h-7' onClick={() => onFix({ step: unmet[0], label: t('flow.go_to_step') })}>
|
|
510
|
+
{t('flow.go_to_step')}
|
|
511
|
+
</Button>
|
|
512
|
+
</div>
|
|
513
|
+
) : (
|
|
514
|
+
body
|
|
515
|
+
)}
|
|
516
|
+
|
|
517
|
+
{diagnostics.length > 0 ? (
|
|
518
|
+
<ul className='flex flex-col gap-2' aria-label={t('health.issues')}>
|
|
519
|
+
{diagnostics.map((d, i) => (
|
|
520
|
+
<li key={d.code ?? i} className='flex flex-wrap items-start justify-between gap-2 rounded-md border px-3 py-2' data-severity={d.severity ?? 'error'}>
|
|
521
|
+
<p className='min-w-0 flex-1 text-sm'>
|
|
522
|
+
{d.message}
|
|
523
|
+
{d.detail ? <span className='block break-words text-xs text-muted-foreground'>{d.detail}</span> : null}
|
|
524
|
+
</p>
|
|
525
|
+
{d.fix ? (
|
|
526
|
+
<Button type='button' size='sm' variant='outline' className='h-7' disabled={busy} onClick={() => onFix(d.fix!)}>
|
|
527
|
+
{d.fix.label ?? t('flow.fix')}
|
|
528
|
+
</Button>
|
|
529
|
+
) : null}
|
|
530
|
+
</li>
|
|
531
|
+
))}
|
|
532
|
+
</ul>
|
|
533
|
+
) : null}
|
|
534
|
+
|
|
535
|
+
{error ? (
|
|
536
|
+
<p role='alert' className='text-sm text-destructive'>
|
|
537
|
+
{error}
|
|
538
|
+
</p>
|
|
539
|
+
) : null}
|
|
540
|
+
|
|
541
|
+
{running ? (
|
|
542
|
+
<div className='flex flex-wrap items-center gap-2 text-xs text-muted-foreground'>
|
|
543
|
+
<Loader2 className='h-3.5 w-3.5 animate-spin motion-reduce:animate-none' aria-hidden='true' />
|
|
544
|
+
<span>{t('flow.running')}</span>
|
|
545
|
+
{pollStopped ? <span>{t('flow.poll_stopped')}</span> : null}
|
|
546
|
+
<Button type='button' size='sm' variant='ghost' className='h-7 gap-1' onClick={() => void onRefresh()} disabled={refreshing}>
|
|
547
|
+
<RefreshCw className='h-3.5 w-3.5' aria-hidden='true' />
|
|
548
|
+
{t('flow.refresh')}
|
|
549
|
+
</Button>
|
|
550
|
+
</div>
|
|
551
|
+
) : null}
|
|
552
|
+
|
|
553
|
+
{step.optional && !isSatisfied(state) ? (
|
|
554
|
+
<div>
|
|
555
|
+
<Button type='button' variant='ghost' size='sm' onClick={() => void onSkip()} disabled={busy}>
|
|
556
|
+
{t('flow.skip')}
|
|
557
|
+
</Button>
|
|
558
|
+
</div>
|
|
559
|
+
) : null}
|
|
560
|
+
</section>
|
|
561
|
+
)
|
|
562
|
+
}
|