"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}