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

useOpenPullRequest.ts

122 lines5.3 KB

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