| 1 | import type { NavigationState } from "@react-navigation/native"; |
| 2 | |
| 3 | export type AndroidWorkspaceDestination = |
| 4 | | { readonly name: "Thread"; readonly params: ReactNavigation.RootParamList["Thread"] } |
| 5 | | { readonly name: "PullRequest"; readonly params: ReactNavigation.RootParamList["PullRequest"] }; |
| 6 | |
| 7 | /** Peer details share one slot above Home; nested routes retain ordinary back navigation. */ |
| 8 | export function androidWorkspaceNavigationState( |
| 9 | state: NavigationState, |
| 10 | destination?: AndroidWorkspaceDestination, |
| 11 | reviewers?: ReactNavigation.RootParamList["PullRequestReviewers"], |
| 12 | ) { |
| 13 | const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" }; |
| 14 | if (!destination) return { ...state, index: 0, routes: [home] }; |
| 15 | const identity = |
| 16 | destination.name === "Thread" |
| 17 | ? ["environmentId", "threadId"] |
| 18 | : ["environmentId", "projectId", "repository", "number", "host"]; |
| 19 | const params = destination.params as Readonly<Record<string, unknown>>; |
| 20 | const existing = state.routes.findLast((route) => { |
| 21 | if (route.name !== destination.name) return false; |
| 22 | const current = route.params as Readonly<Record<string, unknown>> | undefined; |
| 23 | return identity.every((key) => current?.[key] === params[key]); |
| 24 | }); |
| 25 | return { |
| 26 | ...state, |
| 27 | index: reviewers ? 2 : 1, |
| 28 | routes: [ |
| 29 | home, |
| 30 | { ...existing, ...destination }, |
| 31 | ...(reviewers ? [{ name: "PullRequestReviewers", params: reviewers }] : []), |
| 32 | ], |
| 33 | }; |
| 34 | } |