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

PullRequestLabelPicker.tsx

147 lines5.2 KB

1import { useAtomCommand } from "~/state/use-atom-command";
2import type { EnvironmentId, PullRequestLabelCandidate, PullRequestRef } from "@t3tools/contracts";
3import { CheckIcon, TagIcon } from "lucide-react";
4import { msg, useGT, useMessages } from "gt-react";
5import { useState } from "react";
6​
7import { m } from "~/i18n/messages";
8​
9import { pullRequestEnvironment } from "~/state/pullRequests";
10import { useEnvironmentQuery } from "~/state/query";
11import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
12import {
13 emptySelection,
14 isSelected,
15 changeBatches,
16 selectionChanges,
17 toggleSelection,
18} from "@t3tools/client-runtime/pull-request-selection";
19​
20import { toastManager } from "../ui/toast";
21import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
22import { readableFailure } from "./pullRequestDetail.logic";
23import { pullRequestLabelColor } from "./pullRequestList.logic";
24​
25function matches(candidate: PullRequestLabelCandidate, query: string): boolean {
26 if (query.length === 0) return true;
27 const needle = query.toLowerCase();
28 return (
29 candidate.name.toLowerCase().includes(needle) ||
30 (candidate.description ?? "").toLowerCase().includes(needle)
31 );
33​
34export function PullRequestLabelPicker({
35 environmentId,
36 reference,
37 allowed,
38}: {
39 environmentId: EnvironmentId;
40 reference: PullRequestRef;
41 allowed: boolean;
42}) {
43 const gt = useGT();
44 const [open, setOpen] = useState(false);
45 const [query, setQuery] = useState("");
46 const [draft, setDraft] = useState(emptySelection<PullRequestLabelCandidate>);
47​
48 const candidatesQuery = useEnvironmentQuery(
49 open ? pullRequestEnvironment.labelCandidates({ environmentId, input: reference }) : null,
50 useMessages(),
51 );
52 const setLabels = useAtomCommand(pullRequestEnvironment.setLabels, {
53 reportFailure: false,
54 });
55​
56 const candidates = (candidatesQuery.data?.candidates ?? []).filter((entry) =>
57 matches(entry, query),
58 );
59​
60 const send = async (labels: ReadonlyArray<PullRequestLabelCandidate>, applied: boolean) => {
61 for (const batch of changeBatches(labels)) await sendBatch(batch, applied);
62 };
63 const sendBatch = async (labels: ReadonlyArray<PullRequestLabelCandidate>, applied: boolean) => {
64 const names = labels.map((label) => label.name);
65 const result = await setLabels({
66 environmentId,
67 input: { ...reference, labels: names, applied },
68 });
69 if (result._tag === "Success") return;
70 toastManager.add({
71 type: "error",
72 title: applied
73 ? m(msg`Could not put ${{ names: names.join(", ") }} on`)
74 : m(msg`Could not take ${{ names: names.join(", ") }} off`),
75 description: readableFailure(
76 squashAtomCommandFailure(result),
77 m(msg("The host refused it. Check that you have triage access on this repository.")),
78 ),
79 });
80 };
81​
82 const onOpenChange = (nextOpen: boolean) => {
83 setOpen(nextOpen);
84 if (nextOpen) return;
85 const latest = candidatesQuery.data?.candidates ?? [];
86 const { add, remove } = selectionChanges(
87 draft,
88 (name, label) => latest.find((entry) => entry.name === name)?.isApplied ?? label.isApplied,
89 );
90 setDraft(emptySelection());
91 // Adding first keeps the Labels row from showing empty between the two writes.
92 void send(add, true).then(() => send(remove, false));
93 };
94​
95 return (
96 <PullRequestCandidatePicker
97 icon={<TagIcon className="size-3.5" />}
98 label={gt`Change labels`}
99 allowed={allowed}
100 disabledReason={gt`Changing labels needs triage access on this repository`}
101 open={open}
102 onOpenChange={onOpenChange}
103 query={query}
104 onQueryChange={setQuery}
105 searchLabel={gt`Search labels`}
106 isPending={candidatesQuery.isPending && candidatesQuery.data === null}
107 error={candidatesQuery.data === null ? candidatesQuery.error : null}
108 candidates={candidates}
109 emptyLabel={gt`This repository has no labels.`}
110 noMatchLabel={gt`No label matches that.`}
111 errorLabel={gt`The labels could not be read.`}
112 truncated={candidatesQuery.data?.truncated === true}
113 truncatedLabel={gt`This repository has more labels than are listed here. Apply the rest on the host.`}
114 candidateKey={(candidate) => candidate.name}
115 onSelect={(candidate) =>
116 setDraft((current) =>
117 toggleSelection(current, candidate.name, candidate, candidate.isApplied),
118 )
119 }
120 >
121 {(candidate) => {
122 const dot = pullRequestLabelColor(candidate.color);
123 return (
124 <>
125 <span
126 aria-hidden
127 className="size-2 shrink-0 rounded-full bg-muted-foreground"
128 {...(dot ? { style: { backgroundColor: dot } } : {})}
129 />
130 <span className="min-w-0 flex-1 truncate">
131 {candidate.name}
132 {candidate.description ? (
133 <span className="text-muted-foreground"> · {candidate.description}</span>
134 ) : null}
135 </span>
136 {isSelected(draft, candidate.name, candidate.isApplied) ? (
137 <CheckIcon
138 aria-label={gt({ context: "label is applied" })`Applied`}
139 className="size-3.5 shrink-0"
140 />
141 ) : null}
142 </>
143 );
144 }}
145 </PullRequestCandidatePicker>
146 );