'use client'; import Link from 'next/link'; import { useParams } from 'next/navigation'; import { useState } from 'react'; import useSWR from 'swr'; import { COMMENT_MAX_LENGTH, timeAgo, type CommentDTO, type CommentThreadDTO } from '@musicshare/shared'; import { Avatar } from '@/components/Avatar'; import { ReportForm } from '@/components/ProfileMenu'; import { Reactions } from '@/components/Reactions'; import { TrackCard } from '@/components/TrackCard'; import { client, fetcher, useMe } from '@/lib/api'; /** One play and its comments: where the 💬 link and comment notifications lead. */ export default function PlayPage() { const { id } = useParams<{ id: string }>(); const { me, loggedOut } = useMe(); // Polled while open so other people's comments show up without reloading. const { data, error, mutate } = useSWR(me ? `/listens/${id}/comments` : null, fetcher, { refreshInterval: 5000 }); const [draft, setDraft] = useState(''); const [sending, setSending] = useState(false); const [problem, setProblem] = useState(null); const [reporting, setReporting] = useState(null); if (loggedOut) { return (

Log in to see the comments.

); } if (error) return

That play is gone.

; if (!data) return null; const owner = data.owner.displayName ?? data.owner.username; const send = async (e: React.FormEvent) => { e.preventDefault(); const body = draft.trim(); if (!body) return; setSending(true); setProblem(null); try { const comment = await client.addComment(id, { body }); setDraft(''); await mutate((t) => (t ? { ...t, comments: [...t.comments, comment] } : t), { revalidate: false }); } catch (err) { setProblem((err as Error).message); } setSending(false); }; const remove = async (c: CommentDTO) => { if (!confirm('Delete this comment?')) return; try { await client.deleteComment(c.id); await mutate((t) => (t ? { ...t, comments: t.comments.filter((x) => x.id !== c.id) } : t), { revalidate: false }); } catch (err) { setProblem((err as Error).message); } }; return (

{owner} {' '} played this · {timeAgo(data.playedAt)}

Comments

{data.comments.length === 0 &&

{data.canComment ? 'No comments yet. Say something!' : 'No comments yet.'}

} {data.comments.map((c) => (
{c.user.displayName ?? c.user.username} {' '} · {timeAgo(c.createdAt)}

{c.body}

{c.canDelete && ( )} {c.user.id !== me?.id && ( )}
))} {data.canComment ? (