"use client"; import { BrainIcon, ShieldAlertIcon, Trash2Icon } from "lucide-react"; import { useState, type FormEvent } from "react"; import { toast } from "sonner"; import { EmptyState, ErrorState, PageHeader } from "@/components/shell/page"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Skeleton } from "@/components/ui/skeleton"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { api, ApiError } from "@/lib/api"; import { useAuth } from "@/lib/auth"; import { formatDateTime } from "@/lib/format"; import { useI18n } from "@/lib/i18n"; import { useApi } from "@/lib/use-api"; import { cn } from "@/lib/utils"; import type { MemoryItem, MemoryKind } from "./types"; const KINDS: (MemoryKind | "all")[] = ["all", "customer", "prefs", "case"]; function summarize(value: Record | null): string { if (!value) return ""; return Object.entries(value) .map(([k, v]) => `${k}: ${typeof v === "string" ? v : JSON.stringify(v)}`) .join(" ยท "); } function ForgetCustomer({ onDone }: { onDone: () => void }) { const { t } = useI18n(); const [id, setId] = useState(""); const submit = async (e: FormEvent) => { e.preventDefault(); if (!id.trim()) return; try { const r = await api<{ deleted: number }>(`/memory/customers/${encodeURIComponent(id.trim())}`, { method: "DELETE", }); toast.success(t("memory.forgetCustomerDone", { n: r.deleted })); setId(""); onDone(); } catch (err) { toast.error(err instanceof ApiError ? err.detail : t("common.error")); } }; return (
setId(e.target.value)} className="w-64" />
); } export function MemoryView() { const { t, m, locale } = useI18n(); const { hasRole, me } = useAuth(); const [kind, setKind] = useState("all"); const allowed = hasRole("manager"); const items = useApi(allowed ? "/memory/items" : null, { query: kind === "all" ? {} : { kind }, }); const forget = async (item: MemoryItem) => { try { await api(`/memory/items/${item.id}`, { method: "DELETE" }); toast.success(t("memory.forgotten")); items.reload(); } catch (err) { toast.error(err instanceof ApiError ? err.detail : t("common.error")); } }; let body; if (me && !allowed) { body = ; } else if (items.error && !items.data) { body = ; } else if (!items.data) { body = ; } else if (items.data.length === 0) { body = ; } else { body = (
{t("memory.namespace")} {t("memory.key")} {t("memory.value")} {t("memory.expires")} {items.data.map((it) => ( {it.kind === "other" ? it.namespace.slice(2).join(" / ") : m.memory.kinds[it.kind]} {it.namespace.length > 3 && ( {it.namespace.slice(3).join("/")} )} {it.key} {it.integrity_ok ? ( summarize(it.value) ) : ( {t("memory.tampered")} )} {formatDateTime(it.expires_at, locale)} ))}
); } return (
{allowed && (
{KINDS.map((k) => ( ))}
)} {body}
); }