Files
2026-07-08 16:15:37 +02:00

202 lines
9.3 KiB
TypeScript

// R-P5 Interim-UI (AD-28) — connect/reconnect wizard as a REAL ~/ui Dialog
// (D-P5 "Connection-Setup-Wizard", archetype A5+A6, size M 720) with a
// StepperHorizontal: prerequisites → auto-generated credential form (from the
// app's connectionSchema, secret fields with reveal toggle) → review & connect.
// Submit posts the existing connect/reconnect route action (adapter validation +
// health check server-side). OAuth2 apps without credential fields show an
// interim hint (the guided OAuth redirect ships with the module phases P14/P16).
// Final Subframe optics land as a tracked design-nachzug once David's D-P5 ships.
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherEye, FeatherEyeOff } from "@subframe/core";
import type { AppCredentialField } from "@hivegrid/integrations";
import { Alert } from "~/ui/components/Alert";
import { Button } from "~/ui/components/Button";
import { Dialog } from "~/ui/components/Dialog";
import { IconButton } from "~/ui/components/IconButton";
import { StepperHorizontal } from "~/ui/components/StepperHorizontal";
import { TextInput } from "~/ui/components/TextInput";
export type ConnectWizardMode = { type: "connect" } | { type: "reconnect"; connectionId: string; label: string };
export type ConnectWizardSubmit = { label: string; credentials: Record<string, string> };
export type ConnectWizardDialogProps = {
open: boolean;
mode: ConnectWizardMode;
appTitleKey: string;
connectionType: string;
credentialFields: readonly AppCredentialField[];
submitting: boolean;
errorMessage: string | null;
onSubmit: (input: ConnectWizardSubmit) => void;
onClose: () => void;
};
const STEPS = ["prerequisites", "credentials", "confirm"] as const;
export function ConnectWizardDialog({ open, mode, appTitleKey, connectionType, credentialFields, submitting, errorMessage, onSubmit, onClose }: ConnectWizardDialogProps) {
const { t } = useTranslation();
const [step, setStep] = useState(0);
const [label, setLabel] = useState(mode.type === "reconnect" ? mode.label : "");
const [values, setValues] = useState<Record<string, string>>({});
const [revealed, setRevealed] = useState<ReadonlySet<string>>(new Set());
// Reset the wizard whenever it (re)opens or the target changes (the parent
// passes a fresh `mode` object per open, so state identity stays stable
// across mere re-renders like submitting toggles).
useEffect(() => {
if (open) {
setStep(0);
setLabel(mode.type === "reconnect" ? mode.label : "");
setValues({});
setRevealed(new Set());
}
}, [open, mode]);
const isOauthWithoutForm = connectionType === "oauth2" && credentialFields.length === 0;
const labelMissing = mode.type === "connect" && label.trim().length === 0;
const requiredMissing = credentialFields.some((field) => field.required && !(values[field.name] ?? "").trim());
const credentialsIncomplete = labelMissing || requiredMissing;
function toggleReveal(name: string) {
setRevealed((prev) => {
const next = new Set(prev);
if (next.has(name)) next.delete(name);
else next.add(name);
return next;
});
}
function submit() {
onSubmit({ label: label.trim(), credentials: values });
}
return (
<Dialog open={open} onOpenChange={(nextOpen: boolean) => (nextOpen ? undefined : onClose())}>
<Dialog.Content className="w-[720px] max-w-[90vw] p-6">
<div className="flex w-full flex-col gap-6" data-ui-id="ui.integrations.app.wizard">
<div className="flex flex-col gap-2">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">
{mode.type === "reconnect" ? t("integrations.wizard.reconnectTitle", { app: t(appTitleKey) }) : t("integrations.wizard.title", { app: t(appTitleKey) })}
</span>
{errorMessage ? <Alert variant="error" title={t(errorMessage)} /> : null}
</div>
<StepperHorizontal>
{STEPS.map((key, index) => (
<StepperHorizontal.Step
key={key}
firstStep={index === 0}
lastStep={index === STEPS.length - 1}
stepNumber={String(index + 1)}
label={t(`integrations.wizard.step.${key}`)}
variant={index === step ? "active" : index < step ? "completed" : "default"}
/>
))}
</StepperHorizontal>
{step === 0 ? (
<div className="flex w-full flex-col gap-2" data-wizard-step="prerequisites">
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("integrations.wizard.prerequisites.body", { app: t(appTitleKey) })}
</span>
{isOauthWithoutForm ? (
<Alert variant="information" title={t("integrations.wizard.oauthHint")} />
) : null}
</div>
) : null}
{step === 1 ? (
<div className="flex w-full flex-col gap-4" data-wizard-step="credentials">
{mode.type === "connect" ? (
<TextInput className="w-full" label={t("integrations.wizard.label")} showLabel helpText={t("integrations.wizard.labelHelp")}>
<TextInput.Input value={label} onChange={(event) => setLabel(event.target.value)} required data-ui-id="ui.integrations.app.wizard.label" />
</TextInput>
) : (
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("integrations.wizard.reconnectHint", { label: mode.label })}
</span>
)}
{credentialFields.map((field) => {
const isSecret = field.type === "secret";
const shown = revealed.has(field.name);
return (
<TextInput
key={field.name}
className="w-full"
label={t(field.labelKey)}
showLabel
type={isSecret && !shown ? "password" : "text"}
iconRight={
isSecret ? (
<IconButton
size="small"
icon={shown ? <FeatherEyeOff /> : <FeatherEye />}
onClick={() => toggleReveal(field.name)}
aria-label={t(shown ? "integrations.wizard.hideSecret" : "integrations.wizard.revealSecret")}
/>
) : undefined
}
>
<TextInput.Input
type={isSecret && !shown ? "password" : "text"}
value={values[field.name] ?? ""}
onChange={(event) => setValues((prev) => ({ ...prev, [field.name]: event.target.value }))}
required={field.required}
data-wizard-field={field.name}
/>
</TextInput>
);
})}
{credentialFields.length === 0 && !isOauthWithoutForm ? (
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("integrations.wizard.noFields")}
</span>
) : null}
{isOauthWithoutForm ? <Alert variant="information" title={t("integrations.wizard.oauthHint")} /> : null}
</div>
) : null}
{step === 2 ? (
<div className="flex w-full flex-col gap-2" data-wizard-step="confirm">
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("integrations.wizard.confirm.body")}
</span>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">
{t("integrations.wizard.label")}: {mode.type === "reconnect" ? mode.label : label || "—"}
</span>
{credentialFields.map((field) => (
<span key={field.name} className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">
{t(field.labelKey)}: {(values[field.name] ?? "").trim() ? (field.type === "secret" ? "••••••" : values[field.name]) : "—"}
</span>
))}
</div>
) : null}
<div className="flex w-full items-center justify-end gap-2">
<Button variant="tertiary" onClick={onClose}>
{t("shared.cancel")}
</Button>
{step > 0 ? (
<Button variant="secondary" onClick={() => setStep(step - 1)}>
{t("shared.back")}
</Button>
) : null}
{step < STEPS.length - 1 ? (
<Button disabled={step === 1 && credentialsIncomplete} onClick={() => setStep(step + 1)} data-ui-id="ui.integrations.app.wizard.next">
{t("shared.next")}
</Button>
) : (
<Button loading={submitting} disabled={credentialsIncomplete} onClick={submit} data-ui-id="ui.integrations.app.wizard.submit">
{t("integrations.wizard.connect")}
</Button>
)}
</div>
</div>
</Dialog.Content>
</Dialog>
);
}