Skip to content
TypeScript

fix(web): a numeric q in the pull requests URL no longer crashes the page (#18666)

Scott Nortemandf26ef99859a
diff --git a/apps/web/src/components/pullRequest/pullRequestListPreferences.ts b/apps/web/src/components/pullRequest/pullRequestListPreferences.ts
index afb926c5862..e58e36bb9e7 100644
--- a/apps/web/src/components/pullRequest/pullRequestListPreferences.ts
+++ b/apps/web/src/components/pullRequest/pullRequestListPreferences.ts
@@ -25,7 +25,7 @@ export interface PullRequestListPreferences {
readonly environmentId?: EnvironmentId;
readonly projectId?: ProjectId;
readonly host?: string;
- readonly q?: string;
+ readonly q?: string | undefined;
readonly draft?: "only" | "hide";
readonly review?: NonNullable<PullRequestListFilters["review"]>;
readonly checks?: NonNullable<PullRequestListFilters["checks"]>;
diff --git a/apps/web/src/components/pullRequest/pullRequestsRouteSearch.test.ts b/apps/web/src/components/pullRequest/pullRequestsRouteSearch.test.ts
new file mode 100644
index 00000000000..320cfc5df85
--- /dev/null
+++ b/apps/web/src/components/pullRequest/pullRequestsRouteSearch.test.ts
@@ -0,0 +1,40 @@
+import {
+ createMemoryHistory,
+ createRootRoute,
+ createRoute,
+ createRouter,
+} from "@tanstack/react-router";
+import { describe, expect, it } from "vite-plus/test";
+
+import { Route as PullRequestsRoute } from "../../routes/_chat.pull-requests";
+
+/** Matches a URL in a router shaped like the app's: a layout without its own search rules. */
+function matchedSearch(url: string) {
+ const root = createRootRoute();
+ const chat = createRoute({ getParentRoute: () => root, id: "_chat" });
+ const pullRequests = createRoute({
+ getParentRoute: () => chat,
+ path: "pull-requests",
+ validateSearch: PullRequestsRoute.options.validateSearch,
+ });
+ const router = createRouter({
+ routeTree: root.addChildren([chat.addChildren([pullRequests])]),
+ history: createMemoryHistory({ initialEntries: [url] }),
+ });
+ // The search a component reads through `useSearch` is the matched route's, not the URL's.
+ return router.matchRoutes(router.latestLocation).at(-1)?.search as
+ | Record<string, unknown>
+ | undefined;
+}
+
+describe("pull requests route search", () => {
+ it("drops a q the URL parser turned into a number", () => {
+ expect(matchedSearch("/pull-requests?q=4089")?.q).toBeUndefined();
+ expect(matchedSearch("/pull-requests?q=true")?.q).toBeUndefined();
+ });
+
+ it("keeps a q that is text, including quoted digits", () => {
+ expect(matchedSearch("/pull-requests?q=fix")?.q).toBe("fix");
+ expect(matchedSearch("/pull-requests?q=%224089%22")?.q).toBe("4089");
+ });
+});
diff --git a/apps/web/src/routes/_chat.pull-requests.tsx b/apps/web/src/routes/_chat.pull-requests.tsx
index b3d1b140aec..f5ff710c1ac 100644
--- a/apps/web/src/routes/_chat.pull-requests.tsx
+++ b/apps/web/src/routes/_chat.pull-requests.tsx
@@ -332,7 +332,9 @@ export const Route = createFileRoute("/_chat/pull-requests")({
...(typeof raw.selectedEnvironmentId === "string" && raw.selectedEnvironmentId
? { selectedEnvironmentId: raw.selectedEnvironmentId as EnvironmentId }
: {}),
- ...(typeof raw.q === "string" && raw.q ? { q: raw.q.slice(0, 200) } : {}),
+ // Always an own key: the router lays this result over the parent route's unvalidated
+ // search, so leaving out a `q` the URL parser made a number would let the number through.
+ q: typeof raw.q === "string" && raw.q ? raw.q.slice(0, 200) : undefined,
...(raw.draft === "only" || raw.draft === "hide" ? { draft: raw.draft } : {}),
...(raw.review === "approved" ||
raw.review === "changes-requested" ||