"use client"; import { memo, useCallback } from "react"; import { Eye, Pencil, Trash2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { CopyableId } from "@/components/shared/copyable-id"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { useI18n } from "@/i18n/i18n-context"; import type { Locale } from "@/i18n/config"; import { formatDateTime } from "@/lib/format-datetime"; import type { MemoryUnit } from "@/types/memory"; interface MemoryUnitTableProps { units: MemoryUnit[]; readOnly?: boolean; isActionDisabled?: (unit: MemoryUnit) => boolean; onView?: (unit: MemoryUnit) => void; onEdit?: (unit: MemoryUnit) => void; onDelete?: (unit: MemoryUnit) => void; } function unitId(unit: MemoryUnit): string { return unit.id ?? unit.memoryId ?? "-"; } function formatTimestamp( value: string | number | undefined, locale: Locale = "zh", ): string { return formatDateTime(value, locale, { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", }); } interface MemoryUnitRowProps { unit: MemoryUnit; actionDisabled: boolean; disabledTitle: string; viewTitle: string; editTitle: string; deleteTitle: string; onView?: (unit: MemoryUnit) => void; onEdit?: (unit: MemoryUnit) => void; onDelete?: (unit: MemoryUnit) => void; locale: Locale; } /** * Row component is memoized so that re-rendering the table (e.g. when only * the selected unit changes) skips rows whose `unit` reference is stable. * Action callbacks are wrapped per-row in useCallback to keep referential * equality across renders that don't change the underlying unit. */ const MemoryUnitRow = memo(function MemoryUnitRow({ unit, actionDisabled, disabledTitle, viewTitle, editTitle, deleteTitle, onView, onEdit, onDelete, locale, }: MemoryUnitRowProps) { const handleView = useCallback(() => onView?.(unit), [onView, unit]); const handleEdit = useCallback(() => onEdit?.(unit), [onEdit, unit]); const handleDelete = useCallback(() => onDelete?.(unit), [onDelete, unit]); return (

{unit.text}

{unit.search_text && (

{unit.search_text}

)}
{unit.unit_type ?? "-"} {unit.version ?? "-"} {formatTimestamp(unit.created_at ?? unit.createdAt, locale)}
); }); export function MemoryUnitTable({ units, readOnly = false, isActionDisabled, onView, onEdit, onDelete, }: MemoryUnitTableProps) { const { t, locale } = useI18n(); // `created_at` is the event time of the source message, not the write time — // surface the fallback rules on hover so the column label isn't read as an // insert timestamp. const hintCreatedAt = t("memory.units.tableCreatedAtHint"); if (!units.length) { return (
{t("memory.units.emptyUnits")}
); } return (
{t("memory.units.tableId")} {t("memory.units.tableContent")} {t("memory.units.tableType")} {t("memory.units.tableVersion")} {t("memory.units.tableCreatedAt")} {t("memory.units.tableActions")} {units.map((unit, index) => { const actionDisabled = readOnly || isActionDisabled?.(unit) || false; const id = unitId(unit); return ( ); })}
); } export function getMemoryUnitId(unit: MemoryUnit): string { return unitId(unit); }