'use client' import { useEffect, useRef, useState } from 'react' import './latest-activity.css' import { lockBodyScroll } from '../../lib/scrollLock' type Event = { id: string; service: string; state: string; message: string; started_at?: string; finished_at?: string } type Operation = { id: string; label: string; status: string; events: Event[] } export default function LatestActivity({ operation, besideDownload, onDismiss }: { operation: Operation; besideDownload: boolean; onDismiss: () => void }) { const dialog = useRef(null) const trigger = useRef(null) const [open, setOpen] = useState(true) useEffect(() => { setOpen(true) }, [operation.id]) const latest = [...operation.events].sort((a, b) => (a.finished_at ?? a.started_at ?? '').localeCompare(b.finished_at ?? b.started_at ?? '') ).at(-1) const status = operation.status === 'running' ? 'Working' : operation.status === 'complete' ? 'Finished' : operation.status === 'searching' ? 'Search still running' : 'Needs attention' useEffect(() => { if (!open) return const element = dialog.current element?.showModal() const unlock = lockBodyScroll() return () => { element?.close() unlock() trigger.current?.focus() } }, [open]) return (
setOpen(false)} onClose={() => setOpen(false)}>
Activity details

{operation.label}

{status} · {operation.events.length} updates
{operation.status === 'running' &&

Working on your request. This can take a minute while the media services search.

}
    {operation.events.map((event) =>
  1. {event.state === 'active' ? 'Working' : event.state === 'error' ? 'Needs attention' : 'Done'}
    {event.service}

    {event.message}

  2. )}
{operation.status !== 'running' && }
) }