214 lines · 10.7 KBRaw
| 1 | import { AgentAvatar, Ago, Banner, Button, InputArea, LayerCard } from "@refs/design-system"; |
| 2 | import { GitMergeIcon, GitPullRequestIcon, TextAaIcon } from "@refs/design-system/icons"; |
| 3 | import { MAX_BODY_LENGTH, pullLink, type Attribution, type MentionState, type OnlineAgent, type TimelineItem } from "@refs/backend/contract"; |
| 4 | import { Fragment, useState } from "react"; |
| 5 | import { Link, data, useFetcher, useRouteLoaderData } from "react-router"; |
| 6 | import type { Route } from "./+types/pull"; |
| 7 | import type { loader as pullLoader } from "./pull-layout"; |
| 8 | import { useLive } from "../components/live"; |
| 9 | import { MentionStates, MentionTools, RUN_STATES, RunRows, agentsOn } from "../components/mentions"; |
| 10 | import { MergeBox } from "../components/merge-box"; |
| 11 | import { CI_STATE_LABEL, CiStatusIcon } from "../components/ci-status"; |
| 12 | import { ReviewThreadCard, type ThreadComment } from "../components/review-thread"; |
| 13 | import { THREAD_INTENTS, threadAction } from "../lib/threads"; |
| 14 | import { failed, formText, found, signedIn, fullName } from "../lib/session"; |
| 15 | import { useOnSuccess } from "../components/use-on-success"; |
| 16 | import { ActorMark, ActorName, AgentName, EventLine, Field, Timeline, TimelineRow, openerOf, toneOf } from "../components/timeline"; |
| 17 | |
| 18 | export 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 refs = signedIn(context); |
| 22 | const [timeline, agents, mentions, threads, mergeability, checks] = await Promise.all([ |
| 23 | pull.timeline(), |
| 24 | repo.agents(), |
| 25 | pull.mentions(), |
| 26 | pull.threads(), |
| 27 | pull.detail.status === "open" ? pull.mergeability() : null, |
| 28 | repo.ci().runs({ pull: pull.detail.number, limit: 5 }), |
| 29 | ]); |
| 30 | return { timeline, agents, mentions, threads, mergeability, checks, canResolve: refs.viewer.agent === null && repo.summary.can.write }; |
| 31 | } |
| 32 | |
| 33 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 34 | const repo = await found(signedIn(context).repo(fullName(params))); |
| 35 | const pull = await found(repo.pull(Number(params.number))); |
| 36 | const form = await request.formData(); |
| 37 | const intent = formText(form, "intent"); |
| 38 | if (intent === "merge") { |
| 39 | const outcome = await pull.merge(); |
| 40 | return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent }); |
| 41 | } |
| 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 !== "comment") return data({ intent, error: "Unknown action." }, { status: 400 }); |
| 47 | const then = formText(form, "toggle") === "1" ? (pull.detail.status === "open" ? "closed" : "open") : undefined; |
| 48 | const outcome = await pull.comment(formText(form, "body"), then, formText(form, "assign") === "1"); |
| 49 | return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent }); |
| 50 | } |
| 51 | |
| 52 | function Comment({ by, verb, at, html, now, mentions = [] }: { by: Attribution; verb: string; at: number; html: string | null; now: number; mentions?: readonly MentionState[] }) { |
| 53 | return ( |
| 54 | <TimelineRow tone={toneOf(by)}> |
| 55 | <LayerCard> |
| 56 | <LayerCard.Secondary className="font-normal"> |
| 57 | <ActorMark by={by} /> |
| 58 | <ActorName by={by} /> |
| 59 | <span>{verb}</span> |
| 60 | <Ago at={at} now={now} /> |
| 61 | </LayerCard.Secondary> |
| 62 | <LayerCard.Primary className="px-4 py-3">{html ? <article className="markdown" dangerouslySetInnerHTML={{ __html: html }} /> : <p className="m-0 text-muted italic">No description provided.</p>}</LayerCard.Primary> |
| 63 | <MentionStates mentions={mentions} /> |
| 64 | </LayerCard> |
| 65 | </TimelineRow> |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | function PullEvent({ event, now }: { event: Exclude<TimelineItem, { kind: "pull.commented" | "pull.opened" }>; now: number }) { |
| 70 | const [icon, text] = |
| 71 | event.kind === "pull.merged" |
| 72 | ? [<GitMergeIcon size={14} className="text-success" />, <>merged commit <code className="font-mono text-xs">{event.data.head.slice(0, 7)}</code></>] |
| 73 | : event.kind === "pull.closed" |
| 74 | ? [<GitPullRequestIcon size={14} className="text-danger" />, "closed this"] |
| 75 | : event.kind === "pull.reopened" |
| 76 | ? [<GitPullRequestIcon size={14} className="text-success" />, "reopened this"] |
| 77 | : event.kind === "pull.renamed" |
| 78 | ? [<TextAaIcon size={14} />, <>changed the title from <s>{event.data.from}</s> to <span className="text-ink">{event.data.to}</span></>] |
| 79 | : [null, null]; |
| 80 | if (!text) return null; |
| 81 | return ( |
| 82 | <EventLine by={event.by} icon={icon}> |
| 83 | <span>{text}</span> |
| 84 | <Ago at={event.at} now={now} /> |
| 85 | </EventLine> |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | function Composer({ status, agents }: { status: "open" | "closed" | "merged"; agents: readonly OnlineAgent[] }) { |
| 90 | const fetcher = useFetcher<typeof action>(); |
| 91 | const [body, setBody] = useState(""); |
| 92 | const [round, setRound] = useState(0); |
| 93 | const busy = fetcher.state !== "idle"; |
| 94 | useOnSuccess(fetcher, () => { |
| 95 | setBody(""); |
| 96 | setRound((value) => value + 1); |
| 97 | }); |
| 98 | const toggleLabel = status === "open" ? (body ? "Close with comment" : "Close pull request") : body ? "Reopen with comment" : "Reopen pull request"; |
| 99 | |
| 100 | return ( |
| 101 | <TimelineRow tone="event"> |
| 102 | <LayerCard className="p-4"> |
| 103 | <fetcher.Form key={round} method="post" className="flex flex-col gap-3"> |
| 104 | <input type="hidden" name="intent" value="comment" /> |
| 105 | <InputArea |
| 106 | label="Add a comment" |
| 107 | name="body" |
| 108 | value={body} |
| 109 | onValueChange={setBody} |
| 110 | maxLength={MAX_BODY_LENGTH} |
| 111 | rows={4} |
| 112 | description="Markdown is supported. Mentioned agents receive it here, and can be assigned this pull request's branch." |
| 113 | /> |
| 114 | <MentionTools agents={agents} body={body} onBody={setBody} canAssign={status === "open"} /> |
| 115 | {fetcher.data?.error && <Banner variant="error" description={fetcher.data.error} />} |
| 116 | <div className="flex flex-wrap justify-end gap-2"> |
| 117 | {status !== "merged" && ( |
| 118 | <Button type="submit" name="toggle" value="1" variant="secondary" icon={<GitPullRequestIcon size={16} />} loading={busy && fetcher.formData?.get("toggle") === "1"}> |
| 119 | {toggleLabel} |
| 120 | </Button> |
| 121 | )} |
| 122 | <Button type="submit" variant="primary" disabled={!body.trim()} loading={busy && fetcher.formData?.get("toggle") !== "1"}> |
| 123 | Comment |
| 124 | </Button> |
| 125 | </div> |
| 126 | </fetcher.Form> |
| 127 | </LayerCard> |
| 128 | </TimelineRow> |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | export default function PullConversation({ loaderData }: Route.ComponentProps) { |
| 133 | const { timeline, agents, mentions, threads, mergeability, checks, canResolve } = loaderData; |
| 134 | const threadById = new Map(threads.map((thread) => [thread.id, thread])); |
| 135 | const threadComments = (thread: (typeof threads)[number]) => timeline.filter((event): event is ThreadComment => event.kind === "pull.commented" && event.data.thread === thread.id); |
| 136 | const mentionsOf = (comment: string) => mentions.filter((mention) => mention.comment === comment && !RUN_STATES.has(mention.state)); |
| 137 | const runsOf = (comment: string) => mentions.filter((mention) => mention.comment === comment && RUN_STATES.has(mention.state)); |
| 138 | const { pull, now } = useRouteLoaderData<typeof pullLoader>("routes/pull-layout")!; |
| 139 | const working = agentsOn(agents, pull.head); |
| 140 | useLive(pull.repo); |
| 141 | return ( |
| 142 | <div className="grid items-start gap-8 lg:grid-cols-[minmax(0,1fr)_17rem]"> |
| 143 | <Timeline> |
| 144 | <Comment by={openerOf(pull)} verb="opened this" at={pull.createdAt} html={pull.bodyHtml} now={now} /> |
| 145 | {timeline.map((event) => |
| 146 | event.kind === "pull.opened" ? null : event.kind === "pull.commented" && event.data.thread !== undefined ? ( |
| 147 | threadById.get(event.data.thread)?.comments[0] === event.id ? ( |
| 148 | <TimelineRow key={event.id} tone={toneOf(event.by)}> |
| 149 | <ReviewThreadCard |
| 150 | thread={threadById.get(event.data.thread)!} |
| 151 | comments={threadComments(threadById.get(event.data.thread)!)} |
| 152 | pullBase={pullLink(pull.repo, pull.number)} |
| 153 | agents={agents} |
| 154 | canResolve={canResolve} |
| 155 | mentionsOf={(comment) => mentions.filter((mention) => mention.comment === comment)} |
| 156 | now={now} |
| 157 | /> |
| 158 | </TimelineRow> |
| 159 | ) : null |
| 160 | ) : event.kind === "pull.commented" ? ( |
| 161 | <Fragment key={event.id}> |
| 162 | <Comment by={event.by} verb="commented" at={event.at} html={event.data.html} now={now} mentions={mentionsOf(event.id)} /> |
| 163 | <RunRows mentions={runsOf(event.id)} /> |
| 164 | </Fragment> |
| 165 | ) : ( |
| 166 | <PullEvent key={event.id} event={event as Exclude<TimelineItem, { kind: "pull.commented" | "pull.opened" }>} now={now} /> |
| 167 | ), |
| 168 | )} |
| 169 | {(mergeability || pull.status === "merged") && ( |
| 170 | <TimelineRow tone="event" offset="top-5"> |
| 171 | <MergeBox mergeability={mergeability} base={pull.base} head={pull.head} canMerge={canResolve} /> |
| 172 | </TimelineRow> |
| 173 | )} |
| 174 | <Composer status={pull.status} agents={agents} /> |
| 175 | </Timeline> |
| 176 | <aside className="flex flex-col"> |
| 177 | <Field label="Checks"> |
| 178 | {checks.length ? ( |
| 179 | <ul className="m-0 flex list-none flex-col gap-2 p-0"> |
| 180 | {checks.map((run) => ( |
| 181 | <li key={run.id}> |
| 182 | <Link to={`/${pull.repo}/runs/${run.id}`} className="flex items-center gap-2 text-ink hover:text-accent-text"> |
| 183 | <CiStatusIcon state={run.state} size={14} /> |
| 184 | <span className="min-w-0 flex-1 truncate">{CI_STATE_LABEL[run.state]}</span> |
| 185 | <span className="font-mono text-xs text-faint">{run.sha.slice(0, 7)}</span> |
| 186 | </Link> |
| 187 | </li> |
| 188 | ))} |
| 189 | </ul> |
| 190 | ) : ( |
| 191 | <span className="text-muted">No CI runs for this pull request.</span> |
| 192 | )} |
| 193 | </Field> |
| 194 | <Field label="Agents"> |
| 195 | {working.length ? ( |
| 196 | <ul className="m-0 flex list-none flex-col gap-2 p-0"> |
| 197 | {working.map((agent) => ( |
| 198 | <li key={agent.session} className="flex items-start gap-2"> |
| 199 | <AgentAvatar name={agent.runtime} size="xs" className="mt-0.5" /> |
| 200 | <span className="flex min-w-0 flex-col leading-tight"> |
| 201 | <AgentName agent={agent} /> |
| 202 | {agent.machine && <span className="truncate text-xs text-faint">{agent.machine}</span>} |
| 203 | </span> |
| 204 | </li> |
| 205 | ))} |
| 206 | </ul> |
| 207 | ) : ( |
| 208 | <span className="text-muted">No agent is working on {pull.head}.</span> |
| 209 | )} |
| 210 | </Field> |
| 211 | </aside> |
| 212 | </div> |
| 213 | ); |
| 214 | } |