Open History to see the last commit
1import { useNavigation, useRoute, StackActions } from "@react-navigation/native";
2import { nativePullRequestDestination } from "../lib/pullRequestNavigation";
3import { useCallback } from "react";
4import { Platform } from "react-native";
5import { useSelectWorkspaceDetail } from "../features/workspace/useSelectWorkspaceDetail";
6import { EnvironmentId, ThreadId } from "@t3tools/contracts";
7import { useProjects, useThreadShell } from "../state/entities";
8import { tryOpenExternalUrl } from "../lib/openExternalUrl";
9import { appAtomRegistry } from "../state/atom-registry";
10import { environmentServerConfigsAtom } from "../state/server";
11
12type PullRequestContext = { readonly environmentId: string; readonly projectId: string };
13
14// Avoid useRoute so layouts without route context can open any thread's pull requests.
15export function useNavigateToPullRequest(options?: { readonly replace?: boolean }) {
16 const navigation = useNavigation();
17 const selectDetail = useSelectWorkspaceDetail();
18 const projects = useProjects();
19 const replace = options?.replace;
20 return useCallback(
21 async (url: string, context: PullRequestContext | null) => {
22 // Read at press time so a capability push does not hand every link a new handler.
23 const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom);
24 const params = nativePullRequestDestination(
25 url,
26 context,
27 projects,
28 (environmentId) => serverConfigs.get(environmentId)?.environment.capabilities,
29 );
30 if (!params) return tryOpenExternalUrl(url, "pull-request");
31 // Android shows one page at a time; iOS opens it over the current page, so Back returns.
32 if (Platform.OS === "android") selectDetail({ name: "PullRequest", params });
33 else if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
34 else navigation.dispatch(StackActions.push("PullRequest", params));
35 return true;
36 },
37 [navigation, projects, replace, selectDetail],
38 );
39}
40
41export function useOpenPullRequest(options?: {
42 readonly replace?: boolean;
43 readonly context?: PullRequestContext;
44}) {
45 const route = useRoute();
46 const routeContext = route.params as
47 | {
48 environmentId?: string | string[];
49 projectId?: string | string[];
50 threadId?: string | string[];
51 }
52 | undefined;
53 const first = (value: string | string[] | undefined) => {
54 const text = Array.isArray(value) ? value[0] : value;
55 return text?.trim() ? text : undefined;
56 };
57 const environmentId = options?.context?.environmentId ?? first(routeContext?.environmentId);
58 const threadId = first(routeContext?.threadId);
59 const thread = useThreadShell(
60 environmentId && threadId
61 ? { environmentId: EnvironmentId.make(environmentId), threadId: ThreadId.make(threadId) }
62 : null,
63 );
64 const projectId =
65 options?.context?.projectId ?? first(routeContext?.projectId) ?? thread?.projectId;
66 const navigateToPullRequest = useNavigateToPullRequest({ replace: options?.replace });
67 return useCallback(
68 (url: string) =>
69 navigateToPullRequest(
70 url,
71 environmentId && projectId
72 ? { environmentId: String(environmentId), projectId: String(projectId) }
73 : null,
74 ),
75 [environmentId, projectId, navigateToPullRequest],
76 );
77}