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

LinkPullRequestDialog.tsx

187 lines5.9 KB

1import type { ScopedThreadRef } from "@t3tools/contracts";
2import {
3 pullRequestLinkProject,
4 resolveLinkPullRequestInput,
5} from "@t3tools/client-runtime/pull-request-link-input";
6import { useAtomValue } from "@effect/atom-react";
7import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
8import { useEffect, useRef, useState } from "react";
9​
10import { useProjects, useThreadShell } from "~/state/entities";
11import { usePullRequestLinking } from "~/hooks/usePullRequestLinking";
12import { appAtomRegistry } from "~/rpc/atomRegistry";
13import { Atom } from "effect/reactivity";
14import { Button } from "../ui/button";
15import {
16 Dialog,
17 DialogDescription,
18 DialogFooter,
19 DialogHeader,
20 DialogPanel,
21 DialogPopup,
22 DialogTitle,
23} from "../ui/dialog";
24import { Input } from "../ui/input";
25​
26/**
27 * Which thread has the link dialog open, set by whichever entry point asked (command palette,
28 * pull-requests surface, detail panel) and rendered once by the chat view so the dialog outlives
29 * a palette that closes the moment its command runs.
30 */
31const linkPullRequestDialogThreadAtom = Atom.make<ScopedThreadRef | null>(null).pipe(
32 Atom.keepAlive,
33 Atom.withLabel("pull-requests:link-dialog-thread"),
34);
35​
36export function openLinkPullRequestDialog(threadRef: ScopedThreadRef): void {
37 appAtomRegistry.set(linkPullRequestDialogThreadAtom, threadRef);
39​
40interface LinkPullRequestDialogProps {
41 open: boolean;
42 threadRef: ScopedThreadRef;
43 projectId: string | null;
44 onOpenChange: (open: boolean) => void;
46​
47export function LinkPullRequestDialogHost() {
48 const threadRef = useAtomValue(linkPullRequestDialogThreadAtom);
49 const thread = useThreadShell(threadRef);
50 const linking = usePullRequestLinking(threadRef?.environmentId);
51 if (threadRef === null || linking.mode === "unsupported") return null;
52 return (
53 <LinkPullRequestDialog
54 open
55 threadRef={threadRef}
56 projectId={thread?.projectId ?? null}
57 onOpenChange={(open) => {
58 if (!open) appAtomRegistry.set(linkPullRequestDialogThreadAtom, null);
59 }}
60 />
61 );
63​
64function LinkPullRequestDialog({
65 open,
66 threadRef,
67 projectId,
68 onOpenChange,
69}: LinkPullRequestDialogProps) {
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 useEffect(() => {
93 if (!open) return;
94 const frame = window.requestAnimationFrame(() => inputRef.current?.focus());
95 return () => window.cancelAnimationFrame(frame);
96 }, [open]);
97​
98 const resolved = resolveLinkPullRequestInput({
99 reference,
100 project: ownProject,
101 hasProject: (reference) => linking.canLink(reference.url),
102 });
103​
104 const submit = async () => {
105 setDirty(true);
106 if (resolved === null || "error" in resolved) return;
107 setSubmitError(null);
108 setPending(true);
109 const result = await settlePromise(() =>
110 linking.changeLink(threadRef, resolved.link.url, true),
111 );
112 setPending(false);
113 if (result._tag === "Failure") {
114 const error = squashAtomCommandFailure(result);
115 setSubmitError(error instanceof Error ? error.message : "Could not link the pull request.");
116 return;
117 }
118 onOpenChange(false);
119 };
120​
121 const validation = !dirty
122 ? null
123 : reference.trim().length === 0
124 ? "Paste a pull request URL or enter 123 / #123."
125 : resolved === null
126 ? "Use a pull request URL, 123, or #123."
127 : "error" in resolved
128 ? resolved.error
129 : null;
130​
131 return (
132 <Dialog open={open} onOpenChange={(next) => (pending ? undefined : onOpenChange(next))}>
133 <DialogPopup className="max-w-xl">
134 <DialogHeader>
135 <DialogTitle>Link pull request</DialogTitle>
136 <DialogDescription>
137 Attach a pull request to this thread by its URL, or by its number for this thread's
138 repository.
139 </DialogDescription>
140 </DialogHeader>
141 <DialogPanel>
142 <Input
143 ref={inputRef}
144 placeholder="Pull request URL or #42"
145 value={reference}
146 onChange={(event) => {
147 setDirty(true);
148 setReference(event.target.value);
149 }}
150 onKeyDown={(event) => {
151 if (event.key !== "Enter") return;
152 event.preventDefault();
153 void submit();
154 }}
155 />
156 {resolved !== null && "link" in resolved ? (
157 <p className="truncate text-muted-foreground text-xs">
158 {resolved.link.host}/{resolved.link.repository} #{resolved.link.number}
159 </p>
160 ) : null}
161 {(validation ?? submitError) ? (
162 <p className="text-destructive text-xs">{validation ?? submitError}</p>
163 ) : null}
164 </DialogPanel>
165 <DialogFooter>
166 <Button
167 type="button"
168 variant="outline"
169 size="sm"
170 onClick={() => onOpenChange(false)}
171 disabled={pending}
172 >
173 Cancel
174 </Button>
175 <Button
176 type="button"
177 size="sm"
178 onClick={() => void submit()}
179 disabled={pending || resolved === null || "error" in resolved}
180 >
181 {pending ? "Linking..." : "Link"}
182 </Button>
183 </DialogFooter>
184 </DialogPopup>
185 </Dialog>
186 );