import { AgentAvatar, Ago, Banner, Button, InputArea, LayerCard } from "@refs/design-system"; import { GitMergeIcon, GitPullRequestIcon, TextAaIcon } from "@refs/design-system/icons"; import { MAX_BODY_LENGTH, pullLink, type Attribution, type MentionState, type OnlineAgent, type TimelineItem } from "@refs/backend/contract"; import { Fragment, useState } from "react"; import { Link, data, useFetcher, useRouteLoaderData } from "react-router"; import type { Route } from "./+types/pull"; import type { loader as pullLoader } from "./pull-layout"; import { useLive } from "../components/live"; import { MentionStates, MentionTools, RUN_STATES, RunRows, agentsOn } from "../components/mentions"; import { MergeBox } from "../components/merge-box"; import { CI_STATE_LABEL, CiStatusIcon } from "../components/ci-status"; import { ReviewThreadCard, type ThreadComment } from "../components/review-thread"; import { THREAD_INTENTS, threadAction } from "../lib/threads"; import { failed, formText, found, signedIn, fullName } from "../lib/session"; import { useOnSuccess } from "../components/use-on-success"; import { ActorMark, ActorName, AgentName, EventLine, Field, Timeline, TimelineRow, openerOf, toneOf } from "../components/timeline"; export async function loader({ params, context }: Route.LoaderArgs) { const repo = await found(signedIn(context).repo(fullName(params))); const pull = await found(repo.pull(Number(params.number))); const refs = signedIn(context); const [timeline, agents, mentions, threads, mergeability, checks] = await Promise.all([ pull.timeline(), repo.agents(), pull.mentions(), pull.threads(), pull.detail.status === "open" ? pull.mergeability() : null, repo.ci().runs({ pull: pull.detail.number, limit: 5 }), ]); return { timeline, agents, mentions, threads, mergeability, checks, canResolve: refs.viewer.agent === null && repo.summary.can.write }; } 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 (intent === "merge") { const outcome = await pull.merge(); return outcome.ok ? { intent, error: null } : failed(outcome.problem, { 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 !== "comment") return data({ intent, error: "Unknown action." }, { status: 400 }); const then = formText(form, "toggle") === "1" ? (pull.detail.status === "open" ? "closed" : "open") : undefined; const outcome = await pull.comment(formText(form, "body"), then, formText(form, "assign") === "1"); return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent }); } function Comment({ by, verb, at, html, now, mentions = [] }: { by: Attribution; verb: string; at: number; html: string | null; now: number; mentions?: readonly MentionState[] }) { return ( {verb} {html ?
:

No description provided.

} ); } function PullEvent({ event, now }: { event: Exclude; now: number }) { const [icon, text] = event.kind === "pull.merged" ? [, <>merged commit {event.data.head.slice(0, 7)}] : event.kind === "pull.closed" ? [, "closed this"] : event.kind === "pull.reopened" ? [, "reopened this"] : event.kind === "pull.renamed" ? [, <>changed the title from {event.data.from} to {event.data.to}] : [null, null]; if (!text) return null; return ( {text} ); } function Composer({ status, agents }: { status: "open" | "closed" | "merged"; agents: readonly OnlineAgent[] }) { const fetcher = useFetcher(); const [body, setBody] = useState(""); const [round, setRound] = useState(0); const busy = fetcher.state !== "idle"; useOnSuccess(fetcher, () => { setBody(""); setRound((value) => value + 1); }); const toggleLabel = status === "open" ? (body ? "Close with comment" : "Close pull request") : body ? "Reopen with comment" : "Reopen pull request"; return ( {fetcher.data?.error && }
{status !== "merged" && ( )}
); } export default function PullConversation({ loaderData }: Route.ComponentProps) { const { timeline, agents, mentions, threads, mergeability, checks, canResolve } = loaderData; const threadById = new Map(threads.map((thread) => [thread.id, thread])); const threadComments = (thread: (typeof threads)[number]) => timeline.filter((event): event is ThreadComment => event.kind === "pull.commented" && event.data.thread === thread.id); const mentionsOf = (comment: string) => mentions.filter((mention) => mention.comment === comment && !RUN_STATES.has(mention.state)); const runsOf = (comment: string) => mentions.filter((mention) => mention.comment === comment && RUN_STATES.has(mention.state)); const { pull, now } = useRouteLoaderData("routes/pull-layout")!; const working = agentsOn(agents, pull.head); useLive(pull.repo); return (
{timeline.map((event) => event.kind === "pull.opened" ? null : event.kind === "pull.commented" && event.data.thread !== undefined ? ( threadById.get(event.data.thread)?.comments[0] === event.id ? ( mentions.filter((mention) => mention.comment === comment)} now={now} /> ) : null ) : event.kind === "pull.commented" ? ( ) : ( } now={now} /> ), )} {(mergeability || pull.status === "merged") && ( )}
); }