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 onChanged,
37}: {
38 environmentId: EnvironmentId;
39 reference: PullRequestRef;
40 /** False where the host would refuse this account's change. Disabled with the reason rather
41 * than hidden, like the reviewer control beside it. */
42 allowed: boolean;
43 /** The detail carries the labels, so it is re-read once the host has taken the change. */
44 onChanged: () => void;
45}) {
46 const [open, setOpen] = useState(false);
47 const [query, setQuery] = useState("");
48 const [pending, setPending] = useState<string | null>(null);
49
50 // Mounted with the menu closed, so nothing is asked of the host until it opens.
51 const candidatesQuery = useEnvironmentQuery(
52 open ? pullRequestEnvironment.labelCandidates({ environmentId, input: reference }) : null,
53 );
54 const setLabels = useAtomCommand(pullRequestEnvironment.setLabels, { reportFailure: false });
55
56 const candidates = useMemo(
57 () => (candidatesQuery.data?.candidates ?? []).filter((entry) => matches(entry, query)),
58 [candidatesQuery.data, query],
59 );
60
61 const toggle = async (candidate: PullRequestLabelCandidate) => {
62 if (pending !== null) return;
63 setPending(candidate.name);
64 const result = await setLabels({
65 environmentId,
66 input: { ...reference, labels: [candidate.name], applied: !candidate.isApplied },
67 });
68 setPending(null);
69 if (result._tag === "Failure") {
70 toastManager.add({
71 type: "error",
72 title: candidate.isApplied
73 ? `Could not take ${candidate.name} off`
74 : `Could not put ${candidate.name} on`,
75 description: readableFailure(
76 squashAtomCommandFailure(result),
77 "The host refused it. Check that you have triage access on this repository.",
78 ),
79 });
80 return;
81 }
82 onChanged();
83 candidatesQuery.refresh();
84 };
85
86 return (
87 <PullRequestCandidatePicker
88 icon={<TagIcon className="size-3.5" />}
89 label="Change labels"
90 allowed={allowed}
91 disabledReason="Changing labels needs triage access on this repository"
92 open={open}
93 onOpenChange={setOpen}
94 query={query}
95 onQueryChange={setQuery}
96 searchLabel="Search labels"
97 isPending={candidatesQuery.isPending}
98 error={candidatesQuery.error}
99 candidates={candidates}
100 emptyLabel="This repository has no labels."
101 noMatchLabel="No label matches that."
102 errorLabel="The labels could not be read."
103 truncated={candidatesQuery.data?.truncated === true}
104 truncatedLabel="This repository has more labels than are listed here. Apply the rest on the host."
105 candidateKey={(candidate) => candidate.name}
106 disabled={pending !== null}
107 onSelect={(candidate) => void toggle(candidate)}
108 >
109 {(candidate) => {
110 const dot = pullRequestLabelColor(candidate.color);
111 return (
112 <>
113 <span
114 aria-hidden
115 className="size-2 shrink-0 rounded-full bg-muted-foreground"
116 {...(dot ? { style: { backgroundColor: dot } } : {})}
117 />
118 <span className="min-w-0 flex-1 truncate">
119 {candidate.name}
120 {candidate.description ? (
121 <span className="text-muted-foreground"> · {candidate.description}</span>
122 ) : null}
123 </span>
124 {candidate.isApplied ? (
125 <CheckIcon aria-label="Applied" className="size-3.5 shrink-0" />
126 ) : null}
127 </>
128 );
129 }}
130 </PullRequestCandidatePicker>
131 );
132}