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