peppa/apps/web/components/AccessRequests.tsx

98 lines
4.9 KiB
TypeScript

import Link from "@/components/Link";
import { KeyRound } from "lucide-react";
import { DEFAULT_GRANT_TERM, GRANT_TERM_TEXT, GRANT_TERMS, type AccessRequestView } from "@peppa/core";
import type { Translator } from "@peppa/shared/i18n";
import { denyAccessRequestAction, grantAccessButtonAction } from "@/app/(app)/actions";
import { Hidden } from "@/components/cards";
import { ago } from "@/components/common";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
const SELECT = "h-9 rounded-md border border-input bg-background px-2 text-sm";
/** «читать» / «читать и менять». */
export function accessModeText(mode: AccessRequestView["mode"], t: Translator): string {
return mode === "write" ? t("читать и менять") : t("только читать");
}
export function AccessStatus({ r, t }: { r: AccessRequestView; t: Translator }) {
if (r.status === "open") return <Badge variant="warn">{t("ждёт вашего решения")}</Badge>;
if (r.status === "granted") return <Badge variant="ok">{t("доступ выдан")}</Badge>;
if (r.status === "denied") return <Badge variant="danger">{t("отказано")}</Badge>;
return <Badge variant="outline">{t("агент отменил запрос")}</Badge>;
}
/** PEP-261: the term of a grant — 1 day by default. */
export function GrantTermSelect({ t }: { t: Translator }) {
return (
<select name="term" defaultValue={DEFAULT_GRANT_TERM} aria-label={t("Срок")} className={SELECT}>
{GRANT_TERMS.map((term) => (
<option key={term} value={term}>
{t.text(GRANT_TERM_TEXT[term])}
</option>
))}
</select>
);
}
/** PEP-261: «чтение» / «полные права» — by default what the agent asked for. */
export function GrantRightsSelect({ r, t }: { r: AccessRequestView; t: Translator }) {
return (
<select name="rights" defaultValue={r.mode === "write" ? "full" : "read"} aria-label={t("Что можно")} className={SELECT}>
<option value="read">{t("Чтение")}</option>
<option value="full">{t("Полные права: чтение и изменения")}</option>
</select>
);
}
/** PEP-250/261: «Агенты просят доступ» on «Подключения» — the owner's open requests with «Дать» buttons. `grantable` — requests with a fitting connection. */
export function AccessRequestList({ list, grantable, t }: { list: AccessRequestView[]; grantable: Set<number>; t: Translator }) {
return (
<ul className="m-0 flex list-none flex-col gap-2 p-0">
{list.map((r) => (
<li key={r.id}>
<Card className="flex flex-wrap items-center gap-x-3 gap-y-1 p-3 text-sm">
<KeyRound className="size-4 text-primary" />
<span className="font-medium">{r.service}</span>
<span className="text-muted-foreground">· {accessModeText(r.mode, t)}</span>
<span className="text-muted-foreground">
· {r.agentName ? t("агент {name}", { name: r.agentName }) : t("агент")} · {r.taskKey ?? r.projectKey}
</span>
<span className="basis-full truncate text-muted-foreground">{r.purpose}</span>
<span className="text-xs text-muted-foreground">{ago(r.updatedAt, t)}</span>
<Link href={`/connections/requests/${r.id}`} className="ml-auto text-primary underline-offset-2 hover:underline">
{t("Посмотреть и решить")}
</Link>
<div className="flex basis-full flex-wrap items-center gap-2 pt-1">
{grantable.has(r.id) ? (
<form action={grantAccessButtonAction} className="flex flex-wrap items-center gap-2">
<Hidden fields={{ back: "/connections#requests", requestId: r.id }} />
<Button size="sm" name="rights" value="read" data-access-grant="read">
{t("Дать: чтение")}
</Button>
<Button size="sm" variant="secondary" name="rights" value="full" data-access-grant="full">
{t("Дать: полные права")}
</Button>
<span className="text-muted-foreground">{t("на срок")}</span>
<GrantTermSelect t={t} />
</form>
) : (
<Button asChild size="sm">
<Link href={`/connections/requests/${r.id}`}>{t("Подключить")}</Link>
</Button>
)}
<form action={denyAccessRequestAction}>
<Hidden fields={{ back: "/connections#requests", requestId: r.id }} />
<Button size="sm" variant="outline">
{t("Отказать")}
</Button>
</form>
</div>
</Card>
</li>
))}
</ul>
);
}