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

154 lines
5.5 KiB
TypeScript

// "Neuer API-Schluessel" as a popup on the shared FormDialog chrome, plus the
// one-time reveal that follows a successful create.
//
// The create route used to stack two OpenModal overlays with the boilerplate
// FormGroup/InputText/InputDate controls inside — a different popup shape from
// every other form in the app. Both steps now wear the same header/body/footer
// bar the task, role, group and member popups wear.
//
// Presentational: the route owns the submit and the navigation back to the list.
// The admin API-key routes keep the shared ApiKeyForm for now.
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherCheck, FeatherCopy, FeatherKey } from "@subframe/core";
import { Alert } from "~/ui/components/Alert";
import { Button } from "~/ui/components/Button";
import { Checkbox } from "~/ui/components/Checkbox";
import { TextInput } from "~/ui/components/TextInput";
import { useCopyToast } from "~/hooks/use-copy-toast";
import { DateField } from "../tasks/DateField";
import { FormDialog } from "../shared/FormDialog";
export type ApiKeyNewInput = {
/** Free-text name the list shows next to the redacted key. */
alias: string;
/** ISO date (yyyy-mm-dd) or "" for a key that does not expire. */
expires: string;
active: boolean;
};
export function ApiKeyNewDialog({
open,
onOpenChange,
submitting,
errorMessage,
onCreate,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
submitting: boolean;
/** Server-side rejection (e.g. the alias is already taken). */
errorMessage: string | null;
onCreate: (input: ApiKeyNewInput) => void;
}): React.JSX.Element {
const { t } = useTranslation();
const [alias, setAlias] = useState("");
const [expires, setExpires] = useState("");
const [active, setActive] = useState(true);
const canSubmit = alias.trim().length > 0 && !submitting;
return (
<FormDialog
open={open}
onOpenChange={onOpenChange}
title={t("models.apiKey.newTitle")}
subtitle={t("models.apiKey.newDescription")}
uiId="ui.settings.apiKeys.newDialog"
footer={
<>
<Button type="button" variant="tertiary" onClick={() => onOpenChange(false)} disabled={submitting}>
{t("shared.cancel")}
</Button>
<Button
icon={<FeatherKey />}
loading={submitting}
disabled={!canSubmit}
onClick={() => onCreate({ alias: alias.trim(), expires, active })}
data-ui-id="ui.settings.apiKeys.newDialog.submit"
>
{t("shared.create")}
</Button>
</>
}
>
{errorMessage ? <Alert variant="error" title={errorMessage} /> : null}
<TextInput className="w-full max-w-full" label={t("models.apiKey.alias")} showLabel>
<TextInput.Input
value={alias}
onChange={(event) => setAlias(event.target.value)}
autoFocus
autoComplete="off"
required
data-ui-id="ui.settings.apiKeys.newDialog.alias"
/>
</TextInput>
<div className="flex w-full flex-col gap-1">
<DateField label={t("models.apiKey.expires")} name="expires" value={expires} onChange={setExpires} />
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t("models.apiKey.expiresHint")}</span>
</div>
<Checkbox className="w-fit" label={t("models.apiKey.active")} checked={active} onCheckedChange={(checked: boolean) => setActive(checked)} />
</FormDialog>
);
}
/**
* One-time reveal. The raw key exists only in this response, so the dialog does
* not offer a way to dismiss it without the user having seen the copy button.
*/
export function ApiKeyCreatedDialog({
apiKey,
onClose,
action = "created",
}: {
apiKey: { key: string; alias: string };
onClose: () => void;
/** A rotated key replaces a live one, so it says so. */
action?: "created" | "rotated";
}): React.JSX.Element {
const { t } = useTranslation();
const copyText = useCopyToast();
const [copied, setCopied] = useState(false);
async function copyApiKey() {
const didCopy = await copyText(apiKey.key, { successKey: "models.apiKey.copied", errorKey: "models.apiKey.copyFailed" });
if (didCopy) setCopied(true);
}
return (
<FormDialog
open
onOpenChange={(next: boolean) => {
if (!next) onClose();
}}
title={t(action === "rotated" ? "models.apiKey.rotatedTitle" : "models.apiKey.createdTitle")}
subtitle={t("models.apiKey.aliasWithValue", { alias: apiKey.alias })}
uiId="ui.settings.apiKeys.createdDialog"
footer={
<>
<Button
variant="tertiary"
icon={copied ? <FeatherCheck /> : <FeatherCopy />}
onClick={copyApiKey}
data-ui-id="ui.settings.apiKeys.createdDialog.copy"
>
{copied ? t("shared.copied") : t("shared.copy")}
</Button>
<Button onClick={onClose} data-ui-id="ui.settings.apiKeys.createdDialog.done">
{t("shared.acceptAndContinue")}
</Button>
</>
}
>
<Alert variant="warning" title={t(action === "rotated" ? "models.apiKey.oneTimeDescriptionRotated" : "models.apiKey.oneTimeDescriptionCreated")} />
<TextInput className="w-full max-w-full" label={t("models.apiKey.oneTimeLabel")} showLabel>
<TextInput.Input className="select-all font-mono" value={apiKey.key} readOnly data-ui-id="ui.settings.apiKeys.createdDialog.value" />
</TextInput>
</FormDialog>
);
}