page.tsx167 lines, 5.7 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { Button } from "@/components/ui/button"; |
| 4 | import { Card } from "@/components/ui/card"; |
| 5 | import { |
| 6 | Select, |
| 7 | SelectContent, |
| 8 | SelectItem, |
| 9 | SelectTrigger, |
| 10 | SelectValue, |
| 11 | } from "@/components/ui/select"; |
| 12 | import { Skeleton } from "@/components/ui/skeleton"; |
| 13 | import { useAllJobs, useCancelJob } from "@/lib/hooks/queries/videos"; |
| 14 | import { AlertCircle, FileText, LayoutGrid } from "lucide-react"; |
| 15 | import Link from "next/link"; |
| 16 | import { useState } from "react"; |
| 17 | import { JobCard } from "../components/job-card"; |
| 18 | |
| 19 | function JobCardSkeleton() { |
| 20 | return ( |
| 21 | <Card className="overflow-hidden border-l-4 border-muted"> |
| 22 | <div className="p-4 sm:p-6"> |
| 23 | <div className="flex flex-col sm:flex-row items-start gap-4"> |
| 24 | <Skeleton className="flex-shrink-0 w-full sm:w-32 h-20 rounded-md" /> |
| 25 | <div className="flex-1 min-w-0 space-y-2"> |
| 26 | <div className="flex justify-between items-start gap-2"> |
| 27 | <Skeleton className="h-5 w-3/4" /> |
| 28 | <Skeleton className="h-4 w-16" /> |
| 29 | </div> |
| 30 | <Skeleton className="h-4 w-full" /> |
| 31 | <Skeleton className="h-4 w-5/6" /> |
| 32 | <div className="flex gap-3 pt-2"> |
| 33 | <Skeleton className="h-3 w-20" /> |
| 34 | <Skeleton className="h-3 w-20" /> |
| 35 | </div> |
| 36 | <div className="flex justify-end pt-2"> |
| 37 | <Skeleton className="h-8 w-24" /> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | </div> |
| 42 | </Card> |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | export default function HistoryPage() { |
| 47 | const [filter, setFilter] = useState<string>("all"); |
| 48 | const { data, isLoading, error, refetch } = useAllJobs(); |
| 49 | const cancelJobMutation = useCancelJob(); |
| 50 | |
| 51 | if (error) { |
| 52 | return ( |
| 53 | <div className="flex flex-col items-center justify-center min-h-[400px] text-center p-6 bg-red-50 border border-red-200 rounded-lg shadow-sm"> |
| 54 | <AlertCircle className="size-12 text-red-500 mb-4" /> |
| 55 | <h2 className="text-xl font-semibold text-red-700 mb-2"> |
| 56 | Oops! Something went wrong. |
| 57 | </h2> |
| 58 | <p className="text-red-600 mb-6 max-w-md"> |
| 59 | We couldn't load your job history at the moment. Please try again. |
| 60 | </p> |
| 61 | <Button |
| 62 | onClick={() => refetch()} |
| 63 | variant={"destructive"} |
| 64 | className="gap-2" |
| 65 | > |
| 66 | <AlertCircle className="size-4" /> |
| 67 | Try Again |
| 68 | </Button> |
| 69 | </div> |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | if (isLoading || !data) { |
| 74 | return ( |
| 75 | <div className="space-y-6"> |
| 76 | <div className="flex items-center justify-between"> |
| 77 | <div className="space-y-1.5"> |
| 78 | <Skeleton className="h-7 w-48" /> |
| 79 | <Skeleton className="h-4 w-64" /> |
| 80 | </div> |
| 81 | <Skeleton className="h-10 w-[180px]" /> |
| 82 | </div> |
| 83 | <div className="grid grid-cols-1 lg:grid-cols-2 gap-4"> |
| 84 | {Array.from({ length: 4 }).map((_, i) => ( |
| 85 | <JobCardSkeleton key={i} /> |
| 86 | ))} |
| 87 | </div> |
| 88 | </div> |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | const filteredJobs = |
| 93 | data.jobs?.filter((job) => { |
| 94 | if (filter === "all") return true; |
| 95 | if (filter === "active") |
| 96 | return job.state === "active" || job.state === "waiting"; |
| 97 | return job.state === filter; |
| 98 | }) || []; |
| 99 | |
| 100 | if (!filteredJobs.length && filter === "all") { |
| 101 | return ( |
| 102 | <div className="flex flex-col items-center justify-center min-h-[400px] text-center p-6 bg-gray-50 border border-dashed border-gray-300 rounded-lg shadow-sm"> |
| 103 | <FileText className="size-12 text-gray-400 mb-4" /> |
| 104 | <h2 className="text-xl font-semibold text-gray-700 mb-2"> |
| 105 | No Job History Yet |
| 106 | </h2> |
| 107 | <p className="text-muted-foreground mb-6 max-w-md"> |
| 108 | It looks like you haven't created any video processing jobs. Get |
| 109 | started by creating your first one! |
| 110 | </p> |
| 111 | <Button asChild size="lg"> |
| 112 | <Link href={"/dashboard/create"} className="gap-2"> |
| 113 | <LayoutGrid className="size-4" /> |
| 114 | Create New Job |
| 115 | </Link> |
| 116 | </Button> |
| 117 | </div> |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | return ( |
| 122 | <div className="space-y-6"> |
| 123 | <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4"> |
| 124 | <div> |
| 125 | <h1 className="text-2xl font-bold tracking-tight">Job History</h1> |
| 126 | <p className="text-muted-foreground"> |
| 127 | Review and manage your past video processing tasks. |
| 128 | </p> |
| 129 | </div> |
| 130 | <Select value={filter} onValueChange={setFilter}> |
| 131 | <SelectTrigger className="w-full sm:w-[200px]"> |
| 132 | <SelectValue placeholder="Filter by status" /> |
| 133 | </SelectTrigger> |
| 134 | <SelectContent> |
| 135 | <SelectItem value="all">All Jobs</SelectItem> |
| 136 | <SelectItem value="active">Active (Processing/Queued)</SelectItem> |
| 137 | <SelectItem value="completed">Completed Jobs</SelectItem> |
| 138 | <SelectItem value="failed">Failed Jobs</SelectItem> |
| 139 | <SelectItem value="delayed">Delayed Jobs</SelectItem> |
| 140 | </SelectContent> |
| 141 | </Select> |
| 142 | </div> |
| 143 | |
| 144 | {filteredJobs.length > 0 ? ( |
| 145 | <div className="grid grid-cols-1 lg:grid-cols-2 gap-4"> |
| 146 | {filteredJobs.map((job) => ( |
| 147 | <JobCard |
| 148 | key={job.id} |
| 149 | job={{ |
| 150 | ...job, |
| 151 | thumbnail: job.thumbnail || undefined, |
| 152 | }} |
| 153 | onCancel={() => cancelJobMutation.mutate(job.id as string)} |
| 154 | /> |
| 155 | ))} |
| 156 | </div> |
| 157 | ) : ( |
| 158 | <div className="flex flex-col items-center justify-center min-h-[250px] text-center p-6 bg-gray-50/50 border border-dashed border-gray-200 rounded-lg"> |
| 159 | <FileText className="size-10 text-gray-400 mb-3" /> |
| 160 | <p className="text-muted-foreground"> |
| 161 | No jobs found matching the filter "{filter}". |
| 162 | </p> |
| 163 | </div> |
| 164 | )} |
| 165 | </div> |
| 166 | ); |
| 167 | } |