109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
"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 (
|
|
<StandardPopupLayout
|
|
title="관로 조사보고서"
|
|
showDefaultCancelAction={false}
|
|
actions={
|
|
<>
|
|
<span className="text-[1.05rem] text-label">
|
|
관로번호 {pipeNumber}
|
|
{reportNo ? ` · 보고서번호 ${reportNo}` : ""}
|
|
</span>
|
|
<Button type="button" color="white" onClick={() => window.close()}>
|
|
취소
|
|
</Button>
|
|
</>
|
|
}
|
|
bodyClassName="flex min-h-0 flex-1 p-3"
|
|
>
|
|
<section className="min-h-0 flex-1">
|
|
{errorMessage ? (
|
|
<div className="flex h-full items-center justify-center rounded-sm border border-red-200 bg-red-50 p-6 text-center text-[1.1rem] text-red-700">
|
|
{errorMessage}
|
|
</div>
|
|
) : reportUrl ? (
|
|
<iframe
|
|
title={`${pipeNumber} 관로 조사보고서`}
|
|
src={reportUrl}
|
|
className="h-full w-full border border-border bg-background"
|
|
/>
|
|
) : (
|
|
<div className="flex h-full items-center justify-center rounded-sm border border-border bg-background text-[1.1rem] text-muted-foreground">
|
|
조사보고서를 불러오는 중입니다.
|
|
</div>
|
|
)}
|
|
</section>
|
|
</StandardPopupLayout>
|
|
);
|
|
}
|