66 lines · 3.4 KBRaw
| 1 | import { Button, DropdownMenu, InputArea } from "@refs/design-system"; |
| 2 | import { UserIcon, XIcon } from "@refs/design-system/icons"; |
| 3 | import { useState } from "react"; |
| 4 | import { Form } from "react-router"; |
| 5 | import type { ReviewContext } from "./diff-view"; |
| 6 | import { MentionTools } from "./mentions"; |
| 7 | |
| 8 | export type LineSide = "deletions" | "additions"; |
| 9 | export type LineSelection = { start: number; side: LineSide; end: number; endSide: LineSide }; |
| 10 | |
| 11 | function label(side: LineSide, line: number): string { |
| 12 | return `${side === "deletions" ? "L" : "R"}${line}`; |
| 13 | } |
| 14 | |
| 15 | /** The form for a review comment on a range of lines. It posts to the page's action with the `inline-comment` intent. */ |
| 16 | export function LineComposer({ path, range, revision, review, onClose }: { path: string; range: LineSelection; revision: string; review: Pick<ReviewContext, "agents" | "people">; onClose: () => void }) { |
| 17 | const [body, setBody] = useState(""); |
| 18 | const single = range.start === range.end && range.side === range.endSide; |
| 19 | const insert = (text: string) => setBody(`${body}${body && !/\s$/.test(body) ? " " : ""}${text} `); |
| 20 | return ( |
| 21 | <div className="bg-raised p-4 font-sans shadow-[inset_0_1px_0_var(--hairline),inset_0_-1px_0_var(--hairline)]"> |
| 22 | <Form method="post" className="mx-auto flex max-w-3xl flex-col gap-3"> |
| 23 | <input type="hidden" name="intent" value="inline-comment" /> |
| 24 | <input type="hidden" name="revision" value={revision} /> |
| 25 | <input type="hidden" name="path" value={path} /> |
| 26 | <input type="hidden" name="start" value={range.start} /> |
| 27 | <input type="hidden" name="side" value={range.side} /> |
| 28 | <input type="hidden" name="end" value={range.end} /> |
| 29 | <input type="hidden" name="endSide" value={range.endSide} /> |
| 30 | <div className="flex items-center justify-between gap-3"> |
| 31 | <strong className="text-compact font-semibold"> |
| 32 | Comment on {single ? `line ${label(range.side, range.start)}` : `lines ${label(range.side, range.start)}–${label(range.endSide, range.end)}`} |
| 33 | </strong> |
| 34 | <Button type="button" variant="ghost" size="sm" shape="square" className="hit-area" aria-label="Cancel line comment" onClick={onClose}> |
| 35 | <XIcon /> |
| 36 | </Button> |
| 37 | </div> |
| 38 | <InputArea label="Review comment" name="body" value={body} onValueChange={setBody} rows={4} required autoFocus description="Markdown is supported." /> |
| 39 | <div className="flex flex-wrap items-center justify-between gap-3"> |
| 40 | <div className="flex flex-wrap items-center gap-3"> |
| 41 | <DropdownMenu> |
| 42 | <DropdownMenu.Trigger |
| 43 | render={ |
| 44 | <Button type="button" variant="secondary" size="sm" icon={<UserIcon size={16} />} disabled={review.people.length === 0}> |
| 45 | Mention teammate |
| 46 | </Button> |
| 47 | } |
| 48 | /> |
| 49 | <DropdownMenu.Content align="start"> |
| 50 | {review.people.map((person) => ( |
| 51 | <DropdownMenu.Item key={person.id} onClick={() => insert(`@${person.name}`)}> |
| 52 | {person.name} |
| 53 | </DropdownMenu.Item> |
| 54 | ))} |
| 55 | </DropdownMenu.Content> |
| 56 | </DropdownMenu> |
| 57 | <MentionTools agents={review.agents} body={body} onBody={setBody} canAssign /> |
| 58 | </div> |
| 59 | <Button type="submit" variant="primary" disabled={!body.trim()}> |
| 60 | Add review comment |
| 61 | </Button> |
| 62 | </div> |
| 63 | </Form> |
| 64 | </div> |
| 65 | ); |
| 66 | } |