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