ReviewOS

also looking at this

stacks/analyticshq

fix(dashboard): the regions panel says California, not CA

#63
Merged glennmichael123 wants to merge fix/region-panel-shows-names into main
4 files +145 -9
9 days ago

From Chris's screenshot: Top regions showed CA and 07.

07 says nothing to anyone. CA reads as California only to someone already expecting it — sitting beside a flag it is equally the country code for Canada.

The reason it was a code no longer holds

The panel's own comment explained the trade-off:

Spelling it out would mean shipping a table of every ISO 3166-2 subdivision name — some four thousand of them — to render a panel that is off by default.

That table is already shipped. app/Analytics/subdivisions.json — 194 countries, 3,234 subdivisions — and geo.ts has needed it since region geo started working at all: DB-IP's City Lite carries subdivision names and no codes, so the name is looked up on the way in. Read the other way round it answers this question for nothing.

US-CA  →  California
PH-07  →  Central Visayas

The tie-break, because reversing the map needs one

DB-IP carries aliases, so 384 codes have more than one spelling. The obvious rule picks badly — shortest-wins turns Baden-Württemberg into the unaccented Baden-Wurttemberg.

So: most non-ASCII characters wins, shortest breaks the tie. Prefer the properly accented name, then the plainest form of it.

codecandidatespicked
DE-BWBaden-Wurttemberg, Baden-WürttembergBaden-Württemberg
DE-BEBerlin, State of BerlinBerlin
AL-03Elbasan, Elbasan CountyElbasan
PH-05Bicol, Bicol RegionBicol

What is deliberately unchanged

  • The stored value. US-CA is still what the column holds, what addFilterUrl puts in the query string, and what the filter matches. Only the label changes.
  • The API. /api/sites/{id}/regions still returns codes — that is a contract someone may be reading.
  • Cities. cityLabel appends the subdivision only for US, CA and AU, because "San Diego, CA" is how that address is written and "Consolacion, 07" is not; the flag already carries the country. The bare Consolacion in the screenshot is correct, not a bug.
  • Unknown codes. A subdivision the table cannot name keeps showing its code. PH-XX on screen is honest about not knowing; an invented name would not be.

Verification

subdivision-name.test.ts covers the lookup, the tie-break in both directions, and the null cases — and one test guards the data, since subdivisions.json is regenerated from DB-IP and a rebuild that dropped countries would otherwise pass every other assertion.

region-geo.test.ts asserted the old label expression; updated, plus a new assertion that the panel falls back to the code.

1172 unit tests, 0 failures. Lint and typecheck clean. (One pre-existing pg_rewind ENOENT error is unrelated — confirmed present on a clean main.)

Not rendered in a browser: this needs a site with recorded region traffic, so the coverage here is the unit tests on the lookup plus a source assertion on the panel expression.

🤖 Generated with Claude Code

4 changed files on the files tab, with 0 review threads.

Sign in to comment on this pull request.