TypeScript
fix(web): deduplicate PR project filter choices (#9948)
Julius Marmingee5a87e8b9ca9
diff --git a/apps/web/src/components/pullRequest/PullRequestListFilters.tsx b/apps/web/src/components/pullRequest/PullRequestListFilters.tsxindex e45a687e981..f77e2b84508 100644--- a/apps/web/src/components/pullRequest/PullRequestListFilters.tsx+++ b/apps/web/src/components/pullRequest/PullRequestListFilters.tsx@@ -37,6 +37,7 @@ import {MenuPopup,MenuRadioGroup,MenuRadioItem,+ MenuRadioItemIndicator,MenuSeparator,MenuSub,MenuSubPopup,@@ -205,6 +206,7 @@ function PullRequestFilterRadioGroup<Value extends string>({<PullRequestFilterOptionIcon option={option} /><span className="min-w-0 flex-1 truncate">{option.label}</span>{option.unavailable ? <span className="shrink-0">· Unavailable</span> : null}+ <MenuRadioItemIndicator /></span></MenuRadioItem>);@@ -455,8 +457,8 @@ export function PullRequestFiltersMenu({readonly environmentId: EnvironmentId;readonly title: string;readonly workspaceRoot: string;- readonly faviconPath?: string | null;- readonly projectIcon?: ProjectIconOverride | null;+ readonly faviconPath?: string | null | undefined;+ readonly projectIcon?: ProjectIconOverride | null | undefined;}>;projectId: ProjectId | undefined;/**diff --git a/apps/web/src/components/pullRequest/pullRequestProjectFilter.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestProjectFilter.logic.test.tsnew file mode 100644index 00000000000..5c660118ed2--- /dev/null+++ b/apps/web/src/components/pullRequest/pullRequestProjectFilter.logic.test.ts@@ -0,0 +1,158 @@+import { EnvironmentId, ProjectId } from "@t3tools/contracts";+import { describe, expect, it } from "vite-plus/test";++import { findScopedProject } from "./pullRequestList.logic";+import { pullRequestFilterProjects } from "./pullRequestProjectFilter.logic";++const cups = EnvironmentId.make("env-cups");+const nucbox = EnvironmentId.make("env-nucbox");+const labels = new Map([+ [cups, "cups"],+ [nucbox, "nucbox-1"],+]);++function project(+ id: string,+ environmentId = nucbox,+ canonicalKey: string | null = "github.com/pingdotgg/t3code",+) {+ return {+ id: ProjectId.make(id),+ environmentId,+ title: "t3code",+ workspaceRoot: `/work/${id}`,+ repositoryIdentity: canonicalKey === null ? null : { canonicalKey },+ faviconPath: `${id}/favicon.png`,+ };+}++describe("pull request project filter choices", () => {+ it("collapses three checkouts on one server without dropping another server's copy", () => {+ const projects = [+ project("main"),+ project("worktree-1"),+ project("worktree-2"),+ project("main", cups),+ ];++ const choices = pullRequestFilterProjects(projects, labels);++ expect(choices.map(({ id, environmentId, title }) => ({ id, environmentId, title }))).toEqual([+ { id: "main", environmentId: cups, title: "t3code · cups" },+ { id: "main", environmentId: nucbox, title: "t3code · nucbox-1" },+ ]);+ expect(choices[1]?.workspaceRoot).toBe("/work/main");+ expect(choices[1]?.faviconPath).toBe("main/favicon.png");+ });++ it("keeps a saved worktree selection as the repository's only choice", () => {+ const projects = [project("main"), project("worktree"), project("worktree", cups)];+ const selected = findScopedProject(projects, nucbox, "worktree");++ const choices = pullRequestFilterProjects(projects, labels, selected);++ expect(choices.filter((choice) => choice.environmentId === nucbox)).toEqual([+ { ...projects[1], title: "t3code · nucbox-1" },+ ]);+ expect(findScopedProject(choices, nucbox, "worktree")).toBeDefined();+ expect(findScopedProject(choices, nucbox, "main")).toBeUndefined();+ expect(findScopedProject(choices, cups, "worktree")).toBeDefined();+ });++ it("matches canonical repositories regardless of casing", () => {+ const main = project("main");+ const worktree = project("worktree", nucbox, "GitHub.com/PingDotGG/T3Code");++ expect(pullRequestFilterProjects([main, worktree], labels)).toEqual([main]);+ });++ it("does not add a server suffix after duplicate checkouts have collapsed", () => {+ const main = project("main");++ expect(pullRequestFilterProjects([main, project("worktree")], labels)).toEqual([main]);+ expect(main.title).toBe("t3code");+ });++ it("distinguishes same-named repositories on one server by checkout path", () => {+ const projects = [+ project("upstream"),+ project("fork", nucbox, "github.com/juliusmarminge/t3code"),+ ];++ const choices = pullRequestFilterProjects(projects, labels);++ expect(choices.map((choice) => choice.title)).toEqual([+ "t3code · nucbox-1 · /work/fork",+ "t3code · nucbox-1 · /work/upstream",+ ]);+ });++ it("keeps repositories on different hosts separate", () => {+ const choices = pullRequestFilterProjects(+ [project("github"), project("enterprise", nucbox, "git.example.com/pingdotgg/t3code")],+ labels,+ );++ expect(choices.map((choice) => choice.id)).toEqual(["enterprise", "github"]);+ });++ it("does not merge projects whose repository identity is unknown", () => {+ const choices = pullRequestFilterProjects(+ [project("first", nucbox, null), project("second", nucbox, null)],+ labels,+ );++ expect(choices.map((choice) => choice.title)).toEqual([+ "t3code · nucbox-1 · /work/first",+ "t3code · nucbox-1 · /work/second",+ ]);+ });++ it("distinguishes servers with the same display name and checkout path", () => {+ const first = project("main");+ const second = project("main", cups);+ const repeatedLabels = new Map([+ [cups, "nucbox-1"],+ [nucbox, "nucbox-1"],+ ]);++ const choices = pullRequestFilterProjects([first, second], repeatedLabels);++ expect(choices.map((choice) => choice.title)).toEqual([+ "t3code · nucbox-1 · /work/main · env-cups",+ "t3code · nucbox-1 · /work/main · env-nucbox",+ ]);+ });++ it("uses the environment id when its label is unavailable", () => {+ const choices = pullRequestFilterProjects(+ [project("main"), project("remote", cups)],+ new Map(),+ );++ expect(choices.map((choice) => choice.title)).toEqual([+ "t3code · env-cups",+ "t3code · env-nucbox",+ ]);+ });++ it("can distinguish unresolved project records that also share a checkout path", () => {+ const first = project("first", nucbox, null);+ const second = { ...project("second", nucbox, null), workspaceRoot: first.workspaceRoot };++ const choices = pullRequestFilterProjects([first, second], labels);++ expect(choices.map((choice) => choice.title)).toEqual([+ "t3code · nucbox-1 · /work/first · env-nucbox · first",+ "t3code · nucbox-1 · /work/first · env-nucbox · second",+ ]);+ });++ it("leaves unrelated names unchanged and orders them alphabetically", () => {+ const app = { ...project("app"), title: "Zebra" };+ const tools = { ...project("tools", nucbox, "github.com/acme/tools"), title: "Alpha" };++ expect(pullRequestFilterProjects([app, tools], labels)).toEqual([tools, app]);+ expect(pullRequestFilterProjects([], labels)).toEqual([]);+ });+});diff --git a/apps/web/src/components/pullRequest/pullRequestProjectFilter.logic.ts b/apps/web/src/components/pullRequest/pullRequestProjectFilter.logic.tsnew file mode 100644index 00000000000..5b214e3f37f--- /dev/null+++ b/apps/web/src/components/pullRequest/pullRequestProjectFilter.logic.ts@@ -0,0 +1,53 @@+import type { EnvironmentId } from "@t3tools/contracts";++import type { AssignableProject } from "./pullRequestProjectAssignment.logic";++interface FilterProject extends AssignableProject {+ readonly title: string;+ readonly workspaceRoot: string;+}++function distinguishTitles<Project extends FilterProject>(+ projects: ReadonlyArray<Project>,+ suffix: (project: Project) => string,+) {+ const counts = new Map<string, number>();+ for (const project of projects) {+ counts.set(project.title, (counts.get(project.title) ?? 0) + 1);+ }+ return projects.map((project) =>+ (counts.get(project.title) ?? 0) > 1+ ? { ...project, title: `${project.title} · ${suffix(project)}` }+ : project,+ );+}++/** One choice per repository per server, retaining the selected checkout for saved scopes. */+export function pullRequestFilterProjects<Project extends FilterProject>(+ projects: ReadonlyArray<Project>,+ environmentLabels: ReadonlyMap<EnvironmentId, string>,+ selectedProject?: Pick<AssignableProject, "id" | "environmentId">,+) {+ const byRepository = new Map<string, Project>();+ for (const project of projects) {+ const repository = project.repositoryIdentity?.canonicalKey?.toLowerCase();+ const key = JSON.stringify([+ project.environmentId,+ repository ? ["repository", repository] : ["project", project.id],+ ]);+ const selected =+ project.id === selectedProject?.id && project.environmentId === selectedProject.environmentId;+ if (!byRepository.has(key) || selected) byRepository.set(key, project);+ }++ const byServer = distinguishTitles(+ [...byRepository.values()],+ (project) => environmentLabels.get(project.environmentId) ?? project.environmentId,+ );+ const byPath = distinguishTitles(byServer, (project) => project.workspaceRoot);+ // Separate environments can share both their display name and their checkout path.+ const byEnvironment = distinguishTitles(byPath, (project) => project.environmentId);+ return distinguishTitles(byEnvironment, (project) => project.id).toSorted((left, right) =>+ left.title.localeCompare(right.title),+ );+}diff --git a/apps/web/src/components/ui/menu.tsx b/apps/web/src/components/ui/menu.tsxindex d7892cb228a..fbdcbc03480 100644--- a/apps/web/src/components/ui/menu.tsx+++ b/apps/web/src/components/ui/menu.tsx@@ -1,7 +1,7 @@"use client";import { Menu as MenuPrimitive } from "@base-ui/react/menu";-import { ChevronRightIcon } from "lucide-react";+import { CheckIcon, ChevronRightIcon } from "lucide-react";import type * as React from "react";import { cn } from "~/lib/utils";@@ -177,6 +177,23 @@ function MenuRadioItem({);}+function MenuRadioItemIndicator({+ className,+ children,+ ...props+}: MenuPrimitive.RadioItemIndicator.Props) {+ return (+ <MenuPrimitive.RadioItemIndicator+ aria-hidden+ className={cn("flex shrink-0", className)}+ data-slot="menu-radio-item-indicator"+ {...props}+ >+ {children ?? <CheckIcon className="size-3.5" />}+ </MenuPrimitive.RadioItemIndicator>+ );+}+function MenuGroupLabel({className,inset,@@ -300,6 +317,7 @@ export {MenuRadioGroup as DropdownMenuRadioGroup,MenuRadioItem,MenuRadioItem as DropdownMenuRadioItem,+ MenuRadioItemIndicator,MenuGroupLabel,MenuGroupLabel as DropdownMenuLabel,MenuSeparator,diff --git a/apps/web/src/routes/_chat.pull-requests.tsx b/apps/web/src/routes/_chat.pull-requests.tsxindex a9537423a7f..37522ea3b0a 100644--- a/apps/web/src/routes/_chat.pull-requests.tsx+++ b/apps/web/src/routes/_chat.pull-requests.tsx@@ -86,6 +86,7 @@ import {writePullRequestListPreferences,} from "../components/pullRequest/pullRequestListPreferences";import { assignProjectsToEnvironments } from "../components/pullRequest/pullRequestProjectAssignment.logic";+import { pullRequestFilterProjects } from "../components/pullRequest/pullRequestProjectFilter.logic";import { environmentMachineIcon } from "../components/EnvironmentMachineIcon";import { PullRequestDetailPanel } from "../components/pullRequest/PullRequestDetailPanel";import {@@ -356,27 +357,6 @@ function PullRequestsRouteView() {),[environments],);- const scopedProjects = useMemo(() => {- // Two machines can hold the same repository, so a title the workspace carries twice is told- // apart by the environment it lives on rather than left as two identical rows.- const titleCounts = new Map<string, number>();- for (const project of projects) {- titleCounts.set(project.title, (titleCounts.get(project.title) ?? 0) + 1);- }- return projects- .map((project) => ({- id: project.id,- environmentId: project.environmentId,- title:- (titleCounts.get(project.title) ?? 0) > 1- ? `${project.title} · ${environmentLabels.get(project.environmentId) ?? project.environmentId}`- : project.title,- workspaceRoot: project.workspaceRoot,- faviconPath: project.faviconPath ?? null,- projectIcon: project.projectIcon ?? null,- }))- .toSorted((left, right) => left.title.localeCompare(right.title));- }, [environmentLabels, projects]);// The scope the URL asks for, once the environments have had their say about whether it exists.const scopedProjectId = useMemo(() => resolveProjectScope(search.projectId, projects, projectsKnown),@@ -386,6 +366,10 @@ function PullRequestsRouteView() {() => findScopedProject(projects, scopedEnvironmentId, scopedProjectId),[projects, scopedEnvironmentId, scopedProjectId],);+ const scopedProjects = useMemo(+ () => pullRequestFilterProjects(projects, environmentLabels, scopedProject),+ [environmentLabels, projects, scopedProject],+ );// A link from a thread or the sidebar only knows the repository, so the owning project is// resolved here; an explicit `projectId` in the URL still wins.