@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, 03:37 UTC

main
ts 136 lines 5.95 KB
Raw
1 import { useNavigation, useRoute, StackActions } from "@react-navigation/native";
2 import { isStandaloneIssueLink, resolveIssueLinkTarget } from "@t3tools/client-runtime/issue-links";
3 import {
4 nativePullRequestDestination,
5 pullRequestCandidateForIssueLink,
6 } from "../lib/pullRequestNavigation";
7 import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime";
8 import { EnvironmentId, ThreadId } from "@t3tools/contracts";
9 import { useProjects, useThreadShell } from "../state/entities";
10 import { tryOpenExternalUrl } from "../lib/openExternalUrl";
11 import { appAtomRegistry } from "../state/atom-registry";
12 import { environmentServerConfigsAtom } from "../state/server";
13 import { issueTrackerEnvironment } from "../state/issueTrackers";
14 import { composerPullRequests } from "../state/pull-requests";
15 import { useAtomQueryRunner } from "../state/use-atom-query-runner";
16 import { withKnownPullRequestSummary } from "../state/use-thread-pr";
17
18 let linkNavigationId = 0;
19 const nextLinkNavigationId = () => ++linkNavigationId;
20
21 type PullRequestContext = { readonly environmentId: string; readonly projectId: string };
22
23 // Avoid useRoute so layouts without route context can open any thread's pull requests.
24 export function useNavigateToPullRequest(options?: { readonly replace?: boolean }) {
25 const navigation = useNavigation();
26 const projects = useProjects();
27 const replace = options?.replace;
28 const readPreview = useAtomQueryRunner(composerPullRequests.preview, {
29 reportFailure: false,
30 reportDefect: false,
31 });
32 const readTrackerConnections = useAtomQueryRunner(issueTrackerEnvironment.connections, {
33 reportFailure: false,
34 reportDefect: false,
35 });
36 return async (url: string, context: PullRequestContext | null) => {
37 // Read at press time so a config push does not hand every link a new handler.
38 const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom);
39 const destination = nativePullRequestDestination(url, context, projects, (environmentId) =>
40 serverConfigs.has(environmentId),
41 );
42 if (!destination) {
43 // An issue link from the same text opens the issue; anything else leaves for the browser.
44 const environmentId = context && EnvironmentId.make(context.environmentId);
45 const issuesEnabled =
46 environmentId !== null &&
47 serverConfigs.get(environmentId)?.environment.capabilities.issues === true;
48 // Only a standalone tracker's link needs the bindings, whose read probes the tracker.
49 const connections =
50 issuesEnabled && isStandaloneIssueLink(url)
51 ? await readTrackerConnections({ environmentId, input: {} })
52 : null;
53 const issue =
54 issuesEnabled && context !== null
55 ? resolveIssueLinkTarget({
56 url,
57 projects: projects.filter((project) => project.environmentId === environmentId),
58 preferredProjectId: context.projectId,
59 trackers: connections?._tag === "Success" ? connections.value : undefined,
60 })
61 : null;
62 if (!issue || !environmentId) return tryOpenExternalUrl(url, "pull-request");
63 const candidate = pullRequestCandidateForIssueLink(url, issue.ref);
64 if (candidate) {
65 const preview = await readPreview({ environmentId, input: candidate.target });
66 if (preview._tag === "Success") {
67 const params = withKnownPullRequestSummary({
68 ...candidate.target,
69 environmentId,
70 ...(candidate.navigationUrl
71 ? { navigationUrl: candidate.navigationUrl, navigationId: nextLinkNavigationId() }
72 : {}),
73 });
74 if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
75 else navigation.dispatch(StackActions.push("PullRequest", params));
76 return true;
77 }
78 // A dropped connection says nothing about what the link names; the browser knows.
79 if (isAtomCommandInterrupted(preview)) return tryOpenExternalUrl(url, "pull-request");
80 }
81 const issueParams = {
82 environmentId,
83 projectId: issue.ref.projectId,
84 tracker: issue.ref.tracker,
85 ...(issue.ref.host === undefined ? {} : { host: issue.ref.host }),
86 scope: issue.ref.scope,
87 issueKey: issue.ref.key,
88 };
89 if (replace) navigation.dispatch(StackActions.replace("Issue", issueParams));
90 else navigation.dispatch(StackActions.push("Issue", issueParams));
91 return true;
92 }
93 const params = withKnownPullRequestSummary({
94 ...destination,
95 ...(destination.navigationUrl ? { navigationId: nextLinkNavigationId() } : {}),
96 });
97 if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
98 else navigation.dispatch(StackActions.push("PullRequest", params));
99 return true;
100 };
101 }
102
103 export function useOpenPullRequest(options?: {
104 readonly replace?: boolean;
105 readonly context?: PullRequestContext;
106 }) {
107 const route = useRoute();
108 const routeContext = route.params as
109 | {
110 environmentId?: string | string[];
111 projectId?: string | string[];
112 threadId?: string | string[];
113 }
114 | undefined;
115 const first = (value: string | string[] | undefined) => {
116 const text = Array.isArray(value) ? value[0] : value;
117 return text?.trim() ? text : undefined;
118 };
119 const environmentId = options?.context?.environmentId ?? first(routeContext?.environmentId);
120 const threadId = first(routeContext?.threadId);
121 const thread = useThreadShell(
122 environmentId && threadId
123 ? { environmentId: EnvironmentId.make(environmentId), threadId: ThreadId.make(threadId) }
124 : null,
125 );
126 const projectId =
127 options?.context?.projectId ?? first(routeContext?.projectId) ?? thread?.projectId;
128 const navigateToPullRequest = useNavigateToPullRequest({ replace: options?.replace });
129 return (url: string) =>
130 navigateToPullRequest(
131 url,
132 environmentId && projectId
133 ? { environmentId: String(environmentId), projectId: String(projectId) }
134 : null,
135 );
136 }