Open History to see the last commit
1/**
2 * Putting a label on, and taking one off, from the row that says which it already wears.
3 *
4 * The repository's labels are read only once this menu opens, for the reason the reviewer menu
5 * reads its people then: they are worth a request when somebody wants them and worth nothing on
6 * every pull request they merely open.
7 */
8import type { EnvironmentId, PullRequestLabelCandidate, PullRequestRef } from "@t3tools/contracts";
9import { CheckIcon, TagIcon } from "lucide-react";
10import { useMemo, useState } from "react";
11
12import { pullRequestEnvironment } from "~/state/pullRequests";
13import { useEnvironmentQuery } from "~/state/query";
14import { useAtomCommand } from "~/state/use-atom-command";
15import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
16
17import { toastManager } from "../ui/toast";
18import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
19import { readableFailure } from "./pullRequestDetail.logic";
20import { pullRequestLabelColor } from "./pullRequestList.logic";
21
22/** Narrows only what arrived: the host is asked once, when the menu opens. */
23function matches(candidate: PullRequestLabelCandidate, query: string): boolean {
24 if (query.length === 0) return true;
25 const needle = query.toLowerCase();
26 return (
27 candidate.name.toLowerCase().includes(needle) ||
28 (candidate.description ?? "").toLowerCase().includes(needle)
29 );
30}
31
32export function PullRequestLabelPicker({
33 environmentId,
34 reference,
35 allowed,
36}: {
37 environmentId: EnvironmentId;
38 reference: PullRequestRef;
39 /** False where the host would refuse this account's change. Disabled with the reason rather
40 * than hidden, like the reviewer control beside it. */
41 allowed: boolean;
42}) {
43 const [open, setOpen] = useState(false);
44 const [query, setQuery] = useState("");
45 const [pending, setPending] = useState<string | null>(null);
46
47 // Mounted with the menu closed, so nothing is asked of the host until it opens.
48 const candidatesQuery = useEnvironmentQuery(
49 open ? pullRequestEnvironment.labelCandidates({ environmentId, input: reference }) : null,
50 );
51 const setLabels = useAtomCommand(pullRequestEnvironment.setLabels, { reportFailure: false });
52
53 const candidates = useMemo(
54 () => (candidatesQuery.data?.candidates ?? []).filter((entry) => matches(entry, query)),
55 [candidatesQuery.data, query],
56 );
57
58 const toggle = async (candidate: PullRequestLabelCandidate) => {
59 if (pending !== null) return;
60 setPending(candidate.name);
61 const result = await setLabels({
62 environmentId,
63 input: { ...reference, labels: [candidate.name], applied: !candidate.isApplied },
64 });
65 setPending(null);
66 if (result._tag === "Failure") {
67 toastManager.add({
68 type: "error",
69 title: candidate.isApplied
70 ? `Could not take ${candidate.name} off`
71 : `Could not put ${candidate.name} on`,
72 description: readableFailure(
73 squashAtomCommandFailure(result),
74 "The host refused it. Check that you have triage access on this repository.",
75 ),
76 });
77 return;
78 }
79 };
80
81 return (
82 <PullRequestCandidatePicker
83 icon={<TagIcon className="size-3.5" />}
84 label="Change labels"
85 allowed={allowed}
86 disabledReason="Changing labels needs triage access on this repository"
87 open={open}
88 onOpenChange={setOpen}
89 query={query}
90 onQueryChange={setQuery}
91 searchLabel="Search labels"
92 isPending={candidatesQuery.isPending && candidatesQuery.data === null}
93 error={candidatesQuery.data === null ? candidatesQuery.error : null}
94 candidates={candidates}
95 emptyLabel="This repository has no labels."
96 noMatchLabel="No label matches that."
97 errorLabel="The labels could not be read."
98 truncated={candidatesQuery.data?.truncated === true}
99 truncatedLabel="This repository has more labels than are listed here. Apply the rest on the host."
100 candidateKey={(candidate) => candidate.name}
101 disabled={pending !== null}
102 onSelect={(candidate) => void toggle(candidate)}
103 >
104 {(candidate) => {
105 const dot = pullRequestLabelColor(candidate.color);
106 return (
107 <>
108 <span
109 aria-hidden
110 className="size-2 shrink-0 rounded-full bg-muted-foreground"
111 {...(dot ? { style: { backgroundColor: dot } } : {})}
112 />
113 <span className="min-w-0 flex-1 truncate">
114 {candidate.name}
115 {candidate.description ? (
116 <span className="text-muted-foreground"> · {candidate.description}</span>
117 ) : null}
118 </span>
119 {candidate.isApplied ? (
120 <CheckIcon aria-label="Applied" className="size-3.5 shrink-0" />
121 ) : null}
122 </>
123 );
124 }}
125 </PullRequestCandidatePicker>
126 );
127}