Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/integrations/ConnectionDetailPage.tsx
T

220 lines
9.4 KiB
TypeScript

// R-P5 Interim-UI (AD-28) — per-app connection management screen composed from
// ~/ui (D-P5 "App-Detail / Connection-Verwaltung", archetype A6+A2). Pure
// presentational: renders the server-built ConnectionDetailVM and reports the
// lifecycle mutations (connect / reconnect / disconnect / manual re-sync)
// through injected actions — the route owns the fetcher. Degraded/broken
// connections raise a screen banner with a reconnect solution button (D-P5
// Klartext requirement). Final Subframe optics = tracked design-nachzug.
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherDownloadCloud, FeatherPlug, FeatherPlus } from "@subframe/core";
import type { AppCredentialField } from "@hivegrid/integrations";
import type { ConnectionDetailVM, ConnectionRow } from "@hivegrid/view-models";
import { Alert } from "~/ui/components/Alert";
import { Button } from "~/ui/components/Button";
import { Dialog } from "~/ui/components/Dialog";
import { EmptyState } from "~/ui/components/EmptyState";
import { PageBody, PageIntro, PageShell } from "../shared/PageShell";
import { ConnectionCard } from "./ConnectionCard";
import { ConnectWizardDialog, type ConnectWizardMode, type ConnectWizardSubmit } from "./ConnectWizardDialog";
export type ConnectionDetailActions = {
/** POSTs intent=connect to the app route action. */
onConnect: (input: ConnectWizardSubmit) => void;
/** POSTs intent=reconnect (same connection id, no history loss). */
onReconnect: (connectionId: string, input: ConnectWizardSubmit) => void;
/** POSTs intent=disconnect (status revoked, no hard delete). */
onDisconnect: (connectionId: string) => void;
/** POSTs intent=manual-sync. */
onManualSync: (connectionId: string) => void;
};
export type ConnectionDetailPageProps = {
vm: ConnectionDetailVM;
connectionType: string;
credentialFields: readonly AppCredentialField[];
actions: ConnectionDetailActions;
submitting: boolean;
errorMessage: string | null;
feedback?: {
readonly variant: "success" | "error";
readonly titleKey: string;
readonly descriptionKey: string;
} | null;
/** Increments after a successful mutation — closes any open dialog. */
closeSignal: number;
};
type WizardState = ConnectWizardMode | null;
export function ConnectionDetailPage({ vm, connectionType, credentialFields, actions, submitting, errorMessage, feedback, closeSignal }: ConnectionDetailPageProps) {
const { t } = useTranslation();
const [wizard, setWizard] = useState<WizardState>(null);
const [disconnectRow, setDisconnectRow] = useState<ConnectionRow | null>(null);
useEffect(() => {
if (closeSignal > 0) {
setWizard(null);
setDisconnectRow(null);
}
}, [closeSignal]);
if (vm.status !== "ready" || !vm.data) {
return <PageShell uiId="ui.integrations.app" />;
}
const { appKey, appTitleKey, rows, canConnect, canDisconnect, canManualSync, connectHref, projectImportHref, emptyHeadlineKey, emptyBodyKey } = vm.data;
const unhealthy = rows.filter((row) => row.status === "degraded" || row.status === "broken");
const openConnect = (mode: ConnectWizardMode) => {
if (appKey === "kwp") {
window.location.assign(connectHref);
return;
}
setWizard(mode);
};
return (
<PageShell uiId="ui.integrations.app">
<PageBody>
{/* Embedded in the Erweiterungen hub (AK-SET-01): the hub owns the page
<h1> and tab bar, so this screen carries a simple section heading. */}
<div className="flex w-full items-start justify-between gap-4 mobile:flex-col">
<div className="flex min-w-0 flex-col gap-1">
<div className="flex items-center gap-2">
<FeatherPlug className="text-heading-3 font-heading-3 text-icon-neutral-tertiary" />
<span className="text-heading-3 font-heading-3 text-text-neutral-primary" data-ui-id="ui.integrations.app.title">
{t(appTitleKey)}
</span>
</div>
<PageIntro uiId="ui.integrations.app.intro">{t("integrations.connections.subtitle")}</PageIntro>
</div>
{/* The empty state below carries its own connect button — only show the
header action once at least one connection exists. */}
{canConnect && rows.length > 0 ? (
<Button icon={<FeatherPlus />} onClick={() => openConnect({ type: "connect" })} data-ui-id="ui.integrations.app.connect">
{t("integrations.connections.add")}
</Button>
) : null}
</div>
{feedback ? (
<Alert
variant={feedback.variant}
title={t(feedback.titleKey)}
description={t(feedback.descriptionKey)}
data-ui-id="ui.integrations.kwp.oauth.feedback"
/>
) : null}
{projectImportHref ? (
<Alert
variant="information"
title={t("integrations.kwp.projectImport.entry.title")}
description={t("integrations.kwp.projectImport.entry.body")}
actions={
<Button
variant="tertiary"
size="small"
icon={<FeatherDownloadCloud />}
onClick={() => window.location.assign(projectImportHref)}
data-ui-id="ui.integrations.kwp.project-import.open"
>
{t("integrations.kwp.projectImport.entry.action")}
</Button>
}
/>
) : null}
{/* Screen banner (hierarchy §8): at least one degraded/broken connection. */}
{unhealthy.length > 0 ? (
<Alert
variant={unhealthy.some((row) => row.status === "broken") ? "error" : "warning"}
title={t("integrations.connections.banner.title", { count: unhealthy.length })}
description={unhealthy[0].healthMessageKey ? t(unhealthy[0].healthMessageKey) : undefined}
actions={
canConnect ? (
<Button variant="secondary" size="small" onClick={() => openConnect({ type: "reconnect", connectionId: unhealthy[0].id, label: unhealthy[0].label })}>
{t("integrations.connections.actions.reconnect")}
</Button>
) : undefined
}
data-ui-id="ui.integrations.app.banner"
/>
) : null}
{rows.length === 0 ? (
<div className="flex w-full flex-col items-center gap-4" data-ui-id="ui.integrations.app.empty">
<EmptyState title={t(emptyHeadlineKey)} body={t(emptyBodyKey)} />
{canConnect ? (
<Button icon={<FeatherPlus />} onClick={() => openConnect({ type: "connect" })}>
{t("integrations.connections.add")}
</Button>
) : null}
</div>
) : (
<div className="grid w-full grid-cols-1 gap-4 lg:grid-cols-2" data-ui-id="ui.integrations.app.list">
{rows.map((row) => (
<ConnectionCard
key={row.id}
row={row}
canDisconnect={canDisconnect}
canManualSync={canManualSync}
canReconnect={canConnect}
submitting={submitting}
onManualSync={actions.onManualSync}
onReconnect={(target) => openConnect({ type: "reconnect", connectionId: target.id, label: target.label })}
onDisconnect={setDisconnectRow}
/>
))}
</div>
)}
{/* Connect/Reconnect wizard (A5, M 720) */}
{wizard ? (
<ConnectWizardDialog
open
mode={wizard}
appTitleKey={appTitleKey}
connectionType={connectionType}
credentialFields={credentialFields}
submitting={submitting}
errorMessage={errorMessage}
onSubmit={(input) => {
if (wizard.type === "reconnect") actions.onReconnect(wizard.connectionId, input);
else actions.onConnect(input);
}}
onClose={() => setWizard(null)}
/>
) : null}
{/* Disconnect confirm (XS 420, Klartext: sync jobs stop, history preserved) */}
<Dialog open={disconnectRow !== null} onOpenChange={(open: boolean) => (open ? undefined : setDisconnectRow(null))}>
<Dialog.Content className="w-[420px] max-w-[90vw] p-4">
{disconnectRow ? (
<div className="flex w-full flex-col gap-6" data-ui-id="ui.integrations.app.disconnectConfirm">
<div className="flex flex-col gap-2">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("integrations.disconnect.title", { label: disconnectRow.label })}</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("integrations.disconnect.description")}
</span>
{errorMessage ? <Alert variant="error" title={t(errorMessage)} /> : null}
</div>
<div className="flex w-full items-center justify-end gap-2">
<Button variant="tertiary" onClick={() => setDisconnectRow(null)}>
{t("shared.cancel")}
</Button>
<Button variant="destructive" loading={submitting} onClick={() => actions.onDisconnect(disconnectRow.id)} data-ui-id="ui.integrations.app.disconnectSubmit">
{t("integrations.connections.actions.disconnect")}
</Button>
</div>
</div>
) : (
<div />
)}
</Dialog.Content>
</Dialog>
</PageBody>
</PageShell>
);
}