"use client"; import { useEffect, useState } from "react"; import { downloadExmnPipeReport } from "@/features/exmn/api/exmn-business-api"; import { StandardPopupLayout } from "@common/business_components"; import { Button } from "@common/components"; import type { WindowPopupContentProps } from "../model/types"; const getParam = (value: string | string[] | undefined) => Array.isArray(value) ? (value[0] ?? "") : (value ?? ""); /** 공통 Blob API 오류 문자열에서 백엔드의 사용자 안내 메시지만 추출한다. */ const getReportErrorMessage = (error: unknown) => { if (!(error instanceof Error)) { return "관로 조사보고서를 불러오지 못했습니다."; } const backendMessage = error.message.match( /"message":"((?:\\.|[^"\\])*)"/, )?.[1]; if (!backendMessage) return error.message; try { return JSON.parse(`"${backendMessage}"`) as string; } catch { return backendMessage; } }; /** 저장된 관로별 PDF 구간을 공통 WindowPopup 안에서 표시한다. */ export default function ExmnPipeReportPopupContent({ searchParams, }: WindowPopupContentProps) { const exmnBizId = getParam(searchParams.exmnBizId); const pipeNumber = getParam(searchParams.pipeNumber); const reportNo = getParam(searchParams.reportNo); const [reportUrl, setReportUrl] = useState(""); const [errorMessage, setErrorMessage] = useState(""); useEffect(() => { let active = true; let objectUrl = ""; setReportUrl(""); setErrorMessage(""); void downloadExmnPipeReport(exmnBizId, pipeNumber) .then((blob) => { if (!active) return; const pdfBlob = blob.type === "application/pdf" ? blob : new Blob([blob], { type: "application/pdf" }); objectUrl = URL.createObjectURL(pdfBlob); setReportUrl(objectUrl); }) .catch((error) => { if (!active) return; console.warn("관로 조사보고서 조회 실패:", error); setErrorMessage(getReportErrorMessage(error)); }); return () => { active = false; if (objectUrl) URL.revokeObjectURL(objectUrl); }; }, [exmnBizId, pipeNumber]); return ( 관로번호 {pipeNumber} {reportNo ? ` · 보고서번호 ${reportNo}` : ""} } bodyClassName="flex min-h-0 flex-1 p-3" >
{errorMessage ? (
{errorMessage}
) : reportUrl ? (