Skip to content

Commit ec7f8be

Browse files
committed
Stats: tap a country tier to open Places filtered; restore the Cities tier
Two asks: bring back the "Cities" (all 15k+) coverage next to the other tiers, and make each tier open the matching Places list — scoped so the filter is the "good" one and survives moving between pages. - Country card now shows Cities · Big · Mega · Regions (Cities was missing). Near-zero shares read as "<1%" instead of a discouraging "0%". - Each expanded tier is a button: Cities / Big / Mega open Places on the cities browse scoped to that country + population tier; Sites opens the country's monuments; Regions (no Places list of their own) open the country's full page. - New shared `country` filter dimension: narrows every Places list to one country, shows as a removable "🇫🇷 France" chip, and — living in the shared store — persists as you navigate away and back. The map ignores it (you pan to a country there), like the other list-only dimensions. - The browse draws a country drill-down from that country's OWN city/site lists (not the global top-2000 pool), so nothing is missing. - World-level KPI tiles clear any country drill-down so they stay global.
1 parent 3d1a490 commit ec7f8be

14 files changed

Lines changed: 208 additions & 34 deletions

File tree

‎apps/postcards/src/features/filter/applyFilters.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Visit } from "../../lib/schema/models";
22
import type { ReferenceData } from "../../lib/reference/types";
33
import type { FilterState, FilterStatus } from "../../lib/store/useFilters";
44
import { mapDateMatches, rangeExactYear } from "../travel/period";
5+
import { countryFlag } from "../../lib/format/format";
56
import type { TFunction, MessageKey } from "../../lib/i18n";
67

78
// Pure predicates shared by every screen that slices places (spec 016 D3). No I/O,
@@ -50,6 +51,9 @@ export function placeMatches(v: Visit, ref: ReferenceData, s: FilterState): bool
5051
if (pop !== null && pop < s.minPop) return false;
5152
}
5253
if (s.continent && ref.continentOf(v.place.countryId) !== s.continent) return false;
54+
// One-country drill-down (set from the Stats country card). ISO codes compare
55+
// directly; a place with no country ("ZZ") never matches a real code.
56+
if (s.country && v.place.countryId !== s.country) return false;
5357
return true;
5458
}
5559

@@ -74,8 +78,9 @@ export interface ActiveFilterChip {
7478
}
7579

7680
/** Localised, ordered summary of every non-default dimension (one chip each). The
77-
* caller wires each chip's ✕ to clearField(field). Pure: no store dependency. */
78-
export function activeChips(s: FilterState, t: TFunction): ActiveFilterChip[] {
81+
* caller wires each chip's ✕ to clearField(field). Pure: no store dependency.
82+
* `ref` (optional) names the country chip — without it, the ISO code shows. */
83+
export function activeChips(s: FilterState, t: TFunction, ref?: ReferenceData): ActiveFilterChip[] {
7984
const chips: ActiveFilterChip[] = [];
8085
// Only a partial selection is a "filter" (empty or all three = show everything).
8186
if (s.status.length > 0 && s.status.length < 3) {
@@ -100,6 +105,10 @@ export function activeChips(s: FilterState, t: TFunction): ActiveFilterChip[] {
100105
if (s.hasPhoto) chips.push({ field: "hasPhoto", label: t("filter.hasPhoto") });
101106
if (s.hasNote) chips.push({ field: "hasNote", label: t("filter.hasNote") });
102107
if (s.continent) chips.push({ field: "continent", label: s.continent });
108+
if (s.country) {
109+
const name = ref?.countryByIso2(s.country)?.name ?? s.country;
110+
chips.push({ field: "country", label: `${countryFlag(s.country)} ${name}` });
111+
}
103112
if (s.sort !== "pop") chips.push({ field: "sort", label: t("filter.sort.az") });
104113
return chips;
105114
}

‎apps/postcards/src/features/map/MapScreen.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const MAP_HIDDEN_FIELDS: (keyof FilterState)[] = [
5555
"hasPhoto",
5656
"hasNote",
5757
"continent",
58+
// One-country drill-down is a lists action (set from the Stats card); on the map
59+
// you just pan to the country, so it neither filters the markers nor shows a chip.
60+
"country",
5861
// Place-kind mode is its own prominent pill now, not a filter — its state is
5962
// visible in the pill itself, so it must not double up as a summary chip or
6063
// light the Filter badge.

‎apps/postcards/src/features/stats/StatsView.tsx‎

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -142,37 +142,66 @@ function CountryRow({
142142
if (hit) useUi.getState().openCity(hit.id);
143143
};
144144

145+
// Tapping a metric drills into Places, scoped to THIS country + the tier — and the
146+
// filter is the shared store, so it survives leaving and returning to the list.
147+
const drill = (view: "cities" | "monuments", minPop: number) => () => {
148+
useFilters.getState().set({ country: c.iso2, minPop, listOnly: false });
149+
useUi.getState().openPlaces(view);
150+
};
151+
// Cities coverage is a sliver of a huge denominator, so it often rounds to 0 —
152+
// floor a real, non-zero share to "<1%" so it never reads as "nothing seen".
153+
const pctText = (p: number) => {
154+
const s = formatPercent(p);
155+
return p > 0 && s === formatPercent(0) ? "<1%" : s;
156+
};
157+
145158
// Slim summary meter: a tiny label + percentage + bar.
146159
const meter = (labelKey: MessageKey, ariaKey: MessageKey, pctVal: number, color?: string) => (
147160
<div className="cmeter">
148161
<span className="cmeter-cap">
149-
{t(labelKey)} <b>{formatPercent(pctVal)}</b>
162+
{t(labelKey)} <b>{pctText(pctVal)}</b>
150163
</span>
151164
<Bar value={pctVal} label={t(ariaKey, { name: c.name })} color={color} />
152165
</div>
153166
);
154-
// Full metric row (expanded): label, "x/y · pct" detail, bar.
167+
// Full metric row (expanded): label, "x/y · pct" detail, bar. With an onClick it
168+
// renders as a button that opens the matching, country-scoped Places list.
155169
const metric = (
156170
labelKey: MessageKey,
157171
detailKey: MessageKey,
158172
ariaKey: MessageKey,
159173
visited: number,
160174
total: number,
161175
pctVal: number,
162-
color?: string,
163-
) => (
164-
<div className="metric">
165-
<div className="metric-label">
166-
<span>{t(labelKey)}</span>
167-
<span className="muted">{t(detailKey, { pct: formatPercent(pctVal), visited, total })}</span>
168-
</div>
169-
<Bar value={pctVal} label={t(ariaKey, { name: c.name })} color={color} />
170-
</div>
171-
);
176+
color: string | undefined,
177+
onClick?: () => void,
178+
) => {
179+
const body = (
180+
<>
181+
<div className="metric-label">
182+
<span>{t(labelKey)}</span>
183+
<span className="muted">{t(detailKey, { pct: pctText(pctVal), visited, total })}</span>
184+
</div>
185+
<Bar value={pctVal} label={t(ariaKey, { name: c.name })} color={color} />
186+
</>
187+
);
188+
if (!onClick) return <div className="metric">{body}</div>;
189+
return (
190+
<button
191+
type="button"
192+
className="metric metric-btn"
193+
onClick={onClick}
194+
title={t("stats.country.exploreHint", { label: t(labelKey), name: c.name })}
195+
>
196+
{body}
197+
</button>
198+
);
199+
};
172200

173201
const hasMega = c.megaCitiesTotal > 0;
174202
const MEGA_COLOR = "var(--stat-fav)";
175203
const BIG_COLOR = "var(--stat-been)";
204+
const CITY_COLOR = "var(--stat-air)";
176205
const REGION_COLOR = "var(--accent)";
177206

178207
return (
@@ -188,13 +217,16 @@ function CountryRow({
188217
<span className="country-caret" aria-hidden>
189218
›
190219
</span>
191-
{/* Only the meaningful tiers — mega (1M+), big (100k+), regions. Hidden once
192-
open (the body shows the same, with counts) so nothing is duplicated. */}
193-
{(hasMega || c.bigCitiesTotal > 0 || c.regionsTotal > 0) && (
220+
{/* Coverage tiers at a glance — cities (all), big (100k+), mega (1M+) and
221+
regions. Hidden once open (the body shows the same, with counts and as
222+
tappable drill-downs) so nothing is duplicated. */}
223+
{(c.citiesTotal > 0 || c.regionsTotal > 0) && (
194224
<div className="country-meters">
195-
{hasMega && meter("stats.country.metricMega", "stats.country.megaCityBarAria", c.megaCityPct, MEGA_COLOR)}
225+
{c.citiesTotal > 0 &&
226+
meter("stats.country.metricCities", "stats.country.cityBarAria", c.cityPct, CITY_COLOR)}
196227
{c.bigCitiesTotal > 0 &&
197228
meter("stats.country.metricBigCities", "stats.country.bigCityBarAria", c.bigCityPct, BIG_COLOR)}
229+
{hasMega && meter("stats.country.metricMega", "stats.country.megaCityBarAria", c.megaCityPct, MEGA_COLOR)}
198230
{c.regionsTotal > 0 &&
199231
meter("stats.country.metricRegions", "stats.country.regionBarAria", c.regionPct, REGION_COLOR)}
200232
</div>
@@ -211,15 +243,19 @@ function CountryRow({
211243
{t("stats.country.openPage")} <span aria-hidden>↗</span>
212244
</button>
213245

214-
{hasMega &&
246+
{/* Each tier is tappable — it opens Places filtered to this country + tier,
247+
so you can browse (and add) exactly what it counts. Regions have no
248+
Places list of their own, so that one opens the country's full page. */}
249+
{c.citiesTotal > 0 &&
215250
metric(
216-
"stats.country.metricMega",
217-
"stats.country.metricMegaDetail",
218-
"stats.country.megaCityBarAria",
219-
c.megaCitiesVisited,
220-
c.megaCitiesTotal,
221-
c.megaCityPct,
222-
MEGA_COLOR,
251+
"stats.country.metricCities",
252+
"stats.country.metricCitiesDetail",
253+
"stats.country.cityBarAria",
254+
c.citiesVisited,
255+
c.citiesTotal,
256+
c.cityPct,
257+
CITY_COLOR,
258+
drill("cities", 0),
223259
)}
224260
{c.bigCitiesTotal > 0 &&
225261
metric(
@@ -230,6 +266,18 @@ function CountryRow({
230266
c.bigCitiesTotal,
231267
c.bigCityPct,
232268
BIG_COLOR,
269+
drill("cities", 100_000),
270+
)}
271+
{hasMega &&
272+
metric(
273+
"stats.country.metricMega",
274+
"stats.country.metricMegaDetail",
275+
"stats.country.megaCityBarAria",
276+
c.megaCitiesVisited,
277+
c.megaCitiesTotal,
278+
c.megaCityPct,
279+
MEGA_COLOR,
280+
drill("cities", 1_000_000),
233281
)}
234282
{c.regionsTotal > 0 &&
235283
metric(
@@ -240,6 +288,7 @@ function CountryRow({
240288
c.regionsTotal,
241289
c.regionPct,
242290
REGION_COLOR,
291+
() => useUi.getState().openCountry(c.iso2),
243292
)}
244293
{c.heritageTotal > 0 &&
245294
metric(
@@ -250,6 +299,7 @@ function CountryRow({
250299
c.heritageTotal,
251300
c.heritagePct,
252301
"var(--stat-want)",
302+
drill("monuments", 0),
253303
)}
254304

255305
{detail && (
@@ -316,7 +366,9 @@ export function StatsView() {
316366
// 100k = big cities, 1M = megacities) via the app's ONE shared filter, so the
317367
// tile drills into exactly the cities it counts.
318368
function openCitiesFiltered(minPop: number) {
319-
useFilters.getState().set({ minPop });
369+
// A world-level tile: clear any country drill-down a card left set, so this
370+
// shows the tier across every country, not just the last one you opened.
371+
useFilters.getState().set({ minPop, country: "" });
320372
useUi.getState().openPlaces("visited");
321373
}
322374
function flyToRegion(iso2: string) {

‎apps/postcards/src/features/visits/PlacesScreen.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ function mapRequest(view: PlacesView): { kind?: Kind; status?: Status; collectio
104104
return { kind: "all", status: "wishlist", collection: null };
105105
case "countries":
106106
return { kind: "countries", status: "all", collection: null };
107+
case "cities":
108+
return { kind: "cities", status: "all", collection: null };
107109
case "monuments":
108110
return { kind: "monuments", status: "all", collection: null };
109111
case "moments":
@@ -582,6 +584,7 @@ export function PlacesScreen() {
582584
filters.hasPhoto,
583585
filters.hasNote,
584586
filters.continent,
587+
filters.country,
585588
],
586589
);
587590
const filterVisits = useCallback(
@@ -652,7 +655,7 @@ export function PlacesScreen() {
652655
return { rows: [] as BrowseRow[], hasMore: false };
653656
return browseList(kind, status, currentFilters(filters), ref, visits, deferredFilter.trim(), shown);
654657
// eslint-disable-next-line react-hooks/exhaustive-deps
655-
}, [kind, status, filters.continent, filters.minPop, filters.category, ref, visits, deferredFilter, shown]);
658+
}, [kind, status, filters.continent, filters.minPop, filters.category, filters.country, ref, visits, deferredFilter, shown]);
656659
const browseRows = browse.rows;
657660

658661
// The years your visits span, newest first, for the date filter chips.
@@ -687,7 +690,7 @@ export function PlacesScreen() {
687690
// The active dimensions Places actually acts on (status + map mode are excluded —
688691
// status is the axis, mode is map-only). Drives the Filter button's badge.
689692
const placesFilterChips = useMemo(
690-
() => activeChips(currentFilters(filters), t).filter((c) => c.field !== "status" && c.field !== "mode"),
693+
() => activeChips(currentFilters(filters), t, ref).filter((c) => c.field !== "status" && c.field !== "mode"),
691694
// eslint-disable-next-line react-hooks/exhaustive-deps
692695
[
693696
filters.date,
@@ -698,6 +701,8 @@ export function PlacesScreen() {
698701
filters.hasPhoto,
699702
filters.hasNote,
700703
filters.continent,
704+
filters.country,
705+
ref,
701706
t,
702707
],
703708
);

‎apps/postcards/src/features/visits/browseList.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export function browseList(
9999
}
100100
};
101101
const continentOk = (iso2: string) => !filter.continent || ref.continentOf(iso2) === filter.continent;
102+
const countryOk = (iso2: string) => !filter.country || iso2 === filter.country;
102103
const countryName = (iso2: string) => ref.countryByIso2(iso2)?.name ?? iso2;
103104

104105
const rows: BrowseRow[] = [];
@@ -115,8 +116,15 @@ export function browseList(
115116
};
116117

117118
if (kind === "cities") {
118-
const base: City[] = q ? ref.searchCities(q, 500) : populousCities(ref);
119+
// Country drill-down browses ALL of that country's cities (already population-
120+
// desc), not just the global top-2000 pool, so nothing is missing from it.
121+
const base: City[] = q
122+
? ref.searchCities(q, 500)
123+
: filter.country
124+
? ref.citiesOf(filter.country)
125+
: populousCities(ref);
119126
for (const c of base) {
127+
if (!countryOk(c.countryIso2)) continue;
120128
if (!continentOk(c.countryIso2)) continue;
121129
if (filter.minPop > 0 && (c.population ?? 0) < filter.minPop) continue;
122130
const place: PlaceRef = { kind: "city", id: c.id, name: c.name, countryId: c.countryIso2 };
@@ -131,7 +139,11 @@ export function browseList(
131139
}
132140

133141
if (kind === "monuments") {
134-
let base: HeritageSite[] = q ? ref.searchHeritage(q, 500) : sortedMonuments(ref);
142+
let base: HeritageSite[] = q
143+
? ref.searchHeritage(q, 500)
144+
: filter.country
145+
? ref.heritageOf(filter.country)
146+
: sortedMonuments(ref);
135147
// Searchable BY COUNTRY (FR-007): a query that names a country surfaces that
136148
// country's sites, not only ones whose own name matches the query.
137149
if (q) {
@@ -143,6 +155,7 @@ export function browseList(
143155
}
144156
for (const h of base) {
145157
if (filter.category && h.category !== filter.category) continue;
158+
if (!countryOk(h.countryIso2)) continue;
146159
if (!continentOk(h.countryIso2)) continue;
147160
const place: PlaceRef = { kind: "heritage", id: h.id, name: h.name, countryId: h.countryIso2 };
148161
const o = overlay(place);
@@ -167,6 +180,7 @@ export function browseList(
167180
}
168181
}
169182
for (const a of base) {
183+
if (!countryOk(a.countryIso2)) continue;
170184
if (!continentOk(a.countryIso2)) continue;
171185
const name = `${a.name} (${a.id})`;
172186
const place: PlaceRef = { kind: "airport", id: a.id, name, countryId: a.countryIso2 };

‎apps/postcards/src/lib/i18n/en.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export const en = {
229229
"stats.country.metricMegaDetail": "{pct} · {visited}/{total} cities of 1M+",
230230
"stats.country.megaCityBarAria": "{name}: mega cities (1M+) visited",
231231
"stats.country.openPage": "Open full page",
232+
"stats.country.exploreHint": "Show {label} of {name} in Places",
232233
"stats.country.metricRegions": "Regions",
233234
"stats.country.metricRegionsDetail": "{pct} · {visited}/{total} regions",
234235
"stats.country.datasetNotLoaded": "dataset not loaded",

‎apps/postcards/src/lib/i18n/fr.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export const fr: Messages = {
222222
"stats.country.metricMegaDetail": "{pct} · {visited}/{total} villes de 1M+",
223223
"stats.country.megaCityBarAria": "{name} : mégapoles (1M+) visitées",
224224
"stats.country.openPage": "Ouvrir la page",
225+
"stats.country.exploreHint": "Voir {label} de {name} dans Lieux",
225226
"stats.country.metricRegions": "Régions",
226227
"stats.country.metricRegionsDetail": "{pct} · {visited}/{total} régions",
227228
"stats.country.datasetNotLoaded": "jeu de données non chargé",

‎apps/postcards/src/lib/i18n/ko.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export const ko: Messages = {
223223
"stats.country.metricMegaDetail": "{pct} · {visited}/{total} 도시 (100만+)",
224224
"stats.country.megaCityBarAria": "{name}: 방문한 메가시티(100만+)",
225225
"stats.country.openPage": "전체 페이지 열기",
226+
"stats.country.exploreHint": "{name}의 {label}을(를) 장소에서 보기",
226227
"stats.country.metricRegions": "지역",
227228
"stats.country.metricRegionsDetail": "{pct} · 지역 {visited}/{total}",
228229
"stats.country.datasetNotLoaded": "데이터셋 미로드",

‎apps/postcards/src/lib/store/useFilters.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export interface FilterState {
3838
/** Monument category filter (kind = monuments): "" (all) | cultural | natural | mixed.
3939
* Session-scoped, like date/folder; the map ignores it. */
4040
category: string;
41+
/** Narrow every list to ONE country (ISO 3166-1 alpha-2; "" = all). Set from the
42+
* Stats country card ("show France's mega cities…"); session-scoped and, like
43+
* category, a lists dimension the map ignores (you pan to a country there). */
44+
country: string;
4145
// Growth dimensions (US4) — off by default, hosted in the one panel.
4246
favoritesOnly: boolean;
4347
hasPhoto: boolean;
@@ -60,6 +64,7 @@ export const DEFAULT_FILTERS: FilterState = {
6064
sort: "pop",
6165
mode: "all",
6266
category: "",
67+
country: "",
6368
favoritesOnly: false,
6469
hasPhoto: false,
6570
hasNote: false,
@@ -126,6 +131,7 @@ export function isDefault(s: FilterState): boolean {
126131
s.sort === "pop" &&
127132
s.mode === "all" &&
128133
s.category === "" &&
134+
s.country === "" &&
129135
!s.favoritesOnly &&
130136
!s.hasPhoto &&
131137
!s.hasNote &&
@@ -174,6 +180,7 @@ export function currentFilters(s: FilterStore): FilterState {
174180
sort: s.sort,
175181
mode: s.mode,
176182
category: s.category,
183+
country: s.country,
177184
favoritesOnly: s.favoritesOnly,
178185
hasPhoto: s.hasPhoto,
179186
hasNote: s.hasNote,

‎apps/postcards/src/lib/store/useUi.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type PlacesView =
99
| "favorites"
1010
| "wishlist"
1111
| "countries"
12+
| "cities"
1213
| "monuments"
1314
| "moments"
1415
| "passport";

0 commit comments

Comments
 (0)