| 1 | import type { Metadata } from "next"; |
| 2 | import { cookies } from "next/headers"; |
| 3 | import { headers } from "next/headers"; |
| 4 | import Link from "next/link"; |
| 5 | import { groveApiUrl } from "@/lib/utils"; |
| 6 | import { CollabLogo } from "@/app/components/collab-logo"; |
| 7 | import { CollabRepoList } from "./collab-repo-list"; |
| 8 | |
| 9 | export const metadata: Metadata = { |
| 10 | title: "Collab", |
| 11 | }; |
| 12 | |
| 13 | interface Repo { |
| 14 | name: string; |
| 15 | owner_name: string; |
| 16 | description: string | null; |
| 17 | last_commit_ts: number | null; |
| 18 | updated_at: string | null; |
| 19 | } |
| 20 | |
| 21 | async function getRepos(token: string | undefined): Promise<Repo[]> { |
| 22 | try { |
| 23 | const headers: Record<string, string> = token |
| 24 | ? { Authorization: `Bearer ${token}` } |
| 25 | : {}; |
| 26 | const res = await fetch(`${groveApiUrl}/api/repos`, { |
| 27 | headers, |
| 28 | cache: "no-store", |
| 29 | }); |
| 30 | if (!res.ok) return []; |
| 31 | const data = await res.json(); |
| 32 | return data.repos ?? []; |
| 33 | } catch { |
| 34 | return []; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | export default async function CollabHomePage() { |
| 39 | const cookieStore = await cookies(); |
| 40 | const token = cookieStore.get("grove_hub_token")?.value; |
| 41 | const signedIn = !!token; |
| 42 | const headerStore = await headers(); |
| 43 | const host = |
| 44 | headerStore.get("x-forwarded-host") ?? |
| 45 | headerStore.get("host") ?? |
| 46 | ""; |
| 47 | const protocol = headerStore.get("x-forwarded-proto") ?? "http"; |
| 48 | const canonicalHost = host.split(",")[0]?.trim() ?? ""; |
| 49 | const groveHost = canonicalHost.replace(/^(canopy|ring|collab)\./, ""); |
| 50 | const groveOrigin = groveHost ? `${protocol}://${groveHost}` : ""; |
| 51 | const loginHref = groveOrigin ? `${groveOrigin}/login` : "/login"; |
| 52 | const exploreHref = groveOrigin ? `${groveOrigin}/` : "/"; |
| 53 | |
| 54 | if (!signedIn) { |
| 55 | return ( |
| 56 | <div className="max-w-3xl mx-auto px-4 py-8"> |
| 57 | <div |
| 58 | className="p-4 sm:p-8 text-center" |
| 59 | style={{ |
| 60 | backgroundColor: "var(--bg-card)", |
| 61 | border: "1px solid var(--border-subtle)", |
| 62 | }} |
| 63 | > |
| 64 | <div className="mx-auto mb-4 w-fit opacity-70"> |
| 65 | <CollabLogo size={44} /> |
| 66 | </div> |
| 67 | <h1 className="text-lg mb-1">Collab</h1> |
| 68 | <p className="text-sm mb-4" style={{ color: "var(--text-faint)" }}> |
| 69 | Collaborate on Mermaid diagrams for your Grove repositories. |
| 70 | </p> |
| 71 | <div className="flex items-center justify-center gap-2"> |
| 72 | <Link |
| 73 | href={loginHref} |
| 74 | className="px-3 py-1.5 text-sm" |
| 75 | style={{ |
| 76 | backgroundColor: "var(--accent)", |
| 77 | color: "var(--accent-text)", |
| 78 | }} |
| 79 | > |
| 80 | Sign in |
| 81 | </Link> |
| 82 | <Link |
| 83 | href={exploreHref} |
| 84 | className="px-3 py-1.5 text-sm" |
| 85 | style={{ |
| 86 | border: "1px solid var(--border-subtle)", |
| 87 | color: "var(--text-secondary)", |
| 88 | }} |
| 89 | > |
| 90 | Explore Grove |
| 91 | </Link> |
| 92 | </div> |
| 93 | </div> |
| 94 | </div> |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | const repos = await getRepos(token); |
| 99 | |
| 100 | return <CollabRepoList repos={repos} />; |
| 101 | } |
| 102 | |