Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/range-button-ids.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ import {
DEFAULT_RANGE,
loadPage,
PAGE_PATH,
isRangeAction,
PAGE_REFRESH_ACTION,
parseRange,
RANGE_ACTION,
renderPage,
SETUP_ACTION,
type RangeDays,
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion src/ui/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
}),
Expand Down
4 changes: 3 additions & 1 deletion tests/page-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
});

Expand Down
14 changes: 12 additions & 2 deletions tests/page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -164,14 +164,24 @@ 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);
const refresh = find(blocks, "button").find((b) => b.action_id === PAGE_REFRESH_ACTION);
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 } }>;
Expand Down