"use client"; /** * ส่วนลากเรียงแถวของ DataTable (dnd-kit) — แยกไฟล์และโหลดแบบ lazy เฉพาะเมื่อส่ง `onReorder` * เดิม import ที่ data-table.tsx ทำให้ทุกตารางพก dnd-kit (~16 kB gzip) แม้แทบไม่มีตารางไหนลากเรียง */ import { closestCenter, DndContext, type DragEndEvent, KeyboardSensor, PointerSensor, useSensor, useSensors, } from "@dnd-kit/core"; import { arrayMove, SortableContext, sortableKeyboardCoordinates, useSortable, verticalListSortingStrategy, } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { TableCell, TableRow } from "@suphark/ui/components/ui/table"; import { cn } from "@suphark/ui/lib/utils"; import type { Row, Table } from "@tanstack/react-table"; import type { CSSProperties, ReactNode } from "react"; import { toast } from "sonner"; import { DataTableRowError, ROW_ERROR_CLASS, renderCellContent, rowErrorCellId, } from "./data-table-cell-content"; interface RowStyleProps { enableColumnResizing?: boolean; getRowClassName?: (row: Row) => string; stickyEdgeColumns?: boolean; getRowError?: (row: Row) => ReactNode; onRowRetry?: (row: Row) => void; } function DraggableRow({ row, enableColumnResizing, getRowClassName, stickyEdgeColumns, getRowError, onRowRetry, }: RowStyleProps & { row: Row }) { const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: row.id, }); const style: CSSProperties = { transform: CSS.Transform.toString(transform), transition, opacity: isDragging ? 0.8 : 1, zIndex: isDragging ? 1 : 0, position: "relative", }; const rowError = getRowError?.(row); const errorCell = rowError ? rowErrorCellId(row) : undefined; return ( {row.getVisibleCells().map((cell) => { const isDndColumn = cell.column.id === "dnd-handle"; return ( {renderCellContent(cell)} {cell.id === errorCell && ( )} ); })} ); } /** แถวที่ลากได้ (อยู่ใน TableBody) */ export function SortableTableRows({ rows, ...rowProps }: RowStyleProps & { rows: Row[] }) { return ( row.id)} strategy={verticalListSortingStrategy}> {rows.map((row) => ( ))} ); } /** DndContext + sensor + คำนวณลำดับใหม่ (ครอบตารางทั้งตัว) */ export function SortableTableProvider({ table, data, getRowId, onReorder, children, }: { table: Table; data: TData[]; getRowId?: (row: TData) => string; onReorder: (data: TData[]) => void; children: ReactNode; }) { const ui = useUiText(); const t = ui.sortable; const sensors = useSensors( useSensor(PointerSensor, { activationConstraint: { distance: 5, }, }), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates, }), ); const rowIds = table.getRowModel().rows.map((r) => r.id); const position = (template: string | undefined, fallback: string, id: string | number) => (template ?? fallback) .replace("{index}", String(rowIds.indexOf(String(id)) + 1)) .replace("{total}", String(rowIds.length)); function handleDragEnd(event: DragEndEvent) { const state = table.getState(); const isFiltered = state.columnFilters.length > 0 || state.globalFilter; const isSorted = state.sorting.length > 0; if (isFiltered || isSorted) { toast.warning(ui.table?.reorderBlocked ?? "จัดลำดับไม่ได้ขณะกรองหรือเรียงคอลัมน์อยู่"); return; } const { active, over } = event; if (over && active.id !== over.id) { // id ของแถวใน tanstack เป็น string เสมอ (getRowId / row.id / index) — เทียบแบบ string const idOf = (item: TData, index: number) => String(getRowId ? getRowId(item) : ((item as { id?: string | number }).id ?? index)); const oldIndex = data.findIndex((item, i) => idOf(item, i) === String(active.id)); const newIndex = data.findIndex((item, i) => idOf(item, i) === String(over.id)); if (oldIndex !== -1 && newIndex !== -1) { onReorder(arrayMove(data, oldIndex, newIndex)); } } } return ( position(t?.started, "เริ่มลากรายการที่ {index} จาก {total}", active.id), onDragOver: ({ over }) => over ? position(t?.moved, "ย้ายไปตำแหน่งที่ {index} จาก {total}", over.id) : undefined, onDragEnd: ({ over }) => over ? position(t?.dropped, "วางที่ตำแหน่งที่ {index} จาก {total}", over.id) : undefined, onDragCancel: () => t?.cancelled ?? "ยกเลิกการลาก", }, }} > {children} ); }