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 { CommonActions } from "@react-navigation/native";
6import { androidWorkspaceNavigationState } from "../lib/android-workspace-navigation";
7import { EnvironmentId, ThreadId } from "@t3tools/contracts";
8import { useThreadShell } from "../state/entities";
9import { tryOpenExternalUrl } from "../lib/openExternalUrl";
10
11/** Use the thread's environment; the shared router resolves the repository's project there. */
12export function useOpenPullRequest(options?: {
13 readonly replace?: boolean;
14 readonly context?: { readonly environmentId: string; readonly projectId: string };
15}) {
16 const navigation = useNavigation();
17 const route = useRoute();
18 const routeContext = route.params as
19 | {
20 environmentId?: string | string[];
21 projectId?: string | string[];
22 threadId?: string | string[];
23 }
24 | undefined;
25 const first = (value: string | string[] | undefined) => {
26 const text = Array.isArray(value) ? value[0] : value;
27 return text?.trim() ? text : undefined;
28 };
29 const environmentId = options?.context?.environmentId ?? first(routeContext?.environmentId);
30 const threadId = first(routeContext?.threadId);
31 const thread = useThreadShell(
32 environmentId && threadId
33 ? { environmentId: EnvironmentId.make(environmentId), threadId: ThreadId.make(threadId) }
34 : null,
35 );
36 const projectId =
37 options?.context?.projectId ?? first(routeContext?.projectId) ?? thread?.projectId;
38 const replace = options?.replace;
39 return useCallback(
40 async (url: string) => {
41 const params = nativePullRequestDestination(
42 url,
43 environmentId && projectId
44 ? { environmentId: String(environmentId), projectId: String(projectId) }
45 : null,
46 );
47 if (!params) return tryOpenExternalUrl(url, "pull-request");
48 if (Platform.OS === "android")
49 navigation.dispatch((state) =>
50 CommonActions.reset(
51 androidWorkspaceNavigationState(state, { name: "PullRequest", params }),
52 ),
53 );
54 else if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
55 else navigation.navigate("PullRequest", params);
56 return true;
57 },
58 [environmentId, projectId, navigation, replace],
59 );
60}