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 { toastManager } from "../ui/toast";
22import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
23import { PullRequestActorLabel } from "./pullRequestPresentation";
24import { readableFailure } from "./pullRequestDetail.logic";
25
26/** Long lists are common — an organisation repository lists everyone — so what arrived can be
27 * narrowed here. It narrows only what arrived: the host is asked once, when the menu opens. */
28function matches(candidate: PullRequestReviewerCandidate, query: string): boolean {
29 if (query.length === 0) return true;
30 const needle = query.toLowerCase();
31 return (
32 candidate.login.toLowerCase().includes(needle) ||
33 (candidate.name ?? "").toLowerCase().includes(needle)
34 );
35}
36
37export function PullRequestReviewerPicker({
38 environmentId,
39 reference,
40 allowed,
41 onRequested,
42}: {
43 environmentId: EnvironmentId;
44 reference: PullRequestRef;
45 /** False where the host would refuse this account's request, which is worth saying rather than
46 * hiding: the control disabled with a reason answers the question its absence would raise. */
47 allowed: boolean;
48 /** The detail carries who is requested, so it is re-read once the host has taken the change. */
49 onRequested: () => void;
50}) {
51 const [open, setOpen] = useState(false);
52 const [query, setQuery] = useState("");
53 const [pending, setPending] = useState<string | null>(null);
54
55 // Mounted with the menu closed, so nothing is asked of the host until it opens.
56 const candidatesQuery = useEnvironmentQuery(
57 open ? pullRequestEnvironment.reviewerCandidates({ environmentId, input: reference }) : null,
58 );
59 const requestReviewers = useAtomCommand(pullRequestEnvironment.requestReviewers, {
60 reportFailure: false,
61 });
62
63 const candidates = useMemo(
64 () => (candidatesQuery.data?.candidates ?? []).filter((entry) => matches(entry, query)),
65 [candidatesQuery.data, query],
66 );
67
68 const toggle = async (candidate: PullRequestReviewerCandidate) => {
69 if (pending !== null) return;
70 setPending(candidate.id);
71 const result = await requestReviewers({
72 environmentId,
73 input: {
74 ...reference,
75 reviewers: [{ id: candidate.id, kind: candidate.kind }],
76 requested: !candidate.isRequested,
77 },
78 });
79 setPending(null);
80 if (result._tag === "Failure") {
81 toastManager.add({
82 type: "error",
83 title: candidate.isRequested
84 ? `Could not take back the review request to ${candidate.login}`
85 : `Could not ask ${candidate.login} for a review`,
86 description: readableFailure(
87 squashAtomCommandFailure(result),
88 "The host refused it. Check that you have write access on this repository, and that they still have access to it.",
89 ),
90 });
91 return;
92 }
93 toastManager.add({
94 type: "success",
95 title: candidate.isRequested
96 ? `Review request to ${candidate.login} taken back`
97 : `Review requested from ${candidate.login}`,
98 });
99 onRequested();
100 candidatesQuery.refresh();
101 };
102
103 return (
104 <PullRequestCandidatePicker
105 icon={<UserPlusIcon className="size-3.5" />}
106 label="Request a review"
107 allowed={allowed}
108 disabledReason="Asking someone to review needs write access on this repository"
109 open={open}
110 onOpenChange={setOpen}
111 query={query}
112 onQueryChange={setQuery}
113 searchLabel="Search people with access"
114 isPending={candidatesQuery.isPending}
115 error={candidatesQuery.error}
116 candidates={candidates}
117 emptyLabel="Nobody else has access to this repository."
118 noMatchLabel="Nobody with access matches that."
119 errorLabel="The people with access could not be read."
120 truncated={candidatesQuery.data?.truncated === true}
121 truncatedLabel="This repository has more people with access than are listed here. Ask for the rest on the host."
122 candidateKey={(candidate) => `${candidate.kind}:${candidate.id}`}
123 disabled={pending !== null}
124 onSelect={(candidate) => void toggle(candidate)}
125 >
126 {(candidate) => (
127 <>
128 <PullRequestActorLabel actor={candidate} className="min-w-0 flex-1 truncate" />
129 {candidate.kind === "team" ? (
130 <span className="shrink-0 text-muted-foreground">team</span>
131 ) : null}
132 {candidate.isRequested ? (
133 <CheckIcon aria-label="Already asked" className="size-3.5 shrink-0" />
134 ) : null}
135 </>
136 )}
137 </PullRequestCandidatePicker>
138 );
139}