103 lines · 5.6 KBRaw
| 1 | import { pullLink, refsErrorCode } from "@refs/backend/contract"; |
| 2 | import { Banner, Empty, LayerCard, LinkButton } from "@refs/design-system"; |
| 3 | import { GitDiffIcon } from "@refs/design-system/icons"; |
| 4 | import { useMemo } from "react"; |
| 5 | import { redirect, useRouteLoaderData } from "react-router"; |
| 6 | import type { Route } from "./+types/pull-map"; |
| 7 | import type { loader as pullLoader } from "./pull-layout"; |
| 8 | import type { CardNotes } from "../components/code-map/code-card"; |
| 9 | import { CodeMapPanel } from "../components/code-map/map-states"; |
| 10 | import { LineComposer } from "../components/line-composer"; |
| 11 | import { ReviewThreadCard, type ThreadComment } from "../components/review-thread"; |
| 12 | import { THREAD_INTENTS, threadAction } from "../lib/threads"; |
| 13 | import { failed, formText, found, signedIn, fullName, nowSeconds } from "../lib/session"; |
| 14 | |
| 15 | export const meta: Route.MetaFunction = ({ params }) => [{ title: `Map · Pull request #${params.number} · ${fullName(params)} · refs` }]; |
| 16 | |
| 17 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 18 | const refs = signedIn(context); |
| 19 | const repo = await found(refs.repo(fullName(params))); |
| 20 | const pull = await found(repo.pull(Number(params.number))); |
| 21 | const [codeMap, agents, choices, threads, timeline, mentions] = await Promise.all([ |
| 22 | pull.codeMap().catch((error: unknown) => { |
| 23 | if (refsErrorCode(error) === "not_found") return null; |
| 24 | throw error; |
| 25 | }), |
| 26 | repo.agents(), |
| 27 | refs.taskChoices(), |
| 28 | pull.threads(), |
| 29 | pull.timeline(), |
| 30 | pull.mentions(), |
| 31 | ]); |
| 32 | const comments = timeline.filter((event): event is ThreadComment => event.kind === "pull.commented" && event.data.thread !== undefined); |
| 33 | return { repo: repo.summary.name, number: pull.detail.number, codeMap, human: refs.viewer.agent === null, canResolve: refs.viewer.agent === null && repo.summary.can.write, agents, people: choices.people, threads, comments, mentions, now: nowSeconds() }; |
| 34 | } |
| 35 | |
| 36 | /** Line comments and thread replies from the map's code cards. They post here and the reader stays on the map. */ |
| 37 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 38 | const repo = await found(signedIn(context).repo(fullName(params))); |
| 39 | const pull = await found(repo.pull(Number(params.number))); |
| 40 | const form = await request.formData(); |
| 41 | const intent = formText(form, "intent"); |
| 42 | if (THREAD_INTENTS.has(intent)) { |
| 43 | const outcome = await threadAction(pull, intent, form); |
| 44 | return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent }); |
| 45 | } |
| 46 | if (intent === "inline-comment") { |
| 47 | const side = formText(form, "side") === "deletions" ? "deletions" : "additions"; |
| 48 | const endSide = formText(form, "endSide") === "deletions" ? "deletions" : "additions"; |
| 49 | const range = { start: Number(formText(form, "start")), side, end: Number(formText(form, "end")), endSide } as const; |
| 50 | const outcome = await pull.reviewComment({ revision: formText(form, "revision"), path: formText(form, "path"), range, body: formText(form, "body"), assign: formText(form, "assign") === "1" }); |
| 51 | return outcome.ok ? redirect(`${pullLink(pull.detail.repo, pull.detail.number)}/map`) : failed(outcome.problem, { intent }); |
| 52 | } |
| 53 | return failed({ code: "invalid", message: "Unknown review action." }, { intent }); |
| 54 | } |
| 55 | |
| 56 | export default function PullMap({ loaderData, actionData }: Route.ComponentProps) { |
| 57 | const { repo, number, codeMap, human, canResolve, agents, people, threads, comments, mentions, now } = loaderData; |
| 58 | const { pull } = useRouteLoaderData<typeof pullLoader>("routes/pull-layout")!; |
| 59 | const head = codeMap?.head ?? null; |
| 60 | const notes = useMemo<CardNotes | undefined>(() => { |
| 61 | if (!head) return undefined; |
| 62 | const open = threads.filter((thread) => thread.path && thread.range && thread.range.endSide === "additions" && !thread.outdated); |
| 63 | const review = { agents, people }; |
| 64 | return { |
| 65 | threadsAt: (path, line) => |
| 66 | open |
| 67 | .filter((thread) => thread.path === path && thread.range!.end === line) |
| 68 | .map((thread) => ( |
| 69 | <ReviewThreadCard |
| 70 | key={thread.id} |
| 71 | thread={thread} |
| 72 | comments={comments.filter((comment) => comment.data.thread === thread.id)} |
| 73 | pullBase={pullLink(repo, number)} |
| 74 | agents={agents} |
| 75 | canResolve={canResolve} |
| 76 | mentionsOf={(comment) => mentions.filter((mention) => mention.comment === comment)} |
| 77 | now={now} |
| 78 | /> |
| 79 | )), |
| 80 | composer: (path, line, close) => (human ? <LineComposer path={path} range={{ start: line, side: "additions", end: line, endSide: "additions" }} revision={head} review={review} onClose={close} /> : null), |
| 81 | }; |
| 82 | }, [agents, canResolve, comments, head, human, mentions, now, number, people, repo, threads]); |
| 83 | if (!codeMap) { |
| 84 | return ( |
| 85 | <LayerCard> |
| 86 | <Empty icon={<GitDiffIcon />} title="The branches of this pull request are gone" description="The map needs the head branch. Restore the branch, or read the conversation." contents={<LinkButton href={pullLink(repo, number)} variant="secondary">Open the conversation</LinkButton>} /> |
| 87 | </LayerCard> |
| 88 | ); |
| 89 | } |
| 90 | if (!codeMap.changes.length) { |
| 91 | return ( |
| 92 | <LayerCard> |
| 93 | <Empty icon={<GitDiffIcon />} title="No changes to map" description="This pull request changes no files yet. The map shows its changes after the next push." /> |
| 94 | </LayerCard> |
| 95 | ); |
| 96 | } |
| 97 | return ( |
| 98 | <> |
| 99 | {actionData?.error && <Banner variant="error" description={actionData.error} className="mb-3" />} |
| 100 | <CodeMapPanel repo={repo} summary={codeMap.map} changes={codeMap.changes} notes={notes} codeHref={`${pullLink(repo, number)}/files`} /> |
| 101 | </> |
| 102 | ); |
| 103 | } |