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