import { pullLink, refsErrorCode } from "@refs/backend/contract"; import { Banner, Empty, LayerCard, LinkButton } from "@refs/design-system"; import { GitDiffIcon } from "@refs/design-system/icons"; import { useMemo } from "react"; import { redirect, useRouteLoaderData } from "react-router"; import type { Route } from "./+types/pull-map"; import type { loader as pullLoader } from "./pull-layout"; import type { CardNotes } from "../components/code-map/code-card"; import { CodeMapPanel } from "../components/code-map/map-states"; import { LineComposer } from "../components/line-composer"; import { ReviewThreadCard, type ThreadComment } from "../components/review-thread"; import { THREAD_INTENTS, threadAction } from "../lib/threads"; import { failed, formText, found, signedIn, fullName, nowSeconds } from "../lib/session"; export const meta: Route.MetaFunction = ({ params }) => [{ title: `Map · Pull request #${params.number} · ${fullName(params)} · refs` }]; export async function loader({ params, context }: Route.LoaderArgs) { const refs = signedIn(context); const repo = await found(refs.repo(fullName(params))); const pull = await found(repo.pull(Number(params.number))); const [codeMap, agents, choices, threads, timeline, mentions] = await Promise.all([ pull.codeMap().catch((error: unknown) => { if (refsErrorCode(error) === "not_found") return null; throw error; }), repo.agents(), refs.taskChoices(), pull.threads(), pull.timeline(), pull.mentions(), ]); const comments = timeline.filter((event): event is ThreadComment => event.kind === "pull.commented" && event.data.thread !== undefined); 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() }; } /** Line comments and thread replies from the map's code cards. They post here and the reader stays on the map. */ export async function action({ request, params, context }: Route.ActionArgs) { const repo = await found(signedIn(context).repo(fullName(params))); const pull = await found(repo.pull(Number(params.number))); const form = await request.formData(); const intent = formText(form, "intent"); if (THREAD_INTENTS.has(intent)) { const outcome = await threadAction(pull, intent, form); return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent }); } if (intent === "inline-comment") { const side = formText(form, "side") === "deletions" ? "deletions" : "additions"; const endSide = formText(form, "endSide") === "deletions" ? "deletions" : "additions"; const range = { start: Number(formText(form, "start")), side, end: Number(formText(form, "end")), endSide } as const; const outcome = await pull.reviewComment({ revision: formText(form, "revision"), path: formText(form, "path"), range, body: formText(form, "body"), assign: formText(form, "assign") === "1" }); return outcome.ok ? redirect(`${pullLink(pull.detail.repo, pull.detail.number)}/map`) : failed(outcome.problem, { intent }); } return failed({ code: "invalid", message: "Unknown review action." }, { intent }); } export default function PullMap({ loaderData, actionData }: Route.ComponentProps) { const { repo, number, codeMap, human, canResolve, agents, people, threads, comments, mentions, now } = loaderData; const { pull } = useRouteLoaderData("routes/pull-layout")!; const head = codeMap?.head ?? null; const notes = useMemo(() => { if (!head) return undefined; const open = threads.filter((thread) => thread.path && thread.range && thread.range.endSide === "additions" && !thread.outdated); const review = { agents, people }; return { threadsAt: (path, line) => open .filter((thread) => thread.path === path && thread.range!.end === line) .map((thread) => ( comment.data.thread === thread.id)} pullBase={pullLink(repo, number)} agents={agents} canResolve={canResolve} mentionsOf={(comment) => mentions.filter((mention) => mention.comment === comment)} now={now} /> )), composer: (path, line, close) => (human ? : null), }; }, [agents, canResolve, comments, head, human, mentions, now, number, people, repo, threads]); if (!codeMap) { return ( } title="The branches of this pull request are gone" description="The map needs the head branch. Restore the branch, or read the conversation." contents={Open the conversation} /> ); } if (!codeMap.changes.length) { return ( } title="No changes to map" description="This pull request changes no files yet. The map shows its changes after the next push." /> ); } return ( <> {actionData?.error && } ); }