The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { NavigationState } from "@react-navigation/native"; |
| 2 | import { |
| 3 | EnvironmentId, |
| 4 | IssueTrackerKind, |
| 5 | ProjectId, |
| 6 | type IssueListEntry, |
| 7 | type IssueRef, |
| 8 | } from "@t3tools/contracts"; |
| 9 | |
| 10 | /** Route params that name one issue. Plain strings, so a link can carry them. */ |
| 11 | export type IssueParams = { |
| 12 | readonly environmentId: string; |
| 13 | readonly projectId: string; |
| 14 | readonly tracker: string; |
| 15 | readonly host?: string; |
| 16 | readonly scope: string; |
| 17 | readonly issueKey: string; |
| 18 | /** The list row the issue was opened from, shown until its detail arrives. */ |
| 19 | readonly initialSummary?: IssueListEntry; |
| 20 | }; |
| 21 | |
| 22 | export function issueTarget(params: IssueParams) { |
| 23 | const input: IssueRef = { |
| 24 | projectId: ProjectId.make(params.projectId), |
| 25 | tracker: IssueTrackerKind.make(params.tracker), |
| 26 | ...(params.host ? { host: params.host } : {}), |
| 27 | scope: params.scope, |
| 28 | key: params.issueKey, |
| 29 | }; |
| 30 | return { environmentId: EnvironmentId.make(params.environmentId), input }; |
| 31 | } |
| 32 | |
| 33 | /** The params without the list row, for the screens an issue opens. */ |
| 34 | export function issueIdentity({ initialSummary: _, ...identity }: IssueParams) { |
| 35 | return identity; |
| 36 | } |
| 37 | |
| 38 | /** The identity the Issues tab remembers and the list marks as selected. */ |
| 39 | export function issueParamsKey( |
| 40 | params: Pick<IssueParams, "environmentId" | "projectId" | "host" | "scope" | "issueKey">, |
| 41 | ) { |
| 42 | return [ |
| 43 | params.environmentId, |
| 44 | params.projectId, |
| 45 | (params.host ?? "").toLowerCase(), |
| 46 | params.scope.toLowerCase(), |
| 47 | params.issueKey, |
| 48 | ].join("\u0000"); |
| 49 | } |
| 50 | |
| 51 | /** The selected issue's key from a route's params, when they name one. */ |
| 52 | export function selectedIssueKeyOf(params: object | undefined) { |
| 53 | const value = params as Partial<Record<keyof IssueParams, unknown>> | undefined; |
| 54 | return typeof value?.environmentId === "string" && |
| 55 | typeof value.projectId === "string" && |
| 56 | typeof value.scope === "string" && |
| 57 | typeof value.issueKey === "string" |
| 58 | ? issueParamsKey({ |
| 59 | environmentId: value.environmentId, |
| 60 | projectId: value.projectId, |
| 61 | scope: value.scope, |
| 62 | issueKey: value.issueKey, |
| 63 | ...(typeof value.host === "string" ? { host: value.host } : {}), |
| 64 | }) |
| 65 | : null; |
| 66 | } |
| 67 | |
| 68 | /** The issue the detail pane shows, for marking its row in the Android sidebar. */ |
| 69 | export function selectedIssueKey(state: Pick<NavigationState, "routes" | "index">) { |
| 70 | for (let index = state.index; index >= 0; index--) { |
| 71 | const route = state.routes[index]; |
| 72 | if (!route) continue; |
| 73 | if (route.name === "Home" || !route.name.startsWith("Issue")) return null; |
| 74 | const key = selectedIssueKeyOf(route.params); |
| 75 | if (key !== null || route.name === "Issue") return key; |
| 76 | } |
| 77 | return null; |
| 78 | } |