diff --git a/.changeset/range-button-ids.md b/.changeset/range-button-ids.md new file mode 100644 index 0000000..e63ee5b --- /dev/null +++ b/.changeset/range-button-ids.md @@ -0,0 +1,5 @@ +--- +"@eisbachcode/emdash-plugin-analytics": patch +--- + +Fixes the 7, 30 and 90 day buttons on the Analytics page sharing one identifier, which the admin uses to tell the buttons of a row apart. React reported the duplicate key in the browser console during development. diff --git a/src/plugin.ts b/src/plugin.ts index f08fd76..ff2c1ae 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -38,9 +38,9 @@ import { DEFAULT_RANGE, loadPage, PAGE_PATH, + isRangeAction, PAGE_REFRESH_ACTION, parseRange, - RANGE_ACTION, renderPage, SETUP_ACTION, type RangeDays, @@ -291,7 +291,7 @@ function parseInteraction(input: unknown): Interaction { if (isAction && record.action_id === PAGE_REFRESH_ACTION) { return { surface: "page", kind: "refresh", range: parseRange(record.value) }; } - if (isAction && record.action_id === RANGE_ACTION) { + if (isAction && isRangeAction(record.action_id)) { return { surface: "page", kind: "load", range: parseRange(record.value) }; } if (isAction && record.action_id === SETUP_ACTION) { diff --git a/src/ui/page.ts b/src/ui/page.ts index 58a6ec6..2c30733 100644 --- a/src/ui/page.ts +++ b/src/ui/page.ts @@ -50,6 +50,20 @@ export const PAGE_PATH = "/analytics"; /** The per-entry page (`./content.ts`), named here to keep the import one-way. */ const CONTENT_PAGE_PATH = "/analytics/content"; export const RANGE_ACTION = "analytics:range"; + +/** + * The action id of one range's button. The admin keys the elements of an + * `actions` block by their action id, so buttons sharing one id would be + * children with the same React key. The range still travels in the value. + */ +export function rangeAction(days: number): string { + return `${RANGE_ACTION}|${days}`; +} + +/** A range button's id, or the plain id that the setup check's way back sends. */ +export function isRangeAction(id: unknown): boolean { + return id === RANGE_ACTION || (typeof id === "string" && id.startsWith(`${RANGE_ACTION}|`)); +} export const PAGE_REFRESH_ACTION = "analytics:page:refresh"; /** Opens the setup check (`./setup.ts`), carrying the range to return to. */ export const SETUP_ACTION = "analytics:setup"; @@ -383,7 +397,7 @@ function controls(input: PageInput, lang: Lang) { // closed, and its label sets it out of line with the buttons beside it. const elements = [ ...RANGES.map((days) => - button(RANGE_ACTION, t(lang, "rangeDays", { count: days }), { + button(rangeAction(days), t(lang, "rangeDays", { count: days }), { style: days === input.range ? "primary" : "secondary", value: days, }), diff --git a/tests/page-sync.test.ts b/tests/page-sync.test.ts index 3c1d6d1..65479ef 100644 --- a/tests/page-sync.test.ts +++ b/tests/page-sync.test.ts @@ -2,6 +2,8 @@ import { validateBlockResponse } from "@emdash-cms/blocks/server"; import { createPluginRuntimeTestHost, type PluginRuntimeTestHost } from "@emdash-cms/plugin-test"; import { afterEach, describe, expect, it } from "vitest"; +import { rangeAction } from "../src/ui/page.js"; + /** * The analytics page through the real sandbox, fed by the demo provider. * Plugin storage outlives `dispose()` within a file; these tests only read @@ -48,7 +50,7 @@ describe("the analytics page", () => { expect(validateBlockResponse(first, { pluginPagePaths: pages }).valid).toBe(true); expect(chartLengths(first)).toEqual([30, 30]); - const wider = await host.admin.act("/analytics", "analytics:range", { value: 90 }); + const wider = await host.admin.act("/analytics", rangeAction(90), { value: 90 }); expect(chartLengths(wider)).toEqual([90, 90]); }); diff --git a/tests/page.test.ts b/tests/page.test.ts index 2e3611e..47935f2 100644 --- a/tests/page.test.ts +++ b/tests/page.test.ts @@ -7,9 +7,9 @@ import { CONTENT_PATH } from "../src/ui/content.js"; import { formatDay } from "../src/ui/format.js"; import { PAGE_PATH, + isRangeAction, PAGE_REFRESH_ACTION, parseRange, - RANGE_ACTION, mergeDays, renderPage, summarizeDaily, @@ -164,7 +164,7 @@ describe("renderPage", () => { // buttons beside it. const blocks = renderPage(input({ range: 90 })); expect(find(blocks, "select")).toHaveLength(0); - const ranges = find(blocks, "button").filter((b) => b.action_id === RANGE_ACTION); + const ranges = find(blocks, "button").filter((b) => isRangeAction(b.action_id)); expect(ranges.map((b) => b.value)).toEqual([7, 30, 90]); expect(ranges.find((b) => b.value === 90)?.style).toBe("primary"); expect(ranges.filter((b) => b.style === "primary")).toHaveLength(1); @@ -172,6 +172,16 @@ describe("renderPage", () => { expect(refresh?.value).toBe(90); }); + it("gives the elements of an actions block distinct action ids", () => { + // The admin renders each element under its action id as the React key, + // so two buttons with one id are two children with the same key. + for (const block of find(renderPage(input()), "actions")) { + const ids = (block.elements as AnyBlock[]).map((el) => el.action_id).filter((id) => id !== undefined); + expect(ids.length).toBeGreaterThan(0); + expect(new Set(ids).size, ids.join(", ")).toBe(ids.length); + } + }); + it("links to the provider's own dashboard when there is one, and not otherwise", () => { const url = "https://dash.cloudflare.com/acct/web-analytics/overview?siteTag~in=tag"; const links = find(renderPage(input()), "link") as unknown as Array<{ target: { kind: string; url?: string } }>;