import { useEffect, useRef, useState } from "react"; import { toast } from "sonner"; import CopyButton from "@/components/common/CopyButton"; import MessagePreview from "./MessagePreview"; import { fetchMessageInfo, deleteMessage } from "@/api/message.api"; import type { MessageInfoResponse } from "../types"; interface MessageSlidePanelProps { isOpen: boolean; onClose: () => void; messageCode: string | null; serviceCode: string | null; } export default function MessageSlidePanel({ isOpen, onClose, messageCode, serviceCode, }: MessageSlidePanelProps) { const [detail, setDetail] = useState(null); const [loading, setLoading] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [deleting, setDeleting] = useState(false); const bodyRef = useRef(null); // 메시지 상세 조회 useEffect(() => { if (!isOpen || !messageCode || !serviceCode) { setDetail(null); return; } (async () => { setLoading(true); try { const res = await fetchMessageInfo( { message_code: messageCode }, serviceCode, ); setDetail(res.data.data); } catch { setDetail(null); toast.error("메시지 상세 조회에 실패했습니다."); } finally { setLoading(false); } })(); }, [isOpen, messageCode, serviceCode]); // 패널 열릴 때 스크롤 최상단으로 리셋 useEffect(() => { if (isOpen) { bodyRef.current?.scrollTo(0, 0); } }, [isOpen, messageCode]); // ESC 닫기 useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape" && isOpen) onClose(); }; document.addEventListener("keydown", handleKeyDown); return () => document.removeEventListener("keydown", handleKeyDown); }, [isOpen, onClose]); // 패널 열릴 때 body 스크롤 잠금 useEffect(() => { if (isOpen) { document.body.style.overflow = "hidden"; } else { document.body.style.overflow = ""; } return () => { document.body.style.overflow = ""; }; }, [isOpen]); // 삭제 처리 const handleDelete = async () => { if (!detail?.message_code || !serviceCode) return; setDeleting(true); try { await deleteMessage( { message_code: detail.message_code }, serviceCode, ); toast.success(`${detail.message_code} 메시지가 삭제되었습니다.`); setShowDeleteConfirm(false); onClose(); } catch { toast.error("메시지 삭제에 실패했습니다."); } finally { setDeleting(false); } }; // 기타 정보 문자열 변환 const extraText = detail?.data != null ? typeof detail.data === "string" ? detail.data : JSON.stringify(detail.data, null, 2) : ""; return ( <> {/* 오버레이 */}
{/* 패널 */} {/* 삭제 확인 모달 */} {showDeleteConfirm && detail && (
setShowDeleteConfirm(false)} />
warning

메시지 삭제

{detail.message_code} 메시지를 삭제하시겠습니까?

info 삭제 후 복구가 불가능합니다.
)} ); }