"use client"; import { useState, useEffect } from "react"; import { cmsApi, BackgroundTask } from "@/lib/api"; import { Loader2, RefreshCw, XCircle, PlayCircle } from "lucide-react"; import { format } from "date-fns"; import ProtectedRoute from "@/components/AuthGuard"; export default function BackgroundTasksPage() { const [tasks, setTasks] = useState([]); const [loading, setLoading] = useState(true); const [actionLoading, setActionLoading] = useState(null); const fetchTasks = async () => { try { const data = await cmsApi.getBackgroundTasks(); setTasks(data); } catch (error) { console.error(error); } finally { setLoading(false); } }; useEffect(() => { fetchTasks(); const interval = setInterval(fetchTasks, 5000); // Poll every 5 seconds return () => clearInterval(interval); }, []); const handleRetry = async (id: string) => { setActionLoading(id); try { await cmsApi.retryTask(id); await fetchTasks(); } catch (error) { console.error("Failed to retry task", error); } finally { setActionLoading(null); } }; const handleCancel = async (id: string) => { if (!confirm("Are you sure you want to cancel this task? It will be removed from the queue.")) return; setActionLoading(id); try { await cmsApi.cancelTask(id); await fetchTasks(); } catch (error) { console.error("Failed to cancel task", error); } finally { setActionLoading(null); } }; const getStatusBadge = (task: BackgroundTask) => { const status = task.status; const progress = task.progress; switch (status) { case 'processing': return (
Processing {task.task_type !== 'lesson_transcription' && (
{progress}% completo
)}
); case 'queued': return Queued; case 'failed': case 'error': return Failed; case 'completed': return Completed; case 'idle': return Idle; default: return {status}; } }; const getTaskTypeBadge = (type: string) => { let label = 'Unknown'; let color = 'bg-slate-100 text-slate-800'; switch (type) { case 'lesson_transcription': label = 'Transcription'; color = 'bg-purple-100 text-purple-800'; break; case 'lesson_image': label = 'Lesson Image'; color = 'bg-blue-100 text-blue-800'; break; case 'course_image': label = 'Course Cover'; color = 'bg-emerald-100 text-emerald-800'; break; } return {label}; }; return (

Background Tasks

Monitor and manage asynchronous processing jobs and AI transcriptions.

{loading && tasks.length === 0 ? (
) : tasks.length === 0 ? (
🌱

All Clear

There are no pending or stuck background tasks at the moment.

) : (
{tasks.map((task) => ( ))}
Task / Context Type Status Last Updated Actions
{task.title}
{task.course_title || 'Unknown Course'}
{task.id}
{getTaskTypeBadge(task.task_type)} {getStatusBadge(task)} {format(new Date(task.updated_at), 'MMM d, h:mm a')}
({format(new Date(task.updated_at), 'yyyy')})
{task.status === 'failed' && ( )}
)}
); }