|
| 1 | +import Link from "next/link"; |
| 2 | +import { ENGINES, type Engine } from "@/lib/credits"; |
| 3 | +import { ScoreBadge } from "@/components/score-badge"; |
| 4 | + |
| 5 | +export type PremiumSibling = { |
| 6 | + id: string; |
| 7 | + engine: Engine; |
| 8 | + status: string; |
| 9 | + score: number | null; |
| 10 | + share_token: string | null; |
| 11 | + failed_reason: string | null; |
| 12 | + completed_at: string | null; |
| 13 | + summary: { pass?: number; warn?: number; fail?: number } | null; |
| 14 | +}; |
| 15 | + |
| 16 | +export function PremiumEngines({ siblings }: { siblings: PremiumSibling[] }) { |
| 17 | + const completed = siblings.filter( |
| 18 | + (s) => s.status === "complete" && s.score !== null, |
| 19 | + ); |
| 20 | + const avgScore = |
| 21 | + completed.length > 0 |
| 22 | + ? Math.round( |
| 23 | + completed.reduce((s, r) => s + (r.score ?? 0), 0) / completed.length, |
| 24 | + ) |
| 25 | + : null; |
| 26 | + const runningCount = siblings.filter( |
| 27 | + (s) => s.status === "queued" || s.status === "running", |
| 28 | + ).length; |
| 29 | + |
| 30 | + return ( |
| 31 | + <div className="space-y-6"> |
| 32 | + <div className="card flex flex-wrap items-center justify-between gap-3 p-5"> |
| 33 | + <div> |
| 34 | + <div className="text-xs font-semibold uppercase tracking-wider text-[var(--color-accent)]"> |
| 35 | + Premium |
| 36 | + </div> |
| 37 | + <h3 className="mt-1 text-lg font-bold">AI engine reports</h3> |
| 38 | + <p className="mt-1 text-sm text-[var(--color-muted)]"> |
| 39 | + {completed.length} of {siblings.length} engine |
| 40 | + {siblings.length === 1 ? "" : "s"} complete |
| 41 | + {avgScore !== null && ` · avg score ${avgScore}/100`} |
| 42 | + {runningCount > 0 && ` · ${runningCount} still running`} |
| 43 | + </p> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + |
| 47 | + <div className="grid gap-3 md:grid-cols-2"> |
| 48 | + {siblings.map((a) => ( |
| 49 | + <EngineCard key={a.id} audit={a} /> |
| 50 | + ))} |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +function EngineCard({ audit }: { audit: PremiumSibling }) { |
| 57 | + const meta = ENGINES[audit.engine]; |
| 58 | + const pending = audit.status === "queued" || audit.status === "running"; |
| 59 | + return ( |
| 60 | + <div className="card p-4"> |
| 61 | + <div className="flex items-start justify-between gap-3"> |
| 62 | + <div className="min-w-0"> |
| 63 | + <div className="flex items-center gap-2"> |
| 64 | + <h3 className="truncate text-base font-semibold"> |
| 65 | + {meta?.label ?? audit.engine} |
| 66 | + </h3> |
| 67 | + {meta && ( |
| 68 | + <span className="text-xs text-[var(--color-muted)]"> |
| 69 | + {meta.cost === 0 |
| 70 | + ? "free" |
| 71 | + : `${meta.cost} credit${meta.cost === 1 ? "" : "s"}`} |
| 72 | + </span> |
| 73 | + )} |
| 74 | + </div> |
| 75 | + <p className="mt-0.5 truncate text-xs text-[var(--color-muted)]"> |
| 76 | + {meta?.blurb ?? ""} |
| 77 | + </p> |
| 78 | + </div> |
| 79 | + <div className="flex shrink-0 items-center gap-2"> |
| 80 | + {pending && ( |
| 81 | + <span |
| 82 | + aria-hidden |
| 83 | + className="inline-block h-3 w-3 animate-spin rounded-full border-2 border-[var(--color-border)] border-t-[var(--color-accent)]" |
| 84 | + /> |
| 85 | + )} |
| 86 | + <ScoreBadge score={audit.score} status={audit.status} /> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + |
| 90 | + {audit.status === "complete" && audit.summary && ( |
| 91 | + <div className="mt-3 flex gap-3 text-xs text-[var(--color-muted)]"> |
| 92 | + <span> |
| 93 | + <span className="font-semibold text-[var(--color-pass)]"> |
| 94 | + {audit.summary.pass ?? 0} |
| 95 | + </span>{" "} |
| 96 | + pass |
| 97 | + </span> |
| 98 | + <span> |
| 99 | + <span className="font-semibold text-[var(--color-warn)]"> |
| 100 | + {audit.summary.warn ?? 0} |
| 101 | + </span>{" "} |
| 102 | + warn |
| 103 | + </span> |
| 104 | + <span> |
| 105 | + <span className="font-semibold text-[var(--color-fail)]"> |
| 106 | + {audit.summary.fail ?? 0} |
| 107 | + </span>{" "} |
| 108 | + fail |
| 109 | + </span> |
| 110 | + </div> |
| 111 | + )} |
| 112 | + |
| 113 | + {audit.status === "failed" && audit.failed_reason && ( |
| 114 | + <p className="mt-3 break-words text-xs text-[var(--color-fail)]"> |
| 115 | + {audit.failed_reason} |
| 116 | + </p> |
| 117 | + )} |
| 118 | + |
| 119 | + <div className="mt-3 flex items-center gap-3 text-xs"> |
| 120 | + {audit.status === "complete" && ( |
| 121 | + <Link |
| 122 | + href={`/audits/${audit.id}`} |
| 123 | + className="text-[var(--color-accent)] hover:underline" |
| 124 | + > |
| 125 | + View report → |
| 126 | + </Link> |
| 127 | + )} |
| 128 | + {audit.share_token && audit.status === "complete" && ( |
| 129 | + <Link |
| 130 | + href={`/r/${audit.share_token}`} |
| 131 | + className="text-[var(--color-muted)] hover:underline" |
| 132 | + > |
| 133 | + Share link |
| 134 | + </Link> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + ); |
| 139 | +} |
0 commit comments