"use client"; import { useCallback, useEffect, useState } from "react"; import { arrayMove } from "@dnd-kit/sortable"; import type { ElaprDocPrgrsClientResponseDto } from "@/shared/model"; import { assignPrgrsSttsAtrzSeqNumbers, type ElaprPrgrsSttsRow, isPrgrsSttsRowEditable, PRGRS_STTS_WAITING_CD, } from "./prgrs-stts-save-utils"; import { createDraftRowKey } from "./use-apln-rows"; function getVisibleRows(rows: ElaprPrgrsSttsRow[]): ElaprPrgrsSttsRow[] { return rows.filter((row) => !row.isDeleted); } function toRow(item: ElaprDocPrgrsClientResponseDto): ElaprPrgrsSttsRow { return { rowKey: item.elaprPrgrsId ?? createDraftRowKey(), elaprPrgrsId: item.elaprPrgrsId, elaprDmndId: item.elaprDmndId, aprvrUuid: item.aprvrUuid, aprvrNm: item.aprvrNm, atrzSeComCd: item.atrzSeComCd, atrzSeComNm: item.atrzSeComNm, atrzSeqNo: item.atrzSeqNo, atrzPrgrsSttsComCd: item.atrzPrgrsSttsComCd, atrzPrgrsSttsComNm: item.atrzPrgrsSttsComNm, rjctRsn: item.rjctRsn, ognzNm: item.ognzNm, jbpsSeNm: item.jbpsSeNm, jbgdSeNm: item.jbgdSeNm, dcarbYn: item.dcarbYn ?? "N", isNew: false, isDirty: false, isDeleted: false, }; } interface UsePrgrsSttsRowsOptions { serverRows?: ElaprDocPrgrsClientResponseDto[]; elaprDmndId?: string | null; } export function usePrgrsSttsRows({ serverRows, elaprDmndId }: UsePrgrsSttsRowsOptions) { const [rows, setRows] = useState([]); const [editingRowKey, setEditingRowKey] = useState(null); useEffect(() => { setRows((serverRows ?? []).map(toRow)); setEditingRowKey(null); }, [serverRows]); const isEditingLocked = editingRowKey != null; const visibleRows = getVisibleRows(rows); const addRow = useCallback(() => { if (!elaprDmndId || isEditingLocked) return; const rowKey = createDraftRowKey(); setRows((prev) => [ ...prev, { rowKey, elaprDmndId, atrzSeComCd: "", aprvrUuid: "", aprvrNm: "", atrzPrgrsSttsComCd: PRGRS_STTS_WAITING_CD, dcarbYn: "N", isNew: true, isDirty: true, isDeleted: false, }, ]); setEditingRowKey(rowKey); }, [elaprDmndId, isEditingLocked]); const startEdit = useCallback( (rowKey: string) => { if (isEditingLocked) return; const target = rows.find((r) => r.rowKey === rowKey); if (!target || !isPrgrsSttsRowEditable(target)) return; setEditingRowKey(rowKey); setRows((prev) => prev.map((row) => (row.rowKey === rowKey ? { ...row, snapshot: { ...row } } : row)), ); }, [isEditingLocked, rows], ); const updateRow = useCallback( (rowKey: string, patch: Partial) => { if (editingRowKey !== rowKey) return; setRows((prev) => prev.map((row) => (row.rowKey === rowKey ? { ...row, ...patch, isDirty: true } : row)), ); }, [editingRowKey], ); const setDcarbYn = useCallback( (rowKey: string, checked: boolean) => { if (editingRowKey != null && editingRowKey !== rowKey) return; setRows((prev) => { const target = prev.find((r) => r.rowKey === rowKey); if (!target || target.isDeleted) return prev; const nextYn = checked ? "Y" : "N"; return prev.map((row) => { if (row.isDeleted) return row; if (row.rowKey === rowKey) { if ((row.dcarbYn ?? "N") === nextYn) return row; return { ...row, dcarbYn: nextYn, isDirty: true }; } if (!checked) return row; if ((row.dcarbYn ?? "N") === "N") return row; return { ...row, dcarbYn: "N", isDirty: true }; }); }); }, [editingRowKey], ); const cancelEdit = useCallback(() => { if (!editingRowKey) return; setRows((prev) => { const target = prev.find((r) => r.rowKey === editingRowKey); if (target?.isNew) { return prev.filter((r) => r.rowKey !== editingRowKey); } return prev.map((row) => { if (row.rowKey !== editingRowKey) return row; if (row.snapshot) { const { snapshot: _s, isNew: _n, isDirty: _d, isDeleted: _del, ...restored } = row.snapshot; return { ...restored, isNew: false, isDirty: false, isDeleted: false }; } return { ...row, isDirty: false }; }); }); setEditingRowKey(null); }, [editingRowKey]); const commitEdit = useCallback((rowKey: string) => { setRows((prev) => prev.map((row) => { if (row.rowKey !== rowKey) return row; const { snapshot: _s, ...committed } = row; return { ...committed, isDirty: true }; }), ); setEditingRowKey(null); }, []); const markDeleted = useCallback((rowKey: string) => { setRows((prev) => { const target = prev.find((r) => r.rowKey === rowKey); if (!target || !isPrgrsSttsRowEditable(target)) return prev; if (target.isNew) { return prev.filter((r) => r.rowKey !== rowKey); } return prev.map((row) => row.rowKey === rowKey ? { ...row, isDeleted: true, isDirty: true } : row, ); }); setEditingRowKey((key) => (key === rowKey ? null : key)); }, []); const reorderRows = useCallback( (activeKey: string, overKey: string): ElaprPrgrsSttsRow[] => { if (isEditingLocked) return []; let payloadRows: ElaprPrgrsSttsRow[] = []; setRows((prev) => { const visible = getVisibleRows(prev); const activeRow = visible.find((r) => r.rowKey === activeKey); const overRow = visible.find((r) => r.rowKey === overKey); if (!activeRow || !overRow) return prev; if (!isPrgrsSttsRowEditable(activeRow) || !isPrgrsSttsRowEditable(overRow)) return prev; const oldIndex = visible.findIndex((r) => r.rowKey === activeKey); const newIndex = visible.findIndex((r) => r.rowKey === overKey); if (oldIndex < 0 || newIndex < 0) return prev; const reorderedVisible = assignPrgrsSttsAtrzSeqNumbers( arrayMove(visible, oldIndex, newIndex), ).map((row) => (isPrgrsSttsRowEditable(row) ? { ...row, isDirty: true } : row)); const visibleKeys = new Set(reorderedVisible.map((r) => r.rowKey)); const deletedRows = prev.filter((r) => r.isDeleted); const merged = [ ...reorderedVisible, ...deletedRows.filter((r) => !visibleKeys.has(r.rowKey)), ]; payloadRows = reorderedVisible.filter(isPrgrsSttsRowEditable); return merged; }); return payloadRows; }, [isEditingLocked], ); return { rows, visibleRows, editingRowKey, isEditingLocked, addRow, startEdit, updateRow, setDcarbYn, cancelEdit, commitEdit, markDeleted, reorderRows, }; }