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

PullRequestReviewerPicker.tsx

153 lines5.6 KB

1import { useAtomCommand } from "~/state/use-atom-command";
2import type {
3 EnvironmentId,
4 PullRequestRef,
5 PullRequestReviewerCandidate,
6} from "@t3tools/contracts";
7import {
8 hostReviewerQuery,
9 reviewerCandidateView,
10 type ReviewerTruncation,
11} from "@t3tools/client-runtime/pull-request-reviewers";
12import { CheckIcon, UserPlusIcon } from "lucide-react";
13import { useState } from "react";
14​
15import { pullRequestEnvironment } from "~/state/pullRequests";
16import { useEnvironmentQuery } from "~/state/query";
17import { useDebouncedValue } from "~/state/queries";
18import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
19​
20import { toastManager } from "../ui/toast";
21import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
22import { PullRequestActorLabel } from "./pullRequestPresentation";
23import { readableFailure } from "./pullRequestDetail.logic";
24​
25const TRUNCATED_LABELS: Record<ReviewerTruncation, (error: string | null) => string> = {
26 unsearchable: () =>
27 "This repository has more people with access than are listed here. Ask for the rest on the host.",
28 searchable: () =>
29 "This repository has more people with access than are listed here. Search to find them.",
30 "search-failed": (error) =>
31 `Only the people already listed are matched: the host search failed. ${error ?? ""}`,
32 "search-truncated": () =>
33 "More people match than the host returned. Keep typing to narrow it down.",
34};
35​
36export function PullRequestReviewerPicker({
37 environmentId,
38 reference,
39 allowed,
40 searchable,
41}: {
42 environmentId: EnvironmentId;
43 reference: PullRequestRef;
44 allowed: boolean;
45 searchable: boolean;
46}) {
47 const [open, setOpen] = useState(false);
48 const [query, setQuery] = useState("");
49 const [pending, setPending] = useState<string | null>(null);
50​
51 const candidatesQuery = useEnvironmentQuery(
52 open ? pullRequestEnvironment.reviewerCandidates({ environmentId, input: reference }) : null,
53 );
54 // Past the first page the host is asked, since what was loaded cannot answer for the rest.
55 const debouncedQuery = useDebouncedValue(query.trim(), 250);
56 const searchInput = { loaded: candidatesQuery.data, searchable, query, debouncedQuery };
57 const hostQuery = hostReviewerQuery(searchInput);
58 const searchResult = useEnvironmentQuery(
59 open && hostQuery !== null
60 ? pullRequestEnvironment.reviewerSearch({
61 environmentId,
62 input: { ...reference, query: hostQuery },
63 })
64 : null,
65 );
66 const requestReviewers = useAtomCommand(pullRequestEnvironment.requestReviewers, {
67 reportFailure: false,
68 });
69​
70 const { candidates, searchPending, searchError, truncation } = reviewerCandidateView({
71 ...searchInput,
72 search: searchResult,
73 });
74​
75 const toggle = async (candidate: PullRequestReviewerCandidate) => {
76 if (pending !== null) return;
77 setPending(candidate.id);
78 const result = await requestReviewers({
79 environmentId,
80 input: {
81 ...reference,
82 reviewers: [{ id: candidate.id, kind: candidate.kind }],
83 requested: !candidate.isRequested,
84 },
85 });
86 setPending(null);
87 if (result._tag === "Failure") {
88 toastManager.add({
89 type: "error",
90 title: candidate.isRequested
91 ? `Could not take back the review request to ${candidate.login}`
92 : `Could not ask ${candidate.login} for a review`,
93 description: readableFailure(
94 squashAtomCommandFailure(result),
95 "The host refused it. Check that you have write access on this repository, and that they still have access to it.",
96 ),
97 });
98 return;
99 }
100 // The command reads the loaded list again only for somebody it lacked, and a search result
101 // is keyed by what was typed, so the one on screen is read here or it keeps the old request.
102 if (hostQuery !== null) searchResult.refresh();
103 toastManager.add({
104 type: "success",
105 title: candidate.isRequested
106 ? `Review request to ${candidate.login} taken back`
107 : `Review requested from ${candidate.login}`,
108 });
109 };
110​
111 return (
112 <PullRequestCandidatePicker
113 icon={<UserPlusIcon className="size-3.5" />}
114 label="Request a review"
115 allowed={allowed}
116 disabledReason="Asking someone to review needs write access on this repository"
117 open={open}
118 onOpenChange={setOpen}
119 query={query}
120 onQueryChange={setQuery}
121 searchLabel="Search people with access"
122 isPending={(candidatesQuery.isPending && candidatesQuery.data === null) || searchPending}
123 error={
124 candidatesQuery.data === null
125 ? candidatesQuery.error
126 : candidates.length === 0
127 ? searchError
128 : null
129 }
130 candidates={candidates}
131 emptyLabel="Nobody else has access to this repository."
132 noMatchLabel="Nobody with access matches that."
133 errorLabel="The people with access could not be read."
134 truncated={truncation !== null}
135 truncatedLabel={truncation === null ? "" : TRUNCATED_LABELS[truncation](searchError)}
136 candidateKey={(candidate) => `${candidate.kind}:${candidate.id}`}
137 disabled={pending !== null}
138 onSelect={(candidate) => void toggle(candidate)}
139 >
140 {(candidate) => (
141 <>
142 <PullRequestActorLabel actor={candidate} className="flex-1" />
143 {candidate.kind === "team" ? (
144 <span className="shrink-0 text-muted-foreground">team</span>
145 ) : null}
146 {candidate.isRequested ? (
147 <CheckIcon aria-label="Already asked" className="size-3.5 shrink-0" />
148 ) : null}
149 </>
150 )}
151 </PullRequestCandidatePicker>
152 );