73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
'use client'
|
|
|
|
import { use } from 'react'
|
|
import useSWR from 'swr'
|
|
import PollCard from '@/components/PollCard'
|
|
import CommentThread from '@/components/social/CommentThread'
|
|
import CommentInput from '@/components/social/CommentInput'
|
|
import LoadingSpinner from '@/components/ui/LoadingSpinner'
|
|
import EmptyState from '@/components/ui/EmptyState'
|
|
import type { Poll, Comment } from '@/types'
|
|
|
|
const fetcher = (url: string) => fetch(url).then(r => r.json())
|
|
|
|
export default function PollDetailPage({ params }: { params: Promise<{ id: string }> }) {
|
|
const { id } = use(params)
|
|
|
|
const { data: pollData, isLoading: pollLoading } = useSWR<{ success: boolean; data: Poll }>(
|
|
`/api/poll/${id}`,
|
|
fetcher
|
|
)
|
|
|
|
const { data: commentsData, isLoading: commentsLoading, mutate } = useSWR<{ success: boolean; data: Comment[] }>(
|
|
`/api/poll/${id}/comments`,
|
|
fetcher
|
|
)
|
|
|
|
if (pollLoading) {
|
|
return (
|
|
<div className="flex justify-center items-center min-h-screen">
|
|
<LoadingSpinner size="lg" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const poll = pollData?.data
|
|
|
|
if (!poll) {
|
|
return (
|
|
<div className="max-w-2xl mx-auto p-4">
|
|
<EmptyState emoji="❓" title="Poll Not Found" description="This poll doesn't exist or has been removed" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="max-w-2xl mx-auto p-4 space-y-6">
|
|
<PollCard poll={poll} />
|
|
|
|
<div className="bg-white rounded-2xl p-6 border border-gray-200">
|
|
<h2 className="text-xl font-bold mb-4">💬 Comments ({commentsData?.data?.length || 0})</h2>
|
|
|
|
<div className="mb-6">
|
|
<CommentInput pollId={id} onSuccess={() => mutate()} />
|
|
</div>
|
|
|
|
{commentsLoading ? (
|
|
<div className="flex justify-center py-8">
|
|
<LoadingSpinner />
|
|
</div>
|
|
) : commentsData?.data?.length ? (
|
|
<CommentThread comments={commentsData.data} pollId={id} onUpdate={() => mutate()} />
|
|
) : (
|
|
<EmptyState
|
|
emoji="💬"
|
|
title="No Comments Yet"
|
|
description="Be the first to share your thoughts!"
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|