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

useOpenPullRequest.ts

67 lines3.0 KB

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