Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestReviewerPicker.tsx

188 lines7.0 KB

1/**
2 * Asking someone to review, from the row that says who is already reviewing.
3 *
4 * The people who may be asked are read only once this menu opens: on a large repository that is
5 * a list of everyone with access, which is worth a request when somebody wants it and worth
6 * nothing on every pull request they merely open.
7 */
8import type {
9 EnvironmentId,
10 PullRequestRef,
11 PullRequestReviewerCandidate,
12} from "@t3tools/contracts";
13import { CheckIcon, UserPlusIcon } from "lucide-react";
14import { useMemo, useState } from "react";
15​
16import { pullRequestEnvironment } from "~/state/pullRequests";
17import { useEnvironmentQuery } from "~/state/query";
18import { useAtomCommand } from "~/state/use-atom-command";
19import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
20​
21import { Button } from "../ui/button";
22import { Input } from "../ui/input";
23import { Menu, MenuPopup, MenuTrigger } from "../ui/menu";
24import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
25import { toastManager } from "../ui/toast";
26import { PullRequestPeopleGhost } from "./PullRequestGhosts";
27import { PullRequestActorLabel } from "./pullRequestPresentation";
28import { readableFailure } from "./pullRequestDetail.logic";
29​
30/** Long lists are common — an organisation repository lists everyone — so what arrived can be
31 * narrowed here. It narrows only what arrived: the host is asked once, when the menu opens. */
32function matches(candidate: PullRequestReviewerCandidate, query: string): boolean {
33 if (query.length === 0) return true;
34 const needle = query.toLowerCase();
35 return (
36 candidate.login.toLowerCase().includes(needle) ||
37 (candidate.name ?? "").toLowerCase().includes(needle)
38 );
40​
41export function PullRequestReviewerPicker({
42 environmentId,
43 reference,
44 allowed,
45 onRequested,
46}: {
47 environmentId: EnvironmentId;
48 reference: PullRequestRef;
49 /** False where the host would refuse this account's request, which is worth saying rather than
50 * hiding: the control disabled with a reason answers the question its absence would raise. */
51 allowed: boolean;
52 /** The detail carries who is requested, so it is re-read once the host has taken the change. */
53 onRequested: () => void;
54}) {
55 const [open, setOpen] = useState(false);
56 const [query, setQuery] = useState("");
57 const [pending, setPending] = useState<string | null>(null);
58​
59 // Mounted with the menu closed, so nothing is asked of the host until it opens.
60 const candidatesQuery = useEnvironmentQuery(
61 open ? pullRequestEnvironment.reviewerCandidates({ environmentId, input: reference }) : null,
62 );
63 const requestReviewers = useAtomCommand(pullRequestEnvironment.requestReviewers, {
64 reportFailure: false,
65 });
66​
67 const candidates = useMemo(
68 () => (candidatesQuery.data?.candidates ?? []).filter((entry) => matches(entry, query)),
69 [candidatesQuery.data, query],
70 );
71​
72 const toggle = async (candidate: PullRequestReviewerCandidate) => {
73 if (pending !== null) return;
74 setPending(candidate.id);
75 const result = await requestReviewers({
76 environmentId,
77 input: {
78 ...reference,
79 reviewers: [{ id: candidate.id, kind: candidate.kind }],
80 requested: !candidate.isRequested,
81 },
82 });
83 setPending(null);
84 if (result._tag === "Failure") {
85 toastManager.add({
86 type: "error",
87 title: candidate.isRequested
88 ? `Could not take back the review request to ${candidate.login}`
89 : `Could not ask ${candidate.login} for a review`,
90 description: readableFailure(
91 squashAtomCommandFailure(result),
92 "The host refused it. Check that you have write access on this repository, and that they still have access to it.",
93 ),
94 });
95 return;
96 }
97 toastManager.add({
98 type: "success",
99 title: candidate.isRequested
100 ? `Review request to ${candidate.login} taken back`
101 : `Review requested from ${candidate.login}`,
102 });
103 onRequested();
104 candidatesQuery.refresh();
105 };
106​
107 if (!allowed) {
108 return (
109 <Tooltip>
110 <TooltipTrigger
111 render={
112 <Button size="icon-xs" variant="ghost" disabled aria-label="Request a review">
113 <UserPlusIcon className="size-3.5" />
114 </Button>
115 }
116 />
117 <TooltipPopup side="bottom">
118 Asking someone to review needs write access on this repository
119 </TooltipPopup>
120 </Tooltip>
121 );
122 }
123​
124 return (
125 <Menu open={open} onOpenChange={setOpen}>
126 <MenuTrigger
127 render={
128 <Button size="icon-xs" variant="ghost" aria-label="Request a review">
129 <UserPlusIcon className="size-3.5" />
130 </Button>
131 }
132 />
133 <MenuPopup align="start" side="bottom" className="w-72 p-0">
134 <div className="border-b border-border/60 p-2">
135 <Input
136 autoFocus
137 value={query}
138 onChange={(event) => setQuery(event.currentTarget.value)}
139 placeholder="Search people with access"
140 aria-label="Search people with access"
141 size="compact"
142 />
143 </div>
144 <div className="max-h-72 overflow-y-auto p-1">
145 {candidatesQuery.isPending ? (
146 <PullRequestPeopleGhost rows={4} />
147 ) : candidatesQuery.error !== null ? (
148 <p className="p-2 text-xs text-muted-foreground">
149 The people with access could not be read. {candidatesQuery.error}
150 </p>
151 ) : candidates.length === 0 ? (
152 <p className="p-2 text-xs text-muted-foreground">
153 {query.length > 0
154 ? "Nobody with access matches that."
155 : "Nobody else has access to this repository."}
156 </p>
157 ) : (
158 candidates.map((candidate) => (
159 <button
160 key={`${candidate.kind}:${candidate.id}`}
161 type="button"
162 disabled={pending !== null}
163 onClick={() => void toggle(candidate)}
164 className="flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-xs hover:bg-accent/60 disabled:opacity-60"
165 >
166 <PullRequestActorLabel actor={candidate} className="min-w-0 flex-1 truncate" />
167 {candidate.kind === "team" ? (
168 <span className="shrink-0 text-muted-foreground">team</span>
169 ) : null}
170 {candidate.isRequested ? (
171 <CheckIcon aria-label="Already asked" className="size-3.5 shrink-0" />
172 ) : null}
173 </button>
174 ))
175 )}
176 {candidatesQuery.data?.truncated ? (
177 // Typing filters what arrived; it does not ask the host again, so this says what the
178 // list is rather than offering a search that would find nothing further.
179 <p className="px-2 py-1.5 text-xs text-muted-foreground">
180 This repository has more people with access than are listed here. Ask for the rest on
181 the host.
182 </p>
183 ) : null}
184 </div>
185 </MenuPopup>
186 </Menu>
187 );