'use client'; import { useState, useEffect, useCallback, use } from 'react'; import Image from 'next/image'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { ClipLoader } from 'react-spinners'; import { GalleryPost, getGalleryPost, deleteGalleryPost, getSortedGalleryContent, type GalleryContentItem } from '@/lib/services'; import { useAuth, useImageModal } from '@/hooks'; export default function GalleryDetailPage({ params, }: { params: Promise<{ id: string }>; }) { const { id } = use(params); const router = useRouter(); const [post, setPost] = useState(null); const [loading, setLoading] = useState(true); const [deleting, setDeleting] = useState(false); const { user } = useAuth(); const sortedImages = post?.images.sort((a, b) => a.order - b.order) || []; const { selectedIndex, isOpen, open, close, next, prev } = useImageModal(sortedImages.length); const loadData = useCallback(async () => { setLoading(true); try { const data = await getGalleryPost(parseInt(id, 10)); setPost(data); } catch (error) { console.error('Failed to fetch post:', error); } finally { setLoading(false); } }, [id]); useEffect(() => { loadData(); }, [loadData]); const handleDelete = async () => { if (!confirm('정말 삭제하시겠습니까?')) return; setDeleting(true); try { await deleteGalleryPost(parseInt(id, 10)); router.push('/gallery'); } catch (error) { console.error('Failed to delete post:', error); alert('삭제에 실패했습니다.'); setDeleting(false); } }; if (loading) { return (
); } if (!post) { return (

갤러리를 찾을 수 없습니다.

목록으로 돌아가기
); } return (
{/* 헤더 */}

{post.title}

{new Date(post.createdAt).toLocaleDateString('ko-KR')}

{user && ( )}
{/* 콘텐츠 (이미지 + 텍스트 블록) */}
{(() => { const sortedContent = getSortedGalleryContent(post); return sortedContent.map((item: GalleryContentItem, idx: number) => { if (item.type === 'image') { const imageId = item.data.id; const aspectRatio = item.data.aspectRatio; return (
{ // 전체 이미지 배열에서의 인덱스 찾기 const actualIndex = sortedImages.findIndex(img => img.id === item.data.id); open(actualIndex); }} > {item.data.displayUrl && ( {`${post.title} )}
); } else { return (
{item.data.content}
); } }); })()}
{/* 이미지 모달 */} {isOpen && selectedIndex !== null && (
{sortedImages[selectedIndex]?.displayUrl && ( {`${post.title} )}
{selectedIndex + 1} / {sortedImages.length}
)}
); }