"use client"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import type { BaseSeDto, SeDraftBase, SeGridConfig, SeGridState, SeRowState } from "./grid-types"; import { createDraftRowKey, createSeGridUtils } from "./grid-utils"; interface UseSeGridRowsOptions< TDto extends BaseSeDto, TBaseEntry extends TDto & { rowKey: string }, _TRow extends TBaseEntry & SeRowState, _TDraft extends SeDraftBase, _TPatch extends Partial, TSaveDto, > { serverContent: TDto[] | undefined; onAlert: (message: string) => void; onSave: (payload: TSaveDto[]) => Promise; } export function useSeGridRows< TDto extends BaseSeDto, TBaseEntry extends TDto & { rowKey: string }, TRow extends TBaseEntry & SeRowState, TDraft extends SeDraftBase, TPatch extends Partial, TSaveDto, >( config: SeGridConfig, { serverContent, onAlert, onSave, }: UseSeGridRowsOptions, ) { const utils = useMemo(() => createSeGridUtils(config), [config]); const [grid, setGrid] = useState>( utils.createEmptyGridState, ); const hasPendingRef = useRef(false); hasPendingRef.current = utils.hasGridPendingChanges(grid); useEffect(() => { if (serverContent == null) return; if (hasPendingRef.current) return; setGrid(utils.hydrateGridFromServer(serverContent)); }, [serverContent, utils]); const displayRows = useMemo(() => { return grid.structure .filter((slot) => !grid.deletedKeys.has(slot.rowKey)) .map((slot) => { const row = utils.mergeRow(grid, slot.rowKey); return row ? { row, depth: slot.depth } : null; }) .filter((item): item is { row: TRow; depth: number } => item != null); }, [grid, utils]); const canSave = utils.hasGridPendingChanges(grid); const updateRow = useCallback( (rowKey: string, patch: Partial) => { setGrid((prev) => utils.patchGridRow(prev, rowKey, patch)); }, [utils], ); const addRootRow = useCallback(() => { const rowKey = createDraftRowKey(); setGrid((prev) => ({ ...prev, drafts: { ...prev.drafts, [rowKey]: config.createEmptyDraft({ rowKey, parentRowKey: null, cdLvNo: 1, }), }, structure: utils.insertRootSlot(prev.structure, { rowKey, depth: 0, parentRowKey: null, }), })); }, [config, utils]); const addChildRow = useCallback( (parent: TRow) => { const rowKey = createDraftRowKey(); setGrid((prev) => { const parentSlot = prev.structure.find((item) => item.rowKey === parent.rowKey); const depth = (parentSlot?.depth ?? 0) + 1; return { ...prev, drafts: { ...prev.drafts, [rowKey]: config.createEmptyDraft({ rowKey, parentRowKey: parent.rowKey, cdLvNo: (parent.cdLvNo ?? 1) + 1, }), }, structure: utils.insertChildSlot(prev.structure, parent.rowKey, { rowKey, depth, parentRowKey: parent.rowKey, }), }; }); }, [config, utils], ); const removeDraft = useCallback( (rowKey: string) => { setGrid((prev) => utils.removeDraftFromGrid(prev, rowKey)); }, [utils], ); const markDeleted = useCallback( (rowKey: string) => { setGrid((prev) => utils.markRowsDeleted(prev, rowKey)); }, [utils], ); const resetFromServer = useCallback( (content: TDto[]) => { setGrid(utils.hydrateGridFromServer(content)); }, [utils], ); const save = useCallback(async () => { const mergedRows = utils.mergeAllRows(grid); const payload = config.buildSavePayload(mergedRows); if (payload.length === 0) return false; for (const row of mergedRows) { if (!row.isDeleted && (row.isNew || row.isDirty)) { const parentCodeError = utils.validateDraftParentCode(grid, row.rowKey); if (parentCodeError) { onAlert(parentCodeError); return false; } const fieldError = config.validateRow(row); if (fieldError) { onAlert(fieldError); return false; } } } await onSave(payload); return true; }, [config, grid, onAlert, onSave, utils]); return { displayRows, canSave, updateRow, addRootRow, addChildRow, removeDraft, markDeleted, resetFromServer, save, }; }