The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 211 lines 7.58 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
3 import { visibleLinkedThreads } from "@t3tools/client-runtime/state/threads";
4 import { AuthOrchestrationOperateScope, threadIssueKeysEqual } from "@t3tools/contracts";
5 import type {
6 EnvironmentId,
7 IssueDetail,
8 IssueRef,
9 ScopedThreadRef,
10 ThreadId,
11 } from "@t3tools/contracts";
12 import { Link2Icon, Link2OffIcon, MessageSquareIcon } from "lucide-react";
13 import { msg, T, useGT } from "gt-react";
14 import { useState } from "react";
15
16 import { openCommandPalette } from "~/commandPaletteBus";
17 import { m } from "~/i18n/messages";
18 import { appAtomRegistry } from "~/rpc/atomRegistry";
19 import {
20 useAnyEnvironmentHiding,
21 useEnvironmentThreadRefs,
22 useServerConfigs,
23 useThreadShell,
24 } from "~/state/entities";
25 import { issueEnvironment } from "~/state/issues";
26 import { useEnvironmentQuery } from "~/state/query";
27 import { useEnvironmentScope } from "~/state/session";
28 import { environmentThreadShells, threadEnvironment } from "~/state/threads";
29 import { useAtomCommand } from "~/state/use-atom-command";
30
31 import { readableFailure } from "../pullRequest/pullRequestDetail.logic";
32 import { ThreadLinkPicker } from "../ThreadLinkPicker";
33 import { Button } from "../ui/button";
34 import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
35 import { toastManager } from "../ui/toast";
36 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
37
38 /**
39 * The threads an issue is linked to, and the control that links it to one more: the thread it is
40 * shown beside, or one picked from the environment when it is shown on its own.
41 */
42 export function IssueThreadLinks({
43 environmentId,
44 reference,
45 issue,
46 threadRef,
47 }: {
48 environmentId: EnvironmentId;
49 reference: IssueRef & { readonly host: string };
50 issue: Pick<IssueDetail, "url" | "displayKey" | "title">;
51 threadRef: ScopedThreadRef | null;
52 }) {
53 const gt = useGT();
54 const thread = useThreadShell(threadRef?.environmentId === environmentId ? threadRef : null);
55 const canOperate = useEnvironmentScope(environmentId, AuthOrchestrationOperateScope);
56 const listsThreads =
57 useServerConfigs().get(environmentId)?.environment.capabilities.issueLinkedThreads === true;
58 const link = useAtomCommand(threadEnvironment.linkIssue, { reportFailure: false });
59 const unlink = useAtomCommand(threadEnvironment.unlinkIssue, { reportFailure: false });
60 const hiddenKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom);
61 const hidingAnywhere = useAnyEnvironmentHiding();
62 const loadedThreadRefs = useEnvironmentThreadRefs(environmentId);
63 const key = {
64 tracker: reference.tracker,
65 host: reference.host,
66 scope: reference.scope,
67 key: reference.key,
68 };
69 const relationsAtom = issueEnvironment.linkedThreads({ environmentId, input: key });
70 const relations = useEnvironmentQuery(listsThreads ? relationsAtom : null);
71 // Retain the last count while refresh temporarily clears query data.
72 const [lastRelations, setLastRelations] = useState(relations.data);
73 if (relations.data !== null && relations.data !== lastRelations) {
74 setLastRelations(relations.data);
75 }
76 const [pending, setPending] = useState(false);
77 const [pickerOpen, setPickerOpen] = useState(false);
78
79 const linkedHere = (thread?.issues ?? []).some((existing) => threadIssueKeysEqual(existing, key));
80 const changeLink = async (threadId: ThreadId, remove: boolean) => {
81 if (pending) return;
82 setPending(true);
83 const result = await (remove
84 ? unlink({ environmentId, input: { threadId, ...key } })
85 : link({
86 environmentId,
87 input: {
88 threadId,
89 ...key,
90 projectId: reference.projectId,
91 url: issue.url,
92 displayKey: issue.displayKey,
93 title: issue.title,
94 source: "manual",
95 },
96 }));
97 setPending(false);
98 if (result._tag === "Failure") {
99 toastManager.add({
100 type: "error",
101 title: remove ? m(msg("Could not unlink the issue")) : m(msg("Could not link the issue")),
102 description: readableFailure(
103 squashAtomCommandFailure(result),
104 m(msg("The server refused it.")),
105 ),
106 });
107 return;
108 }
109 if (listsThreads) appAtomRegistry.refresh(relationsAtom);
110 setPickerOpen(false);
111 };
112
113 const linkedThreads = visibleLinkedThreads(
114 (relations.data ?? lastRelations)?.threads ?? [],
115 environmentId,
116 loadedThreadRefs,
117 hiddenKeys,
118 hidingAnywhere,
119 );
120 const linkedThreadsLabel =
121 linkedThreads.length > 0
122 ? gt(
123 "{count, plural, one {Linked from {count} thread} other {Linked from {count} threads}}",
124 { count: linkedThreads.length },
125 )
126 : gt("Linked threads");
127 // A failed read shows "?" and falls back to searching threads for the issue's link.
128 const relationsKnown = (relations.data ?? lastRelations) !== null;
129 return (
130 <>
131 {linkedThreads.length > 0 || (!relationsKnown && relations.error !== null) ? (
132 <Tooltip>
133 <TooltipTrigger
134 render={
135 <Button
136 size="xs"
137 variant="ghost"
138 aria-label={linkedThreadsLabel}
139 onClick={() =>
140 openCommandPalette({
141 query: issue.url,
142 ...(relationsKnown
143 ? { linkedThreads: { environmentId, threads: linkedThreads } }
144 : {}),
145 })
146 }
147 >
148 <MessageSquareIcon aria-hidden className="size-3.5" />
149 <span aria-hidden>{linkedThreads.length || "?"}</span>
150 </Button>
151 }
152 />
153 <TooltipPopup>
154 {gt("{label}. Search in the command palette.", { label: linkedThreadsLabel })}
155 </TooltipPopup>
156 </Tooltip>
157 ) : null}
158 {canOperate ? (
159 <Tooltip>
160 <TooltipTrigger
161 render={
162 <Button
163 size="icon-xs"
164 variant="ghost"
165 aria-label={linkedHere ? gt("Unlink from thread") : gt("Link to thread")}
166 aria-pressed={thread === null ? undefined : linkedHere}
167 disabled={pending}
168 onClick={() => {
169 if (thread === null) setPickerOpen(true);
170 else void changeLink(thread.id, linkedHere);
171 }}
172 />
173 }
174 >
175 {linkedHere ? (
176 <Link2OffIcon aria-hidden className="size-3.5" />
177 ) : (
178 <Link2Icon aria-hidden className="size-3.5" />
179 )}
180 </TooltipTrigger>
181 <TooltipPopup>
182 {linkedHere ? (
183 <T>Unlink from this thread</T>
184 ) : thread === null ? (
185 gt("Link to thread")
186 ) : (
187 <T>Link to this thread</T>
188 )}
189 </TooltipPopup>
190 </Tooltip>
191 ) : null}
192 {pickerOpen ? (
193 <Dialog open onOpenChange={setPickerOpen}>
194 <DialogPopup className="max-w-md" showCloseButton={false}>
195 <DialogTitle className="sr-only">
196 <T>Link issue to a thread</T>
197 </DialogTitle>
198 <ThreadLinkPicker
199 environmentId={environmentId}
200 pending={pending}
201 isLinked={(candidate) =>
202 (candidate.issues ?? []).some((existing) => threadIssueKeysEqual(existing, key))
203 }
204 onSelect={(threadId) => void changeLink(threadId, false)}
205 />
206 </DialogPopup>
207 </Dialog>
208 ) : null}
209 </>
210 );
211 }