| 1da9874 | | | 1 | import type { Metadata } from "next"; |
| 1da9874 | | | 2 | import Link from "next/link"; |
| 73fdc9e | | | 3 | import { cookies } from "next/headers"; |
| a33b2b6 | | | 4 | import { headers } from "next/headers"; |
| a011f1e | | | 5 | import { groveApiUrl } from "@/lib/utils"; |
| 1da9874 | | | 6 | import { Badge } from "@/app/components/ui/badge"; |
| a011f1e | | | 7 | import { TimeAgo } from "@/app/components/time-ago"; |
| 73fdc9e | | | 8 | import { CanopyLogo } from "@/app/components/canopy-logo"; |
| 9d879c0 | | | 9 | import { |
| 9d879c0 | | | 10 | formatTriggerType, |
| 9d879c0 | | | 11 | getInvocationRunSlug, |
| 9d879c0 | | | 12 | getInvocationStatus, |
| 9d879c0 | | | 13 | groupByInvocation, |
| 9d879c0 | | | 14 | } from "@/lib/canopy-invocations"; |
| 5bcd5db | | | 15 | import { CanopyLiveRefresh } from "@/app/components/canopy-live-refresh"; |
| 1da9874 | | | 16 | |
| 1da9874 | | | 17 | export const metadata: Metadata = { |
| 1da9874 | | | 18 | title: "Recent Builds", |
| 1da9874 | | | 19 | }; |
| 1da9874 | | | 20 | |
| 1da9874 | | | 21 | interface Run { |
| 1da9874 | | | 22 | id: number; |
| 1da9874 | | | 23 | pipeline_name: string; |
| 1da9874 | | | 24 | status: string; |
| 9d879c0 | | | 25 | trigger_type?: string | null; |
| 1da9874 | | | 26 | commit_id: string | null; |
| 1da9874 | | | 27 | commit_message: string | null; |
| 9d879c0 | | | 28 | trigger_ref?: string | null; |
| 9d879c0 | | | 29 | started_at?: string | null; |
| 9d879c0 | | | 30 | duration_ms?: number | null; |
| 1da9874 | | | 31 | created_at: string; |
| 1da9874 | | | 32 | repo_name: string; |
| 1da9874 | | | 33 | owner_name: string; |
| 1da9874 | | | 34 | } |
| 1da9874 | | | 35 | |
| 1da9874 | | | 36 | const statusLabels: Record<string, string> = { |
| 1da9874 | | | 37 | pending: "Pending", |
| 1da9874 | | | 38 | running: "Running", |
| 1da9874 | | | 39 | passed: "Passed", |
| 1da9874 | | | 40 | failed: "Failed", |
| 1da9874 | | | 41 | cancelled: "Cancelled", |
| 1da9874 | | | 42 | }; |
| 1da9874 | | | 43 | |
| 73fdc9e | | | 44 | interface RecentRunsResult { |
| 73fdc9e | | | 45 | runs: Run[]; |
| 73fdc9e | | | 46 | unauthorized: boolean; |
| 73fdc9e | | | 47 | } |
| 73fdc9e | | | 48 | |
| 73fdc9e | | | 49 | async function getRecentRuns(): Promise<RecentRunsResult> { |
| 1da9874 | | | 50 | try { |
| 721afa6 | | | 51 | const res = await fetch(`${groveApiUrl}/api/canopy/recent-runs?per_repo=20`, { |
| 1da9874 | | | 52 | cache: "no-store", |
| 1da9874 | | | 53 | }); |
| 73fdc9e | | | 54 | if (res.status === 401) { |
| 73fdc9e | | | 55 | return { runs: [], unauthorized: true }; |
| 73fdc9e | | | 56 | } |
| 73fdc9e | | | 57 | if (!res.ok) return { runs: [], unauthorized: false }; |
| 1da9874 | | | 58 | const data = await res.json(); |
| 73fdc9e | | | 59 | return { runs: data.runs ?? [], unauthorized: false }; |
| 1da9874 | | | 60 | } catch { |
| 73fdc9e | | | 61 | return { runs: [], unauthorized: false }; |
| 1da9874 | | | 62 | } |
| 1da9874 | | | 63 | } |
| 1da9874 | | | 64 | |
| 9d879c0 | | | 65 | function groupByRepo(runs: Run[]): { key: string; owner: string; repo: string; runs: Run[]; newestRunId: number }[] { |
| 9d879c0 | | | 66 | const groups: Map<string, { owner: string; repo: string; runs: Run[]; newestRunId: number }> = new Map(); |
| 9d879c0 | | | 67 | const sorted = [...runs].sort((a, b) => b.id - a.id); |
| 9d879c0 | | | 68 | for (const run of sorted) { |
| a011f1e | | | 69 | const key = `${run.owner_name}/${run.repo_name}`; |
| a011f1e | | | 70 | let group = groups.get(key); |
| a011f1e | | | 71 | if (!group) { |
| 9d879c0 | | | 72 | group = { owner: run.owner_name, repo: run.repo_name, runs: [], newestRunId: run.id }; |
| a011f1e | | | 73 | groups.set(key, group); |
| a011f1e | | | 74 | } |
| a011f1e | | | 75 | group.runs.push(run); |
| a011f1e | | | 76 | } |
| 9d879c0 | | | 77 | return Array.from(groups, ([key, g]) => ({ key, ...g })).sort((a, b) => b.newestRunId - a.newestRunId); |
| a011f1e | | | 78 | } |
| a011f1e | | | 79 | |
| 1da9874 | | | 80 | export default async function CanopyHomePage() { |
| 73fdc9e | | | 81 | const cookieStore = await cookies(); |
| 73fdc9e | | | 82 | const signedIn = cookieStore.has("grove_hub_token"); |
| a33b2b6 | | | 83 | const headerStore = await headers(); |
| a33b2b6 | | | 84 | const host = |
| a33b2b6 | | | 85 | headerStore.get("x-forwarded-host") ?? |
| a33b2b6 | | | 86 | headerStore.get("host") ?? |
| a33b2b6 | | | 87 | ""; |
| a33b2b6 | | | 88 | const protocol = headerStore.get("x-forwarded-proto") ?? "http"; |
| a33b2b6 | | | 89 | const canonicalHost = host.split(",")[0]?.trim() ?? ""; |
| a33b2b6 | | | 90 | const groveHost = canonicalHost.replace(/^(canopy|ring)\./, ""); |
| a33b2b6 | | | 91 | const groveOrigin = groveHost ? `${protocol}://${groveHost}` : ""; |
| a33b2b6 | | | 92 | const loginHref = groveOrigin ? `${groveOrigin}/login` : "/login"; |
| a33b2b6 | | | 93 | const exploreHref = groveOrigin ? `${groveOrigin}/` : "/"; |
| 73fdc9e | | | 94 | |
| 73fdc9e | | | 95 | if (!signedIn) { |
| 73fdc9e | | | 96 | return ( |
| 73fdc9e | | | 97 | <div className="max-w-3xl mx-auto px-4 py-8"> |
| 73fdc9e | | | 98 | <div |
| 8a2c7d4 | | | 99 | className="p-4 sm:p-8 text-center" |
| 73fdc9e | | | 100 | style={{ |
| 73fdc9e | | | 101 | backgroundColor: "var(--bg-card)", |
| 73fdc9e | | | 102 | border: "1px solid var(--border-subtle)", |
| 73fdc9e | | | 103 | }} |
| 73fdc9e | | | 104 | > |
| 73fdc9e | | | 105 | <div className="mx-auto mb-4 w-fit opacity-70"> |
| 73fdc9e | | | 106 | <CanopyLogo size={44} /> |
| 73fdc9e | | | 107 | </div> |
| 73fdc9e | | | 108 | <h1 className="text-lg mb-1">Canopy</h1> |
| 73fdc9e | | | 109 | <p className="text-sm mb-4" style={{ color: "var(--text-faint)" }}> |
| 73fdc9e | | | 110 | Build and track workflows for your Grove repositories. |
| 73fdc9e | | | 111 | </p> |
| 73fdc9e | | | 112 | <div className="flex items-center justify-center gap-2"> |
| 73fdc9e | | | 113 | <Link |
| a33b2b6 | | | 114 | href={loginHref} |
| 73fdc9e | | | 115 | className="px-3 py-1.5 text-sm" |
| 73fdc9e | | | 116 | style={{ |
| 73fdc9e | | | 117 | backgroundColor: "var(--accent)", |
| 73fdc9e | | | 118 | color: "var(--accent-text)", |
| 73fdc9e | | | 119 | }} |
| 73fdc9e | | | 120 | > |
| 73fdc9e | | | 121 | Sign in |
| 73fdc9e | | | 122 | </Link> |
| 73fdc9e | | | 123 | <Link |
| a33b2b6 | | | 124 | href={exploreHref} |
| 73fdc9e | | | 125 | className="px-3 py-1.5 text-sm" |
| 73fdc9e | | | 126 | style={{ |
| 73fdc9e | | | 127 | border: "1px solid var(--border-subtle)", |
| 73fdc9e | | | 128 | color: "var(--text-secondary)", |
| 73fdc9e | | | 129 | }} |
| 73fdc9e | | | 130 | > |
| 73fdc9e | | | 131 | Explore Grove |
| 73fdc9e | | | 132 | </Link> |
| 73fdc9e | | | 133 | </div> |
| 73fdc9e | | | 134 | </div> |
| 73fdc9e | | | 135 | </div> |
| 73fdc9e | | | 136 | ); |
| 73fdc9e | | | 137 | } |
| 73fdc9e | | | 138 | |
| 73fdc9e | | | 139 | const { runs, unauthorized } = await getRecentRuns(); |
| 73fdc9e | | | 140 | |
| 73fdc9e | | | 141 | if (unauthorized) { |
| 73fdc9e | | | 142 | return ( |
| 73fdc9e | | | 143 | <div className="max-w-3xl mx-auto px-4 py-8"> |
| 73fdc9e | | | 144 | <div |
| 8a2c7d4 | | | 145 | className="p-4 sm:p-8 text-center" |
| 73fdc9e | | | 146 | style={{ |
| 73fdc9e | | | 147 | backgroundColor: "var(--bg-card)", |
| 73fdc9e | | | 148 | border: "1px solid var(--border-subtle)", |
| 73fdc9e | | | 149 | }} |
| 73fdc9e | | | 150 | > |
| 73fdc9e | | | 151 | <div className="mx-auto mb-4 w-fit opacity-70"> |
| 73fdc9e | | | 152 | <CanopyLogo size={44} /> |
| 73fdc9e | | | 153 | </div> |
| 73fdc9e | | | 154 | <h1 className="text-lg mb-1">Canopy</h1> |
| 73fdc9e | | | 155 | <p className="text-sm mb-4" style={{ color: "var(--text-faint)" }}> |
| 73fdc9e | | | 156 | Build and track workflows for your Grove repositories. |
| 73fdc9e | | | 157 | </p> |
| 73fdc9e | | | 158 | <div className="flex items-center justify-center gap-2"> |
| 73fdc9e | | | 159 | <Link |
| a33b2b6 | | | 160 | href={loginHref} |
| 73fdc9e | | | 161 | className="px-3 py-1.5 text-sm" |
| 73fdc9e | | | 162 | style={{ |
| 73fdc9e | | | 163 | backgroundColor: "var(--accent)", |
| 73fdc9e | | | 164 | color: "var(--accent-text)", |
| 73fdc9e | | | 165 | }} |
| 73fdc9e | | | 166 | > |
| 73fdc9e | | | 167 | Sign in |
| 73fdc9e | | | 168 | </Link> |
| 73fdc9e | | | 169 | <Link |
| a33b2b6 | | | 170 | href={exploreHref} |
| 73fdc9e | | | 171 | className="px-3 py-1.5 text-sm" |
| 73fdc9e | | | 172 | style={{ |
| 73fdc9e | | | 173 | border: "1px solid var(--border-subtle)", |
| 73fdc9e | | | 174 | color: "var(--text-secondary)", |
| 73fdc9e | | | 175 | }} |
| 73fdc9e | | | 176 | > |
| 73fdc9e | | | 177 | Explore Grove |
| 73fdc9e | | | 178 | </Link> |
| 73fdc9e | | | 179 | </div> |
| 73fdc9e | | | 180 | </div> |
| 73fdc9e | | | 181 | </div> |
| 73fdc9e | | | 182 | ); |
| 73fdc9e | | | 183 | } |
| 1da9874 | | | 184 | |
| 1da9874 | | | 185 | if (runs.length === 0) { |
| 1da9874 | | | 186 | return ( |
| 1da9874 | | | 187 | <div className="max-w-3xl mx-auto px-4 py-6"> |
| 1da9874 | | | 188 | <p |
| 1da9874 | | | 189 | className="text-sm py-8 text-center" |
| 1da9874 | | | 190 | style={{ color: "var(--text-faint)" }} |
| 1da9874 | | | 191 | > |
| 087adca | | | 192 | No builds yet. |
| 1da9874 | | | 193 | </p> |
| 1da9874 | | | 194 | </div> |
| 1da9874 | | | 195 | ); |
| 1da9874 | | | 196 | } |
| da0f651 | | | 197 | |
| a011f1e | | | 198 | const repos = groupByRepo(runs); |
| a011f1e | | | 199 | |
| da0f651 | | | 200 | return ( |
| a011f1e | | | 201 | <div className="max-w-3xl mx-auto px-4 py-6 flex flex-col gap-6"> |
| 5bcd5db | | | 202 | <CanopyLiveRefresh scope="global" /> |
| a011f1e | | | 203 | {repos.map((group) => ( |
| a011f1e | | | 204 | <div key={group.key}> |
| a011f1e | | | 205 | <div className="mb-2"> |
| a011f1e | | | 206 | <Link |
| a011f1e | | | 207 | href={`/${group.owner}/${group.repo}`} |
| a011f1e | | | 208 | className="text-sm font-medium hover:underline" |
| a011f1e | | | 209 | style={{ color: "var(--text-primary)" }} |
| 1da9874 | | | 210 | > |
| a011f1e | | | 211 | {group.owner}/{group.repo} |
| a011f1e | | | 212 | </Link> |
| a011f1e | | | 213 | </div> |
| 8a2c7d4 | | | 214 | <div> |
| 8a2c7d4 | | | 215 | {(() => { |
| 8a2c7d4 | | | 216 | const invocations = groupByInvocation(group.runs); |
| 8a2c7d4 | | | 217 | return invocations.slice(0, 5).map((invocation) => { |
| 9d879c0 | | | 218 | const title = |
| 9d879c0 | | | 219 | invocation.commitMessage || |
| 9d879c0 | | | 220 | (invocation.commitId |
| 9d879c0 | | | 221 | ? invocation.commitId.substring(0, 8) |
| 9d879c0 | | | 222 | : `${formatTriggerType(invocation.triggerType)} build`); |
| 9d879c0 | | | 223 | |
| 9d879c0 | | | 224 | const summaryStatus = getInvocationStatus(invocation.runs); |
| 9d879c0 | | | 225 | const newestRun = invocation.runs[invocation.runs.length - 1]; |
| 8a2c7d4 | | | 226 | const invocationHref = `/${group.owner}/${group.repo}/runs/${getInvocationRunSlug(invocation, invocations)}`; |
| 9d879c0 | | | 227 | |
| 9d879c0 | | | 228 | return ( |
| 8a2c7d4 | | | 229 | <Link |
| 8a2c7d4 | | | 230 | key={invocation.key} |
| 8a2c7d4 | | | 231 | href={invocationHref} |
| 8a2c7d4 | | | 232 | className="flex items-start gap-2 sm:gap-3 py-2.5 px-1 hover-row" |
| 8a2c7d4 | | | 233 | style={{ borderBottom: "1px solid var(--divide)", textDecoration: "none", color: "inherit" }} |
| 8a2c7d4 | | | 234 | > |
| 8a2c7d4 | | | 235 | <span className="shrink-0 mt-0.5 sm:hidden"> |
| 8a2c7d4 | | | 236 | <Badge variant={summaryStatus} compact> |
| 8a2c7d4 | | | 237 | {(statusLabels[summaryStatus] ?? summaryStatus).charAt(0)} |
| 8a2c7d4 | | | 238 | </Badge> |
| 8a2c7d4 | | | 239 | </span> |
| 8a2c7d4 | | | 240 | <span className="shrink-0 mt-0.5 hidden sm:block"> |
| 8a2c7d4 | | | 241 | <Badge |
| 8a2c7d4 | | | 242 | variant={summaryStatus} |
| 8a2c7d4 | | | 243 | style={{ minWidth: "4rem", textAlign: "center" }} |
| 8a2c7d4 | | | 244 | > |
| 8a2c7d4 | | | 245 | {statusLabels[summaryStatus] ?? summaryStatus} |
| 8a2c7d4 | | | 246 | </Badge> |
| 8a2c7d4 | | | 247 | </span> |
| 8a2c7d4 | | | 248 | <div className="min-w-0 flex-1"> |
| 8a2c7d4 | | | 249 | <div className="text-sm truncate" style={{ color: "var(--text-primary)" }}> |
| 8a2c7d4 | | | 250 | {title} |
| 8a2c7d4 | | | 251 | </div> |
| 8a2c7d4 | | | 252 | <div className="flex items-center gap-2 mt-0.5 text-xs" style={{ color: "var(--text-faint)" }}> |
| 8a2c7d4 | | | 253 | <span>{formatTriggerType(invocation.triggerType)}</span> |
| 8a2c7d4 | | | 254 | {invocation.commitId && ( |
| 8a2c7d4 | | | 255 | <span className="font-mono">{invocation.commitId.substring(0, 8)}</span> |
| 8a2c7d4 | | | 256 | )} |
| 8a2c7d4 | | | 257 | <span> |
| 8a2c7d4 | | | 258 | {invocation.runs.length} workflow{invocation.runs.length === 1 ? "" : "s"} |
| 8a2c7d4 | | | 259 | </span> |
| 8a2c7d4 | | | 260 | <span className="ml-auto shrink-0"> |
| 8a2c7d4 | | | 261 | <TimeAgo date={newestRun.created_at} /> |
| 8a2c7d4 | | | 262 | </span> |
| 8a2c7d4 | | | 263 | </div> |
| 8a2c7d4 | | | 264 | </div> |
| 8a2c7d4 | | | 265 | </Link> |
| 9d879c0 | | | 266 | ); |
| 8a2c7d4 | | | 267 | }); |
| 8a2c7d4 | | | 268 | })()} |
| 8a2c7d4 | | | 269 | </div> |
| a011f1e | | | 270 | <div className="pt-1.5"> |
| a011f1e | | | 271 | <Link |
| a011f1e | | | 272 | href={`/${group.owner}/${group.repo}`} |
| a011f1e | | | 273 | className="text-xs hover:underline" |
| a011f1e | | | 274 | style={{ color: "var(--text-muted)" }} |
| a011f1e | | | 275 | > |
| 087adca | | | 276 | View all builds |
| a011f1e | | | 277 | </Link> |
| a011f1e | | | 278 | </div> |
| a011f1e | | | 279 | </div> |
| a011f1e | | | 280 | ))} |
| da0f651 | | | 281 | </div> |
| da0f651 | | | 282 | ); |
| da0f651 | | | 283 | } |