Skip to content
Open
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
10 changes: 5 additions & 5 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
| `containers/MetricOverviewContainer.jsx` | `${dashboardSource}/${activeRegion}/${slug}/index.json` | title, description, `type: 'line'|'table'`, axes, columns, data, annotations |
| `containers/SummaryMetricContainer.jsx` | same per-metric endpoint | buckets populations under 5% into "Other" |

`src/lib/withFetch.jsx` **is the entire data layer** and is used in exactly those three files: `withFetch(props => url)(Container)` passes a `dataFetch` prop (`pending`, `rejected`, `fulfilled`, `value`, `reason`), refetches when the URL changes (back to pending first), rejects on non-2xx, and drops a response that arrives after a newer request started. Responses are kept in `lib/fetchCache.js` until the page reloads: a URL fetched before is fulfilled from the first render, components asking for one URL at once share a request, and a failed request is forgotten so the next one tries again. Each one handles `dataFetch.pending` / `.rejected` / `.fulfilled` itself: `DashboardContainer` renders `views/Spinner.jsx` while pending, the two metric containers render a same-sized placeholder (`views/MetricOverviewPlaceholder.jsx`, `views/SummaryMetricPlaceholder.jsx`) so nothing moves when the data arrives, and all three render `views/Error.jsx` on rejection. All reshaping is client-side, in the `formatData` of `ChartContainer`, `DataTableContainer`, and `SummaryMetricContainer` (`ChartContainer` sorts populations by max y and drops nulls; `DataTableContainer` sorts rows by value descending).
`src/lib/withFetch.jsx` **is the entire data layer** and is used in exactly those three files: `withFetch(props => url)(Container)` passes a `dataFetch` prop (`pending`, `rejected`, `fulfilled`, `value`, `reason`), refetches when the URL changes (back to pending first), rejects on non-2xx, and drops a response that arrives after a newer request started. Responses are kept in `lib/fetchCache.js` until the page reloads: a URL fetched before is fulfilled from the first render, components asking for one URL at once share a request, and a failed request is forgotten so the next one tries again. On a dashboard URL, `src/index.jsx` calls `withFetch`'s `prefetchJSON` for that dashboard's `index.json` before rendering, so the request doesn't wait for the dashboard's code; `lib/utils.js`'s `dashboardIndexUrl` builds that URL for both. Each one handles `dataFetch.pending` / `.rejected` / `.fulfilled` itself: `DashboardContainer` renders `views/Spinner.jsx` while pending, the two metric containers render a same-sized placeholder (`views/MetricOverviewPlaceholder.jsx`, `views/SummaryMetricPlaceholder.jsx`) so nothing moves when the data arrives, and all three render `views/Error.jsx` on rejection. All reshaping is client-side, in the `formatData` of `ChartContainer`, `DataTableContainer`, and `SummaryMetricContainer` (`ChartContainer` sorts populations by max y and drops nulls; `DataTableContainer` sorts rows by value descending).

Metric `description` strings are rendered as inline Markdown through `markdown-it('zero')` with only `link` and `entity` enabled plus the `markdown-it-sup` plugin, via `dangerouslySetInnerHTML`. The dashboard description in `views/Dashboard.jsx` enables only `emphasis`. `src/tests/jest/MetricOverview.test.jsx` exists **specifically to guard that sanitization** — never widen the enabled-rules list without extending that test.

Expand Down Expand Up @@ -54,7 +54,7 @@ tests/playwright/
└── utils.js shared helpers: formatPercent, linkWorks, linksWork, flagForUpdate, metricTitleIsCorrect, changeRegionAndWaitForMetrics
```

There is no `pages/`, `store/`, `hooks/`, `api/`, `locales/`, or `data/`. `index.html` at the repository root is Vite's entry point (with a hardcoded `og:image` and the `<noscript>` message). `public/` holds static files copied as-is: `manifest.json`, `contribute.json`, a Search Console verification file, and `img/`. `build/` is gitignored output. `docs/architecture/` describes the frontend and the data pipeline. Routes are exactly `/`, `/contact`, `/dashboard/<key>`, and a catch-all `NotFound`.
There is no `pages/`, `store/`, `hooks/`, `api/`, `locales/`, or `data/`. `index.html` at the repository root is Vite's entry point (with a hardcoded `og:image` and the `<noscript>` message). `scripts/indexHtmlPlugin.mjs`, a Vite plugin, fills its empty `#root` with a copy of the header and an empty `main` (`.app-shell`), built from `src/config.json`, so something paints before the scripts run; React replaces it on its first render, which `src/index.jsx` makes synchronous so it can hand focus from a shell link to the same link in React's header. On the dev server it also links the compiled `Application.css` and `Header.css` from the head, since the dev server otherwise adds styles only with the scripts and the shell would flash unstyled (hot reload doesn't update those links, so a deleted rule lingers until a page reload). In a build it also adds `modulepreload` links for the route chunks and what they import, and `preload` links for their CSS, so they download alongside the main script; the chart chunk is deliberately left out (it would take bandwidth from the main script). Change `views/Header.jsx` and the copy together — `tests/playwright/specs/fixtures/shell.spec.js` fails when they differ. `public/` holds static files copied as-is: `manifest.json`, `contribute.json`, a Search Console verification file, and `img/`. `build/` is gitignored output. `docs/architecture/` describes the frontend and the data pipeline. Routes are exactly `/`, `/contact`, `/dashboard/<key>`, and a catch-all `NotFound`.

## Current state: read this before estimating anything

Expand Down Expand Up @@ -94,7 +94,7 @@ npm run size # source-map-explorer on build/assets/index

Run one Vitest file: `npx vitest run src/tests/jest/Dashboard.test.jsx`. Run one Playwright spec: `npx playwright test contact.spec.js`.

**Environment:** `.env` **is checked into git** and holds only public build-time config — `VITE_GA_MEASUREMENT_ID='G-XXXXXXXXXX'` (placeholder; the real GA4 measurement ID is a deploy decision outside this repo), `VITE_SITE_TITLE='Firefox Public Data Report'`, `VITE_VALUE_DECIMAL_PLACES=3`. Consumed through `import.meta.env` by `decorators/withTracker.jsx` and `lib/utils.js`. Override inline: `VITE_SITE_TITLE='…' npm start`.
**Environment:** `.env` **is checked into git** and holds only public build-time config — `VITE_GA_MEASUREMENT_ID='G-XXXXXXXXXX'` (placeholder; the real GA4 measurement ID is a deploy decision outside this repo. With the placeholder or no ID, `withTracker.jsx` loads no analytics at all; with a real one it initialises on import, and each page view carries its own `page_location`), `VITE_SITE_TITLE='Firefox Public Data Report'`, `VITE_VALUE_DECIMAL_PLACES=3`. Consumed through `import.meta.env` by `decorators/withTracker.jsx` and `lib/utils.js`. Override inline: `VITE_SITE_TITLE='…' npm start`.

## Coding Style & Naming Conventions

Expand All @@ -116,7 +116,7 @@ Errors: `eqeqeq`, `no-var`, `prefer-const`, `no-console`, `no-global-assign`, `n

## Styling: Stylus, outside Vite

`src/components/views/styl/*.styl` → `src/components/views/css/*.css` via the `stylus` CLI (`build:css` / `watch:css`). Vite never sees the Stylus. One `.styl` per component (PascalCase, mirroring the component), plus `Application.styl` (global reset, Mozilla Headline and Mozilla Text `@font-face`, body type, ~146 lines) and shared partials `Metric.styl` and `LabelledSelector.styl`.
`src/components/views/styl/*.styl` → `src/components/views/css/*.css` via the `stylus` CLI (`build:css` / `watch:css`). Vite never sees the Stylus. One `.styl` per component (PascalCase, mirroring the component), plus `Application.styl` (global reset, Mozilla Headline and Mozilla Text `@font-face` plus their size-adjusted Arial fallbacks — re-measure the `size-adjust` values if the fonts change, `fixtures/font-fallback.spec.js` fails when they're off — body type, ~230 lines) and shared partials `Metric.styl` and `LabelledSelector.styl`.

Shared variables live in `src/components/views/styl/includes/lib.styl` and it is nearly empty — `$base-tablet`, `$base-desktop`, `$link-color-normal = #0070ff`, and a `// TODO: padding/margin spacing.`. Import it per-file with `@import 'includes/lib'`; it is not globally injected. **No mixins file; most colors are hardcoded hex inline** (stylint's `colors` check is off).

Expand Down Expand Up @@ -152,7 +152,7 @@ This applies to everything written for a person to read: ExecPlans, PR descripti
- **Waiting for lazy routes:** wait with React Testing Library's `waitFor` / `findBy*`, never `vi.waitFor`. RTL turns off React's act() check while it waits, so a `React.lazy` component can resolve. Under `vi.waitFor`, or after a test that returns while a lazy route is still loading, React 18 logs "A suspended resource finished loading inside a test, but the event was not wrapped in act(...)", and the console.error trap turns that into an unhandled error that fails the run.
- **Testing data shaping:** containers that hand formatted props to a view that needs real layout (`views/Chart` → Observable Plot, `views/SummaryMetric` → d3) are tested by replacing the view with `vi.mock('../../components/views/Chart', () => ({ default: vi.fn(() => null) }))` and asserting on the props it was last called with. Views that are plain markup (`views/DataTable`) render for real.
- **Testing chart views:** `Chart.test.jsx` renders the real `views/Chart`, and Observable Plot draws real SVG in jsdom. It asserts on the drawn chart: `svg.scale('y').domain` for bounds, tick and label text, line `stroke`s against legend colors, and the `chart-*` classes on each mark's `<g>`. jsdom can't hover, so it sets `svg.value` and dispatches an `input` event, as Plot does, to check the readout. `SummaryMetric.test.jsx` renders the real d3 summary bar inside `<div id="application"><main>`, with `vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(490)` — jsdom reports 0px, and at 490px the bar draws at its 450px maximum, so segment widths are predictable.
- **Routing:** `Routes.test.jsx` renders `views/Application` inside `MemoryRouter` through one `renderAt(path)` function — the only place a router upgrade has to touch — wraps it in react-helmet-async's `HelmetProvider` (any test that renders a page with a `<Helmet>` needs one — `render(ui, { wrapper: HelmetProvider })`), and mocks `react-ga4` so no analytics load. Its cases are generated from `config.json`, so adding a dashboard adds a route test.
- **Routing:** `Routes.test.jsx` renders `views/Application` inside `MemoryRouter` through one `renderAt(path)` function — the only place a router upgrade has to touch — wraps it in react-helmet-async's `HelmetProvider` (any test that renders a page with a `<Helmet>` needs one — `render(ui, { wrapper: HelmetProvider })`), and mocks `react-ga4` so no analytics load, with `VITE_GA_MEASUREMENT_ID` stubbed to a real-looking ID in `vi.hoisted` so that page views are sent at all. `withTracker.test.jsx` covers the placeholder and each view's `page_location`. Its cases are generated from `config.json`, so adding a dashboard adds a route test.

**Playwright** specs in `tests/playwright/specs/` are **`camelCase.spec.js` or `kebab-case.spec.js`, CommonJS**. Every spec except `jsDisabled.spec.js` imports `test`, `expect` (and, where needed, `readFixture`) from **`tests/playwright/fixtureData.js`**, not `@playwright/test`. `tests/playwright/utils.js` exports `formatPercent`, `linkWorks`, `linksWork`, `metricTitleIsCorrect`, `changeRegionAndWaitForMetrics`, and `flagForUpdate` — which **deliberately fails when an element count changes**, to force a human to look.

Expand Down
6 changes: 4 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Run one Vitest file directly: `npx vitest run src/tests/jest/Dashboard.test.jsx`
working). Run one Playwright spec directly: `npx playwright test contact.spec.js`.

**Environment:** `.env` is checked into git and holds only public build-time config —
`VITE_GA_MEASUREMENT_ID='G-XXXXXXXXXX'` (a placeholder for a GA4 measurement ID),
`VITE_GA_MEASUREMENT_ID='G-XXXXXXXXXX'` (a placeholder for a GA4 measurement ID; with it, no
analytics load at all),
`VITE_SITE_TITLE='Firefox Public Data Report'`, `VITE_VALUE_DECIMAL_PLACES=3`. These are consumed by
`src/components/decorators/withTracker.jsx` and `src/lib/utils.js` via `import.meta.env.VITE_*` —
Vite's own convention, not Node's `process.env` (which is not populated in the browser bundle at
Expand Down Expand Up @@ -188,7 +189,8 @@ dispatch an `input` event, as Plot does. `SummaryMetric.test.jsx` renders the re
reports 0px, and at 490px the bar draws at its 450px maximum, so segment widths are predictable.
`Routes.test.jsx` renders `views/Application` inside `MemoryRouter` through a single `renderAt(path)`
function, so a router upgrade changes one place, wraps it in react-helmet-async's `HelmetProvider`,
and mocks `react-ga4`. Any test that renders a page with a `<Helmet>` needs that provider:
and mocks `react-ga4`, stubbing `VITE_GA_MEASUREMENT_ID` to a real-looking ID first, because the
placeholder sends nothing. Any test that renders a page with a `<Helmet>` needs that provider:
`render(ui, { wrapper: HelmetProvider })`. Its route cases come from
`config.json`. ESLint
allows the name `screen` in test files despite `no-shadow`'s `builtinGlobals`, because importing
Expand Down
16 changes: 15 additions & 1 deletion docs/architecture/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ It refetches when the URL changes, going back to pending first, treats a non-2xx
rejection, and ignores a response that arrives after a newer request has started. Responses are
kept in `src/lib/fetchCache.js` until the page reloads, so a page visited before shows its data on
the first render, and components asking for the same URL at once share one request. A failed
request isn't kept, so the next component to ask tries again. Each container
request isn't kept, so the next component to ask tries again. `src/index.jsx` uses the same cache
before React renders: on a dashboard's URL it calls `withFetch`'s `prefetchJSON` for that
dashboard's `index.json`. That way the request goes out while the much larger dashboard code is
still downloading, and `DashboardContainer` joins it when it mounts. Each container
handles those states itself: `DashboardContainer` renders `views/Spinner.jsx` while pending, the
two metric containers render a placeholder the size of what will replace it
(`views/MetricOverviewPlaceholder.jsx`, `views/SummaryMetricPlaceholder.jsx`), and all three render
Expand Down Expand Up @@ -145,6 +148,17 @@ these architecture docs.
There is no `pages/`, `store/`, `hooks/`, `api/`, or `locales/` directory — this is a small enough
application that none has been needed. `public/` holds static files served as-is (`manifest.json`,
`contribute.json`, `img/`, and the Google Search Console verification file); `index.html` itself lives at the repository root, Vite's convention.
`scripts/indexHtmlPlugin.mjs` puts a copy of the header and an empty `main` into its `#root` at
build time and in the dev server, so the header paints as soon as the stylesheet arrives. React
replaces it on its first render. `src/index.jsx` renders that first time synchronously and, if a
visitor had already tabbed to a shell link, moves focus to the same link in React's header. The dev server only adds styles once the scripts run, so there the
plugin also links the compiled `Application.css` and `Header.css` from the head, and the shell
doesn't paint unstyled. In a build, the same plugin lists the route chunks, what they
import, and their stylesheets as preloads, so they download alongside the main script rather than
after it. `lazyLoad` would fetch them anyway, so this adds no bytes. The chart chunk is left out,
because on a slow connection it takes bandwidth from the main script before anything needs it.
`tests/playwright/specs/fixtures/shell.spec.js` checks that the copy
matches `views/Header.jsx` and sits in the same place.
`build/` is gitignored output. Routes are exactly `/`, `/contact`, `/dashboard/<key>`, and a
catch-all not-found page.

Expand Down
Loading
Loading