Skip to content
refs
History
131 lines · 6.3 KBRaw
1import { CheckCircleIcon, GitBranchIcon } from "@refs/design-system/icons";
2import { pullLink, refsErrorCode } from "@refs/backend/contract";
3import { Ago, LineBadge, LinkButton, TabCount, Tabs } from "@refs/design-system";
4import { netDecisions, parseDecisions } from "@refs/protocol";
5import { Outlet, data } from "react-router";
6import { PendingView, under, useIntendedPath } from "../components/pending";
7import { AppLink } from "../components/link";
8import type { Route } from "./+types/pull-layout";
9import { PermissionRequests } from "../components/permission-requests";
10import { PullStatusIcon, STATUS_LABEL } from "../components/pull-status";
11import { Opener, openerOf } from "../components/timeline";
12import { failed, formText, found, signedIn, fullName, nowSeconds } from "../lib/session";
13import { TitleEditor } from "../components/title-editor";
14import { decidePermission } from "../lib/permissions";
15
16export const meta: Route.MetaFunction = ({ loaderData, params }) => [{ title: loaderData ? `${loaderData.pull.title} · Pull request #${params.number} · ${fullName(params)} · refs` : "Pull request · refs" }];
17
18export async function loader({ params, context }: Route.LoaderArgs) {
19 const repo = await found(signedIn(context).repo(fullName(params)));
20 const pull = await found(repo.pull(Number(params.number)));
21 const [changes, permissions, hosted] = await Promise.all([
22 pull.changes().catch((error: unknown) => {
23 if (refsErrorCode(error) === "not_found") return null;
24 throw error;
25 }),
26 pull.permissions(),
27 repo.hostedAgents().then((agents) => agents.list()).catch(() => []),
28 ]);
29 const summary = changes && { ...changes, files: changes.files.map((file) => ({ ...file, before: null, after: null })) };
30 const hostedAgents = hosted.filter((agent) => !agent.replaced).map(({ handle, description }) => ({ handle, description }));
31 return { pull: pull.detail, changes: summary, permissions, hostedAgents, now: nowSeconds() };
32}
33
34export async function action({ request, params, context }: Route.ActionArgs) {
35 const repo = await found(signedIn(context).repo(fullName(params)));
36 const pull = await found(repo.pull(Number(params.number)));
37 const form = await request.formData();
38 const intent = formText(form, "intent");
39 if (intent === "permission") {
40 const outcome = await decidePermission(repo, form);
41 return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent });
42 }
43 if (intent !== "title") return data({ intent, error: "Unknown action." }, { status: 400 });
44 const outcome = await pull.rename(formText(form, "title"));
45 return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent });
46}
47
48type Tab = "reasoning" | "conversation" | "map" | "files";
49
50const STATUS_TEXT = { open: "text-success", merged: "text-success", closed: "text-danger" } as const;
51
52export default function PullLayout({ loaderData }: Route.ComponentProps) {
53 const { pull, changes, permissions, now } = loaderData;
54 const pathname = useIntendedPath();
55 const base = pullLink(pull.repo, pull.number);
56 const current: Tab = pathname.endsWith("/reasoning") ? "reasoning" : pathname.endsWith("/files") ? "files" : pathname.endsWith("/map") ? "map" : "conversation";
57 const hrefs: Record<Tab, string> = { reasoning: `${base}/reasoning`, conversation: base, map: `${base}/map`, files: `${base}/files` };
58 const commits = changes?.commits.length ?? 0;
59 const decisions = netDecisions(changes?.commits.flatMap((commit) => parseDecisions(commit.message ?? "", commit.hash).decisions.map((record) => ({ record }))) ?? []).length;
60 const branch = (name: string) => <LineBadge name={name} main={name === pull.base} />;
61 const tab = (value: Tab, label: string, count: number) => ({
62 value,
63 label: (
64 <>
65 {label}
66 {count > 0 && <TabCount>{count}</TabCount>}
67 </>
68 ),
69 nativeButton: false,
70 render: (props: React.AnchorHTMLAttributes<HTMLAnchorElement>) => <AppLink {...props} href={hrefs[value]} />,
71 });
72 const meta = (edit: React.ReactNode) => (
73 <div className="flex flex-wrap items-center gap-x-2 gap-y-2 text-compact text-muted">
74 <span className={`inline-flex items-center gap-1 font-medium ${STATUS_TEXT[pull.status]}`}>
75 <PullStatusIcon status={pull.status} size={14} />
76 {STATUS_LABEL[pull.status]}
77 </span>
78 <span aria-hidden className="text-faint">·</span>
79 <Opener by={openerOf(pull)} />
80 <span>
81 {pull.status === "merged" ? "merged" : "wants to merge"} {commits} {commits === 1 ? "commit" : "commits"} into
82 </span>
83 {branch(pull.base)}
84 <span>from</span>
85 {branch(pull.head)}
86 {pull.status !== "open" && pull.closedAt && (
87 <span>
88 <Ago at={pull.closedAt} now={now} />
89 </span>
90 )}
91 {changes && (
92 <span className="font-mono text-xs whitespace-nowrap tabular-nums">
93 <span className="text-success">+{changes.additions}</span> <span className="text-danger">−{changes.deletions}</span>
94 </span>
95 )}
96 {pull.branchedFrom && (
97 <AppLink href={`${pullLink(pull.repo, pull.branchedFrom.number)}/reasoning#${pull.branchedFrom.commit}`} className="flex items-center gap-1 text-accent-text hover:underline">
98 <GitBranchIcon size={14} />
99 from #{pull.branchedFrom.number} step <code className="font-mono text-xs">{pull.branchedFrom.commit.slice(0, 7)}</code>
100 </AppLink>
101 )}
102 <span className="flex items-center gap-2 sm:ml-auto">
103 {edit}
104 {pull.status === "open" && changes && (
105 <LinkButton size="sm" variant="primary" icon={<CheckCircleIcon />} href={`${hrefs.files}#review`}>
106 Review
107 </LinkButton>
108 )}
109 </span>
110 </div>
111 );
112
113 return (
114 <>
115 <div className="mb-4">
116 <TitleEditor title={pull.title} number={pull.number} meta={meta} />
117 </div>
118 {permissions.length > 0 && (
119 <div className="mb-4">
120 <PermissionRequests requests={permissions} now={now} />
121 </div>
122 )}
123 <div className="mb-5">
124 <Tabs variant="underline" value={current} tabs={[tab("reasoning", "Reasoning", decisions), tab("conversation", "Conversation", pull.comments), tab("map", "Map", 0), tab("files", "Files", changes?.files.length ?? 0)]} />
125 </div>
126 <PendingView kind="section" covers={(to) => under(to, base)}>
127 <Outlet />
128 </PendingView>
129 </>
130 );
131}