-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(core): make term-list counts opt-out for the editor picker #2243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
khoinguyenpham04
merged 3 commits into
emdash-cms:main
from
MA2153:fix/term-list-counts-opt-out
Jul 31, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes the content editor recomputing taxonomy term usage counts every time it opens. The editor's taxonomy picker never shows counts, but it shares the terms endpoint with the Taxonomies settings page, which does — so opening an entry aggregated the whole content–term assignment table once per applicable taxonomy. `GET /_emdash/api/taxonomies/:name/terms` now takes an `includeCounts` query param (default `true`, so existing callers are unaffected); pass `includeCounts=false` to skip the aggregate, and `count` is then omitted from each term in the response. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/admin/tests/components/taxonomy-term-cache.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { Toasty } from "@cloudflare/kumo"; | ||
| import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
| import * as React from "react"; | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| import { TaxonomyManager } from "../../src/components/TaxonomyManager"; | ||
| import { TaxonomySidebar } from "../../src/components/TaxonomySidebar"; | ||
| import { render } from "../utils/render.tsx"; | ||
|
|
||
| vi.mock("../../src/lib/api/client.js", async () => { | ||
| const actual = await vi.importActual("../../src/lib/api/client.js"); | ||
| return { | ||
| ...actual, | ||
| apiFetch: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| import { apiFetch } from "../../src/lib/api/client.js"; | ||
|
|
||
| const categoriesTaxonomy = { | ||
| id: "tax_categories", | ||
| name: "categories", | ||
| label: "Categories", | ||
| labelSingular: "Category", | ||
| hierarchical: true, | ||
| collections: ["posts"], | ||
| }; | ||
|
|
||
| const terms = [ | ||
| { id: "1", name: "tech", slug: "tech", label: "Technology", parentId: null, children: [] }, | ||
| ]; | ||
|
|
||
| function dataResponse(data: unknown) { | ||
| return Promise.resolve( | ||
| new Response(JSON.stringify({ data }), { | ||
| status: 200, | ||
| headers: { "Content-Type": "application/json" }, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| /** Mirrors the endpoint: counts unless the caller opts out. */ | ||
| function mockApiFetch() { | ||
| vi.mocked(apiFetch).mockImplementation((url: string | URL | Request, init?: RequestInit) => { | ||
| const urlString = typeof url === "string" ? url : url instanceof URL ? url.toString() : url.url; | ||
| const { pathname, searchParams } = new URL(urlString, "http://localhost"); | ||
| const method = init?.method ?? "GET"; | ||
|
|
||
| if (method === "GET" && pathname === "/_emdash/api/taxonomies") { | ||
| return dataResponse({ taxonomies: [categoriesTaxonomy] }); | ||
| } | ||
|
|
||
| if (method === "GET" && pathname === "/_emdash/api/taxonomies/categories/terms") { | ||
| const withCounts = searchParams.get("includeCounts") !== "false"; | ||
| return dataResponse({ | ||
| terms: terms.map((term) => (withCounts ? { ...term, count: 5 } : term)), | ||
| }); | ||
| } | ||
|
|
||
| return dataResponse({}); | ||
| }); | ||
| } | ||
|
|
||
| function makeWrapper() { | ||
| // staleTime mirrors App.tsx: inside that window a mounting consumer is served | ||
| // the cached list without refetching. | ||
| const queryClient = new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { retry: false, staleTime: 1000 * 60 }, | ||
| mutations: { retry: false }, | ||
| }, | ||
| }); | ||
| return function Wrapper({ children }: { children: React.ReactNode }) { | ||
| return ( | ||
| <QueryClientProvider client={queryClient}> | ||
| <Toasty>{children}</Toasty> | ||
| </QueryClientProvider> | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| /** The editor sidebar renders first; the settings page mounts afterwards, as it | ||
| * would when the user navigates to it in the same SPA session. */ | ||
| function EditorThenSettings() { | ||
| const [settingsOpen, setSettingsOpen] = React.useState(false); | ||
| return ( | ||
| <> | ||
| <TaxonomySidebar collection="posts" /> | ||
| <button type="button" onClick={() => setSettingsOpen(true)}> | ||
| Open settings | ||
| </button> | ||
| {settingsOpen ? <TaxonomyManager taxonomyName="categories" /> : null} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| describe("taxonomy term cache", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockApiFetch(); | ||
| }); | ||
|
|
||
| it("shows real counts in the manager after the editor cached a count-free list", async () => { | ||
| const screen = await render(<EditorThenSettings />, { wrapper: makeWrapper() }); | ||
|
|
||
| await expect.element(screen.getByText("Technology")).toBeInTheDocument(); | ||
|
|
||
| await screen.getByRole("button", { name: "Open settings" }).click(); | ||
|
|
||
| await expect.element(screen.getByRole("heading", { name: "Categories" })).toBeInTheDocument(); | ||
| await expect.element(screen.getByText("5", { exact: true })).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.