Add the existing application baseline and repository documentation/ignore rules. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
110 lines
4.4 KiB
TypeScript
110 lines
4.4 KiB
TypeScript
import { useState, useEffect } from 'react'
|
|
import { Play, RefreshCw } from 'lucide-react'
|
|
import { adminApi, type ScrapeJob, type Source } from '@/api/admin'
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import { Select } from '@/components/ui/select'
|
|
import { Spinner } from '@/components/ui/spinner'
|
|
|
|
function jobStatusVariant(status: string) {
|
|
if (status === 'done') return 'bullish'
|
|
if (status === 'error') return 'bearish'
|
|
if (status === 'running') return 'default'
|
|
return 'outline'
|
|
}
|
|
|
|
function fmtDate(t: { Time: string; Valid: boolean } | null) {
|
|
if (!t?.Valid) return '—'
|
|
return new Date(t.Time).toLocaleString('fr-FR')
|
|
}
|
|
|
|
export function Jobs() {
|
|
const [jobs, setJobs] = useState<ScrapeJob[]>([])
|
|
const [sources, setSources] = useState<Source[]>([])
|
|
const [loading, setLoading] = useState(true)
|
|
const [triggering, setTriggering] = useState(false)
|
|
const [selectedSource, setSelectedSource] = useState('')
|
|
|
|
useEffect(() => { load() }, [])
|
|
|
|
async function load() {
|
|
setLoading(true)
|
|
try {
|
|
const [j, s] = await Promise.all([adminApi.listJobs(), adminApi.listSources()])
|
|
setJobs(j ?? [])
|
|
setSources(s ?? [])
|
|
if (s?.[0]) setSelectedSource(s[0].id)
|
|
} finally { setLoading(false) }
|
|
}
|
|
|
|
async function trigger() {
|
|
if (!selectedSource) return
|
|
setTriggering(true)
|
|
await adminApi.triggerJob(selectedSource)
|
|
setTimeout(() => { load(); setTriggering(false) }, 1000)
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex flex-wrap items-center justify-between gap-4">
|
|
<div>
|
|
<h1 className="text-2xl font-bold">Jobs de scraping</h1>
|
|
<p className="text-muted-foreground text-sm">Historique et déclenchement manuel</p>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<Select value={selectedSource} onChange={e => setSelectedSource(e.target.value)} className="w-40">
|
|
{sources.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
|
|
</Select>
|
|
<Button onClick={trigger} disabled={triggering || !selectedSource}>
|
|
{triggering ? <Spinner className="h-4 w-4" /> : <Play className="h-4 w-4" />}
|
|
Lancer
|
|
</Button>
|
|
<Button variant="outline" size="icon" onClick={load}><RefreshCw className="h-4 w-4" /></Button>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="flex justify-center py-12"><Spinner /></div>
|
|
) : (
|
|
<Card>
|
|
<CardHeader><CardTitle>Historique</CardTitle></CardHeader>
|
|
<CardContent className="p-0">
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-sm">
|
|
<thead className="border-b">
|
|
<tr className="text-left text-muted-foreground">
|
|
<th className="px-4 py-3">Source</th>
|
|
<th className="px-4 py-3">Statut</th>
|
|
<th className="px-4 py-3">Début</th>
|
|
<th className="px-4 py-3">Fin</th>
|
|
<th className="px-4 py-3">Articles</th>
|
|
<th className="px-4 py-3">Erreur</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{jobs.map(j => (
|
|
<tr key={j.id} className="border-b last:border-0 hover:bg-muted/30">
|
|
<td className="px-4 py-3 font-medium">{j.source_name}</td>
|
|
<td className="px-4 py-3">
|
|
<Badge variant={jobStatusVariant(j.status) as 'bullish' | 'bearish' | 'default' | 'outline'}>{j.status}</Badge>
|
|
</td>
|
|
<td className="px-4 py-3 text-muted-foreground">{fmtDate(j.started_at)}</td>
|
|
<td className="px-4 py-3 text-muted-foreground">{fmtDate(j.finished_at)}</td>
|
|
<td className="px-4 py-3">{j.articles_found}</td>
|
|
<td className="px-4 py-3 text-destructive max-w-xs truncate">{j.error_msg || '—'}</td>
|
|
</tr>
|
|
))}
|
|
{jobs.length === 0 && (
|
|
<tr><td colSpan={6} className="px-4 py-8 text-center text-muted-foreground">Aucun job</td></tr>
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|