flagon-io/g1t

public

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

g1t/apps/web/app/components/work.tsx

510 lines15,778 bytesCodeBlame
1import { Bot, CircleCheck, CircleSlash } from "lucide-react";
2import type { CSSProperties, ReactNode } from "react";
3import { Form, Link } from "react-router";
4
5import type { Comment, Issue, Pull, State } from "@g1t/contracts";
6
7import { repoAt } from "../lib/markdown-plugins";
8import { Markdown } from "./markdown";
9import { IssueIcon, PullIcon } from "./work-icons";
10import { MentionTextarea } from "./mention-textarea";
11import { Avatar, Button, TimeAgo } from "./ui";
12import { CheckboxOption } from "./ui/checkbox";
13
14export { IssueIcon, PullIcon };
15
16/** Hues for the labels every repository starts with. */
17const LABEL_HUES: Record<string, number> = {
18 bug: 25,
19 feature: 150,
20 docs: 240,
21 chore: 80,
22 question: 310,
23};
24
25/** A stable hue for any other label, from its name. */
26function hueFor(name: string): number {
27 let hash = 0;
28 for (const char of name) hash = (hash * 31 + char.charCodeAt(0)) % 360;
29 return hash;
30}
31
32export function Label({ name }: { name: string }) {
33 const hue = LABEL_HUES[name] ?? hueFor(name);
34 const style: CSSProperties = {
35 color: `oklch(0.84 0.11 ${hue})`,
36 borderColor: `oklch(0.84 0.11 ${hue} / 0.35)`,
37 backgroundColor: `oklch(0.84 0.11 ${hue} / 0.1)`,
38 };
39 return (
40 <span
41 style={style}
42 className="inline-flex shrink-0 items-center rounded-full border px-2 py-px text-xs font-medium"
43 >
44 {name}
45 </span>
46 );
47}
48
49/** The people an issue is assigned to, as overlapping avatars. */
50export function AssigneeStack({ people }: { people: string[] }) {
51 if (people.length === 0) return null;
52 const shown = people.slice(0, 3);
53 return (
54 <span
55 className="flex shrink-0 items-center"
56 title={`Assigned to ${people.join(", ")}`}
57 >
58 {shown.map((name, index) => (
59 <span key={name} className={`rounded-full ring-2 ring-bg ${index > 0 ? "-ml-1.5" : ""}`}>
60 <Avatar name={name} size={18} />
61 </span>
62 ))}
63 {people.length > shown.length && (
64 <span className="ml-1 text-xs text-faint">+{people.length - shown.length}</span>
65 )}
66 </span>
67 );
68}
69
70/** The agent working on an issue now. */
71export function Assignee({ agent }: { agent: string }) {
72 return (
73 <span
74 className="inline-flex shrink-0 items-center gap-1 rounded-full border border-accent/35 bg-accent/10 px-2 py-px text-xs font-medium text-accent"
75 title={`Assigned to ${agent}`}
76 >
77 <Bot size={12} />
78 {agent}
79 </span>
80 );
81}
82
83const PILL = "inline-flex shrink-0 items-center gap-1.5 rounded-full border px-2.5 py-1 text-xs font-medium";
84
85/** The large state badge at the top of an issue. */
86export function IssueState({ issue }: { issue: Pick<Issue, "state" | "reason"> }) {
87 const style =
88 issue.state === "open"
89 ? "border-accent/30 bg-accent/10 text-accent"
90 : issue.reason === "not_planned"
91 ? "border-line bg-surface text-muted"
92 : "border-merged/30 bg-merged/10 text-merged";
93 return (
94 <span className={`${PILL} ${style}`}>
95 <IssueIcon issue={issue} size={14} />
96 {issue.state === "open"
97 ? "Open"
98 : issue.reason === "not_planned"
99 ? "Closed as not planned"
100 : "Closed"}
101 </span>
102 );
103}
104
105const PULL_STYLES: Record<Pull["status"], string> = {
106 draft: "border-line bg-surface text-muted",
107 open: "border-accent/30 bg-accent/10 text-accent",
108 merged: "border-merged/30 bg-merged/10 text-merged",
109 closed: "border-danger/30 bg-danger/10 text-danger",
110};
111
112const PULL_NAMES: Record<Pull["status"], string> = {
113 draft: "Draft",
114 open: "Open",
115 merged: "Merged",
116 closed: "Closed",
117};
118
119export function PullState({ status }: { status: Pull["status"] }) {
120 return (
121 <span className={`${PILL} ${PULL_STYLES[status]}`}>
122 <PullIcon status={status} size={14} />
123 {PULL_NAMES[status]}
124 </span>
125 );
126}
127
128/** The open and closed switch above a list. `to` is the list's own path. */
129export function StateTabs({
130 to,
131 state,
132 query = "",
133 action,
134}: {
135 to: string;
136 state: State;
137 /** Other query parameters to keep, e.g. `label=bug`. */
138 query?: string;
139 action?: ReactNode;
140}) {
141 const link = (value: State) =>
142 `${to}?${[value === "open" ? "" : "state=closed", query].filter(Boolean).join("&")}`;
143 const tab = (value: State, label: string) => (
144 <Link
145 to={link(value)}
146 className={
147 "rounded-md px-3 py-1.5 text-sm transition-colors " +
148 (state === value ? "bg-raised font-medium text-fg" : "text-muted hover:text-fg")
149 }
150 >
151 {label}
152 </Link>
153 );
154 return (
155 <div className="flex flex-wrap items-center justify-between gap-3">
156 <div className="flex gap-1 rounded-lg border border-line p-1">
157 {tab("open", "Open")}
158 {tab("closed", "Closed")}
159 </div>
160 {action}
161 </div>
162 );
163}
164
165const VERDICTS = {
166 approve: { label: "approved these changes", style: "text-accent" },
167 request_changes: { label: "requested changes", style: "text-danger" },
168} as const;
169
170/** How g1t's own agents are named; they have no profile to link to. */
171const AGENTS = new Set(["g1t-agent", "g1t agent", "g1t"]);
172
173/**
174 * A person's name (or `children`) linking to their profile at `/u/<name>`.
175 * g1t's agents, and names that are not usernames, stay plain text.
176 */
177export function PersonLink({
178 name,
179 className,
180 label,
181 children,
182}: {
183 name: string;
184 className?: string;
185 label?: string;
186 children?: ReactNode;
187}) {
188 if (AGENTS.has(name) || !/^[a-z0-9-]{1,39}$/i.test(name)) {
189 return <span className={className}>{children ?? name}</span>;
190 }
191 return (
192 <Link to={`/u/${name.toLowerCase()}`} className={className} aria-label={label}>
193 {children ?? name}
194 </Link>
195 );
196}
197
198/**
199 * Comments in order, then the box to add one. On a pull request, `review`
200 * says where its changes are shown and whether the viewer may give a
201 * verdict.
202 */
203/**
204 * One entry in a conversation: who, what they did and when, then what they
205 * wrote. The author's avatar sits beside it, as on any forge.
206 */
207export function TimelineItem({
208 author,
209 action,
210 at,
211 aside,
212 children,
213}: {
214 author: string;
215 /** What they did, after their name: "commented", "opened this". */
216 action: ReactNode;
217 at?: string;
218 /** Shown at the right of the header. */
219 aside?: ReactNode;
220 children?: ReactNode;
221}) {
222 return (
223 <div className="flex gap-3">
224 <span className="mt-1 hidden shrink-0 sm:block">
225 <PersonLink name={author} label={`${author}'s profile`}>
226 <Avatar name={author} size={32} />
227 </PersonLink>
228 </span>
229 <article className="min-w-0 grow overflow-hidden rounded-xl border border-line bg-surface">
230 <header className="flex flex-wrap items-center gap-x-2 gap-y-1 border-b border-line bg-raised/40 px-4 py-2 text-sm text-muted">
231 <span className="sm:hidden">
232 <Avatar name={author} size={18} />
233 </span>
234 <PersonLink name={author} className="font-medium text-fg hover:underline" />
235 {action}
236 {at && <TimeAgo at={at} />}
237 {aside && <span className="ml-auto min-w-0">{aside}</span>}
238 </header>
239 {children && <div className="px-4 py-3">{children}</div>}
240 </article>
241 </div>
242 );
243}
244
245type Review = { changesUrl: string; canJudge: boolean };
246
247/**
248 * Text with each `#12` linked to the issue or pull request of that number,
249 * and each file named in backticks shown as code, linked to its diff when
250 * `filesUrl` says where the changes are.
251 */
252function WithReferences({ text, base, filesUrl }: { text: string; base?: string; filesUrl?: string }) {
253 if (!base) return <>{text}</>;
254 return (
255 <>
256 {text.split(/(#\d+|`[^`\n]+`)/).map((part, index) =>
257 /^`[^`]+`$/.test(part) ? (
258 filesUrl ? (
259 <Link
260 key={index}
261 to={`${filesUrl}#file-${part.slice(1, -1)}`}
262 className="font-mono text-xs text-fg hover:underline"
263 >
264 {part.slice(1, -1)}
265 </Link>
266 ) : (
267 <code key={index} className="font-mono text-xs text-fg">
268 {part.slice(1, -1)}
269 </code>
270 )
271 ) : /^#\d+$/.test(part) ? (
272 // Issues and pull requests share numbers; the issue page forwards.
273 <Link
274 key={index}
275 to={`${base}/issues/${part.slice(1)}`}
276 className="font-medium text-fg hover:underline"
277 >
278 {part}
279 </Link>
280 ) : (
281 part
282 ),
283 )}
284 </>
285 );
286}
287
288/** Something that happened, as one line on the conversation's rail. */
289function TimelineEvent({ comment, base, filesUrl }: { comment: Comment; base?: string; filesUrl?: string }) {
290 return (
291 <div className="flex items-center gap-3 text-sm text-muted">
292 <span className="hidden w-8 shrink-0 justify-center sm:flex">
293 <span className="size-2 rounded-full border border-line-strong bg-bg" />
294 </span>
295 <span className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-0.5">
296 <Avatar name={comment.author.username} size={16} />
297 <PersonLink name={comment.author.username} className="font-medium text-fg hover:underline" />
298 <span>
299 <WithReferences text={comment.body} base={base} filesUrl={filesUrl} />
300 </span>
301 <span className="text-faint">
302 · <TimeAgo at={comment.createdAt} />
303 </span>
304 </span>
305 </div>
306 );
307}
308
309/**
310 * The conversation of an issue or a pull request, oldest first: what
311 * people and agents wrote, and between those, what happened.
312 */
313export function CommentList({
314 comments,
315 review,
316 base,
317}: {
318 comments: Comment[];
319 review?: Review;
320 /** The repository's path, so that `#12` in an event can be linked. */
321 base?: string;
322}) {
323 return (
324 <>
325 {comments.map((comment) => {
326 if (comment.kind === "event") {
327 return <TimelineEvent key={comment.id} comment={comment} base={base} filesUrl={review?.changesUrl} />;
328 }
329 const verdict = comment.verdict && VERDICTS[comment.verdict];
330 return (
331 <TimelineItem
332 key={comment.id}
333 author={comment.author.username}
334 at={comment.createdAt}
335 action={
336 verdict ? (
337 <span className={`flex items-center gap-1 font-medium ${verdict.style}`}>
338 {comment.verdict === "approve" ? (
339 <CircleCheck size={14} />
340 ) : (
341 <CircleSlash size={14} />
342 )}
343 {verdict.label}
344 </span>
345 ) : (
346 <span>commented</span>
347 )
348 }
349 aside={
350 comment.path &&
351 review && (
352 <Link
353 to={`${review.changesUrl}#file-${comment.path}`}
354 className="block truncate font-mono text-xs text-faint hover:text-fg"
355 >
356 {comment.path}
357 {comment.line != null && `:${comment.line}`}
358 </Link>
359 )
360 }
361 >
362 {comment.body && <Markdown source={comment.body} repo={repoAt(base)} />}
363 </TimelineItem>
364 );
365 })}
366 </>
367 );
368}
369
370/** Where a signed-in person writes a comment, or a review. */
371export function CommentForm({
372 author,
373 resetKey,
374 review,
375}: {
376 /** The viewer's username, or null if they are signed out. */
377 author: string | null;
378 /** Changes when a comment is added, which clears the box. */
379 resetKey: number;
380 review?: Review;
381}) {
382 if (!author) {
383 return (
384 <p className="text-sm text-muted">
385 <Link to="/login" className="text-fg underline underline-offset-4">
386 Sign in
387 </Link>{" "}
388 to comment.
389 </p>
390 );
391 }
392 return (
393 <div className="flex gap-3">
394 <span className="mt-1 hidden shrink-0 sm:block">
395 <Avatar name={author} size={32} />
396 </span>
397 <Form method="post" className="min-w-0 grow space-y-2" key={resetKey}>
398 <input type="hidden" name="action" value="comment" />
399 <MentionTextarea
400 name="body"
401 rows={3}
402 placeholder={
403 review?.canJudge
404 ? "Leave a comment, or a review. Markdown works."
405 : "Leave a comment. Markdown works."
406 }
407 />
408 <div className="flex flex-wrap gap-2">
409 <Button type="submit">Comment</Button>
410 {review?.canJudge && (
411 <>
412 <Button variant="quiet" type="submit" name="verdict" value="approve">
413 <CircleCheck size={14} className="text-accent" />
414 Approve
415 </Button>
416 <Button variant="quiet" type="submit" name="verdict" value="request_changes">
417 <CircleSlash size={14} className="text-danger" />
418 Request changes
419 </Button>
420 </>
421 )}
422 </div>
423 </Form>
424 </div>
425 );
426}
427
428/**
429 * Checkboxes for choosing people: the workspace's members, anyone already
430 * chosen, and a box for other usernames. Posts `name` for each ticked and
431 * `others` for the rest.
432 */
433export function PeoplePicker({
434 name,
435 members,
436 chosen,
437}: {
438 name: string;
439 members: string[];
440 chosen: string[];
441}) {
442 const people = [...new Set([...members, ...chosen])];
443 return (
444 <>
445 {people.length > 0 && (
446 <div className="space-y-1.5">
447 {people.map((person) => (
448 <CheckboxOption
449 key={person}
450 name={name}
451 value={person}
452 defaultChecked={chosen.includes(person)}
453 className="items-center"
454 labelClassName="flex items-center gap-2"
455 label={
456 <>
457 <Avatar name={person} size={18} />
458 <span className="font-mono text-xs">{person}</span>
459 </>
460 }
461 />
462 ))}
463 </div>
464 )}
465 <input
466 name="others"
467 placeholder="Other usernames, comma separated"
468 autoComplete="off"
469 data-1p-ignore
470 className="w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none placeholder:text-faint hover:border-line-strong focus:border-accent-dim"
471 />
472 </>
473 );
474}
475
476/** Where each reviewer stands: their most recent verdict. */
477export function verdicts(comments: Comment[]): { reviewer: string; verdict: NonNullable<Comment["verdict"]> }[] {
478 const latest = new Map<string, NonNullable<Comment["verdict"]>>();
479 for (const comment of comments) {
480 if (comment.verdict) latest.set(comment.author.username, comment.verdict);
481 }
482 return [...latest].map(([reviewer, verdict]) => ({ reviewer, verdict }));
483}
484
485/** `2 files +12 −3`: the size of a pull request's change. */
486export function ChangeSize({ files }: { files: Pull["files"] }) {
487 if (files.length === 0) return null;
488 const additions = files.reduce((sum, file) => sum + file.additions, 0);
489 const deletions = files.reduce((sum, file) => sum + file.deletions, 0);
490 return (
491 <span className="flex shrink-0 items-center gap-1.5 font-mono text-xs text-faint">
492 <span>
493 {files.length} {files.length === 1 ? "file" : "files"}
494 </span>
495 <span className="text-accent">+{additions}</span>
496 <span className="text-danger">−{deletions}</span>
497 </span>
498 );
499}
500
501/** The words of a Markdown text, without its markup, for a one-line preview. */
502export function plainText(markdown: string): string {
503 return markdown
504 .replace(/```[\s\S]*?```/g, " ")
505 .replace(/^\s{0,3}(#{1,6}|[-*+]|\d+\.)\s+/gm, "")
506 .replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
507 .replace(/[*_`~]/g, "")
508 .replace(/\s+/g, " ")
509 .trim();
510}