Open History to see the last commit
1import type { NavigationState } from "@react-navigation/native";
2import {
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. */
11export 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
22export 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. */
34export function issueIdentity({ initialSummary: _, ...identity }: IssueParams) {
35 return identity;
36}
37
38/** The identity the Issues tab remembers and the list marks as selected. */
39export 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/** Whether a list row is the selected issue. A key without a host names the issue on its public host. */
52export function isSelectedIssueRow(
53 selectedKey: string | null | undefined,
54 row: Readonly<Record<"environmentId" | "projectId" | "host" | "scope" | "key", string>>,
55) {
56 return [row.host, ""].some(
57 (host) => selectedKey === issueParamsKey({ ...row, issueKey: row.key, host }),
58 );
59}
60
61/** The selected issue's key from a route's params, when they name one. */
62export function selectedIssueKeyOf(params: object | undefined) {
63 const value = params as Partial<Record<keyof IssueParams, unknown>> | undefined;
64 return typeof value?.environmentId === "string" &&
65 typeof value.projectId === "string" &&
66 typeof value.scope === "string" &&
67 typeof value.issueKey === "string"
68 ? issueParamsKey({
69 environmentId: value.environmentId,
70 projectId: value.projectId,
71 scope: value.scope,
72 issueKey: value.issueKey,
73 ...(typeof value.host === "string" ? { host: value.host } : {}),
74 })
75 : null;
76}
77
78/** The issue the detail pane shows, for marking its row in the Android sidebar. */
79export function selectedIssueKey(state: Pick<NavigationState, "routes" | "index">) {
80 for (let index = state.index; index >= 0; index--) {
81 const route = state.routes[index];
82 if (!route) continue;
83 if (route.name === "Home" || !route.name.startsWith("Issue")) return null;
84 const key = selectedIssueKeyOf(route.params);
85 if (key !== null || route.name === "Issue") return key;
86 }
87 return null;
88}