'use client' import { useEffect, useState } from 'react' import { useTranslations, useLocale } from 'next-intl' type DownloadData = { uuid: string status: 'PENDING' | 'PROCESSING' | 'DONE' | 'FAILED' | 'FILE_DELETED' format: string quality: string fileName: string | null fileSize: string | null errorMsg: string | null downloadToken: string | null tokenExpiresAt: string | null } function Spinner() { return ( ) } export function StatusView({ uuid }: { uuid: string }) { const t = useTranslations('status') const locale = useLocale() const [data, setData] = useState(null) const [notFound, setNotFound] = useState(false) useEffect(() => { let active = true async function poll() { const res = await fetch(`/api/downloads/${uuid}`) if (!active) return if (res.status === 404) { setNotFound(true) return } const body = (await res.json()) as DownloadData setData(body) if (body.status === 'PENDING' || body.status === 'PROCESSING') { setTimeout(poll, 5_000) } } poll() return () => { active = false } }, [uuid]) const cardClass = 'max-w-md w-full mx-auto rounded-2xl border border-gray-100 dark:border-slate-800 bg-gray-50 dark:bg-slate-900 p-8' if (notFound) { return (

{t('notFound')}

) } if (!data) { return (

{t('loading')}

) } if (data.status === 'PENDING' || data.status === 'PROCESSING') { return (

{data.status === 'PENDING' ? t('pending') : t('processing')}

{t('polling')}

) } if (data.status === 'DONE' && data.downloadToken) { const sizeMb = data.fileSize ? `(${(Number(data.fileSize) / 1_048_576).toFixed(1)} MB)` : '' return (

{t('ready')} {sizeMb}

{t('download')} {data.fileName} {data.tokenExpiresAt && (

{t('linkExpires')}{' '} {new Date(data.tokenExpiresAt).toLocaleString(locale)}

)}
) } if (data.status === 'FAILED') { return (

{t('failed')}

          {data.errorMsg}
        
) } if (data.status === 'FILE_DELETED') { return (

{t('deleted')}

) } return (

{t('unknown')}: {data.status}

) }