| 1 | import type { NavigationState } from "@react-navigation/native"; |
| 2 | import { pullRequestListKey } from "./pullRequestList"; |
| 3 | |
| 4 | /** Follow the displayed PR through its detail routes and sheets, not an older stack entry. */ |
| 5 | export function selectedPullRequestKey(state: Pick<NavigationState, "routes" | "index">) { |
| 6 | for (let index = state.index; index >= 0; index--) { |
| 7 | const route = state.routes[index]; |
| 8 | if (!route) continue; |
| 9 | if (route.name === "Home" || route.name.startsWith("Thread")) return null; |
| 10 | if (!route.name.startsWith("PullRequest")) continue; |
| 11 | const params = route.params; |
| 12 | if ( |
| 13 | params && |
| 14 | "environmentId" in params && |
| 15 | "host" in params && |
| 16 | "repository" in params && |
| 17 | "number" in params && |
| 18 | typeof params.environmentId === "string" && |
| 19 | typeof params.host === "string" && |
| 20 | typeof params.repository === "string" && |
| 21 | typeof params.number === "number" |
| 22 | ) { |
| 23 | return `${params.environmentId}:${pullRequestListKey({ |
| 24 | host: params.host, |
| 25 | repository: params.repository, |
| 26 | number: params.number, |
| 27 | })}`; |
| 28 | } |
| 29 | if (route.name === "PullRequest") return null; |
| 30 | } |
| 31 | return null; |
| 32 | } |