diff --git a/src/components/map/category-chips.test.tsx b/src/components/map/category-chips.test.tsx new file mode 100644 index 0000000..e4d225a --- /dev/null +++ b/src/components/map/category-chips.test.tsx @@ -0,0 +1,153 @@ +import { cleanup, fireEvent, render, screen } from "@testing-library/react"; +import { afterEach, describe, expect, it, vi } from "vitest"; + +import { CategoryChips, EXAM_COMBO, matchesCombo } from "@/components/map/category-chips"; +import { PLACE_TYPES, PLACE_TYPE_LABELS } from "@/lib/types"; +import type { PlaceType } from "@/lib/types"; + +afterEach(cleanup); + +const counts = Object.fromEntries(PLACE_TYPES.map((type, i) => [type, i + 1])) as Record< + PlaceType, + number +>; + +describe("CategoryChips (#352)", () => { + it("renders one chip per type, labelled exactly as PLACE_TYPE_LABELS", () => { + render(); + + const group = screen.getByRole("group", { name: "Filter by place type" }); + for (const type of PLACE_TYPES) { + expect(group.textContent).toContain(PLACE_TYPE_LABELS[type]); + } + }); + + it("is the only filter row: no second preset group", () => { + render( + , + ); + + expect(screen.queryByRole("group", { name: "Filter presets" })).toBeNull(); + expect(screen.getAllByRole("group")).toHaveLength(1); + }); + + it("toggles a single type", () => { + const onToggle = vi.fn(); + render(); + + fireEvent.click( + screen.getByRole("button", { name: new RegExp(PLACE_TYPE_LABELS.airport) }), + ); + + expect(onToggle).toHaveBeenCalledWith("airport"); + }); + + it("offers the exam-centres combo only when onSetTypes is given", () => { + const { rerender } = render( + , + ); + expect(screen.queryByRole("button", { name: /All exam centres/ })).toBeNull(); + + rerender( + , + ); + expect(screen.getByRole("button", { name: /All exam centres/ })).toBeTruthy(); + }); + + it("selects both exam types from the combo chip, and clears them on a second tap", () => { + const onSetTypes = vi.fn(); + const { rerender } = render( + , + ); + fireEvent.click(screen.getByRole("button", { name: /All exam centres/ })); + expect(onSetTypes).toHaveBeenLastCalledWith([...EXAM_COMBO]); + + rerender( + , + ); + const chip = screen.getByRole("button", { name: /All exam centres/ }); + expect(chip.getAttribute("aria-pressed")).toBe("true"); + fireEvent.click(chip); + expect(onSetTypes).toHaveBeenLastCalledWith([]); + }); + + it("shows a selected count and a Clear action only while something is selected", () => { + const onClear = vi.fn(); + const { rerender } = render( + , + ); + expect(screen.queryByRole("button", { name: "Clear" })).toBeNull(); + + rerender( + , + ); + expect(screen.getByText(/2 categories selected/)).toBeTruthy(); + fireEvent.click(screen.getByRole("button", { name: "Clear" })); + expect(onClear).toHaveBeenCalledOnce(); + }); + + it("uses the singular for one selected category", () => { + render( + , + ); + expect(screen.getByText(/1 category selected/)).toBeTruthy(); + }); + + it("fades the right edge of the scrolling row instead of clipping a chip", () => { + render(); + + const group = screen.getByRole("group", { name: "Filter by place type" }); + expect(group.className).toContain("overflow-x-auto"); + expect(group.className).toContain("mask-image"); + }); +}); + +describe("matchesCombo", () => { + it("matches the same types in any order", () => { + expect(matchesCombo(["foreign_lang_exam_centre", "sat_centre"], EXAM_COMBO)).toBe( + true, + ); + }); + + it("does not match a subset, a superset or an empty selection", () => { + expect(matchesCombo(["sat_centre"], EXAM_COMBO)).toBe(false); + expect(matchesCombo([...EXAM_COMBO, "library"], EXAM_COMBO)).toBe(false); + expect(matchesCombo([], EXAM_COMBO)).toBe(false); + }); +}); diff --git a/src/components/map/category-chips.tsx b/src/components/map/category-chips.tsx index cce94e3..44ecb09 100644 --- a/src/components/map/category-chips.tsx +++ b/src/components/map/category-chips.tsx @@ -5,10 +5,35 @@ import type { PlaceType } from "@/lib/types"; import { PLACE_TYPE_COLORS } from "@/lib/map"; import { cn } from "@/lib/utils"; +/** + * A multi-type shortcut that single-type chips cannot express: SAT and + * foreign-language exam centres are both "somewhere a student sits a timed + * exam". It is the only combo, so it sits in front of the chips instead of in a + * second filter bar (#352). + */ +export const EXAM_COMBO: readonly PlaceType[] = [ + "sat_centre", + "foreign_lang_exam_centre", +]; + +/** True when `selected` is exactly `combo`, in any order. */ +export function matchesCombo( + selected: readonly PlaceType[], + combo: readonly PlaceType[], +) { + return ( + selected.length === combo.length && combo.every((type) => selected.includes(type)) + ); +} + interface CategoryChipsProps { selected: PlaceType[]; counts: Record; onToggle: (type: PlaceType) => void; + /** Replaces the whole type selection; enables the leading exam-centres chip. */ + onSetTypes?: (types: PlaceType[]) => void; + /** Clears the type selection; enables the "N selected, Clear" line. */ + onClear?: () => void; /** When true, lay out as a single horizontally scrolling row (mobile). */ scroll?: boolean; } @@ -16,61 +41,116 @@ interface CategoryChipsProps { /** * Category filters as colour-coded toggle chips. Each chip doubles as the map * legend (colour dot matches the pin) and shows how many of that type are - * currently visible. Selecting none means everything shows. + * currently visible. Selecting none means everything shows. The label of every + * chip is the one used everywhere else for that type (`PLACE_TYPE_LABELS`). */ export function CategoryChips({ selected, counts, onToggle, + onSetTypes, + onClear, scroll = false, }: CategoryChipsProps) { + const examCount = EXAM_COMBO.reduce((sum, type) => sum + counts[type], 0); + const examActive = matchesCombo(selected, EXAM_COMBO); + return ( -
- {PLACE_TYPES.map((type) => { - const active = selected.includes(type); - const color = PLACE_TYPE_COLORS[type]; - return ( +
+ {onClear && selected.length > 0 && ( +
+ + {selected.length} {selected.length === 1 ? "category" : "categories"} selected + - ); - })} +
+ )} +
+ {onSetTypes && ( + PLACE_TYPE_LABELS[type]).join(" + ")} + count={examCount} + onClick={() => onSetTypes(examActive ? [] : [...EXAM_COMBO])} + /> + )} + {PLACE_TYPES.map((type) => ( + onToggle(type)} + /> + ))} +
); } + +function Chip({ + active, + color, + label, + title, + count, + onClick, +}: { + active: boolean; + color: string; + label: string; + title?: string; + count: number; + onClick: () => void; +}) { + return ( + + ); +} diff --git a/src/components/map/filter-presets.test.tsx b/src/components/map/filter-presets.test.tsx deleted file mode 100644 index 1d52bd1..0000000 --- a/src/components/map/filter-presets.test.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import { cleanup, fireEvent, render, screen } from "@testing-library/react"; -import { afterEach, describe, expect, it, vi } from "vitest"; - -import { FilterPresets } from "@/components/map/filter-presets"; - -afterEach(cleanup); - -describe("FilterPresets", () => { - it("lists the preset buttons in a labelled group", () => { - render(); - - const group = screen.getByRole("group", { name: "Filter presets" }); - expect(group).toBeTruthy(); - for (const label of [ - "Libraries", - "SAT centres", - "Exam centres", - "Govt offices", - "Airports", - ]) { - expect(screen.getByRole("button", { name: label })).toBeTruthy(); - } - }); - - it("applies one type for a single-type preset", () => { - const onApply = vi.fn(); - render(); - - fireEvent.click(screen.getByRole("button", { name: "Airports" })); - - expect(onApply).toHaveBeenCalledWith(["airport"]); - }); - - it("applies both exam types for the Exam centres preset", () => { - const onApply = vi.fn(); - render(); - - fireEvent.click(screen.getByRole("button", { name: "Exam centres" })); - - expect(onApply).toHaveBeenCalledWith(["sat_centre", "foreign_lang_exam_centre"]); - }); -}); diff --git a/src/components/map/filter-presets.tsx b/src/components/map/filter-presets.tsx deleted file mode 100644 index 22d6c92..0000000 --- a/src/components/map/filter-presets.tsx +++ /dev/null @@ -1,68 +0,0 @@ -"use client"; - -import { ClipboardCheck, GraduationCap, Landmark, Library, Plane } from "lucide-react"; -import type { LucideIcon } from "lucide-react"; - -import type { PlaceType } from "@/lib/types"; -import { Button } from "@/components/ui/button"; -import { cn } from "@/lib/utils"; - -interface FilterPreset { - label: string; - types: PlaceType[]; - icon: LucideIcon; -} - -/** - * Common filter combos, built from the real `PlaceType`s only (no invented - * categories). "Exam centres" is the one combo that spans two types - - * SAT and foreign-language exam centres are both "somewhere a student sits - * a timed exam", which is a distinct use case from a single-type filter. - */ -const FILTER_PRESETS: FilterPreset[] = [ - { label: "Libraries", types: ["library"], icon: Library }, - { label: "SAT centres", types: ["sat_centre"], icon: GraduationCap }, - { - label: "Exam centres", - types: ["sat_centre", "foreign_lang_exam_centre"], - icon: ClipboardCheck, - }, - { label: "Govt offices", types: ["gov_offices"], icon: Landmark }, - { label: "Airports", types: ["airport"], icon: Plane }, -]; - -interface FilterPresetsProps { - onApply: (types: PlaceType[]) => void; - className?: string; -} - -/** - * One-tap buttons for common filter combos, so a student doesn't have to - * rebuild "SAT + foreign-lang exam centres" from the category chips every - * time. Each preset replaces the active type filter with its own list. - */ -export function FilterPresets({ onApply, className }: FilterPresetsProps) { - return ( -
- {FILTER_PRESETS.map((preset) => { - const Icon = preset.icon; - return ( - - ); - })} -
- ); -} diff --git a/src/components/map/map-panel.tsx b/src/components/map/map-panel.tsx index 1b535b7..0ab89d4 100644 --- a/src/components/map/map-panel.tsx +++ b/src/components/map/map-panel.tsx @@ -8,7 +8,6 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Separator } from "@/components/ui/separator"; import { CategoryChips } from "@/components/map/category-chips"; -import { FilterPresets } from "@/components/map/filter-presets"; import { CityFilter } from "@/components/map/city-filter"; import { CountryFilter } from "@/components/map/country-filter"; import { NearMeButton } from "@/components/map/near-me-button"; @@ -139,11 +138,11 @@ export function MapPanel({ selected={filters.types} counts={typeCounts} onToggle={toggleType} + onSetTypes={(types) => onFiltersChange({ ...filters, types })} + onClear={() => onFiltersChange({ ...filters, types: [] })} scroll={scrollChips} /> - onFiltersChange({ ...filters, types })} /> -
{ }); }); -describe("PlacesMap filter presets", () => { - it("narrows the results to the preset's type and sets it in the URL", () => { +describe("PlacesMap category filter", () => { + function renderMap() { render( { defaultZoom={11} />, ); + } + + it("narrows the results to the chosen type and sets it in the URL", () => { + renderMap(); expect(screen.getByText("City Library")).toBeTruthy(); - fireEvent.click(screen.getByRole("button", { name: "Airports" })); + fireEvent.click(screen.getByRole("button", { name: /^Airport/ })); expect(screen.getByText("City Airport")).toBeTruthy(); expect(screen.queryByText("City Library")).toBeNull(); @@ -320,6 +324,26 @@ describe("PlacesMap filter presets", () => { advance(500); expect(params().get("types")).toBe("airport"); }); + + it("has one filter row, not a second row of presets (#352)", () => { + renderMap(); + + expect(screen.queryByRole("group", { name: "Filter presets" })).toBeNull(); + expect(screen.getAllByRole("group", { name: "Filter by place type" })).toHaveLength( + 1, + ); + }); + + it("shows how many categories are selected and clears them", () => { + renderMap(); + fireEvent.click(screen.getByRole("button", { name: /^Airport/ })); + expect(screen.getByText(/1 category selected/)).toBeTruthy(); + + fireEvent.click(screen.getByRole("button", { name: "Clear" })); + + expect(screen.queryByText(/category selected/)).toBeNull(); + expect(screen.getByText("City Library")).toBeTruthy(); + }); }); describe("PlacesMap country filter", () => { diff --git a/src/components/map/results-list.test.tsx b/src/components/map/results-list.test.tsx index ecbbb83..9801b5f 100644 --- a/src/components/map/results-list.test.tsx +++ b/src/components/map/results-list.test.tsx @@ -289,3 +289,113 @@ describe("ResultsList details and compare", () => { expect(onToggle).toHaveBeenCalledWith(expect.objectContaining({ id: "p1" })); }); }); + +describe("ResultsList row location (#354)", () => { + const base = { + type: "airport" as const, + lat: 19, + lng: 72, + gmaps_link: "https://maps.google.com/?q=19,72", + added_by: "test", + }; + const emptyState = { activeFilters: [], onReset: vi.fn(), onSuggest: vi.fn() }; + + it("tells two places with the same name apart by city and country", () => { + render( + , + ); + + expect(screen.getByText(/Airport · Mumbai, India/)).toBeTruthy(); + expect(screen.getByText(/Airport · Nagpur, India/)).toBeTruthy(); + }); + + it("puts the distance after the location when it is known", () => { + render( + , + ); + + expect(screen.getByText(/Airport · London, United Kingdom · /)).toBeTruthy(); + }); + + it("omits the country, with no stray comma, for a private place that has none", () => { + render( + , + ); + + expect(screen.getByText("Airport · Thane")).toBeTruthy(); + }); + + it("lets the name wrap to two lines instead of truncating to one", () => { + render( + , + ); + + const name = screen.getByText("A very long place name"); + expect(name.className).toContain("line-clamp-2"); + expect(name.className).not.toContain("truncate"); + }); + + it("keeps the accessible names of the row actions", () => { + render( + , + ); + + expect(screen.getByRole("button", { name: "Details for Heathrow" })).toBeTruthy(); + expect( + screen.getByRole("link", { name: "Open Heathrow in Google Maps" }), + ).toBeTruthy(); + expect(screen.getByRole("link", { name: "Directions to Heathrow" })).toBeTruthy(); + }); +}); diff --git a/src/components/map/results-list.tsx b/src/components/map/results-list.tsx index 97bc6bb..5165888 100644 --- a/src/components/map/results-list.tsx +++ b/src/components/map/results-list.tsx @@ -11,7 +11,8 @@ import { } from "lucide-react"; import type { Place } from "@/lib/types"; -import { PLACE_TYPE_LABELS } from "@/lib/types"; +import { PLACE_TYPE_LABELS, humanizeCity } from "@/lib/types"; +import { countryName } from "@/lib/places-utils"; import { directionsUrl } from "@/lib/map"; import { formatDistance } from "@/lib/geo"; import { Button } from "@/components/ui/button"; @@ -53,6 +54,16 @@ interface ResultsListProps { pageSize?: number; } +/** "Mumbai, India": the city and, when the place has one, its country, so same-named places are told apart (#354). */ +export function placeLocationLabel(place: Place): string { + const city = humanizeCity(place.city); + return place.country ? `${city}, ${countryName(place.country)}` : city; +} + +/** Touch-sized (44px) below the `lg` sidebar breakpoint, where the list sits in the mobile sheet. */ +const ROW_ACTION_CLASS = + "flex min-h-11 min-w-11 shrink-0 items-center justify-center rounded-md p-2 text-muted-foreground transition-colors hover:bg-background hover:text-foreground focus-visible:bg-background focus-visible:text-foreground lg:min-h-0 lg:min-w-0"; + /** Rows rendered before "Show more", so the unfiltered map does not build ~2,000 rows. */ export const RESULTS_PAGE_SIZE = 100; @@ -187,13 +198,13 @@ export function ResultsList({ title={`${place.name} (${PLACE_TYPE_LABELS[place.type]})`} > - + {place.name} - {PLACE_TYPE_LABELS[place.type]} + {PLACE_TYPE_LABELS[place.type]} · {placeLocationLabel(place)} {distanceKm !== undefined && ` · ${formatDistance(distanceKm)}`} @@ -202,7 +213,7 @@ export function ResultsList({ type="button" onClick={() => onDetails(place)} aria-label={"Details for " + place.name} - className="shrink-0 rounded-md p-2 text-muted-foreground transition-colors hover:bg-background hover:text-foreground focus-visible:bg-background focus-visible:text-foreground" + className={ROW_ACTION_CLASS} > @@ -214,7 +225,7 @@ export function ResultsList({ rel="noreferrer" onClick={(e) => e.stopPropagation()} aria-label={`Open ${place.name} in Google Maps`} - className="shrink-0 rounded-md p-2 text-muted-foreground transition-colors hover:bg-background hover:text-foreground focus-visible:bg-background focus-visible:text-foreground" + className={ROW_ACTION_CLASS} > @@ -225,7 +236,7 @@ export function ResultsList({ rel="noreferrer" onClick={(e) => e.stopPropagation()} aria-label={`Directions to ${place.name}`} - className="shrink-0 rounded-md p-2 text-muted-foreground transition-colors hover:bg-background hover:text-foreground focus-visible:bg-background focus-visible:text-foreground" + className={ROW_ACTION_CLASS} >