Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

_chat.pull-requests.tsx

77 lines3.4 KB

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