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

LinkPullRequestDialog.tsx

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