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

LinkPullRequestDialog.tsx

216 lines6.9 KB

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