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

PullRequestLabelPicker.tsx

114 lines4.0 KB

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 );
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​
36 const candidatesQuery = useEnvironmentQuery(
37 open ? pullRequestEnvironment.labelCandidates({ environmentId, input: reference }) : null,
38 );
39 const setLabels = useAtomCommand(pullRequestEnvironment.setLabels, {
40 reportFailure: false,
41 });
42​
43 const candidates = (candidatesQuery.data?.candidates ?? []).filter((entry) =>
44 matches(entry, query),
45 );
46​
47 // Each change shows at once and queues behind the one before it on the host.
48 const toggle = async (candidate: PullRequestLabelCandidate) => {
49 const result = await setLabels({
50 environmentId,
51 input: { ...reference, labels: [candidate.name], applied: !candidate.isApplied },
52 });
53 if (result._tag === "Failure") {
54 toastManager.add({
55 type: "error",
56 title: candidate.isApplied
57 ? `Could not take ${candidate.name} off`
58 : `Could not put ${candidate.name} on`,
59 description: readableFailure(
60 squashAtomCommandFailure(result),
61 "The host refused it. Check that you have triage access on this repository.",
62 ),
63 });
64 return;
65 }
66 };
67​
68 return (
69 <PullRequestCandidatePicker
70 icon={<TagIcon className="size-3.5" />}
71 label="Change labels"
72 allowed={allowed}
73 disabledReason="Changing labels needs triage access on this repository"
74 open={open}
75 onOpenChange={setOpen}
76 query={query}
77 onQueryChange={setQuery}
78 searchLabel="Search labels"
79 isPending={candidatesQuery.isPending && candidatesQuery.data === null}
80 error={candidatesQuery.data === null ? candidatesQuery.error : null}
81 candidates={candidates}
82 emptyLabel="This repository has no labels."
83 noMatchLabel="No label matches that."
84 errorLabel="The labels could not be read."
85 truncated={candidatesQuery.data?.truncated === true}
86 truncatedLabel="This repository has more labels than are listed here. Apply the rest on the host."
87 candidateKey={(candidate) => candidate.name}
88 disabled={false}
89 onSelect={(candidate) => void toggle(candidate)}
90 >
91 {(candidate) => {
92 const dot = pullRequestLabelColor(candidate.color);
93 return (
94 <>
95 <span
96 aria-hidden
97 className="size-2 shrink-0 rounded-full bg-muted-foreground"
98 {...(dot ? { style: { backgroundColor: dot } } : {})}
99 />
100 <span className="min-w-0 flex-1 truncate">
101 {candidate.name}
102 {candidate.description ? (
103 <span className="text-muted-foreground"> · {candidate.description}</span>
104 ) : null}
105 </span>
106 {candidate.isApplied ? (
107 <CheckIcon aria-label="Applied" className="size-3.5 shrink-0" />
108 ) : null}
109 </>
110 );
111 }}
112 </PullRequestCandidatePicker>
113 );