Open History to see the last commit
1import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
2import { splitsSurrogatePair } from "@t3tools/shared/assistantCitations";
3import { createFileRoute } from "@tanstack/react-router";
4
5import { type PullRequestListSort } from "../components/pullRequest/pullRequestListPreferences";
6import { SORT_OPTIONS } from "../components/pullRequest/pullRequestListOptions";
7import { type PullRequestsSearch } from "../components/pullRequest/usePullRequestListView";
8import { PullRequestsPage } from "../components/pullRequest/PullRequestsPage";
9
10const MAX_SEARCH_LABEL_CANDIDATES = 100;
11
12function pullRequestSearchLabels(raw: unknown): PullRequestsSearch["labels"] {
13 const values = (Array.isArray(raw) ? raw : typeof raw === "string" ? [raw] : []).slice(
14 0,
15 MAX_SEARCH_LABEL_CANDIDATES,
16 );
17 const labels: Array<string> = [];
18 const seen = new Set<string>();
19 for (const rawValue of values) {
20 if (typeof rawValue !== "string") continue;
21 const value = rawValue.trim().slice(0, 200);
22 const key = value.toLowerCase();
23 if (value.length === 0 || seen.has(key)) continue;
24 labels.push(value);
25 seen.add(key);
26 if (labels.length === 10) break;
27 }
28 return labels.length === 0 ? undefined : labels;
29}
30
31export const Route = createFileRoute("/_chat/pull-requests")({
32 validateSearch: (raw: Record<string, unknown>): PullRequestsSearch => ({
33 ...(typeof raw.selectedUrl === "string" ? { selectedUrl: raw.selectedUrl.slice(0, 4096) } : {}),
34 involvement:
35 raw.involvement === "reviewing" || raw.involvement === "authored" ? raw.involvement : "all",
36 state:
37 raw.state === "closed" || raw.state === "merged" || raw.state === "all" ? raw.state : "open",
38 ...(SORT_OPTIONS.some((option) => option.value === raw.sort)
39 ? { sort: raw.sort as PullRequestListSort }
40 : {}),
41 ...(typeof raw.repository === "string" && raw.repository
42 ? { repository: raw.repository.slice(0, 200) }
43 : {}),
44 ...(typeof raw.number === "number" && Number.isInteger(raw.number) && raw.number > 0
45 ? { number: raw.number }
46 : {}),
47 ...(typeof raw.projectId === "string" && raw.projectId
48 ? { projectId: raw.projectId as ProjectId }
49 : {}),
50 ...(typeof raw.environmentId === "string" && raw.environmentId
51 ? { environmentId: raw.environmentId as EnvironmentId }
52 : {}),
53 ...(typeof raw.host === "string" && raw.host ? { host: raw.host.slice(0, 200) } : {}),
54 ...(typeof raw.selectedProjectId === "string" && raw.selectedProjectId
55 ? { selectedProjectId: raw.selectedProjectId as ProjectId }
56 : {}),
57 ...(typeof raw.selectedHost === "string" && raw.selectedHost
58 ? { selectedHost: raw.selectedHost.slice(0, 200) }
59 : {}),
60 ...(typeof raw.selectedEnvironmentId === "string" && raw.selectedEnvironmentId
61 ? { selectedEnvironmentId: raw.selectedEnvironmentId as EnvironmentId }
62 : {}),
63 // Always include free-text keys to override numeric values in the parent route's unvalidated search.
64 q:
65 typeof raw.q === "string" && raw.q
66 ? raw.q.slice(0, splitsSurrogatePair(raw.q, 200) ? 199 : 200)
67 : undefined,
68 ...(raw.draft === "only" || raw.draft === "hide" ? { draft: raw.draft } : {}),
69 ...(raw.review === "approved" ||
70 raw.review === "changes-requested" ||
71 raw.review === "review-required" ||
72 raw.review === "none"
73 ? { review: raw.review }
74 : {}),
75 ...(raw.checks === "passing" || raw.checks === "failing" ? { checks: raw.checks } : {}),
76 author:
77 typeof raw.author === "string" && raw.author.trim()
78 ? raw.author.trim().slice(0, 200)
79 : undefined,
80 labels: pullRequestSearchLabels(raw.labels),
81 }),
82 component: PullRequestsPage,
83});