Open History to see the last commit
1import { useAtomCommand } from "~/state/use-atom-command";
2import type {
3 EnvironmentId,
4 PullRequestRef,
5 PullRequestReviewerCandidate,
6} from "@t3tools/contracts";
7import { CheckIcon, UserPlusIcon } from "lucide-react";
8import { useState } from "react";
9
10import { pullRequestEnvironment } from "~/state/pullRequests";
11import { useEnvironmentQuery } from "~/state/query";
12import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
13
14import { toastManager } from "../ui/toast";
15import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
16import { PullRequestActorLabel } from "./pullRequestPresentation";
17import { readableFailure } from "./pullRequestDetail.logic";
18
19function matches(candidate: PullRequestReviewerCandidate, query: string): boolean {
20 if (query.length === 0) return true;
21 const needle = query.toLowerCase();
22 return (
23 candidate.login.toLowerCase().includes(needle) ||
24 (candidate.name ?? "").toLowerCase().includes(needle)
25 );
26}
27
28export function PullRequestReviewerPicker({
29 environmentId,
30 reference,
31 allowed,
32}: {
33 environmentId: EnvironmentId;
34 reference: PullRequestRef;
35 allowed: boolean;
36}) {
37 const [open, setOpen] = useState(false);
38 const [query, setQuery] = useState("");
39 const [pending, setPending] = useState<string | null>(null);
40
41 const candidatesQuery = useEnvironmentQuery(
42 open ? pullRequestEnvironment.reviewerCandidates({ environmentId, input: reference }) : null,
43 );
44 const requestReviewers = useAtomCommand(pullRequestEnvironment.requestReviewers, {
45 reportFailure: false,
46 });
47
48 const candidates = (candidatesQuery.data?.candidates ?? []).filter((entry) =>
49 matches(entry, query),
50 );
51
52 const toggle = async (candidate: PullRequestReviewerCandidate) => {
53 if (pending !== null) return;
54 setPending(candidate.id);
55 const result = await requestReviewers({
56 environmentId,
57 input: {
58 ...reference,
59 reviewers: [{ id: candidate.id, kind: candidate.kind }],
60 requested: !candidate.isRequested,
61 },
62 });
63 setPending(null);
64 if (result._tag === "Failure") {
65 toastManager.add({
66 type: "error",
67 title: candidate.isRequested
68 ? `Could not take back the review request to ${candidate.login}`
69 : `Could not ask ${candidate.login} for a review`,
70 description: readableFailure(
71 squashAtomCommandFailure(result),
72 "The host refused it. Check that you have write access on this repository, and that they still have access to it.",
73 ),
74 });
75 return;
76 }
77 toastManager.add({
78 type: "success",
79 title: candidate.isRequested
80 ? `Review request to ${candidate.login} taken back`
81 : `Review requested from ${candidate.login}`,
82 });
83 };
84
85 return (
86 <PullRequestCandidatePicker
87 icon={<UserPlusIcon className="size-3.5" />}
88 label="Request a review"
89 allowed={allowed}
90 disabledReason="Asking someone to review needs write access on this repository"
91 open={open}
92 onOpenChange={setOpen}
93 query={query}
94 onQueryChange={setQuery}
95 searchLabel="Search people with access"
96 isPending={candidatesQuery.isPending && candidatesQuery.data === null}
97 error={candidatesQuery.data === null ? candidatesQuery.error : null}
98 candidates={candidates}
99 emptyLabel="Nobody else has access to this repository."
100 noMatchLabel="Nobody with access matches that."
101 errorLabel="The people with access could not be read."
102 truncated={candidatesQuery.data?.truncated === true}
103 truncatedLabel="This repository has more people with access than are listed here. Ask for the rest on the host."
104 candidateKey={(candidate) => `${candidate.kind}:${candidate.id}`}
105 disabled={pending !== null}
106 onSelect={(candidate) => void toggle(candidate)}
107 >
108 {(candidate) => (
109 <>
110 <PullRequestActorLabel actor={candidate} className="flex-1" />
111 {candidate.kind === "team" ? (
112 <span className="shrink-0 text-muted-foreground">team</span>
113 ) : null}
114 {candidate.isRequested ? (
115 <CheckIcon aria-label="Already asked" className="size-3.5 shrink-0" />
116 ) : null}
117 </>
118 )}
119 </PullRequestCandidatePicker>
120 );
121}