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