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