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

_chat.pull-requests.tsx

89 lines3.7 KB

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