Something went wrong. Try again.
because I got bored of customising my CV for every job
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216import { Badge, Card, StatusDot } from "@cv/ui";import { useState } from "react";import { type AdminAsyncJobsQuery, AsyncJobStatus } from "@/generated/graphql";
type Connection = AdminAsyncJobsQuery["adminAsyncJobs"];type Job = Connection["edges"][number]["node"];
type DotColor = "green" | "yellow" | "red" | "gray";
const statusDotColor: Record<AsyncJobStatus, DotColor> = { [AsyncJobStatus.Completed]: "green", [AsyncJobStatus.Failed]: "red", [AsyncJobStatus.Pending]: "yellow",};
const statusBadgeColor: Record<AsyncJobStatus, string> = { [AsyncJobStatus.Completed]: "ctp-green", [AsyncJobStatus.Failed]: "ctp-red", [AsyncJobStatus.Pending]: "ctp-yellow",};
const formatRelativeTime = (isoString: string | null | undefined): string => { if (!isoString) { return "-"; } const diff = Date.now() - new Date(isoString).getTime(); const seconds = Math.floor(diff / 1000);
if (seconds < 60) { return `${seconds}s ago`; } const minutes = Math.floor(seconds / 60); if (minutes < 60) { return `${minutes}m ago`; } const hours = Math.floor(minutes / 60); if (hours < 24) { return `${hours}h ago`; } return `${Math.floor(hours / 24)}d ago`;};
const formatAbsoluteTime = (isoString: string | null | undefined): string => isoString ? new Date(isoString).toLocaleString() : "-";
const formatDuration = (ms: number | null | undefined): string => { if (ms == null) { return "-"; } if (ms < 1000) { return `${Math.round(ms)}ms`; } if (ms < 60_000) { return `${(ms / 1000).toFixed(1)}s`; } const minutes = Math.floor(ms / 60_000); const seconds = Math.round((ms % 60_000) / 1000); return `${minutes}m ${seconds}s`;};
const truncate = (s: string, len = 12): string => s.length <= len ? s : `${s.slice(0, len)}...`;
const JobRow = ({ job, isExpanded, onToggle,}: { job: Job; isExpanded: boolean; onToggle: () => void;}) => ( <> <tr className="border-b border-ctp-surface1 last:border-b-0 hover:bg-ctp-surface0/50 cursor-pointer" onClick={onToggle} > <td className="py-2 pr-4"> <span className="flex items-center gap-2"> <StatusDot color={statusDotColor[job.status]} /> <Badge color={statusBadgeColor[job.status] as "ctp-green"}> {job.status} </Badge> </span> </td> <td className="py-2 pr-4 font-mono text-xs text-ctp-text">{job.kind}</td> <td className="py-2 pr-4 font-mono text-xs text-ctp-subtext0" title={job.userId} > {truncate(job.userId, 10)} </td> <td className="py-2 pr-4 text-right tabular-nums text-ctp-text"> {formatDuration(job.durationMs)} </td> <td className="py-2 pr-4 text-ctp-subtext0 whitespace-nowrap"> {formatRelativeTime(job.createdAt)} </td> <td className="py-2 pr-4 text-ctp-subtext0 whitespace-nowrap"> {formatRelativeTime(job.completedAt)} </td> </tr> {isExpanded && ( <tr className="bg-ctp-surface0/30"> <td colSpan={6} className="px-4 py-3"> <div className="grid grid-cols-2 gap-x-8 gap-y-1 text-xs"> <div> <span className="text-ctp-subtext0">Job ID:</span>{" "} <span className="font-mono text-ctp-text">{job.id}</span> </div> <div> <span className="text-ctp-subtext0">User ID:</span>{" "} <span className="font-mono text-ctp-text">{job.userId}</span> </div> <div> <span className="text-ctp-subtext0">Created:</span>{" "} <span className="text-ctp-text"> {formatAbsoluteTime(job.createdAt)} </span> </div> <div> <span className="text-ctp-subtext0">Completed:</span>{" "} <span className="text-ctp-text"> {formatAbsoluteTime(job.completedAt)} </span> </div> {job.error && ( <div className="col-span-2 mt-1"> <span className="text-ctp-subtext0">Error:</span> <pre className="mt-1 whitespace-pre-wrap break-all text-ctp-red bg-ctp-surface0 rounded p-2"> {job.error} </pre> </div> )} </div> </td> </tr> )} </>);
interface AsyncJobsTableProps { connection: Connection; onLoadMore?: (cursor: string) => void; isFetchingMore?: boolean;}
export const AsyncJobsTable = ({ connection, onLoadMore, isFetchingMore,}: AsyncJobsTableProps) => { const [expandedId, setExpandedId] = useState<string | null>(null); const jobs = connection.edges.map((e) => e.node);
return ( <Card> <div className="mb-3 flex items-center justify-between"> <div> <h3 className="text-lg font-medium text-ctp-text">Async Jobs</h3> <p className="text-xs text-ctp-subtext0 mt-0.5"> {connection.totalCount} total </p> </div> </div>
{jobs.length === 0 ? ( <p className="text-sm text-ctp-subtext0">No jobs match the filters</p> ) : ( <div className="overflow-x-auto"> <table className="w-full text-sm"> <thead> <tr className="border-b border-ctp-surface1 text-left text-ctp-subtext0"> <th className="pb-2 pr-4 font-medium">Status</th> <th className="pb-2 pr-4 font-medium">Kind</th> <th className="pb-2 pr-4 font-medium">User</th> <th className="pb-2 pr-4 font-medium text-right">Duration</th> <th className="pb-2 pr-4 font-medium">Created</th> <th className="pb-2 pr-4 font-medium">Completed</th> </tr> </thead> <tbody> {jobs.map((job) => ( <JobRow key={job.id} job={job} isExpanded={expandedId === job.id} onToggle={() => setExpandedId((prev) => (prev === job.id ? null : job.id)) } /> ))} </tbody> </table> {connection.pageInfo.hasNextPage && connection.pageInfo.endCursor && onLoadMore && ( <div className="mt-3 flex justify-center"> <button type="button" disabled={isFetchingMore} onClick={() => onLoadMore(connection.pageInfo.endCursor ?? "") } className="rounded-md border border-ctp-surface1 px-3 py-1.5 text-xs text-ctp-text hover:bg-ctp-surface0 disabled:opacity-50" > {isFetchingMore ? "Loading..." : "Load more"} </button> </div> )} </div> )} </Card> );};