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