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

useOpenPullRequest.ts

69 lines3.1 KB

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​
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 async (url: string, context: PullRequestContext | null) => {
21 // Read at press time so a config push does not hand every link a new handler.
22 const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom);
23 const destination = nativePullRequestDestination(url, context, projects, (environmentId) =>
24 serverConfigs.has(environmentId),
25 );
26 if (!destination) return tryOpenExternalUrl(url, "pull-request");
27 const params = withKnownPullRequestSummary(destination);
28 // Android shows one page at a time; iOS opens it over the current page, so Back returns.
29 if (Platform.OS === "android") selectDetail({ name: "PullRequest", params });
30 else if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
31 else navigation.dispatch(StackActions.push("PullRequest", params));
32 return true;
33 };
35​
36export function useOpenPullRequest(options?: {
37 readonly replace?: boolean;
38 readonly context?: PullRequestContext;
39}) {
40 const route = useRoute();
41 const routeContext = route.params as
42 | {
43 environmentId?: string | string[];
44 projectId?: string | string[];
45 threadId?: string | string[];
46 }
47 | undefined;
48 const first = (value: string | string[] | undefined) => {
49 const text = Array.isArray(value) ? value[0] : value;
50 return text?.trim() ? text : undefined;
51 };
52 const environmentId = options?.context?.environmentId ?? first(routeContext?.environmentId);
53 const threadId = first(routeContext?.threadId);
54 const thread = useThreadShell(
55 environmentId && threadId
56 ? { environmentId: EnvironmentId.make(environmentId), threadId: ThreadId.make(threadId) }
57 : null,
58 );
59 const projectId =
60 options?.context?.projectId ?? first(routeContext?.projectId) ?? thread?.projectId;
61 const navigateToPullRequest = useNavigateToPullRequest({ replace: options?.replace });
62 return (url: string) =>
63 navigateToPullRequest(
64 url,
65 environmentId && projectId
66 ? { environmentId: String(environmentId), projectId: String(projectId) }
67 : null,
68 );