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

PullRequestReviewerPicker.tsx

122 lines4.3 KB

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 { useMemo, 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 );
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 = useMemo(
49 () => (candidatesQuery.data?.candidates ?? []).filter((entry) => matches(entry, query)),
50 [candidatesQuery.data, query],
51 );
52​
53 const toggle = async (candidate: PullRequestReviewerCandidate) => {
54 if (pending !== null) return;
55 setPending(candidate.id);
56 const result = await requestReviewers({
57 environmentId,
58 input: {
59 ...reference,
60 reviewers: [{ id: candidate.id, kind: candidate.kind }],
61 requested: !candidate.isRequested,
62 },
63 });
64 setPending(null);
65 if (result._tag === "Failure") {
66 toastManager.add({
67 type: "error",
68 title: candidate.isRequested
69 ? `Could not take back the review request to ${candidate.login}`
70 : `Could not ask ${candidate.login} for a review`,
71 description: readableFailure(
72 squashAtomCommandFailure(result),
73 "The host refused it. Check that you have write access on this repository, and that they still have access to it.",
74 ),
75 });
76 return;
77 }
78 toastManager.add({
79 type: "success",
80 title: candidate.isRequested
81 ? `Review request to ${candidate.login} taken back`
82 : `Review requested from ${candidate.login}`,
83 });
84 };
85​
86 return (
87 <PullRequestCandidatePicker
88 icon={<UserPlusIcon className="size-3.5" />}
89 label="Request a review"
90 allowed={allowed}
91 disabledReason="Asking someone to review needs write access on this repository"
92 open={open}
93 onOpenChange={setOpen}
94 query={query}
95 onQueryChange={setQuery}
96 searchLabel="Search people with access"
97 isPending={candidatesQuery.isPending && candidatesQuery.data === null}
98 error={candidatesQuery.data === null ? candidatesQuery.error : null}
99 candidates={candidates}
100 emptyLabel="Nobody else has access to this repository."
101 noMatchLabel="Nobody with access matches that."
102 errorLabel="The people with access could not be read."
103 truncated={candidatesQuery.data?.truncated === true}
104 truncatedLabel="This repository has more people with access than are listed here. Ask for the rest on the host."
105 candidateKey={(candidate) => `${candidate.kind}:${candidate.id}`}
106 disabled={pending !== null}
107 onSelect={(candidate) => void toggle(candidate)}
108 >
109 {(candidate) => (
110 <>
111 <PullRequestActorLabel actor={candidate} className="flex-1" />
112 {candidate.kind === "team" ? (
113 <span className="shrink-0 text-muted-foreground">team</span>
114 ) : null}
115 {candidate.isRequested ? (
116 <CheckIcon aria-label="Already asked" className="size-3.5 shrink-0" />
117 ) : null}
118 </>
119 )}
120 </PullRequestCandidatePicker>
121 );