'use client' import { useEffect, useRef, useState } from 'react' import { authFetch, getApiBase } from '../../lib/auth' import { lockBodyScroll } from '../../lib/scrollLock' type Props = { requestId: string title: string year?: number requestType: string } const readError = async (response: Response) => { try { const payload = await response.json() if (typeof payload?.detail === 'string' && payload.detail.trim()) return payload.detail } catch { // Use the friendly fallback below. } return 'Your report could not be submitted. Please try again.' } export default function RequestIssueDialog({ requestId, title, year, requestType }: Props) { const dialog = useRef(null) const detailsInput = useRef(null) const [open, setOpen] = useState(false) const [details, setDetails] = useState('') const [submitting, setSubmitting] = useState(false) const [error, setError] = useState(null) const [issueId, setIssueId] = useState(null) useEffect(() => { if (!open) return const previous = document.activeElement as HTMLElement | null dialog.current?.showModal() const unlock = lockBodyScroll() window.setTimeout(() => detailsInput.current?.focus(), 0) return () => { dialog.current?.close() unlock() previous?.focus() } }, [open]) const close = () => { if (submitting) return setOpen(false) setError(null) if (issueId) { setIssueId(null) setDetails('') } } const submit = async (event: React.FormEvent) => { event.preventDefault() const description = details.trim() if (!description) { setError('Tell us what is wrong so we know what to check.') detailsInput.current?.focus() return } setSubmitting(true) setError(null) try { const response = await authFetch(`${getApiBase()}/portal/items`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ kind: 'issue', title: `Problem with ${title}`, description, media_type: requestType === 'tv' ? 'tv' : 'movie', year: year ?? null, external_ref: `/requests/${requestId}`, issue_type: 'general', priority: 'normal', }), }) if (!response.ok) throw new Error(await readError(response)) const payload = await response.json() const createdId = Number(payload?.item?.id) if (!Number.isInteger(createdId) || createdId <= 0) throw new Error('The issue was created, but its reference could not be loaded.') setIssueId(createdId) } catch (caught) { console.error(caught) setError(caught instanceof Error ? caught.message : 'Your report could not be submitted. Please try again.') } finally { setSubmitting(false) } } return <> { event.preventDefault(); close() }}> {issueId ? (

Thanks, we've got it

Your report is now issue #{issueId}. You can follow its progress from the Issues page.

) : (
Report a problem

What's wrong?

{requestType === 'tv' ? 'TV show' : 'Movie'}{title}{year ? ` (${year})` : ''}Request #{requestId} · Ready to watch