Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

353 linesCodeBlameRaw
1/**
2 * The top of a project's overview for what g1t.page does not host: an app
3 * that deploys itself, an app nobody has said where it runs, docs, and
4 * anything else. An app on g1t keeps its production card, and a library
5 * or a tool its packages (routes/repo/overview.tsx).
6 */
7import { ArrowUpRight, BookOpen, Box, ChevronDown, GitCommitHorizontal, Globe, Rocket } from "lucide-react";
8import { type ReactNode, useState } from "react";
9import { Link, useFetcher } from "react-router";
10
11import type { DeployStatus, Project } from "@g1t/contracts";
12
13import { DeployLink, host, StatusDot } from "./deploy";
14import { ButtonLink, Input, SubmitButton, TimeAgo } from "./ui";
15import { Button } from "./ui/button";
16import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "./ui/dropdown-menu";
17import { Hint } from "./ui/hint";
18
19/**
20 * A deployment g1t.page did not build: one a workflow's job made, or one
21 * reported by another pipeline. When there is one for production, the
22 * card shows it.
23 */
24export type ExternalDeployment = {
25 environment: string;
26 url: string | null;
27 /** success, failure, error, pending, queued, in_progress or inactive. */
28 state: string;
29 sha: string;
30 created_at: string;
31 /** The workflow run that made it, when g1t Actions did. */
32 run_url?: string | null;
33};
34
35/** A reported deployment's state, as g1t's own deployments show theirs. */
36export function deploymentStatus(state: string): DeployStatus {
37 switch (state) {
38 case "success":
39 return "ready";
40 case "failure":
41 case "error":
42 return "failed";
43 case "in_progress":
44 return "building";
45 case "inactive":
46 return "replaced";
47 default:
48 return "queued";
49 }
50}
51
52type Head = {
53 project: Project;
54 base: string;
55 /** May change the project's settings: what it is, its links. */
56 canChange: boolean;
57 /** May turn on Deployments. */
58 canDeploy: boolean;
59};
60
61export function HeadLabel({ icon, children }: { icon: ReactNode; children: ReactNode }) {
62 return (
63 <p className="flex items-center gap-2 text-xs font-medium tracking-wide text-muted uppercase [&_svg]:size-3.25 [&_svg]:text-accent">
64 {icon}
65 {children}
66 </p>
67 );
68}
69
70function BigLink({ url }: { url: string }) {
71 return (
72 <DeployLink href={url} className="mt-2 flex min-w-0 items-center gap-1.5 font-mono text-lg font-medium hover:text-accent">
73 <span className="truncate">{host(url).replace(/\/$/, "")}</span>
74 <ArrowUpRight size={16} className="shrink-0 text-faint" />
75 </DeployLink>
76 );
77}
78
79/** An address, saved for the project in place: production's, or its docs'. */
80function AddressForm({
81 field,
82 placeholder,
83 label,
84 extra,
85}: {
86 field: "productionUrl" | "docsUrl";
87 placeholder: string;
88 label: string;
89 /** Hidden fields sent with it. */
90 extra: Record<string, string>;
91}) {
92 const fetcher = useFetcher<{ error?: string }>();
93 return (
94 <fetcher.Form method="post" className="mt-3 max-w-lg">
95 {Object.entries(extra).map(([name, value]) => (
96 <input key={name} type="hidden" name={name} value={value} />
97 ))}
98 <div className="flex items-center gap-2">
99 <div className="min-w-0 grow">
100 <Input name={field} type="text" inputMode="url" required placeholder={placeholder} aria-label={label} maxLength={255} />
101 </div>
102 <SubmitButton fetcher={fetcher} variant="outline" pending="Saving…">
103 Save
104 </SubmitButton>
105 </div>
106 {fetcher.data?.error && <p className="mt-1.5 text-xs text-danger">{fetcher.data.error}</p>}
107 </fetcher.Form>
108 );
109}
110
111/** Deploying on g1t, offered as an option and never as a step. */
112function DeployQuietly({ base, canDeploy, what }: { base: string; canDeploy: boolean; what: string }) {
113 if (!canDeploy) return null;
114 return (
115 <p className="mt-4 text-xs text-faint">
116 g1t can deploy {what} on g1t.page instead, with a preview for every pull request.{" "}
117 <Link to={`${base}/settings/deployments`} className="underline-offset-4 hover:text-fg hover:underline">
118 Deployment settings
119 </Link>
120 </p>
121 );
122}
123
124/**
125 * An app that deploys itself (its workflows, or another pipeline): production at the address it was
126 * given, with the latest reported deployment, or else the default branch's
127 * latest commit and its checks.
128 */
129export function ElsewhereHead({
130 project,
131 base,
132 canChange,
133 canDeploy,
134 deployment,
135 commit,
136 checks,
137 shot,
138}: Head & {
139 deployment?: ExternalDeployment | null;
140 commit: { hash: string; authoredAt: string } | null;
141 /** The commit's checks, as a badge; left out until there is one to show. */
142 checks?: ReactNode;
143 /** Production's picture, beside the rest, as an app on g1t has (components/production-shot.tsx). */
144 shot?: ReactNode;
145}) {
146 const url = deployment?.url ?? project.productionUrl;
147 return (
148 <div className="flex flex-col gap-5 p-5 sm:flex-row sm:items-start sm:p-6">
149 {shot}
150 <div className="flex min-w-0 grow flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
151 <div className="min-w-0">
152 <HeadLabel icon={<Rocket />}>Production</HeadLabel>
153 {url ? (
154 <>
155 <BigLink url={url} />
156 <p className="mt-1.5 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-muted">
157 {deployment ? (
158 <>
159 <StatusDot status={deploymentStatus(deployment.state)} />
160 {checks}
161 <span className="inline-flex items-center gap-1 font-mono">
162 <GitCommitHorizontal size={13} className="text-faint" />
163 <Link to={`${base}/commit/${deployment.sha}`} className="hover:text-fg">{deployment.sha.slice(0, 7)}</Link>
164 </span>
165 <span>
166 {deployment.state === "success" ? "Deployed" : "Updated"} <TimeAgo at={deployment.created_at} />
167 {deployment.run_url ? (
168 <>
169 {" "}
170 by{" "}
171 <Link to={deployment.run_url} className="underline-offset-4 hover:text-fg hover:underline">
172 its workflow
173 </Link>
174 </>
175 ) : (
176 " by its own pipeline"
177 )}
178 </span>
179 </>
180 ) : (
181 <>
182 <span>Deploys itself</span>
183 {commit && (
184 <span className="inline-flex items-center gap-1.5">
185 <span className="text-faint">·</span>
186 <Link to={`${base}/commit/${commit.hash}`} className="inline-flex items-center gap-1 font-mono hover:text-fg">
187 <GitCommitHorizontal size={13} className="text-faint" />
188 {commit.hash.slice(0, 7)}
189 </Link>
190 {checks}
191 <TimeAgo at={commit.authoredAt} />
192 </span>
193 )}
194 </>
195 )}
196 </p>
197 </>
198 ) : canChange ? (
199 <>
200 <p className="mt-2 text-lg font-medium">Where is production?</p>
201 <p className="mt-1 max-w-lg text-sm text-muted">
202 {project.name} deploys itself. Give production's address, and this card links to it.
203 </p>
204 <AddressForm field="productionUrl" placeholder="https://example.com" label="Production's address" extra={{ intent: "kind", choice: "elsewhere" }} />
205 </>
206 ) : (
207 <p className="mt-2 text-sm text-muted">Deploys itself.</p>
208 )}
209 <DeployQuietly base={base} canDeploy={canDeploy} what="it" />
210 </div>
211 </div>
212 </div>
213 );
214}
215
216const NOT_DEPLOYED: { value: string; label: string }[] = [
217 { value: "library", label: "A library or package" },
218 { value: "tool", label: "A tool or CLI" },
219 { value: "docs", label: "Documentation" },
220 { value: "other", label: "Something else" },
221];
222
223/**
224 * An app that nobody has said where it runs, with Deployments off: one
225 * question, answered in place, instead of a push to turn Deployments on.
226 */
227export function WhereItRuns({ project, base, canChange, canDeploy }: Head) {
228 const fetcher = useFetcher<{ error?: string }>();
229 const [elsewhere, setElsewhere] = useState(false);
230 if (!canChange) {
231 return (
232 <div className="p-5 sm:p-6">
233 <HeadLabel icon={<Globe />}>Homepage</HeadLabel>
234 {project.links.homepage ? <BigLink url={project.links.homepage} /> : <p className="mt-2 text-sm text-muted">No homepage given.</p>}
235 </div>
236 );
237 }
238 return (
239 <div className="p-5 sm:p-6">
240 <HeadLabel icon={<Rocket />}>Production</HeadLabel>
241 <p className="mt-2 text-lg font-medium">Where does {project.name} run?</p>
242 <p className="mt-1 max-w-xl text-sm text-muted">
243 Say once, and this page follows: production where it is deployed, or its packages and releases if it isn't
244 deployed at all. <span className="text-faint">{project.detected.reason.detail}</span>
245 </p>
246 <div className="mt-4 flex flex-wrap items-center gap-2">
247 {canDeploy && (
248 <ButtonLink to={`${base}/settings/deployments`} variant="outline">
249 <Rocket size={14} />
250 Deploy on g1t
251 </ButtonLink>
252 )}
253 <Button
254 type="button"
255 onClick={() => setElsewhere((open) => !open)}
256 aria-expanded={elsewhere}
257 variant="outline"
258 >
259 <Globe size={14} />
260 It deploys itself
261 </Button>
262 <DropdownMenu>
263 <DropdownMenuTrigger asChild>
264 <Button variant="outline">
265 <Box size={14} />
266 It isn't deployed
267 <ChevronDown size={13} className="text-faint" />
268 </Button>
269 </DropdownMenuTrigger>
270 <DropdownMenuContent align="start">
271 {NOT_DEPLOYED.map((option) => (
272 <DropdownMenuItem key={option.value} onSelect={() => fetcher.submit({ intent: "kind", choice: option.value }, { method: "post" })}>
273 {option.label}
274 </DropdownMenuItem>
275 ))}
276 </DropdownMenuContent>
277 </DropdownMenu>
278 </div>
279 {elsewhere && (
280 <AddressForm field="productionUrl" placeholder="Production's address, such as https://example.com" label="Production's address" extra={{ intent: "kind", choice: "elsewhere" }} />
281 )}
282 {fetcher.data?.error && <p className="mt-2 text-xs text-danger">{fetcher.data.error}</p>}
283 </div>
284 );
285}
286
287/** Documentation: where it is read, or a place to say so. */
288export function DocsHead({ project, base, canChange, canDeploy }: Head) {
289 const url = project.links.docs ?? project.productionUrl ?? project.links.homepage;
290 return (
291 <div className="flex flex-col gap-4 p-5 sm:flex-row sm:items-start sm:justify-between sm:p-6">
292 <div className="min-w-0">
293 <HeadLabel icon={<BookOpen />}>Documentation</HeadLabel>
294 {url ? (
295 <BigLink url={url} />
296 ) : canChange ? (
297 <>
298 <p className="mt-2 text-lg font-medium">Where are its docs read?</p>
299 <p className="mt-1 max-w-lg text-sm text-muted">Give the address, and this card and its listing link to it.</p>
300 <AddressForm field="docsUrl" placeholder="https://docs.example.com" label="Where its docs are read" extra={{ intent: "about" }} />
301 </>
302 ) : (
303 <p className="mt-2 text-sm text-muted">Its docs are in its repository.</p>
304 )}
305 <DeployQuietly base={base} canDeploy={canDeploy && project.runs == null} what="its docs as a site" />
306 </div>
307 {url && (
308 <div className="flex shrink-0 items-center gap-2">
309 <ButtonLink to={url} variant="accent" reloadDocument target="_blank" rel="noopener noreferrer">
310 Read
311 <ArrowUpRight size={14} />
312 </ButtonLink>
313 </div>
314 )}
315 </div>
316 );
317}
318
319/** Anything else: its homepage, or where to add its links. */
320export function OtherHead({ project, base, canChange }: Omit<Head, "canDeploy">) {
321 const url = project.links.homepage ?? project.links.docs ?? project.links.custom[0]?.url ?? null;
322 return (
323 <div className="p-5 sm:p-6">
324 <HeadLabel icon={<Globe />}>Homepage</HeadLabel>
325 {url ? (
326 <BigLink url={url} />
327 ) : canChange ? (
328 <p className="mt-2 text-sm text-muted">
329 No links yet.{" "}
330 <Link to={`${base}/settings#links`} className="text-accent hover:underline">
331 Add a homepage, docs or others
332 </Link>{" "}
333 and they show here, on its card and in its workspace's Projects.
334 </p>
335 ) : (
336 <p className="mt-2 text-sm text-muted">No homepage given.</p>
337 )}
338 </div>
339 );
340}
341
342/** Production as g1t serves it, with the project's homepage when that is somewhere else. */
343export function AlsoAt({ url }: { url: string | null }) {
344 if (!url) return null;
345 return (
346 <Hint label="Its homepage">
347 <DeployLink href={url} className="inline-flex items-center gap-1 text-faint hover:text-fg">
348 <Globe size={12} />
349 {host(url).replace(/\/$/, "")}
350 </DeployLink>
351 </Hint>
352 );
353}