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

main
tsx 186 lines 5.81 KB
Raw
1 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2 import type { ScopedThreadRef } from "@t3tools/contracts";
3 import {
4 pullRequestLinkProject,
5 resolveLinkPullRequestInput,
6 } from "@t3tools/client-runtime/pull-request-link-input";
7 import { useAtomValue } from "@effect/atom-react";
8 import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
9 import { msg, T, useGT } from "gt-react";
10 import { useRef, useState } from "react";
11 import { m } from "~/i18n/messages";
12
13 import { useProjects, useThreadShell } from "~/state/entities";
14 import { usePullRequestLinking } from "~/hooks/usePullRequestLinking";
15 import { appAtomRegistry } from "~/rpc/atomRegistry";
16 import { Atom } from "effect/reactivity";
17 import { Button } from "../ui/button";
18 import {
19 Dialog,
20 DialogDescription,
21 DialogFooter,
22 DialogHeader,
23 DialogPanel,
24 DialogPopup,
25 DialogTitle,
26 } from "../ui/dialog";
27 import { Input } from "../ui/input";
28
29 // Host in chat to survive command palette dismissal.
30 const linkPullRequestDialogThreadAtom = Atom.make<ScopedThreadRef | null>(null).pipe(
31 Atom.keepAlive,
32 Atom.withLabel("pull-requests:link-dialog-thread"),
33 );
34
35 export function openLinkPullRequestDialog(threadRef: ScopedThreadRef): void {
36 appAtomRegistry.set(linkPullRequestDialogThreadAtom, threadRef);
37 }
38
39 interface LinkPullRequestDialogProps {
40 open: boolean;
41 threadRef: ScopedThreadRef;
42 projectId: string | null;
43 onOpenChange: (open: boolean) => void;
44 }
45
46 export function LinkPullRequestDialogHost() {
47 const threadRef = useAtomValue(linkPullRequestDialogThreadAtom);
48 const thread = useThreadShell(threadRef);
49 const linking = usePullRequestLinking(threadRef?.environmentId);
50 if (threadRef === null || linking.mode === "unsupported") return null;
51 return (
52 <LinkPullRequestDialog
53 open
54 threadRef={threadRef}
55 projectId={thread?.projectId ?? null}
56 onOpenChange={(open) => {
57 if (!open) appAtomRegistry.set(linkPullRequestDialogThreadAtom, null);
58 }}
59 />
60 );
61 }
62
63 function LinkPullRequestDialog({
64 open,
65 threadRef,
66 projectId,
67 onOpenChange,
68 }: LinkPullRequestDialogProps) {
69 const gt = useGT();
70 const inputRef = useRef<HTMLInputElement>(null);
71 const [reference, setReference] = useState("");
72 const [dirty, setDirty] = useState(false);
73 const [submitError, setSubmitError] = useState<string | null>(null);
74 const projects = useProjects();
75 const ownProject = pullRequestLinkProject(
76 projects.find(
77 (project) => project.environmentId === threadRef.environmentId && project.id === projectId,
78 )?.repositoryIdentity,
79 );
80 const linking = usePullRequestLinking(threadRef.environmentId);
81 const [pending, setPending] = useState(false);
82
83 const [openSeen, setOpenSeen] = useState(open);
84 if (openSeen !== open) {
85 setOpenSeen(open);
86 if (open) {
87 setReference("");
88 setDirty(false);
89 setSubmitError(null);
90 }
91 }
92
93 const resolved = resolveLinkPullRequestInput({
94 reference,
95 project: ownProject,
96 hasProject: (reference) => linking.canLink(reference.url),
97 });
98
99 const submit = async () => {
100 setDirty(true);
101 if (resolved === null || "error" in resolved) return;
102 setSubmitError(null);
103 setPending(true);
104 const result = await settlePromise(() =>
105 linking.changeLink(threadRef, resolved.link.url, true),
106 );
107 setPending(false);
108 if (result._tag === "Failure") {
109 const error = squashAtomCommandFailure(result);
110 setSubmitError(thrownErrorMessage(error, m) ?? m(msg("Could not link the pull request.")));
111 return;
112 }
113 onOpenChange(false);
114 };
115
116 const validation = !dirty
117 ? null
118 : reference.trim().length === 0
119 ? gt("Paste a pull request URL or enter 123 / #123.")
120 : resolved === null
121 ? gt("Use a pull request URL, 123, or #123.")
122 : "error" in resolved
123 ? resolved.error
124 : null;
125
126 return (
127 <Dialog open={open} onOpenChange={(next) => (pending ? undefined : onOpenChange(next))}>
128 <DialogPopup className="max-w-xl" initialFocus={inputRef}>
129 <DialogHeader>
130 <DialogTitle>
131 <T>Link pull request</T>
132 </DialogTitle>
133 <DialogDescription>
134 <T>
135 Attach a pull request to this thread by its URL, or by its number for this thread's
136 repository.
137 </T>
138 </DialogDescription>
139 </DialogHeader>
140 <DialogPanel>
141 <Input
142 ref={inputRef}
143 placeholder={gt("Pull request URL or #42")}
144 value={reference}
145 onChange={(event) => {
146 setDirty(true);
147 setReference(event.target.value);
148 }}
149 onKeyDown={(event) => {
150 if (event.key !== "Enter") return;
151 event.preventDefault();
152 void submit();
153 }}
154 />
155 {resolved !== null && "link" in resolved ? (
156 <p className="truncate text-muted-foreground text-xs">
157 {resolved.link.host}/{resolved.link.repository} #{resolved.link.number}
158 </p>
159 ) : null}
160 {(validation ?? submitError) ? (
161 <p className="text-destructive text-xs">{validation ?? submitError}</p>
162 ) : null}
163 </DialogPanel>
164 <DialogFooter>
165 <Button
166 type="button"
167 variant="outline"
168 size="sm"
169 onClick={() => onOpenChange(false)}
170 disabled={pending}
171 >
172 <T>Cancel</T>
173 </Button>
174 <Button
175 type="button"
176 size="sm"
177 onClick={() => void submit()}
178 disabled={pending || resolved === null || "error" in resolved}
179 >
180 {pending ? gt("Linking...") : gt("Link", { $context: "verb, button" })}
181 </Button>
182 </DialogFooter>
183 </DialogPopup>
184 </Dialog>
185 );
186 }