import { useQuery } from '@tanstack/react-query'
import { useEffect, useState } from 'react'
import { useSearchParams } from 'react-router-dom'
import {
fetchFailureGrid,
type FailureGridRow,
type FailureGridStage,
} from '../api/batchJobs'
// #800: sortable, pageable grid of failed items for any stage. Sort, page, size and filters live in the
// URL (?q, group, sort, dir, page, size) so a refresh or shared link shows the same rows.
const SIZES = [25, 50, 100]
const GROUPS: { key: string; label: string }[] = [
{ key: '', label: 'All reasons' },
{ key: 'bot_block', label: 'Bot check' },
{ key: 'rate_limit', label: 'Rate limited' },
{ key: 'network', label: 'Network / VPN' },
{ key: 'not_yet_available', label: 'Not yet available' },
{ key: 'no_captions', label: 'No captions' },
{ key: 'unavailable', label: 'Private / removed' },
{ key: 'unknown', label: 'No reason recorded' },
{ key: 'other', label: 'Other' },
]
const watchUrl = (id: string) => `https://www.youtube.com/watch?v=${id}`
const dash = (v: string) => (v ? v : '—')
const fmtDate = (iso: string) => (iso ? iso.slice(0, 10) : '—')
const fmtWhen = (iso: string) => (iso ? iso.replace('T', ' ').slice(0, 16) : '—')
function Copy({ text, label }: { text: string; label: string }) {
return (
)
}
function Detail({ row, where }: { row: FailureGridRow; where: string }) {
const ids: [string, string][] = [
['Video id', row.video_id],
['Event id', row.event_id],
['Jurisdiction id', row.jurisdiction_id],
['Channel id', row.channel_id],
]
return (
Full error
{row.error || '—'}
{row.error ?
: null}
{row.group_label}. {row.advice}{' '}
{row.retryable ? '(Retryable: use Retry on the reasons above, then Backfill.)' : '(Not retryable.)'}
Where to look: {where}
{ids.map(([k, v]) => (
- {k}
- {dash(v)}
{v ? : null}
))}
)
}
export default function FailureGrid({
stage,
cohort,
state,
}: {
stage: FailureGridStage
cohort: string
state: string
}) {
const [params, setParams] = useSearchParams()
const q = params.get('q') || ''
const group = params.get('group') || ''
const sort = params.get('sort') || 'last_updated'
const dir = params.get('dir') === 'asc' ? 'asc' : 'desc'
const size = SIZES.includes(Number(params.get('size'))) ? Number(params.get('size')) : 50
const page = Math.max(1, Number(params.get('page')) || 1)
const [qDraft, setQDraft] = useState(q)
const [openId, setOpenId] = useState(null)
useEffect(() => setQDraft(q), [q])
const set = (patch: Record, resetPage = true) => {
const next = new URLSearchParams(params)
for (const [k, v] of Object.entries(patch)) {
if (v === null || v === '') next.delete(k)
else next.set(k, v)
}
if (resetPage) next.delete('page')
setParams(next, { replace: true })
}
const { data, isLoading, isFetching, error } = useQuery({
queryKey: ['batch-failure-grid', stage, cohort, state, q, group, sort, dir, size, page],
queryFn: () =>
fetchFailureGrid({ stage, cohort, state, q, group, sort, dir, limit: size, offset: (page - 1) * size }),
placeholderData: (prev) => prev,
})
const total = data?.total ?? 0
const pages = Math.max(1, Math.ceil(total / size))
const from = total === 0 ? 0 : (page - 1) * size + 1
const to = Math.min(total, page * size)
const go = (n: number) => set({ page: n <= 1 ? null : String(Math.min(n, pages)) }, false)
const header = (key: string, label: string, sortable: boolean) => {
const active = sort === key
if (!sortable) return {label}
return (
)
}
return (
All failed items
{total.toLocaleString()} failed item
{total === 1 ? '' : 's'}
{q ? <> matching “{q}”> : null}
{group ? <> in “{GROUPS.find((g) => g.key === group)?.label ?? group}”> : null}
{isFetching ? updating… : null}
{isLoading ? Loading failed items…
: null}
{error ? (
Failed items are unavailable: {(error as Error).message}
) : null}
{data && total === 0 ? (
{q || group ? 'No failed items match these filters.' : 'No failed items recorded for this scope.'}
) : null}
{data && total > 0 ? (
{data.columns.map((c) => header(c.key, c.label, c.sortable))}
{data.rows.map((r) => {
const id = `${r.video_id}|${r.event_id}`
const open = openId === id
return (
{open ? : null}
)
})}
) : null}
{total > 0 ? (
{from.toLocaleString()}–{to.toLocaleString()} of {total.toLocaleString()}
Page {page} of {pages}
) : null}
)
}