Skip to content
refs
131 lines · 6.4 KBRaw
1import { GitBranchIcon } from "@refs/design-system/icons";
2import { pullLink, refsErrorCode } from "@refs/backend/contract";
3import { Ago, LineBadge, 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 { FinishReview } from "../components/finish-review";
15import { decidePermission } from "../lib/permissions";
16
17export const meta: Route.MetaFunction = ({ loaderData, params }) => [{ title: loaderData ? `${loaderData.pull.title} · Pull request #${params.number} · ${fullName(params)} · refs` : "Pull request · refs" }];
18
19export async function loader({ params, context }: Route.LoaderArgs) {
20 const refs = signedIn(context);
21 const repo = await found(refs.repo(fullName(params)));
22 const pull = await found(repo.pull(Number(params.number)));
23 const [changes, permissions, hosted] = await Promise.all([
24 pull.changes().catch((error: unknown) => {
25 if (refsErrorCode(error) === "not_found") return null;
26 throw error;
27 }),
28 pull.permissions(),
29 repo.hostedAgents().then((agents) => agents.list()).catch(() => []),
30 ]);
31 const summary = changes && { ...changes, files: changes.files.map((file) => ({ ...file, before: null, after: null })) };
32 const hostedAgents = hosted.filter((agent) => !agent.replaced).map(({ handle, description, scope, model }) => ({ handle, description, scope, model: model ? `${model.provider} · ${model.model}` : null }));
33 return { pull: pull.detail, changes: summary, permissions, hostedAgents, human: refs.viewer.agent === null, now: nowSeconds() };
34}
35
36export async function action({ request, params, context }: Route.ActionArgs) {
37 const repo = await found(signedIn(context).repo(fullName(params)));
38 const pull = await found(repo.pull(Number(params.number)));
39 const form = await request.formData();
40 const intent = formText(form, "intent");
41 if (intent === "permission") {
42 const outcome = await decidePermission(repo, form);
43 return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent });
44 }
45 if (intent !== "title") return data({ intent, error: "Unknown action." }, { status: 400 });
46 const outcome = await pull.rename(formText(form, "title"));
47 return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent });
48}
49
50type Tab = "reasoning" | "conversation" | "map" | "files";
51
52const STATUS_TEXT = { open: "text-success", merged: "text-success", closed: "text-danger" } as const;
53
54export default function PullLayout({ loaderData }: Route.ComponentProps) {
55 const { pull, changes, permissions, human, now } = loaderData;
56 const pathname = useIntendedPath();
57 const base = pullLink(pull.repo, pull.number);
58 const current: Tab = pathname.endsWith("/reasoning") ? "reasoning" : pathname.endsWith("/files") ? "files" : pathname.endsWith("/map") ? "map" : "conversation";
59 const hrefs: Record<Tab, string> = { reasoning: `${base}/reasoning`, conversation: base, map: `${base}/map`, files: `${base}/files` };
60 const commits = changes?.commits.length ?? 0;
61 const decisions = netDecisions(changes?.commits.flatMap((commit) => parseDecisions(commit.message ?? "", commit.hash).decisions.map((record) => ({ record }))) ?? []).length;
62 const branch = (name: string) => <LineBadge name={name} main={name === pull.base} />;
63 const tab = (value: Tab, label: string, count: number) => ({
64 value,
65 label: (
66 <>
67 {label}
68 {count > 0 && <TabCount>{count}</TabCount>}
69 </>
70 ),
71 nativeButton: false,
72 render: (props: React.AnchorHTMLAttributes<HTMLAnchorElement>) => <AppLink {...props} href={hrefs[value]} />,
73 });
74 const meta = (edit: React.ReactNode) => (
75 <div className="flex flex-wrap items-center gap-x-2 gap-y-2 text-compact text-muted">
76 <span className={`inline-flex items-center gap-1 font-medium ${STATUS_TEXT[pull.status]}`}>
77 <PullStatusIcon status={pull.status} size={14} />
78 {STATUS_LABEL[pull.status]}
79 </span>
80 <span aria-hidden className="text-faint">·</span>
81 <Opener by={openerOf(pull)} />
82 <span>
83 {pull.status === "merged" ? "merged" : "wants to merge"} {commits} {commits === 1 ? "commit" : "commits"} into
84 </span>
85 {branch(pull.base)}
86 <span>from</span>
87 {branch(pull.head)}
88 {pull.status !== "open" && pull.closedAt && (
89 <span>
90 <Ago at={pull.closedAt} now={now} />
91 </span>
92 )}
93 {changes && (
94 <span className="font-mono text-xs whitespace-nowrap tabular-nums">
95 <span className="text-success">+{changes.additions}</span> <span className="text-danger">−{changes.deletions}</span>
96 </span>
97 )}
98 {pull.branchedFrom && (
99 <AppLink href={`${pullLink(pull.repo, pull.branchedFrom.number)}/reasoning#${pull.branchedFrom.commit}`} className="flex items-center gap-1 text-accent-text hover:underline">
100 <GitBranchIcon size={14} />
101 from #{pull.branchedFrom.number} at <code className="font-mono text-xs">{pull.branchedFrom.commit.slice(0, 7)}</code>
102 </AppLink>
103 )}
104 <span className="flex items-center gap-2 sm:ml-auto">
105 {edit}
106 {human && pull.status === "open" && changes && (
107<FinishReview action={hrefs.files} revision={changes.head} />
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}