/** 하수관로 GIS 편집에 필요한 공통코드를 조회하는 훅이다. */ "use client"; import { useEffect, useState } from "react"; import { getCommonCodes } from "@/components/lib/common/apiHelpers"; import type { GisEditTool } from "./gisEditTypes"; const GIS_EDIT_OPERATION_UP_CD = "GISEDITOPRT"; /** CMS 조회 실패 시 기존 GIS 편집 화면 동작과 문구를 유지하기 위한 최소 fallback이다. */ export const DEFAULT_GIS_EDIT_OPERATION_LABELS: Record = { ADD: "추가", MOVE: "이동", VERTEX_EDIT: "꼭지점편집", DELETE: "삭제", }; let cachedOperationLabels: Record | null = null; let operationLabelsPromise: Promise> | null = null; const isGisEditTool = (value: string): value is GisEditTool => value === "ADD" || value === "MOVE" || value === "VERTEX_EDIT" || value === "DELETE"; /** 같은 브라우저 세션에서는 GIS 편집작업코드를 한 번만 조회해 툴바와 세션이 공유한다. */ const loadGisEditOperationLabels = async () => { if (cachedOperationLabels) return cachedOperationLabels; if (!operationLabelsPromise) { operationLabelsPromise = getCommonCodes(GIS_EDIT_OPERATION_UP_CD) .then((codes) => { const labels = { ...DEFAULT_GIS_EDIT_OPERATION_LABELS }; codes.forEach((code) => { if (isGisEditTool(code.lwpoCd) && code.lwpoCdNm) { labels[code.lwpoCd] = code.lwpoCdNm; } }); cachedOperationLabels = labels; return labels; }) .finally(() => { operationLabelsPromise = null; }); } return operationLabelsPromise; }; /** GIS 편집 도구 코드값은 유지하고 사용자 표시명만 CMS 공통코드로 교체한다. */ export const useGisEditOperationLabels = () => { const [operationLabels, setOperationLabels] = useState< Record >(cachedOperationLabels ?? DEFAULT_GIS_EDIT_OPERATION_LABELS); useEffect(() => { let cancelled = false; loadGisEditOperationLabels() .then((labels) => { if (!cancelled) setOperationLabels(labels); }) .catch((error) => { console.error("GIS 편집작업 공통코드 조회에 실패했습니다.", error); }); return () => { cancelled = true; }; }, []); return operationLabels; };