@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 220 lines 8.34 KB
Raw
1 import {
2 pullRequestLinkProject,
3 resolveLinkPullRequestInput,
4 } from "@t3tools/client-runtime/pull-request-link-input";
5 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
6 import type {
7 EnvironmentProject,
8 EnvironmentThreadShell,
9 } from "@t3tools/client-runtime/state/shell";
10 import {
11 isAtomCommandInterrupted,
12 squashAtomCommandFailure,
13 type AtomCommandResult,
14 } from "@t3tools/client-runtime/state/runtime";
15 import {
16 canStartThreadPullRequestWatch,
17 planThreadPullRequestMutation,
18 } from "@t3tools/client-runtime/thread-pull-request-compatibility";
19 import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
20 import { changeRequestTermKind } from "../../lib/changeRequestTerm";
21 import { AuthOrchestrationOperateScope, type ThreadPullRequestLink } from "@t3tools/contracts";
22 import * as Clipboard from "expo-clipboard";
23 import { useGT } from "gt-react-native";
24
25 import * as Dialog from "../../components/ui/Dialog";
26 import { m } from "../../i18n/messages";
27 import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
28 import type { MenuProps } from "../../components/ui/Menu.types";
29 import type { EnvironmentRuntimeState } from "../../state/remote-runtime-types";
30 import { useEnvironmentScope } from "../../state/session";
31 import { threadEnvironment } from "../../state/threads";
32 import { useAtomCommand } from "../../state/use-atom-command";
33
34 type MenuAction = MenuProps["actions"][number];
35
36 export function useLinkedPullRequestActions(input: {
37 readonly thread: EnvironmentThreadShell | null;
38 readonly project: EnvironmentProject | null;
39 readonly runtime: EnvironmentRuntimeState | null;
40 }) {
41 const gt = useGT();
42 const { thread, project, runtime } = input;
43 const terminology = sourceControlClients.get(project?.repositoryIdentity?.provider).changeRequest;
44 const capabilities = runtime?.serverConfig?.environment.capabilities;
45 const canOperate = useEnvironmentScope(
46 thread?.environmentId ?? null,
47 AuthOrchestrationOperateScope,
48 );
49 const canEdit = canOperate && capabilities !== undefined;
50 const canWatch = canOperate && capabilities?.threadPullRequestWatch === true;
51 const canStartWatch = canWatch && thread !== null && canStartThreadPullRequestWatch(thread);
52 const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false });
53 const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false });
54 const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: false });
55
56 const run = async (
57 failureTitle: string,
58 pending: Promise<AtomCommandResult<unknown, unknown>>,
59 ) => {
60 const result = await pending;
61 if (result._tag === "Success" || isAtomCommandInterrupted(result)) return;
62 const cause = squashAtomCommandFailure(result);
63 Dialog.alert(failureTitle, errorDisplayMessage(cause, m) ?? gt("Try again."));
64 };
65
66 const setLinked = (
67 reference: { host: string; repository: string; number: number; url: string },
68 linked: boolean,
69 ) => {
70 if (thread === null) return;
71 const mutation = planThreadPullRequestMutation({
72 capabilities,
73 threadId: thread.id,
74 reference,
75 linked,
76 });
77 if (mutation === null) return;
78 const environmentId = thread.environmentId;
79 void run(
80 linked
81 ? gt(
82 "{term, select, mr {Couldn't link this merge request} cr {Couldn't link this change request} other {Couldn't link this pull request}}",
83 { term: changeRequestTermKind(terminology) },
84 )
85 : gt(
86 "{term, select, mr {Couldn't unlink this merge request} cr {Couldn't unlink this change request} other {Couldn't unlink this pull request}}",
87 { term: changeRequestTermKind(terminology) },
88 ),
89 mutation.type === "thread.pull-request.link"
90 ? link({ environmentId, input: mutation.input })
91 : unlink({ environmentId, input: mutation.input }),
92 );
93 };
94
95 const promptLink = () => {
96 const term = changeRequestTermKind(terminology);
97 Dialog.prompt({
98 title: gt(
99 "{term, select, mr {Link merge request} cr {Link change request} other {Link pull request}}",
100 { term },
101 ),
102 message: gt(
103 "{term, select, mr {Paste a merge request URL, or enter 123 / #123 for this thread's repository.} cr {Paste a change request URL, or enter 123 / #123 for this thread's repository.} other {Paste a pull request URL, or enter 123 / #123 for this thread's repository.}}",
104 { term },
105 ),
106 placeholder: gt(
107 "{term, select, mr {Merge request URL or #42} cr {Change request URL or #42} other {Pull request URL or #42}}",
108 { term },
109 ),
110 initialValue: "",
111 confirmText: gt("Link", { $context: "verb, confirms linking a pull request" }),
112 onConfirm: (reference) => {
113 const resolved = resolveLinkPullRequestInput(
114 {
115 reference,
116 project: pullRequestLinkProject(project?.repositoryIdentity),
117 hasProject: () => true,
118 },
119 m,
120 );
121 if (resolved === null) {
122 Dialog.alert(
123 gt(
124 "{term, select, mr {Couldn't link this merge request} cr {Couldn't link this change request} other {Couldn't link this pull request}}",
125 { term },
126 ),
127 gt(
128 "{term, select, mr {Use a merge request URL, 123, or #123.} cr {Use a change request URL, 123, or #123.} other {Use a pull request URL, 123, or #123.}}",
129 { term },
130 ),
131 );
132 return;
133 }
134 if ("error" in resolved) {
135 Dialog.alert(
136 gt(
137 "{term, select, mr {Couldn't link this merge request} cr {Couldn't link this change request} other {Couldn't link this pull request}}",
138 { term },
139 ),
140 resolved.error,
141 );
142 return;
143 }
144 setLinked(resolved.link, true);
145 },
146 });
147 };
148
149 const menuActions = (link: ThreadPullRequestLink): MenuAction[] => {
150 const open = link.snapshot === null || link.snapshot.state === "open";
151 const watching = link.watch !== undefined;
152 return [
153 { id: "copy", title: gt("Copy link"), image: "link" },
154 { id: "browser", title: gt("Open in browser"), image: "safari" },
155 ...((watching ? canWatch : canStartWatch) && open
156 ? [
157 {
158 id: "watch",
159 title: watching ? gt("Stop watching") : gt("Watch for changes"),
160 image: watching ? "eye.slash" : "eye",
161 },
162 ]
163 : []),
164 ...(canEdit
165 ? [
166 {
167 id: "unlink",
168 title: link.source === "stack" ? gt("Dismiss from thread") : gt("Unlink from thread"),
169 image: "xmark",
170 attributes: { destructive: true },
171 },
172 ]
173 : []),
174 ];
175 };
176
177 const onMenuAction = (link: ThreadPullRequestLink, action: string) => {
178 if (action === "copy") void Clipboard.setStringAsync(link.url);
179 else if (action === "browser")
180 void tryOpenExternalUrl(link.url, "pull-request").then((opened) => {
181 if (!opened) {
182 const term = changeRequestTermKind(
183 (
184 sourceControlClients.findByChangeRequestUrl(link.url) ??
185 sourceControlClients.get(undefined)
186 ).changeRequest,
187 );
188 Dialog.alert(
189 gt(
190 "{term, select, mr {Unable to open MR} cr {Unable to open change request} other {Unable to open PR}}",
191 { term },
192 ),
193 gt(
194 "{term, select, mr {The merge request could not be opened.} cr {The change request could not be opened.} other {The pull request could not be opened.}}",
195 { term },
196 ),
197 );
198 }
199 });
200 else if (action === "watch" && thread !== null)
201 void run(
202 gt("Couldn't change watching"),
203 watch({
204 environmentId: thread.environmentId,
205 input: {
206 threadId: thread.id,
207 host: link.host,
208 repository: link.repository,
209 number: link.number,
210 watching: link.watch === undefined,
211 },
212 }),
213 );
214 else if (action === "unlink") setLinked(link, false);
215 };
216
217 return { canLink: canEdit, terminology, promptLink, menuActions, onMenuAction };
218 }
219
220 export type LinkedPullRequestActions = ReturnType<typeof useLinkedPullRequestActions>;