Skip to content
refs
History
74 lines · 3.3 KBRaw
1import { Banner, Button, InputArea, Popover, Radio } from "@refs/design-system";
2import { CheckCircleIcon, XIcon } from "@refs/design-system/icons";
3import { useEffect, useRef, useState } from "react";
4import { useFetcher } from "react-router";
5
6const VERDICTS = [
7 { value: "comment", label: "Comment", description: "Give feedback without a verdict." },
8 { value: "approve", label: "Approve", description: "Give feedback and approve the merge of this revision." },
9 { value: "request-changes", label: "Request changes", description: "Give feedback that must be addressed before the merge." },
10] as const;
11
12/** The Review button of a pull request. It opens a form for a summary and a verdict on the revision `revision`. */
13export function FinishReview({ action, revision }: { action: string; revision: string }) {
14 const [open, setOpen] = useState(false);
15 const fetcher = useFetcher<{ error?: string | null }>();
16 const busy = fetcher.state !== "idle";
17 const sent = useRef(false);
18 useEffect(() => {
19 if (busy) sent.current = true;
20 else if (sent.current) {
21 sent.current = false;
22 if (!fetcher.data?.error) setOpen(false);
23 }
24 }, [busy, fetcher.data]);
25 return (
26 <Popover open={open} onOpenChange={setOpen}>
27 <Popover.Trigger
28 render={
29 <Button size="sm" variant="primary" icon={<CheckCircleIcon />}>
30 Review
31 </Button>
32 }
33 />
34 <Popover.Content align="end" className="w-[34rem] max-w-[calc(100vw-2rem)] p-0">
35 <fetcher.Form method="post" action={action} className="flex flex-col">
36 <input type="hidden" name="intent" value="review" />
37 <input type="hidden" name="revision" value={revision} />
38 <div className="flex items-center justify-between gap-3 px-4 pt-3 pb-2">
39 <Popover.Title className="text-base">Finish your review</Popover.Title>
40 <Button type="button" variant="ghost" shape="square" size="sm" aria-label="Close" onClick={() => setOpen(false)}>
41 <XIcon size={16} />
42 </Button>
43 </div>
44 <div className="flex flex-col gap-4 px-4 pb-4">
45 <InputArea
46 name="body"
47 rows={5}
48 autoFocus
49 aria-label="Summary"
50 placeholder="Summarize the revision and your verdict. Markdown works."
51 onKeyDown={(event) => {
52 if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) event.currentTarget.form?.requestSubmit();
53 }}
54 />
55 <Radio.Group name="verdict" defaultValue="comment" appearance="card" legend="Verdict">
56 {VERDICTS.map((verdict) => (
57 <Radio.Item key={verdict.value} value={verdict.value} label={verdict.label} description={verdict.description} />
58 ))}
59 </Radio.Group>
60 {fetcher.data?.error && <Banner variant="error" description={fetcher.data.error} />}
61 </div>
62 <div className="flex justify-end gap-2 px-4 py-3 shadow-[inset_0_1px_0_var(--hairline)]">
63 <Button type="button" variant="secondary" onClick={() => setOpen(false)}>
64 Cancel
65 </Button>
66 <Button type="submit" variant="primary" loading={busy}>
67 Submit review
68 </Button>
69 </div>
70 </fetcher.Form>
71 </Popover.Content>
72 </Popover>
73 );
74}