@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,80 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Fragment, useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { Check, Copy, RefreshCw } from 'lucide-react';
|
|
4
|
+
import { Button } from '@asteby/metacore-ui/primitives';
|
|
5
|
+
import { cn } from '@asteby/metacore-ui/lib';
|
|
6
|
+
import { useSetupT } from './messages';
|
|
7
|
+
import { RECORD_TONE, TONE_BADGE } from './tone';
|
|
8
|
+
/**
|
|
9
|
+
* Copies text with a fallback: the async Clipboard API when the page may use
|
|
10
|
+
* it, otherwise a hidden textarea + `execCommand('copy')` (http pages, older
|
|
11
|
+
* webviews, denied permission). Resolves to whether something was copied.
|
|
12
|
+
*/
|
|
13
|
+
export async function copyText(text) {
|
|
14
|
+
try {
|
|
15
|
+
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
|
|
16
|
+
await navigator.clipboard.writeText(text);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
/* fall through to the legacy path */
|
|
22
|
+
}
|
|
23
|
+
if (typeof document === 'undefined')
|
|
24
|
+
return false;
|
|
25
|
+
const ta = document.createElement('textarea');
|
|
26
|
+
ta.value = text;
|
|
27
|
+
ta.setAttribute('readonly', '');
|
|
28
|
+
ta.style.position = 'fixed';
|
|
29
|
+
ta.style.opacity = '0';
|
|
30
|
+
document.body.appendChild(ta);
|
|
31
|
+
ta.select();
|
|
32
|
+
try {
|
|
33
|
+
return document.execCommand('copy');
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
document.body.removeChild(ta);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function CopyButton({ text, what }) {
|
|
43
|
+
const t = useSetupT();
|
|
44
|
+
const [state, setState] = useState('idle');
|
|
45
|
+
const timer = useRef(undefined);
|
|
46
|
+
useEffect(() => () => clearTimeout(timer.current), []);
|
|
47
|
+
const onCopy = useCallback(async () => {
|
|
48
|
+
const ok = await copyText(text);
|
|
49
|
+
setState(ok ? 'copied' : 'failed');
|
|
50
|
+
clearTimeout(timer.current);
|
|
51
|
+
timer.current = setTimeout(() => setState('idle'), 2500);
|
|
52
|
+
}, [text]);
|
|
53
|
+
return (_jsxs("span", { className: 'inline-flex items-center gap-1', children: [_jsx(Button, { type: 'button', variant: 'ghost', size: 'sm', className: 'h-7 w-7 shrink-0 p-0', onClick: onCopy, "aria-label": t('records.copy_label', { what }), title: t('records.copy'), children: state === 'copied' ? (_jsx(Check, { className: 'h-3.5 w-3.5 text-emerald-600 dark:text-emerald-400', "aria-hidden": 'true' })) : (_jsx(Copy, { className: 'h-3.5 w-3.5', "aria-hidden": 'true' })) }), _jsx("span", { role: 'status', className: cn('text-xs', state === 'failed' ? 'text-destructive' : 'text-muted-foreground', state === 'idle' && 'sr-only'), children: state === 'copied' ? t('records.copied') : state === 'failed' ? t('records.copy_failed') : '' })] }));
|
|
54
|
+
}
|
|
55
|
+
function diagOf(d) {
|
|
56
|
+
if (!d)
|
|
57
|
+
return null;
|
|
58
|
+
return typeof d === 'string' ? { message: d } : d;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Type / name / value table (DNS records and the like) with per-row copy,
|
|
62
|
+
* status and diagnostic. Scrolls horizontally on narrow screens instead of
|
|
63
|
+
* squeezing the values; values wrap (`break-all`) so a long DKIM key is readable.
|
|
64
|
+
*/
|
|
65
|
+
export function RecordsTable({ records, caption, onFix, onRecheck, rechecking, className }) {
|
|
66
|
+
const t = useSetupT();
|
|
67
|
+
if (!records || records.length === 0) {
|
|
68
|
+
return _jsx("p", { className: cn('text-sm text-muted-foreground', className), children: t('records.empty') });
|
|
69
|
+
}
|
|
70
|
+
return (_jsxs("div", { className: cn('flex flex-col gap-2', className), children: [onRecheck ? (_jsx("div", { className: 'flex justify-end', children: _jsxs(Button, { type: 'button', variant: 'outline', size: 'sm', onClick: onRecheck, disabled: rechecking, className: 'gap-2', children: [_jsx(RefreshCw, { className: cn('h-3.5 w-3.5', rechecking && 'animate-spin motion-reduce:animate-none'), "aria-hidden": 'true' }), t('records.recheck')] }) })) : null, _jsx("div", { className: 'overflow-x-auto rounded-md border', children: _jsxs("table", { className: 'w-full min-w-[34rem] border-collapse text-left text-sm', children: [caption ? _jsx("caption", { className: 'sr-only', children: caption }) : null, _jsx("thead", { className: 'bg-muted/50 text-xs text-muted-foreground', children: _jsxs("tr", { children: [_jsx("th", { scope: 'col', className: 'px-3 py-2 font-medium', children: t('records.type') }), _jsx("th", { scope: 'col', className: 'px-3 py-2 font-medium', children: t('records.name') }), _jsx("th", { scope: 'col', className: 'px-3 py-2 font-medium', children: t('records.value') }), _jsx("th", { scope: 'col', className: 'px-3 py-2 font-medium', children: t('records.status') })] }) }), _jsx("tbody", { children: records.map((r, i) => {
|
|
71
|
+
const status = r.status ?? 'pending';
|
|
72
|
+
const diag = diagOf(r.diagnostic);
|
|
73
|
+
const tone = RECORD_TONE[status] ?? 'neutral';
|
|
74
|
+
return (_jsxs(Fragment, { children: [_jsxs("tr", { className: cn(!diag && i < records.length - 1 && 'border-b'), children: [_jsx("td", { className: 'whitespace-nowrap px-3 py-2 align-top', children: _jsx("span", { className: 'rounded border bg-muted px-1.5 py-0.5 font-mono text-xs', children: r.type }) }), _jsx("td", { className: 'px-3 py-2 align-top', children: _jsxs("div", { className: 'flex items-start gap-1', children: [_jsx("code", { className: 'break-all font-mono text-xs', children: r.name }), _jsx(CopyButton, { text: r.name, what: t('records.name').toLowerCase() })] }) }), _jsxs("td", { className: 'px-3 py-2 align-top', children: [_jsxs("div", { className: 'flex items-start gap-1', children: [_jsx("code", { className: 'break-all font-mono text-xs', children: r.value }), _jsx(CopyButton, { text: r.value, what: t('records.value').toLowerCase() })] }), r.found && r.found !== r.value ? (_jsx("p", { className: 'pt-1 text-xs text-muted-foreground break-all', children: t('records.found', { value: r.found }) })) : null] }), _jsx("td", { className: 'px-3 py-2 align-top', children: _jsx("span", { className: cn('inline-flex items-center rounded-full border px-2 py-0.5 text-xs font-medium', TONE_BADGE[tone]), "data-record-status": status, children: t(`record.status.${status}`) }) })] }), diag ? (_jsx("tr", { className: cn(i < records.length - 1 && 'border-b'), children: _jsx(DiagnosticCell, { diag: diag, onFix: onFix ? (f) => onFix(f, r) : undefined }) })) : null] }, r.id ?? `${r.type}-${r.name}-${i}`));
|
|
75
|
+
}) })] }) })] }));
|
|
76
|
+
}
|
|
77
|
+
function DiagnosticCell({ diag, onFix }) {
|
|
78
|
+
const t = useSetupT();
|
|
79
|
+
return (_jsx("td", { colSpan: 4, className: 'bg-muted/30 px-3 py-2 text-xs', children: _jsxs("div", { className: 'flex flex-wrap items-center justify-between gap-2', children: [_jsxs("p", { className: 'min-w-0 flex-1 text-muted-foreground', children: [diag.message, diag.detail ? _jsx("span", { className: 'block break-words', children: diag.detail }) : null] }), diag.fix && onFix ? (_jsx(Button, { type: 'button', size: 'sm', variant: 'outline', className: 'h-7', onClick: () => onFix(diag.fix), children: diag.fix.label ?? t('flow.fix') })) : null] }) }));
|
|
80
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { RunStepInput, SetupFlowClient, SetupFlowDef, SetupFlowState, SetupStepDef, SetupStepState } from './types';
|
|
2
|
+
export interface SetupCustomStepProps {
|
|
3
|
+
step: SetupStepDef;
|
|
4
|
+
state?: SetupStepState;
|
|
5
|
+
/** Runs the step on the server (and updates the flow). */
|
|
6
|
+
run: (input?: RunStepInput) => Promise<void>;
|
|
7
|
+
refresh: () => Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export interface SetupFlowProps {
|
|
10
|
+
flow: SetupFlowDef;
|
|
11
|
+
client: SetupFlowClient;
|
|
12
|
+
/** State already known (e.g. from SSR/cache); the client is still read on mount. */
|
|
13
|
+
initialState?: SetupFlowState;
|
|
14
|
+
/** Renders kind=custom. Default: the federated slot `step.slot ?? setup.<flow.key>.<step.key>`. */
|
|
15
|
+
renderCustom?: (p: SetupCustomStepProps) => React.ReactNode;
|
|
16
|
+
/** Opens `fix.url`. Default: relative paths via location, absolute in a new tab. */
|
|
17
|
+
onNavigate?: (url: string) => void;
|
|
18
|
+
onComplete?: () => void;
|
|
19
|
+
/** Automatic re-reads while something runs before asking for a manual refresh. */
|
|
20
|
+
maxPollAttempts?: number;
|
|
21
|
+
className?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Resumable setup flow (`contributions.setup`). The server owns the state
|
|
25
|
+
* (`client.getState`); the UI renders each step by `kind`, honours
|
|
26
|
+
* `depends_on` / `optional`, polls with backoff while something runs
|
|
27
|
+
* (`retry_after_s` wins), and offers diagnostics with a fix button.
|
|
28
|
+
*/
|
|
29
|
+
export declare function SetupFlow({ flow, client, initialState, renderCustom, onNavigate, onComplete, maxPollAttempts, className, }: SetupFlowProps): import("react").JSX.Element;
|
|
30
|
+
//# sourceMappingURL=setup-flow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-flow.d.ts","sourceRoot":"","sources":["../../src/setup/setup-flow.tsx"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EACR,YAAY,EAGZ,eAAe,EACf,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,cAAc,EACjB,MAAM,SAAS,CAAA;AAEhB,MAAM,WAAW,oBAAoB;IACjC,IAAI,EAAE,YAAY,CAAA;IAClB,KAAK,CAAC,EAAE,cAAc,CAAA;IACtB,0DAA0D;IAC1D,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5C,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC/B;AAED,MAAM,WAAW,cAAc;IAC3B,IAAI,EAAE,YAAY,CAAA;IAClB,MAAM,EAAE,eAAe,CAAA;IACvB,oFAAoF;IACpF,YAAY,CAAC,EAAE,cAAc,CAAA;IAC7B,mGAAmG;IACnG,YAAY,CAAC,EAAE,CAAC,CAAC,EAAE,oBAAoB,KAAK,KAAK,CAAC,SAAS,CAAA;IAC3D,oFAAoF;IACpF,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IAClC,UAAU,CAAC,EAAE,MAAM,IAAI,CAAA;IACvB,kFAAkF;IAClF,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;CACrB;AASD;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,EACtB,IAAI,EACJ,MAAM,EACN,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,UAAU,EACV,eAAoB,EACpB,SAAS,GACZ,EAAE,cAAc,+BAgUhB"}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { Loader2, RefreshCw } from 'lucide-react';
|
|
4
|
+
import { Button } from '@asteby/metacore-ui/primitives';
|
|
5
|
+
import { cn } from '@asteby/metacore-ui/lib';
|
|
6
|
+
import { Slot } from '../slot';
|
|
7
|
+
import { useSetupT } from './messages';
|
|
8
|
+
import { effectiveStatus, flowProgress, isSatisfied, mergeAnswer, nextPollDelayMs, normalizeState, pickActiveStep, safeUrl, unmetDependencies, } from './flow-model';
|
|
9
|
+
import { RecordsTable } from './records-table';
|
|
10
|
+
import { SetupFormStep } from './setup-form-step';
|
|
11
|
+
import { StepStatusBadge } from './step-status-badge';
|
|
12
|
+
import { TONE_TEXT } from './tone';
|
|
13
|
+
const errorMessage = (e, fallback) => {
|
|
14
|
+
const r = e?.response?.data?.message;
|
|
15
|
+
if (typeof r === 'string' && r.trim())
|
|
16
|
+
return r;
|
|
17
|
+
const m = e?.message;
|
|
18
|
+
return typeof m === 'string' && m.trim() ? m : fallback;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Resumable setup flow (`contributions.setup`). The server owns the state
|
|
22
|
+
* (`client.getState`); the UI renders each step by `kind`, honours
|
|
23
|
+
* `depends_on` / `optional`, polls with backoff while something runs
|
|
24
|
+
* (`retry_after_s` wins), and offers diagnostics with a fix button.
|
|
25
|
+
*/
|
|
26
|
+
export function SetupFlow({ flow, client, initialState, renderCustom, onNavigate, onComplete, maxPollAttempts = 40, className, }) {
|
|
27
|
+
const t = useSetupT();
|
|
28
|
+
const [states, setStates] = useState(() => normalizeState(initialState));
|
|
29
|
+
const [serverCompleted, setServerCompleted] = useState(initialState?.completed);
|
|
30
|
+
const [loading, setLoading] = useState(!initialState);
|
|
31
|
+
const [loadError, setLoadError] = useState(null);
|
|
32
|
+
const [selected, setSelected] = useState(null);
|
|
33
|
+
const [busyCount, setBusyCount] = useState({});
|
|
34
|
+
const [refreshing, setRefreshing] = useState(false);
|
|
35
|
+
const [stepErrors, setStepErrors] = useState({});
|
|
36
|
+
const [pollStopped, setPollStopped] = useState(false);
|
|
37
|
+
const [tick, setTick] = useState(0);
|
|
38
|
+
const [announce, setAnnounce] = useState('');
|
|
39
|
+
const attempts = useRef(0);
|
|
40
|
+
const sig = useRef('');
|
|
41
|
+
const prevStatuses = useRef({});
|
|
42
|
+
const wasIncomplete = useRef(false);
|
|
43
|
+
const clientRef = useRef(client);
|
|
44
|
+
clientRef.current = client;
|
|
45
|
+
const onCompleteRef = useRef(onComplete);
|
|
46
|
+
onCompleteRef.current = onComplete;
|
|
47
|
+
const hasSubscribe = !!client.subscribe;
|
|
48
|
+
const inflight = useRef(null);
|
|
49
|
+
const alive = useRef(true);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
alive.current = true;
|
|
52
|
+
return () => {
|
|
53
|
+
alive.current = false;
|
|
54
|
+
};
|
|
55
|
+
}, []);
|
|
56
|
+
const apply = useCallback((s) => {
|
|
57
|
+
setStates(normalizeState(s));
|
|
58
|
+
setServerCompleted(s.completed);
|
|
59
|
+
}, []);
|
|
60
|
+
// `client` / `subscribe` live in refs: a host passing an inline client object
|
|
61
|
+
// (new identity every render) must not restart the fetch/subscribe effects.
|
|
62
|
+
const refresh = useCallback(() => {
|
|
63
|
+
if (inflight.current)
|
|
64
|
+
return inflight.current;
|
|
65
|
+
const run = (async () => {
|
|
66
|
+
if (alive.current)
|
|
67
|
+
setRefreshing(true);
|
|
68
|
+
try {
|
|
69
|
+
const s = await clientRef.current.getState();
|
|
70
|
+
if (!alive.current)
|
|
71
|
+
return;
|
|
72
|
+
apply(s);
|
|
73
|
+
setLoadError(null);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
if (alive.current)
|
|
77
|
+
setLoadError(errorMessage(e, t('flow.load_error')));
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
inflight.current = null;
|
|
81
|
+
if (alive.current) {
|
|
82
|
+
setRefreshing(false);
|
|
83
|
+
setLoading(false);
|
|
84
|
+
setTick((n) => n + 1);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
})();
|
|
88
|
+
inflight.current = run;
|
|
89
|
+
return run;
|
|
90
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
91
|
+
}, [apply]);
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
void refresh();
|
|
94
|
+
}, [refresh]);
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (!hasSubscribe)
|
|
97
|
+
return;
|
|
98
|
+
return clientRef.current.subscribe?.((s) => {
|
|
99
|
+
if (alive.current)
|
|
100
|
+
apply(s);
|
|
101
|
+
});
|
|
102
|
+
}, [hasSubscribe, apply]);
|
|
103
|
+
const defs = flow.steps;
|
|
104
|
+
const progress = flowProgress(defs, states, serverCompleted);
|
|
105
|
+
const activeKey = pickActiveStep(defs, states);
|
|
106
|
+
const currentKey = selected && defs.some((d) => d.key === selected) ? selected : activeKey;
|
|
107
|
+
const current = defs.find((d) => d.key === currentKey);
|
|
108
|
+
// Polling with backoff while anything runs or the server asked to come back.
|
|
109
|
+
const watching = defs.filter((d) => {
|
|
110
|
+
const st = states[d.key];
|
|
111
|
+
return !!st && !st.skipped && (st.status === 'running' || ((st.retry_after_s ?? 0) > 0 && st.status !== 'ok'));
|
|
112
|
+
});
|
|
113
|
+
const retryHints = watching.map((d) => states[d.key].retry_after_s ?? 0).filter((n) => n > 0);
|
|
114
|
+
const retryHint = retryHints.length ? Math.min(...retryHints) : undefined;
|
|
115
|
+
const signature = watching.map((d) => `${d.key}:${states[d.key].status}`).join('|') + `#${retryHint ?? ''}`;
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (watching.length === 0) {
|
|
118
|
+
attempts.current = 0;
|
|
119
|
+
sig.current = '';
|
|
120
|
+
setPollStopped(false);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (sig.current !== signature) {
|
|
124
|
+
sig.current = signature;
|
|
125
|
+
attempts.current = 0;
|
|
126
|
+
setPollStopped(false);
|
|
127
|
+
}
|
|
128
|
+
if (attempts.current >= maxPollAttempts) {
|
|
129
|
+
setPollStopped(true);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const id = setTimeout(() => {
|
|
133
|
+
attempts.current += 1;
|
|
134
|
+
void refresh();
|
|
135
|
+
}, nextPollDelayMs(attempts.current, retryHint));
|
|
136
|
+
return () => clearTimeout(id);
|
|
137
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
138
|
+
}, [signature, tick, maxPollAttempts]);
|
|
139
|
+
// Screen-reader announcement when a step changes status.
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
const prev = prevStatuses.current;
|
|
142
|
+
const msgs = [];
|
|
143
|
+
for (const d of defs) {
|
|
144
|
+
const eff = effectiveStatus(d, states);
|
|
145
|
+
if (prev[d.key] && prev[d.key] !== eff) {
|
|
146
|
+
msgs.push(t('flow.status_changed', { step: d.title, status: t(`status.${eff}`) }));
|
|
147
|
+
}
|
|
148
|
+
prev[d.key] = eff;
|
|
149
|
+
}
|
|
150
|
+
if (msgs.length)
|
|
151
|
+
setAnnounce(msgs.join('. '));
|
|
152
|
+
}, [states, defs, t]);
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
// Fires on the incomplete -> complete transition only: a flow that was
|
|
155
|
+
// already complete when it mounted does not re-announce itself.
|
|
156
|
+
if (loading)
|
|
157
|
+
return;
|
|
158
|
+
if (!progress.complete) {
|
|
159
|
+
wasIncomplete.current = true;
|
|
160
|
+
}
|
|
161
|
+
else if (wasIncomplete.current) {
|
|
162
|
+
wasIncomplete.current = false;
|
|
163
|
+
onCompleteRef.current?.();
|
|
164
|
+
}
|
|
165
|
+
}, [progress.complete, loading]);
|
|
166
|
+
const settle = useCallback(async (key, call, optimistic) => {
|
|
167
|
+
// Functional updates only: polling/subscribe may have written newer
|
|
168
|
+
// state while the call was in flight, and `busy` is tracked per step.
|
|
169
|
+
const mark = (d) => setBusyCount((p) => {
|
|
170
|
+
const n = (p[key] ?? 0) + d;
|
|
171
|
+
const { [key]: _d, ...rest } = p;
|
|
172
|
+
return n > 0 ? { ...rest, [key]: n } : rest;
|
|
173
|
+
});
|
|
174
|
+
mark(1);
|
|
175
|
+
setStepErrors(({ [key]: _d, ...rest }) => rest);
|
|
176
|
+
let marker = null;
|
|
177
|
+
let prev;
|
|
178
|
+
if (optimistic) {
|
|
179
|
+
setStates((p) => {
|
|
180
|
+
prev = p[key];
|
|
181
|
+
marker = { ...(p[key] ?? { key, status: 'pending' }), key, status: 'running' };
|
|
182
|
+
return { ...p, [key]: marker };
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const answer = await call();
|
|
187
|
+
if (!alive.current)
|
|
188
|
+
return;
|
|
189
|
+
if (answer && 'steps' in answer)
|
|
190
|
+
setServerCompleted(answer.completed);
|
|
191
|
+
setStates((p) => mergeAnswer(p, key, answer).next);
|
|
192
|
+
if (!answer)
|
|
193
|
+
await refresh();
|
|
194
|
+
else
|
|
195
|
+
setTick((n) => n + 1);
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
if (!alive.current)
|
|
199
|
+
return;
|
|
200
|
+
if (marker) {
|
|
201
|
+
const m = marker;
|
|
202
|
+
// Undo only our optimistic marker, never a newer server state.
|
|
203
|
+
setStates((p) => {
|
|
204
|
+
if (p[key] !== m)
|
|
205
|
+
return p;
|
|
206
|
+
const { [key]: _d, ...rest } = p;
|
|
207
|
+
return prev ? { ...rest, [key]: prev } : rest;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
setStepErrors((p) => ({ ...p, [key]: errorMessage(e, t('flow.step_failed')) }));
|
|
211
|
+
}
|
|
212
|
+
finally {
|
|
213
|
+
if (alive.current)
|
|
214
|
+
mark(-1);
|
|
215
|
+
}
|
|
216
|
+
}, [refresh, t]);
|
|
217
|
+
const run = useCallback((key, input) => settle(key, () => client.runStep(key, input), true), [client, settle]);
|
|
218
|
+
const skip = useCallback((key) => settle(key, () => client.skipStep(key), false), [client, settle]);
|
|
219
|
+
const onFix = (fix, stepKey) => {
|
|
220
|
+
if (fix.step) {
|
|
221
|
+
setSelected(fix.step);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (fix.url) {
|
|
225
|
+
const url = safeUrl(fix.url);
|
|
226
|
+
if (!url)
|
|
227
|
+
return;
|
|
228
|
+
if (onNavigate)
|
|
229
|
+
onNavigate(url);
|
|
230
|
+
else if (url.startsWith('/'))
|
|
231
|
+
window.location.assign(url);
|
|
232
|
+
else
|
|
233
|
+
window.open(url, '_blank', 'noopener,noreferrer');
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (fix.action)
|
|
237
|
+
void run(stepKey, { action: fix.action });
|
|
238
|
+
};
|
|
239
|
+
if (loading) {
|
|
240
|
+
return (_jsxs("div", { className: cn('flex items-center gap-2 py-8 text-sm text-muted-foreground', className), role: 'status', children: [_jsx(Loader2, { className: 'h-4 w-4 animate-spin motion-reduce:animate-none', "aria-hidden": 'true' }), t('flow.loading')] }));
|
|
241
|
+
}
|
|
242
|
+
if (loadError && Object.keys(states).length === 0) {
|
|
243
|
+
return (_jsxs("div", { className: cn('flex flex-col items-start gap-3 py-6', className), children: [_jsx("p", { role: 'alert', className: 'text-sm text-destructive', children: loadError }), _jsx(Button, { type: 'button', variant: 'outline', size: 'sm', onClick: () => void refresh(), children: t('flow.retry') })] }));
|
|
244
|
+
}
|
|
245
|
+
return (_jsxs("div", { className: cn('flex flex-col gap-4', className), "data-setup-flow": flow.key, children: [_jsxs("header", { className: 'flex flex-col gap-2', children: [flow.title ? _jsx("h2", { className: 'text-lg font-semibold', children: flow.title }) : null, flow.description ? _jsx("p", { className: 'text-sm text-muted-foreground', children: flow.description }) : null, _jsxs("div", { className: 'flex items-center gap-3', children: [_jsx("div", { role: 'progressbar', "aria-label": t('flow.progress'), "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": progress.percent, className: 'h-2 flex-1 overflow-hidden rounded-full bg-muted', children: _jsx("div", { className: 'h-full rounded-full bg-primary transition-all motion-reduce:transition-none', style: { width: `${progress.percent}%` } }) }), _jsx("span", { className: 'shrink-0 text-xs tabular-nums text-muted-foreground', children: t('health.progress', { done: progress.done, total: progress.total }) })] }), progress.complete ? (_jsx("p", { className: cn('text-sm font-medium', TONE_TEXT.good), role: 'status', children: t('flow.complete') })) : null, loadError ? (_jsx("p", { role: 'alert', className: 'text-xs text-destructive', children: loadError })) : null] }), _jsxs("div", { className: 'grid gap-4 md:grid-cols-[16rem_minmax(0,1fr)]', children: [_jsx("nav", { "aria-label": t('flow.steps'), children: _jsx("ol", { className: 'flex gap-2 overflow-x-auto pb-1 md:flex-col md:overflow-visible md:pb-0', children: defs.map((d, i) => {
|
|
246
|
+
const eff = effectiveStatus(d, states);
|
|
247
|
+
const isCurrent = d.key === currentKey;
|
|
248
|
+
return (_jsx("li", { className: 'shrink-0 md:shrink', children: _jsxs("button", { type: 'button', onClick: () => setSelected(d.key), "aria-current": isCurrent ? 'step' : undefined, className: cn('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', isCurrent ? 'border-primary bg-primary/5' : 'hover:bg-muted/50'), children: [_jsxs("span", { className: 'font-medium', children: [_jsxs("span", { className: 'tabular-nums text-muted-foreground', children: [i + 1, ". "] }), d.title] }), _jsxs("span", { className: 'flex flex-wrap items-center gap-1', children: [_jsx(StepStatusBadge, { status: eff }), d.optional ? _jsx("span", { className: 'text-xs text-muted-foreground', children: t('flow.optional') }) : null] })] }) }, d.key));
|
|
249
|
+
}) }) }), current ? (_jsx(StepPanel, { flowKey: flow.key, step: current, index: defs.indexOf(current), total: defs.length, defs: defs, states: states, busy: (busyCount[current.key] ?? 0) > 0, refreshing: refreshing, error: stepErrors[current.key], pollStopped: pollStopped && watching.some((w) => w.key === current.key), client: client, renderCustom: renderCustom, onRun: (input) => run(current.key, input), onSkip: () => skip(current.key), onRefresh: refresh, onFix: (fix) => onFix(fix, current.key) }, current.key)) : null] }), _jsx("p", { className: 'sr-only', role: 'status', "aria-live": 'polite', children: announce })] }));
|
|
250
|
+
}
|
|
251
|
+
function StepPanel({ flowKey, step, index, total, defs, states, busy, refreshing, error, pollStopped, client, renderCustom, onRun, onSkip, onRefresh, onFix, }) {
|
|
252
|
+
const t = useSetupT();
|
|
253
|
+
const state = states[step.key];
|
|
254
|
+
const eff = effectiveStatus(step, states);
|
|
255
|
+
const unmet = unmetDependencies(step, states);
|
|
256
|
+
const blocked = eff === 'blocked';
|
|
257
|
+
const running = eff === 'running';
|
|
258
|
+
const titleOf = (k) => defs.find((d) => d.key === k)?.title ?? k;
|
|
259
|
+
const diagnostics = state?.diagnostics ?? [];
|
|
260
|
+
const mainLabel = step.action_label ??
|
|
261
|
+
(step.kind === 'action' ? t('flow.run') : state && state.status !== 'pending' ? t('flow.recheck') : t('flow.check'));
|
|
262
|
+
let body = null;
|
|
263
|
+
if (step.kind === 'form') {
|
|
264
|
+
body = (_jsx(SetupFormStep, { step: step, state: state, busy: busy || running, onAutosave: client.saveStep ? (values) => client.saveStep(step.key, values) : undefined, onSubmit: (values) => onRun({ values }) }));
|
|
265
|
+
}
|
|
266
|
+
else if (step.kind === 'records') {
|
|
267
|
+
body = (_jsx(RecordsTable, { records: state?.records ?? [], caption: step.title, rechecking: busy || running, onRecheck: blocked ? undefined : () => void onRun(), onFix: (fix) => onFix(fix) }));
|
|
268
|
+
}
|
|
269
|
+
else if (step.kind === 'custom') {
|
|
270
|
+
const props = { step, state, run: onRun, refresh: onRefresh };
|
|
271
|
+
body = renderCustom ? (renderCustom(props)) : (_jsx(Slot, { name: step.slot ?? `setup.${flowKey ?? 'flow'}.${step.key}`, props: props, fallback: _jsx("p", { className: 'text-sm text-muted-foreground', children: t('flow.custom_missing') }) }));
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
body = (_jsx("div", { children: _jsxs(Button, { type: 'button', onClick: () => void onRun(), disabled: busy || running || blocked, children: [busy || running ? _jsx(Loader2, { className: 'mr-2 h-4 w-4 animate-spin motion-reduce:animate-none', "aria-hidden": 'true' }) : null, mainLabel] }) }));
|
|
275
|
+
}
|
|
276
|
+
return (_jsxs("section", { className: 'flex min-w-0 flex-col gap-4 rounded-lg border p-4', "aria-labelledby": `setup-step-${step.key}-title`, children: [_jsxs("header", { className: 'flex flex-col gap-1', children: [_jsx("p", { className: 'text-xs text-muted-foreground', children: t('flow.step_of', { n: index + 1, total }) }), _jsxs("div", { className: 'flex flex-wrap items-center gap-2', children: [_jsx("h3", { id: `setup-step-${step.key}-title`, className: 'text-base font-semibold', children: step.title }), _jsx(StepStatusBadge, { status: eff }), step.optional ? _jsx("span", { className: 'text-xs text-muted-foreground', children: t('flow.optional') }) : null] }), step.description ? _jsx("p", { className: 'text-sm text-muted-foreground', children: step.description }) : null, state?.message ? _jsx("p", { className: 'text-sm', children: state.message }) : null] }), blocked && unmet.length > 0 ? (_jsxs("div", { className: 'flex flex-wrap items-center gap-2 text-sm text-muted-foreground', children: [_jsx("span", { children: t('flow.waiting_for', { steps: unmet.map(titleOf).join(', ') }) }), _jsx(Button, { type: 'button', size: 'sm', variant: 'outline', className: 'h-7', onClick: () => onFix({ step: unmet[0], label: t('flow.go_to_step') }), children: t('flow.go_to_step') })] })) : (body), diagnostics.length > 0 ? (_jsx("ul", { className: 'flex flex-col gap-2', "aria-label": t('health.issues'), children: diagnostics.map((d, i) => (_jsxs("li", { className: 'flex flex-wrap items-start justify-between gap-2 rounded-md border px-3 py-2', "data-severity": d.severity ?? 'error', children: [_jsxs("p", { className: 'min-w-0 flex-1 text-sm', children: [d.message, d.detail ? _jsx("span", { className: 'block break-words text-xs text-muted-foreground', children: d.detail }) : null] }), d.fix ? (_jsx(Button, { type: 'button', size: 'sm', variant: 'outline', className: 'h-7', disabled: busy, onClick: () => onFix(d.fix), children: d.fix.label ?? t('flow.fix') })) : null] }, d.code ?? i))) })) : null, error ? (_jsx("p", { role: 'alert', className: 'text-sm text-destructive', children: error })) : null, running ? (_jsxs("div", { className: 'flex flex-wrap items-center gap-2 text-xs text-muted-foreground', children: [_jsx(Loader2, { className: 'h-3.5 w-3.5 animate-spin motion-reduce:animate-none', "aria-hidden": 'true' }), _jsx("span", { children: t('flow.running') }), pollStopped ? _jsx("span", { children: t('flow.poll_stopped') }) : null, _jsxs(Button, { type: 'button', size: 'sm', variant: 'ghost', className: 'h-7 gap-1', onClick: () => void onRefresh(), disabled: refreshing, children: [_jsx(RefreshCw, { className: 'h-3.5 w-3.5', "aria-hidden": 'true' }), t('flow.refresh')] })] })) : null, step.optional && !isSatisfied(state) ? (_jsx("div", { children: _jsx(Button, { type: 'button', variant: 'ghost', size: 'sm', onClick: () => void onSkip(), disabled: busy, children: t('flow.skip') }) })) : null] }));
|
|
277
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SetupStepDef, SetupStepState } from './types';
|
|
2
|
+
export type AutosaveState = 'idle' | 'saving' | 'saved' | 'error';
|
|
3
|
+
export interface SetupFormStepProps {
|
|
4
|
+
step: SetupStepDef;
|
|
5
|
+
state?: SetupStepState;
|
|
6
|
+
busy?: boolean;
|
|
7
|
+
/** Draft autosave; absent → no autosave and no indicator. */
|
|
8
|
+
onAutosave?: (values: Record<string, unknown>) => Promise<void>;
|
|
9
|
+
/** Final submit: the dirty values (secrets only when typed). */
|
|
10
|
+
onSubmit: (values: Record<string, unknown>) => void | Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Form step of a setup flow. Sections / wizard sub-steps come from the shared
|
|
14
|
+
* `form-layout` grouping and `form-layout-ui` chrome (the same ones model
|
|
15
|
+
* forms use). `write_only` fields are never read back: the input stays empty
|
|
16
|
+
* and an empty value keeps the stored one. Edits autosave (debounced) and the
|
|
17
|
+
* save state is visible and announced.
|
|
18
|
+
*/
|
|
19
|
+
export declare function SetupFormStep({ step, state, busy, onAutosave, onSubmit }: SetupFormStepProps): import("react").JSX.Element;
|
|
20
|
+
//# sourceMappingURL=setup-form-step.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-form-step.d.ts","sourceRoot":"","sources":["../../src/setup/setup-form-step.tsx"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAc,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAEvE,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,GAAG,OAAO,CAAA;AAkBjE,MAAM,WAAW,kBAAkB;IAC/B,IAAI,EAAE,YAAY,CAAA;IAClB,KAAK,CAAC,EAAE,cAAc,CAAA;IACtB,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,6DAA6D;IAC7D,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC/D,gEAAgE;IAChE,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACtE;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,kBAAkB,+BAqM5F"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { Loader2 } from 'lucide-react';
|
|
4
|
+
import { Button, Input, Label, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Switch, Textarea, } from '@asteby/metacore-ui/primitives';
|
|
5
|
+
import { cn } from '@asteby/metacore-ui/lib';
|
|
6
|
+
import { evaluateVisibleWhen, getVisibleWhen } from '../dynamic-form-schema';
|
|
7
|
+
import { groupFieldsBySection } from '../form-layout';
|
|
8
|
+
import { FieldSection, WizardProgress } from '../form-layout-ui';
|
|
9
|
+
import { useSetupT } from './messages';
|
|
10
|
+
const AUTOSAVE_MS = 800;
|
|
11
|
+
function initialValues(fields, saved) {
|
|
12
|
+
const out = {};
|
|
13
|
+
for (const f of fields) {
|
|
14
|
+
if (f.write_only || f.type === 'secret')
|
|
15
|
+
continue;
|
|
16
|
+
if (saved && f.key in saved)
|
|
17
|
+
out[f.key] = saved[f.key];
|
|
18
|
+
else if (f.default !== undefined)
|
|
19
|
+
out[f.key] = f.default;
|
|
20
|
+
else if (f.type === 'select' && f.options?.length)
|
|
21
|
+
out[f.key] = f.options[0].value;
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
const isWriteOnly = (f) => !!f.write_only || f.type === 'secret';
|
|
26
|
+
const isEmpty = (v) => v == null || (typeof v === 'string' && v.trim() === '');
|
|
27
|
+
/**
|
|
28
|
+
* Form step of a setup flow. Sections / wizard sub-steps come from the shared
|
|
29
|
+
* `form-layout` grouping and `form-layout-ui` chrome (the same ones model
|
|
30
|
+
* forms use). `write_only` fields are never read back: the input stays empty
|
|
31
|
+
* and an empty value keeps the stored one. Edits autosave (debounced) and the
|
|
32
|
+
* save state is visible and announced.
|
|
33
|
+
*/
|
|
34
|
+
export function SetupFormStep({ step, state, busy, onAutosave, onSubmit }) {
|
|
35
|
+
const t = useSetupT();
|
|
36
|
+
const fields = step.fields ?? [];
|
|
37
|
+
const [values, setValues] = useState(() => initialValues(fields, state?.values));
|
|
38
|
+
const [stored, setStored] = useState(() => new Set(state?.stored_keys ?? []));
|
|
39
|
+
const [errors, setErrors] = useState({});
|
|
40
|
+
const [autosave, setAutosave] = useState('idle');
|
|
41
|
+
const [stepIdx, setStepIdx] = useState(0);
|
|
42
|
+
const dirty = useRef(new Set());
|
|
43
|
+
const [dirtyTick, setDirtyTick] = useState(0);
|
|
44
|
+
const idPrefix = useId();
|
|
45
|
+
const formRef = useRef(null);
|
|
46
|
+
const valuesRef = useRef(values);
|
|
47
|
+
valuesRef.current = values;
|
|
48
|
+
// Server values arrive (resume / refetch): fill what the person has not touched.
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!state?.values)
|
|
51
|
+
return;
|
|
52
|
+
setValues((prev) => {
|
|
53
|
+
const next = { ...prev };
|
|
54
|
+
for (const [k, v] of Object.entries(state.values))
|
|
55
|
+
if (!dirty.current.has(k))
|
|
56
|
+
next[k] = v;
|
|
57
|
+
return next;
|
|
58
|
+
});
|
|
59
|
+
}, [state?.values]);
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (state?.stored_keys)
|
|
62
|
+
setStored(new Set(state.stored_keys));
|
|
63
|
+
}, [state?.stored_keys]);
|
|
64
|
+
const setValue = (key, v) => {
|
|
65
|
+
setValues((p) => ({ ...p, [key]: v }));
|
|
66
|
+
dirty.current.add(key);
|
|
67
|
+
setDirtyTick((n) => n + 1);
|
|
68
|
+
if (errors[key])
|
|
69
|
+
setErrors(({ [key]: _d, ...rest }) => rest);
|
|
70
|
+
};
|
|
71
|
+
const payloadFor = useCallback((keys) => {
|
|
72
|
+
const out = {};
|
|
73
|
+
const v = valuesRef.current;
|
|
74
|
+
for (const k of keys) {
|
|
75
|
+
const f = fields.find((x) => x.key === k);
|
|
76
|
+
if (!f)
|
|
77
|
+
continue;
|
|
78
|
+
if (isWriteOnly(f) && isEmpty(v[k]))
|
|
79
|
+
continue;
|
|
80
|
+
out[k] = v[k];
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}, [fields]);
|
|
84
|
+
// Debounced autosave of what changed.
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (!onAutosave || dirty.current.size === 0)
|
|
87
|
+
return;
|
|
88
|
+
// Write-only fields (secrets) never autosave: a half-typed secret would be
|
|
89
|
+
// stored truncated. They travel only in the explicit submit.
|
|
90
|
+
const snapshot = new Set([...dirty.current].filter((k) => {
|
|
91
|
+
const f = fields.find((x) => x.key === k);
|
|
92
|
+
return !!f && !isWriteOnly(f);
|
|
93
|
+
}));
|
|
94
|
+
if (snapshot.size === 0)
|
|
95
|
+
return;
|
|
96
|
+
const id = setTimeout(async () => {
|
|
97
|
+
const payload = payloadFor(snapshot);
|
|
98
|
+
if (Object.keys(payload).length === 0)
|
|
99
|
+
return;
|
|
100
|
+
setAutosave('saving');
|
|
101
|
+
try {
|
|
102
|
+
await onAutosave(payload);
|
|
103
|
+
for (const k of snapshot)
|
|
104
|
+
dirty.current.delete(k);
|
|
105
|
+
setAutosave('saved');
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
setAutosave('error');
|
|
109
|
+
}
|
|
110
|
+
}, AUTOSAVE_MS);
|
|
111
|
+
return () => clearTimeout(id);
|
|
112
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
113
|
+
}, [dirtyTick]);
|
|
114
|
+
const visible = useMemo(() => fields.filter((f) => evaluateVisibleWhen(getVisibleWhen(f), values)), [fields, values]);
|
|
115
|
+
const layout = step.form_layout;
|
|
116
|
+
const groups = useMemo(() => groupFieldsBySection(visible, layout, values), [visible, layout, values]);
|
|
117
|
+
const stepMode = layout?.mode === 'steps' && groups.length > 1;
|
|
118
|
+
const idx = Math.min(stepIdx, Math.max(0, groups.length - 1));
|
|
119
|
+
const shown = stepMode ? [groups[idx]] : groups;
|
|
120
|
+
const labelOf = (f) => f.label ?? f.key.replace(/[_-]+/g, ' ').replace(/\b\w/g, (m) => m.toUpperCase());
|
|
121
|
+
const submit = async () => {
|
|
122
|
+
const errs = {};
|
|
123
|
+
for (const f of visible) {
|
|
124
|
+
if (!f.required || f.type === 'boolean')
|
|
125
|
+
continue;
|
|
126
|
+
if (isWriteOnly(f) && stored.has(f.key))
|
|
127
|
+
continue;
|
|
128
|
+
if (f.default != null && f.default !== '')
|
|
129
|
+
continue;
|
|
130
|
+
if (isEmpty(values[f.key]))
|
|
131
|
+
errs[f.key] = t('form.field_required');
|
|
132
|
+
}
|
|
133
|
+
setErrors(errs);
|
|
134
|
+
const missing = Object.keys(errs);
|
|
135
|
+
if (missing.length > 0) {
|
|
136
|
+
const first = fields.find((f) => f.key === missing[0]);
|
|
137
|
+
if (stepMode && first) {
|
|
138
|
+
const gi = groups.findIndex((g) => g.fields.some((x) => x.key === first.key));
|
|
139
|
+
if (gi >= 0)
|
|
140
|
+
setStepIdx(gi);
|
|
141
|
+
}
|
|
142
|
+
window.setTimeout(() => formRef.current?.querySelector(`[id="${idPrefix}-${missing[0]}"]`)?.focus(), 30);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const keys = new Set([...dirty.current, ...visible.filter((f) => !isWriteOnly(f)).map((f) => f.key)]);
|
|
146
|
+
await onSubmit(payloadFor(keys));
|
|
147
|
+
};
|
|
148
|
+
const missingList = Object.keys(errors).map((k) => labelOf(fields.find((f) => f.key === k))).join(', ');
|
|
149
|
+
return (_jsxs("form", { ref: formRef, className: 'flex flex-col gap-4', noValidate: true, onSubmit: (e) => {
|
|
150
|
+
e.preventDefault();
|
|
151
|
+
void submit();
|
|
152
|
+
}, children: [stepMode ? (_jsx(WizardProgress, { groups: groups, stepIndex: idx, stepLabel: t('form.step'), onStepClick: (i) => setStepIdx(i) })) : null, shown.map((g) => (_jsx(FieldSection, { group: g, children: _jsx("div", { className: 'flex flex-col gap-3', children: g.fields.map((f) => (_jsx(FieldControl, { field: f, idPrefix: idPrefix, label: labelOf(f), value: values[f.key], stored: stored.has(f.key), error: errors[f.key], onChange: (v) => setValue(f.key, v) }, f.key))) }) }, g.key))), missingList ? (_jsx("p", { role: 'alert', className: 'text-sm text-destructive', children: t('form.missing', { fields: missingList }) })) : null, _jsxs("div", { className: 'flex flex-wrap items-center justify-between gap-3', children: [_jsx("p", { role: 'status', "aria-live": 'polite', className: cn('text-xs', autosave === 'error' ? 'text-destructive' : 'text-muted-foreground'), children: autosave === 'saving'
|
|
153
|
+
? t('form.saving')
|
|
154
|
+
: autosave === 'saved'
|
|
155
|
+
? t('form.saved')
|
|
156
|
+
: autosave === 'error'
|
|
157
|
+
? t('form.save_error')
|
|
158
|
+
: '' }), _jsxs("div", { className: 'flex gap-2', children: [stepMode && idx > 0 ? (_jsx(Button, { type: 'button', variant: 'outline', onClick: () => setStepIdx(idx - 1), children: t('form.back') })) : null, stepMode && idx < groups.length - 1 ? (_jsx(Button, { type: 'button', onClick: () => setStepIdx(idx + 1), children: t('form.next') })) : (_jsxs(Button, { type: 'submit', disabled: busy, children: [busy ? _jsx(Loader2, { className: 'mr-2 h-4 w-4 animate-spin motion-reduce:animate-none', "aria-hidden": 'true' }) : null, step.action_label ?? t('form.continue')] }))] })] })] }));
|
|
159
|
+
}
|
|
160
|
+
function FieldControl({ field, idPrefix, label, value, stored, error, onChange, }) {
|
|
161
|
+
const t = useSetupT();
|
|
162
|
+
const id = `${idPrefix}-${field.key}`;
|
|
163
|
+
const secret = isWriteOnly(field);
|
|
164
|
+
const describedBy = [field.description ? `${id}-help` : '', error ? `${id}-error` : '', secret && stored ? `${id}-stored` : '']
|
|
165
|
+
.filter(Boolean)
|
|
166
|
+
.join(' ');
|
|
167
|
+
const common = { id, 'aria-invalid': error ? true : undefined, 'aria-describedby': describedBy || undefined };
|
|
168
|
+
return (_jsxs("div", { className: 'flex flex-col gap-1', children: [_jsxs(Label, { htmlFor: id, children: [label, field.required ? _jsx("span", { className: 'text-destructive', "aria-hidden": 'true', children: " *" }) : null] }), field.type === 'boolean' ? (_jsx(Switch, { id: id, checked: !!value, onCheckedChange: (c) => onChange(c), "aria-describedby": describedBy || undefined })) : field.type === 'select' && field.options?.length ? (_jsxs(Select, { value: String(value ?? ''), onValueChange: (v) => onChange(v), children: [_jsx(SelectTrigger, { ...common, children: _jsx(SelectValue, { placeholder: t('form.choose') }) }), _jsx(SelectContent, { children: field.options.map((o) => (_jsx(SelectItem, { value: o.value, children: o.label }, o.value))) })] })) : field.type === 'textarea' ? (_jsx(Textarea, { ...common, value: String(value ?? ''), placeholder: field.placeholder, onChange: (e) => onChange(e.target.value) })) : (_jsx(Input, { ...common, type: secret ? 'password' : field.type === 'number' ? 'number' : 'text', autoComplete: secret ? 'new-password' : 'off', "data-1p-ignore": secret ? true : undefined, "data-lpignore": secret ? 'true' : undefined, spellCheck: false, placeholder: secret && stored ? '••••••••' : field.placeholder, value: String(value ?? ''), className: cn(error && 'border-destructive'), onChange: (e) => onChange(field.type === 'number'
|
|
169
|
+
? e.target.value === ''
|
|
170
|
+
? undefined
|
|
171
|
+
: Number(e.target.value)
|
|
172
|
+
: e.target.value) })), field.description ? (_jsx("p", { id: `${id}-help`, className: 'text-xs text-muted-foreground', children: field.description })) : null, secret && stored ? (_jsx("p", { id: `${id}-stored`, className: 'text-xs text-muted-foreground', children: t('form.stored') })) : null, error ? (_jsx("p", { id: `${id}-error`, role: 'alert', className: 'text-xs text-destructive', children: error })) : null] }));
|
|
173
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { StepStatus } from './types';
|
|
2
|
+
export interface StepStatusBadgeProps {
|
|
3
|
+
status: StepStatus;
|
|
4
|
+
/** Overrides the localized label. */
|
|
5
|
+
label?: string;
|
|
6
|
+
/** Icon only (the label stays available to screen readers). */
|
|
7
|
+
compact?: boolean;
|
|
8
|
+
className?: string;
|
|
9
|
+
}
|
|
10
|
+
/** Pill with icon + text for a step status; the text is always present for a11y. */
|
|
11
|
+
export declare function StepStatusBadge({ status, label, compact, className }: StepStatusBadgeProps): import("react").JSX.Element;
|
|
12
|
+
//# sourceMappingURL=step-status-badge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"step-status-badge.d.ts","sourceRoot":"","sources":["../../src/setup/step-status-badge.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAWzC,MAAM,WAAW,oBAAoB;IACjC,MAAM,EAAE,UAAU,CAAA;IAClB,qCAAqC;IACrC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,+DAA+D;IAC/D,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,oFAAoF;AACpF,wBAAgB,eAAe,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,oBAAoB,+BAqB1F"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { AlertTriangle, Circle, CheckCircle2, Loader2, Lock, XCircle } from 'lucide-react';
|
|
3
|
+
import { cn } from '@asteby/metacore-ui/lib';
|
|
4
|
+
import { useSetupT } from './messages';
|
|
5
|
+
import { STEP_TONE, TONE_BADGE } from './tone';
|
|
6
|
+
const ICONS = {
|
|
7
|
+
pending: Circle,
|
|
8
|
+
running: Loader2,
|
|
9
|
+
ok: CheckCircle2,
|
|
10
|
+
warn: AlertTriangle,
|
|
11
|
+
error: XCircle,
|
|
12
|
+
blocked: Lock,
|
|
13
|
+
};
|
|
14
|
+
/** Pill with icon + text for a step status; the text is always present for a11y. */
|
|
15
|
+
export function StepStatusBadge({ status, label, compact, className }) {
|
|
16
|
+
const t = useSetupT();
|
|
17
|
+
const known = status in ICONS ? status : 'pending';
|
|
18
|
+
const Icon = ICONS[known];
|
|
19
|
+
const text = label ?? t(`status.${known}`);
|
|
20
|
+
return (_jsxs("span", { "data-status": known, className: cn('inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-xs font-medium', TONE_BADGE[STEP_TONE[known]], className), children: [_jsx(Icon, { "aria-hidden": 'true', className: cn('h-3.5 w-3.5 shrink-0', known === 'running' && 'animate-spin motion-reduce:animate-none') }), _jsx("span", { className: cn(compact && 'sr-only'), children: text })] }));
|
|
21
|
+
}
|