@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 105 lines 4.42 KB
Raw
1 import { pullRequestFilterProjects } from "@t3tools/client-runtime/pull-request-project-filter";
2 import type { EnvironmentProject } from "@t3tools/client-runtime/state/models";
3 import {
4 ISSUE_LIST_MAX_EXCLUDED_PROJECTS,
5 type EnvironmentId,
6 type IssueListInput,
7 type ProjectId,
8 } from "@t3tools/contracts";
9
10 import type { IssueListTarget } from "@t3tools/client-runtime/state/issues";
11
12 import type { IssuesSearch } from "./IssuesPage";
13
14 // The router reads a bare `key=42` as a number.
15 const bounded = (value: unknown): string | undefined => {
16 const text = typeof value === "number" ? String(value) : value;
17 return typeof text === "string" && text.trim() ? text.trim().slice(0, 200) : undefined;
18 };
19
20 const labels = (raw: unknown): ReadonlyArray<string> | undefined => {
21 const names = (Array.isArray(raw) ? raw : [raw]).flatMap((name) => bounded(name) ?? []);
22 return names.length === 0 ? undefined : names.slice(0, 20);
23 };
24
25 const projectPairs = (raw: unknown): IssuesSearch["hideProjects"] => {
26 const pairs = (Array.isArray(raw) ? raw : []).flatMap((pair) => {
27 const environmentId = Array.isArray(pair) ? bounded(pair[0]) : undefined;
28 const projectId = Array.isArray(pair) ? bounded(pair[1]) : undefined;
29 return environmentId && projectId
30 ? [[environmentId as EnvironmentId, projectId as ProjectId] as const]
31 : [];
32 });
33 return pairs.length === 0 ? undefined : pairs.slice(0, ISSUE_LIST_MAX_EXCLUDED_PROJECTS);
34 };
35
36 // Every optional key is an own key, even when undefined: the router lays these over the parent
37 // route's unvalidated search, where a key left out would keep its raw value.
38 export const issuesSearchFromUrl = (raw: Record<string, unknown>): IssuesSearch => ({
39 who:
40 raw.who === "assigned" || raw.who === "authored" || raw.who === "mentioned" ? raw.who : "all",
41 status: raw.status === "closed" || raw.status === "all" ? raw.status : "open",
42 hideProjects: projectPairs(raw.hideProjects),
43 host: bounded(raw.host),
44 q: bounded(raw.q),
45 label: labels(raw.label),
46 assignee: bounded(raw.assignee),
47 order: raw.order === "created" ? "created" : undefined,
48 selectedEnvironmentId: bounded(raw.selectedEnvironmentId) as EnvironmentId | undefined,
49 selectedProjectId: bounded(raw.selectedProjectId) as ProjectId | undefined,
50 selectedHost: bounded(raw.selectedHost),
51 tracker: bounded(raw.tracker),
52 scope: bounded(raw.scope),
53 key: bounded(raw.key),
54 });
55
56 /**
57 * One choice per project: projects on one repository can read different Linear teams. Scratch
58 * projects stay, since Settings can bind one to a Linear team.
59 */
60 export const issueFilterProjects = (
61 projects: ReadonlyArray<EnvironmentProject>,
62 environmentLabels: ReadonlyMap<EnvironmentId, string>,
63 ) =>
64 pullRequestFilterProjects(
65 projects.map((project) => ({ ...project, repositoryIdentity: null })),
66 environmentLabels,
67 );
68
69 /** The same key as `pullRequestProjectKey`. */
70 const projectKey = (project: { readonly id: ProjectId; readonly environmentId: EnvironmentId }) =>
71 JSON.stringify([project.environmentId, project.id]);
72
73 export const issueProjectKeys = (pairs: IssuesSearch["hideProjects"]): ReadonlySet<string> =>
74 new Set((pairs ?? []).map((pair) => JSON.stringify(pair)));
75
76 /**
77 * Each environment leaves out its hidden projects, named from the saved pairs so a listing sent
78 * before the project list loads still leaves them out. One with every project hidden is not read.
79 * Rows past the request limit are hidden on the client.
80 */
81 export function issueListTargets(
82 environmentIds: ReadonlyArray<EnvironmentId>,
83 projects: ReadonlyArray<{ readonly id: ProjectId; readonly environmentId: EnvironmentId }>,
84 hidden: IssuesSearch["hideProjects"],
85 input: IssueListInput,
86 ): ReadonlyArray<IssueListTarget> {
87 const keys = issueProjectKeys(hidden);
88 return environmentIds.flatMap((environmentId) => {
89 const own = projects.filter((project) => project.environmentId === environmentId);
90 if (own.length > 0 && own.every((project) => keys.has(projectKey(project)))) return [];
91 const excludeProjectIds = (
92 own.length > 0
93 ? own.filter((project) => keys.has(projectKey(project))).map((project) => project.id)
94 : (hidden ?? [])
95 .filter(([environment]) => environment === environmentId)
96 .map(([, projectId]) => projectId)
97 ).slice(0, ISSUE_LIST_MAX_EXCLUDED_PROJECTS);
98 return [
99 {
100 environmentId,
101 input: excludeProjectIds.length ? { ...input, excludeProjectIds } : input,
102 },
103 ];
104 });
105 }