"use client"; import dayjs from "dayjs"; import Image from "next/image"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { useQuery } from "@tanstack/react-query"; import { Button, Label, RadioGroup, RadioGroupItem, Skeleton, } from "@common/components"; import { AttachmentApi } from "@/entities/attachment"; import { PostApi } from "@/entities/post"; import { BoardApi } from "@/entities/board"; import { ENDPOINT } from "@/shared/config"; import { useAlertStore, useMutationWrapper, useMenuStore } from "@/shared/lib"; import { PostAction } from "./post-action"; import { PostComment } from "./post-comment"; import { PostEvaluationApi } from "../api/post-evaluation-service"; function Post({ bbsId, pstNo }: { bbsId: string; pstNo: string }) { const router = useRouter(); const alert = useAlertStore((state) => state.setMessage); const { bbsInfo, setBbsInfo } = useMenuStore((state) => state); const [filename, setFilename] = useState(""); const [evlScr, setEvlScr] = useState(5); const { data: boardInfo } = useQuery({ queryKey: [ENDPOINT.BOARD_SERVICE.BBS, bbsId], queryFn: () => BoardApi.getBoardInfo(bbsId), }); const { data: postData } = useQuery({ queryKey: [ENDPOINT.BOARD_SERVICE.POSTS, bbsId, pstNo], queryFn: () => PostApi.getPostInfo(parseInt(bbsId), parseInt(pstNo)), }); const { data: downloadResponse, isFetching } = useQuery({ queryKey: [ENDPOINT.ATTACH_SERVICE.DOWNLOAD, postData?.atflCd], queryFn: () => AttachmentApi.downloadAttachmentByAtflCd(postData?.atflCd as string), enabled: !!postData?.atflCd && bbsInfo?.bbsTypeCd !== "BBSIMAGE" && bbsInfo?.bbsTypeCd !== "BBSMOVIE", }); const { data: imageUrl, isLoading } = useQuery({ queryKey: [ENDPOINT.ATTACH_SERVICE.IMAGES, postData?.atflCd], queryFn: () => AttachmentApi.getImageUrlByAtflCd(postData?.atflCd as string), enabled: !!postData?.atflCd, }); const { data: videoAttachment } = useQuery({ queryKey: [ENDPOINT.ATTACH_SERVICE.ATTACHMENTS, postData?.atflCd], queryFn: () => AttachmentApi.getAttachmentsInfo(postData?.atflCd as string), select: (data) => data[0], enabled: !!postData?.atflCd && bbsInfo?.bbsTypeCd === "BBSMOVIE", }); const { data: videoUrl } = useQuery({ queryKey: [ENDPOINT.ATTACH_SERVICE.STREAM_VIDEO, videoAttachment?.atflId], queryFn: () => AttachmentApi.getStreamVideoUrlByAtflId( videoAttachment?.atflId as string, ), enabled: !!videoAttachment?.atflId, }); const { mutateAsync: deletePost } = useMutationWrapper({ mutationFn: (delRsn?: string) => PostApi.deletePost(parseInt(bbsId), parseInt(pstNo), delRsn), onSuccess: () => { alert("삭제되었습니다.", router.back); }, }); const { mutate: postEvaluation } = useMutationWrapper({ mutationFn: (evlScr: number) => PostEvaluationApi.postEvaluation({ pstNo: parseInt(pstNo), evlScr, }), onSuccess: () => { alert("게시물 평가가 완료되었습니다."); }, }); const handleDownload = async () => { if (!downloadResponse) return; try { const blob = new Blob([downloadResponse.data]); const url = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename || "download"; document.body.appendChild(link); link.click(); window.URL.revokeObjectURL(url); document.body.removeChild(link); } catch (error) { console.error(error); } }; useEffect(() => { if (!downloadResponse) return; try { let filename = ""; const contentDisposition = downloadResponse.headers["content-disposition"]; if (contentDisposition) { const filenameMatch = contentDisposition.match( /filename\*=UTF-8''([^;]+)/, ); if (filenameMatch && filenameMatch[1]) { filename = decodeURIComponent(filenameMatch[1]); } } setFilename(filename); } catch { setFilename(""); } }, [downloadResponse]); useEffect(() => { if (!boardInfo) return; setBbsInfo(boardInfo); }, [boardInfo]); return ( <> {postData?.pstTtl} 작성자: {postData?.wrtrNm} 등록일: {dayjs(postData?.regDt).format("YYYY-MM-DD")} 조회수: {postData?.inqNocs} {bbsInfo?.bbsTypeCd === "BBSIMAGE" && ( <> {isLoading && ( )} {imageUrl && ( )} > )} {bbsInfo?.bbsTypeCd === "BBSMOVIE" && ( )} {postData?.pstCn ?? ""} {isFetching ? ( ) : ( filename && ( <> 첨부파일 {filename} > ) )} {bbsInfo?.dgstfnEvlUseYn === "Y" && ( <> 이 페이지에서 제공하는 정보에 대하여 만족하시나요? setEvlScr(parseInt(value))} className="flex items-center space-x-4" > 매우 만족 😊 만족 🙂 보통 😐 불만족 🙁 매우 불만족 😞 postEvaluation(evlScr)} > 평가하기 > )} > ); } export { Post };
{postData?.pstCn ?? ""}
이 페이지에서 제공하는 정보에 대하여 만족하시나요?