From 3374556f35138baf288cf055896b30140fa46b47 Mon Sep 17 00:00:00 2001 From: yujiezhang-ops Date: Thu, 19 Nov 2026 18:30:01 +0800 Subject: [PATCH] fix: keep Marketplace search text typed just before opening a card The search box writes its text to the URL after a 180ms debounce. Opening a card inside that window unmounted the page, the cleanup dropped the timer, and the detail page went back to a URL without the search text. Commit the pending text before navigating to a card, and fold it into filter and history URL writes so neither write overwrites the other. URL writes now build on the latest written params rather than on the last render, which also closes the gap between a write and its render. Co-Authored-By: Claude Sonnet 5 --- frontend/src/pages/MarketplacePage.test.tsx | 61 ++++++++++++++- frontend/src/pages/MarketplacePage.tsx | 84 +++++++++++++++------ 2 files changed, 120 insertions(+), 25 deletions(-) diff --git a/frontend/src/pages/MarketplacePage.test.tsx b/frontend/src/pages/MarketplacePage.test.tsx index f7586e4b..6be69b06 100644 --- a/frontend/src/pages/MarketplacePage.test.tsx +++ b/frontend/src/pages/MarketplacePage.test.tsx @@ -1,6 +1,6 @@ -import { render, screen, within } from "@testing-library/react"; +import { act, fireEvent, render, screen, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { MemoryRouter } from "react-router-dom"; +import { MemoryRouter, Route, Routes, useLocation, useNavigate } from "react-router-dom"; import { afterEach, describe, expect, it, vi } from "vitest"; // Block the real Wails runtime: importing it registers a module-level timer in @@ -68,6 +68,35 @@ afterEach(() => { sessionStorage.clear(); }); +function LocationProbe() { + const location = useLocation(); + return {`${location.pathname}${location.search}`}; +} + +// Stands in for the detail page, which goes back to the recorded returnTo. +function DetailBackProbe() { + const navigate = useNavigate(); + const { returnTo } = useLocation().state as { returnTo: string }; + return ; +} + +function renderMarketplaceRoutes() { + render( + + + + + } /> + } /> + + + , + ); +} + +const currentLocation = () => screen.getByTestId("location").textContent; +const searchBox = () => screen.getByRole("textbox", { name: "Search Marketplace" }); + describe("filterMarketplaceItems", () => { it("returns all items when category is 'all' and query is empty", () => { expect(filterMarketplaceItems(ITEMS, "all", "")).toHaveLength(3); @@ -208,6 +237,34 @@ describe("MarketplacePage category URL", () => { }); }); + it("keeps search text typed just before opening a card when going back", () => { + renderMarketplaceRoutes(); + + // No timer runs between the two events, so the 180ms debounce is pending. + fireEvent.change(searchBox(), { target: { value: "ultracode" } }); + expect(currentLocation()).toBe("/marketplace"); + fireEvent.click(screen.getByRole("link", { name: "Ultracode Skill" })); + + expect(currentLocation()).toBe("/marketplace/skill-a"); + expect(readMarketplaceQuerySession()?.returnTo).toBe("/marketplace?q=ultracode"); + + fireEvent.click(screen.getByRole("button", { name: "Back" })); + expect(currentLocation()).toBe("/marketplace?q=ultracode"); + expect(searchBox().value).toBe("ultracode"); + }); + + it("keeps both the pending search text and a filter changed in the same moment", async () => { + renderMarketplaceRoutes(); + fireEvent.click(screen.getByRole("button", { name: "Tool type" })); + + fireEvent.change(searchBox(), { target: { value: "ultracode" } }); + fireEvent.click(screen.getByLabelText("Skill")); + expect(currentLocation()).toBe("/marketplace?q=ultracode&kind=skill"); + + await act(async () => { await new Promise((resolve) => setTimeout(resolve, 250)); }); + expect(currentLocation()).toBe("/marketplace?q=ultracode&kind=skill"); + }); + it("does not expose filter values or top-level resource categories without tool supply", async () => { const user = userEvent.setup(); render( diff --git a/frontend/src/pages/MarketplacePage.tsx b/frontend/src/pages/MarketplacePage.tsx index 8f8fcc34..485428c5 100644 --- a/frontend/src/pages/MarketplacePage.tsx +++ b/frontend/src/pages/MarketplacePage.tsx @@ -407,7 +407,13 @@ export function KindBadge({ item }: { item: MarketplaceItem }) { return {kinds.length > 1 ? +{kinds.length - 1} : null}; } -function VirtualMarketplaceGrid({ items, onCopied }: { items: MarketplaceItem[]; onCopied: (item: MarketplaceItem) => void }) { +type MarketplaceCardProps = { + onCopied: (item: MarketplaceItem) => void; + // Returns the list's query string after committing any debounced search text. + currentSearch: () => string; +}; + +function VirtualMarketplaceGrid({ items, onCopied, currentSearch }: { items: MarketplaceItem[] } & MarketplaceCardProps) { const [scrollTop, setScrollTop] = useState(0); const [viewportHeight, setViewportHeight] = useState(() => window.innerHeight); const [viewportWidth, setViewportWidth] = useState(() => window.innerWidth); @@ -436,7 +442,7 @@ function VirtualMarketplaceGrid({ items, onCopied }: { items: MarketplaceItem[]; // as if it had added results when the unfiltered virtual list was already // showing only its viewport. Small result sets remain fully accessible. if (items.length <= 24) { - return
    {items.map((item) =>
  • )}
; + return
    {items.map((item) =>
  • )}
; } const rowCount = Math.ceil(items.length / columns); const topRow = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan); @@ -447,7 +453,7 @@ function VirtualMarketplaceGrid({ items, onCopied }: { items: MarketplaceItem[]; return <>