319 lines
14 KiB
TypeScript
319 lines
14 KiB
TypeScript
// AK-P17a Interim-UI (AD-28) — vault detail composed from ~/ui (D-HR
|
|
// "Passwort-Safe Detail", archetype A3 + reveal). SECURITY-CRITICAL: secret
|
|
// values are NEVER in the list — each entry shows only "hasX" flags. A value is
|
|
// fetched ONLY on an explicit, audited reveal (route action) and, once copied, the
|
|
// clipboard is wiped after copyAutoClearMs (30s, DoD #3). Create-entry / grant-
|
|
// access run through injected actions. Final Subframe optics = design-nachzug.
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherPlus } from "@subframe/core";
|
|
import type { EntryRow, VaultDetailVM } from "@hivegrid/view-models";
|
|
import { Breadcrumbs } from "~/ui/components/Breadcrumbs";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { CopyToClipboardButton } from "~/ui/components/CopyToClipboardButton";
|
|
import { Dialog } from "~/ui/components/Dialog";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { LinkButton } from "~/ui/components/LinkButton";
|
|
import { Select } from "~/ui/components/Select";
|
|
import { Tag_Label } from "~/ui/components/Tag_Label";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
|
|
export type VaultSecretField = "username" | "password" | "notes";
|
|
export type VaultAccessLevel = "read" | "write" | "admin";
|
|
|
|
export type HrVaultDetailActions = {
|
|
/** Reveal ONE field of ONE entry (audited server-side); resolves the plaintext. */
|
|
onReveal: (entryId: string, field: VaultSecretField) => Promise<string | null>;
|
|
onCreateEntry: (input: { title: string; url: string; username: string; password: string; notes: string }) => void;
|
|
onArchiveEntry: (entryId: string) => void;
|
|
onGrantAccess: (input: { targetUserId: string; permissionLevel: VaultAccessLevel }) => void;
|
|
};
|
|
|
|
export type HrVaultDetailPageProps = {
|
|
vm: VaultDetailVM;
|
|
backHref: string;
|
|
submitting: boolean;
|
|
actions: HrVaultDetailActions;
|
|
};
|
|
|
|
/** One revealable secret field: reveal on demand, copy, wipe clipboard after the
|
|
* VM-supplied window (30s). The value is held only in local component state. */
|
|
function SecretField({
|
|
entryId,
|
|
field,
|
|
labelKey,
|
|
autoClearMs,
|
|
onReveal,
|
|
}: {
|
|
entryId: string;
|
|
field: VaultSecretField;
|
|
labelKey: string;
|
|
autoClearMs: number;
|
|
onReveal: HrVaultDetailActions["onReveal"];
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const [value, setValue] = useState<string | null>(null);
|
|
const [loading, setLoading] = useState(false);
|
|
const [copied, setCopied] = useState(false);
|
|
const clearTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
useEffect(() => () => {
|
|
if (clearTimer.current) clearTimeout(clearTimer.current);
|
|
}, []);
|
|
|
|
async function reveal() {
|
|
setLoading(true);
|
|
try {
|
|
setValue(await onReveal(entryId, field));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
function scheduleClipboardWipe() {
|
|
setCopied(true);
|
|
if (clearTimer.current) clearTimeout(clearTimer.current);
|
|
clearTimer.current = setTimeout(() => {
|
|
// Wipe the clipboard so a copied secret does not linger (DoD #3).
|
|
void navigator.clipboard?.writeText("").catch(() => {});
|
|
setCopied(false);
|
|
}, autoClearMs);
|
|
}
|
|
|
|
return (
|
|
<div className="flex items-center justify-between gap-3" data-ui-id={`ui.hr.vault.field.${field}`}>
|
|
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">{t(labelKey)}</span>
|
|
{value === null ? (
|
|
<Button variant="tertiary" size="small" loading={loading} onClick={reveal} data-ui-id="ui.hr.vault.reveal">
|
|
{t("hr.passwordSafe.detail.reveal")}
|
|
</Button>
|
|
) : (
|
|
<div className="flex items-center gap-2">
|
|
<span className="font-mono text-body-default-regular-0 text-text-neutral-primary" data-ui-id="ui.hr.vault.revealed">
|
|
{value}
|
|
</span>
|
|
<CopyToClipboardButton
|
|
clipboardText={value}
|
|
tooltipText={t("hr.passwordSafe.detail.copy")}
|
|
onCopy={scheduleClipboardWipe}
|
|
/>
|
|
<Button variant="tertiary" size="small" onClick={() => setValue(null)}>
|
|
{t("hr.passwordSafe.detail.hide")}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
{copied ? (
|
|
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-warning" data-ui-id="ui.hr.vault.copyHint">
|
|
{t("hr.passwordSafe.detail.copyCleared")}
|
|
</span>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function EntryCard({
|
|
entry,
|
|
autoClearMs,
|
|
canWrite,
|
|
submitting,
|
|
onReveal,
|
|
onArchive,
|
|
}: {
|
|
entry: EntryRow;
|
|
autoClearMs: number;
|
|
canWrite: boolean;
|
|
submitting: boolean;
|
|
onReveal: HrVaultDetailActions["onReveal"];
|
|
onArchive: (entryId: string) => void;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<Card data-row-id={entry.id}>
|
|
<div className="flex w-full items-start justify-between gap-4">
|
|
<div className="flex flex-col gap-1">
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{entry.title}</span>
|
|
{entry.url ? (
|
|
<a href={entry.url} target="_blank" rel="noreferrer" className="text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-link-default">
|
|
{entry.url}
|
|
</a>
|
|
) : null}
|
|
{entry.tags.length > 0 ? (
|
|
<div className="flex flex-wrap items-center gap-1">
|
|
{entry.tags.map((tag) => (
|
|
<Tag_Label key={tag} tone="neutral-subtle" size="small" noAction>
|
|
{tag}
|
|
</Tag_Label>
|
|
))}
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
{canWrite ? (
|
|
<Button variant="tertiary" size="small" loading={submitting} onClick={() => onArchive(entry.id)} data-ui-id="ui.hr.vault.archiveEntry">
|
|
{t("hr.passwordSafe.detail.archiveEntry")}
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
<div className="flex w-full flex-col gap-2">
|
|
{entry.hasUsername ? (
|
|
<SecretField entryId={entry.id} field="username" labelKey="hr.passwordSafe.field.username" autoClearMs={autoClearMs} onReveal={onReveal} />
|
|
) : null}
|
|
{entry.hasPassword ? (
|
|
<SecretField entryId={entry.id} field="password" labelKey="hr.passwordSafe.field.password" autoClearMs={autoClearMs} onReveal={onReveal} />
|
|
) : null}
|
|
{entry.hasNotes ? (
|
|
<SecretField entryId={entry.id} field="notes" labelKey="hr.passwordSafe.field.notes" autoClearMs={autoClearMs} onReveal={onReveal} />
|
|
) : null}
|
|
</div>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
export function HrVaultDetailPage({ vm, backHref, submitting, actions }: HrVaultDetailPageProps) {
|
|
const { t } = useTranslation();
|
|
const [entryOpen, setEntryOpen] = useState(false);
|
|
const [accessOpen, setAccessOpen] = useState(false);
|
|
const [entry, setEntry] = useState({ title: "", url: "", username: "", password: "", notes: "" });
|
|
const [access, setAccess] = useState<{ targetUserId: string; permissionLevel: VaultAccessLevel }>({ targetUserId: "", permissionLevel: "read" });
|
|
|
|
if (vm.status !== "ready" || !vm.data) {
|
|
return <div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.hr.vault" />;
|
|
}
|
|
|
|
const { vaultName, rows, canWrite, canManageAccess, copyAutoClearMs, exportHref, emptyHeadlineKey, emptyBodyKey } = vm.data;
|
|
|
|
function submitEntry() {
|
|
actions.onCreateEntry(entry);
|
|
setEntryOpen(false);
|
|
setEntry({ title: "", url: "", username: "", password: "", notes: "" });
|
|
}
|
|
function submitAccess() {
|
|
actions.onGrantAccess(access);
|
|
setAccessOpen(false);
|
|
setAccess({ targetUserId: "", permissionLevel: "read" });
|
|
}
|
|
|
|
return (
|
|
<div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.hr.vault">
|
|
<Breadcrumbs>
|
|
<a href={backHref} className="contents no-underline" data-ui-id="ui.hr.vault.back">
|
|
<Breadcrumbs.Item>{t("hr.passwordSafe.detail.back")}</Breadcrumbs.Item>
|
|
</a>
|
|
<Breadcrumbs.Divider />
|
|
<Breadcrumbs.Item active>{vaultName}</Breadcrumbs.Item>
|
|
</Breadcrumbs>
|
|
|
|
<div className="flex flex-wrap items-start justify-between gap-4">
|
|
<div className="flex flex-col gap-0">
|
|
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{vaultName}</span>
|
|
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
|
|
{t("hr.passwordSafe.detail.subtitle")}
|
|
</span>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<a href={exportHref} className="contents no-underline" data-ui-id="ui.hr.vault.export">
|
|
<LinkButton variant="subtle">{t("hr.passwordSafe.detail.export")}</LinkButton>
|
|
</a>
|
|
{canManageAccess ? (
|
|
<Button variant="secondary" onClick={() => setAccessOpen(true)} data-ui-id="ui.hr.vault.manageAccess">
|
|
{t("hr.passwordSafe.detail.manageAccess")}
|
|
</Button>
|
|
) : null}
|
|
{canWrite ? (
|
|
<Button icon={<FeatherPlus />} onClick={() => setEntryOpen(true)} data-ui-id="ui.hr.vault.newEntry">
|
|
{t("hr.passwordSafe.detail.newEntry")}
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
|
|
{rows.length === 0 ? (
|
|
<div data-ui-id="ui.hr.vault.empty">
|
|
<EmptyState title={t(emptyHeadlineKey)} body={t(emptyBodyKey)} />
|
|
</div>
|
|
) : (
|
|
<div className="flex w-full flex-col gap-4" data-ui-id="ui.hr.vault.entries">
|
|
{rows.map((row) => (
|
|
<EntryCard
|
|
key={row.id}
|
|
entry={row}
|
|
autoClearMs={copyAutoClearMs}
|
|
canWrite={canWrite}
|
|
submitting={submitting}
|
|
onReveal={actions.onReveal}
|
|
onArchive={actions.onArchiveEntry}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{/* New entry dialog (canWrite) */}
|
|
<Dialog open={entryOpen} onOpenChange={setEntryOpen}>
|
|
<Dialog.Content className="w-full max-w-[560px] p-4" data-ui-id="ui.hr.vault.createEntry">
|
|
<div className="flex w-full flex-col gap-6 p-2">
|
|
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("hr.passwordSafe.detail.newEntry")}</span>
|
|
<div className="flex w-full flex-col gap-4">
|
|
<TextInput label={t("hr.passwordSafe.field.entryTitle")} showLabel fillContainer>
|
|
<TextInput.Input value={entry.title} onChange={(e) => setEntry((s) => ({ ...s, title: e.target.value }))} data-ui-id="ui.hr.vault.createEntry.title" />
|
|
</TextInput>
|
|
<TextInput label={t("hr.passwordSafe.field.url")} showLabel fillContainer>
|
|
<TextInput.Input value={entry.url} onChange={(e) => setEntry((s) => ({ ...s, url: e.target.value }))} />
|
|
</TextInput>
|
|
<TextInput label={t("hr.passwordSafe.field.username")} showLabel fillContainer>
|
|
<TextInput.Input value={entry.username} onChange={(e) => setEntry((s) => ({ ...s, username: e.target.value }))} />
|
|
</TextInput>
|
|
<TextInput label={t("hr.passwordSafe.field.password")} showLabel fillContainer type="password">
|
|
<TextInput.Input type="password" value={entry.password} onChange={(e) => setEntry((s) => ({ ...s, password: e.target.value }))} />
|
|
</TextInput>
|
|
<TextInput label={t("hr.passwordSafe.field.notes")} showLabel fillContainer>
|
|
<TextInput.Input value={entry.notes} onChange={(e) => setEntry((s) => ({ ...s, notes: e.target.value }))} />
|
|
</TextInput>
|
|
</div>
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="neutral" onClick={() => setEntryOpen(false)}>
|
|
{t("hr.action.cancel")}
|
|
</Button>
|
|
<Button loading={submitting} disabled={entry.title.trim().length === 0} onClick={submitEntry} data-ui-id="ui.hr.vault.createEntry.submit">
|
|
{t("hr.passwordSafe.detail.createEntry.submit")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
|
|
{/* Grant access dialog (canManageAccess) */}
|
|
<Dialog open={accessOpen} onOpenChange={setAccessOpen}>
|
|
<Dialog.Content className="w-full max-w-[560px] p-4" data-ui-id="ui.hr.vault.grantAccess">
|
|
<div className="flex w-full flex-col gap-6 p-2">
|
|
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("hr.passwordSafe.detail.manageAccess")}</span>
|
|
<div className="flex w-full flex-col gap-4">
|
|
<TextInput label={t("hr.passwordSafe.field.targetUser")} showLabel fillContainer>
|
|
<TextInput.Input value={access.targetUserId} onChange={(e) => setAccess((s) => ({ ...s, targetUserId: e.target.value }))} data-ui-id="ui.hr.vault.grantAccess.user" />
|
|
</TextInput>
|
|
<Select
|
|
label={t("hr.passwordSafe.field.permissionLevel")}
|
|
showLabel
|
|
value={access.permissionLevel}
|
|
onValueChange={(value: string) => setAccess((s) => ({ ...s, permissionLevel: value as VaultAccessLevel }))}
|
|
>
|
|
<Select.Item value="read">{t("hr.passwordSafe.access.read")}</Select.Item>
|
|
<Select.Item value="write">{t("hr.passwordSafe.access.write")}</Select.Item>
|
|
<Select.Item value="admin">{t("hr.passwordSafe.access.admin")}</Select.Item>
|
|
</Select>
|
|
</div>
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="neutral" onClick={() => setAccessOpen(false)}>
|
|
{t("hr.action.cancel")}
|
|
</Button>
|
|
<Button loading={submitting} disabled={access.targetUserId.trim().length === 0} onClick={submitAccess} data-ui-id="ui.hr.vault.grantAccess.submit">
|
|
{t("hr.passwordSafe.detail.grantAccess.submit")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
}
|