diff --git a/client/src/components/Atlas/tourActivity/tourHubFigures.ts b/client/src/components/Atlas/tourActivity/tourHubFigures.ts new file mode 100644 index 000000000..8fb3f7315 --- /dev/null +++ b/client/src/components/Atlas/tourActivity/tourHubFigures.ts @@ -0,0 +1,27 @@ +import type { TourActivityStatsResponse, TourActivitySummary } from '@trek/shared'; +import type { DistanceUnit } from '../../../types'; +import { distanceReading, heightReading } from './tourActivityReading'; +import { splitMeasure } from './tourActivityStatsModel'; + +/** + * What the tours page of the Atlas hub shows above its lists, for the desktop + * and the phone view alike: the headline distance (or the tour count when no + * distance is known), the climbs, the types scaled to the longest, and the + * tours left out of the sums. + */ +export function tourHubFigures( + summary: TourActivitySummary, + data: TourActivityStatsResponse | null, + unit: DistanceUnit +) { + const totals = summary.totals; + const distance = distanceReading(totals, unit); + return { + hero: distance ? splitMeasure(distance.text) : { value: String(totals.count), unit: '' }, + ascent: heightReading(totals.ascentM, unit), + descent: heightReading(totals.descentM, unit), + types: summary.types, + maxKm: Math.max(1e-9, ...summary.types.map((x) => x.distanceKm.value ?? 0)), + uncounted: data?.uncounted ?? { unassigned: 0, undated: 0 }, + }; +} diff --git a/client/src/mobile/screens/atlas/hub/MHubTours.tsx b/client/src/mobile/screens/atlas/hub/MHubTours.tsx index 3c02d9109..8ba2dbae3 100644 --- a/client/src/mobile/screens/atlas/hub/MHubTours.tsx +++ b/client/src/mobile/screens/atlas/hub/MHubTours.tsx @@ -7,14 +7,13 @@ import { } from '../../../../components/Atlas/tourActivity/tourActivityParts'; import { distanceReading, - heightReading, periodLabel, typeIcon, typeLabel, useDistanceUnit, } from '../../../../components/Atlas/tourActivity/tourActivityReading'; import { TourActivityStatsDialog } from '../../../../components/Atlas/tourActivity/TourActivityStatsDialog'; -import { splitMeasure } from '../../../../components/Atlas/tourActivity/tourActivityStatsModel'; +import { tourHubFigures } from '../../../../components/Atlas/tourActivity/tourHubFigures'; import { useTourActivityStats } from '../../../../components/Atlas/tourActivity/useTourActivityStats'; import type { AtlasGlobeState } from '../../../../pages/atlas/useAtlasGlobe'; import MChip from '../../../components/MChip'; @@ -74,13 +73,7 @@ export default function MHubTours({ s }: { s: AtlasGlobeState }): React.ReactEle ); } - const distance = distanceReading(totals, unit); - const hero = distance ? splitMeasure(distance.text) : { value: String(totals.count), unit: '' }; - const ascent = heightReading(totals.ascentM, unit); - const descent = heightReading(totals.descentM, unit); - const types = summary.types; - const maxKm = Math.max(1e-9, ...types.map((x) => x.distanceKm.value ?? 0)); - const uncounted = stats.data?.uncounted ?? { unassigned: 0, undated: 0 }; + const { hero, ascent, descent, types, maxKm, uncounted } = tourHubFigures(summary, stats.data, unit); return (
diff --git a/client/src/mobile/screens/atlas/hub/MHubWishlist.tsx b/client/src/mobile/screens/atlas/hub/MHubWishlist.tsx index fc00798a5..f88de48a3 100644 --- a/client/src/mobile/screens/atlas/hub/MHubWishlist.tsx +++ b/client/src/mobile/screens/atlas/hub/MHubWishlist.tsx @@ -1,4 +1,3 @@ -import { getIntlLanguage } from '@trek/shared'; import { CalendarCheck, CalendarClock, @@ -22,7 +21,7 @@ import CountryFlag from '../../../../components/shared/CountryFlag'; import { splitWishes, visitedOn, whenLabeller, wishCountry } from '../../../../pages/atlas/atlasHubModel'; import type { BucketItem } from '../../../../pages/atlas/atlasModel'; import type { AtlasGlobeState } from '../../../../pages/atlas/useAtlasGlobe'; -import { useGlobePlaceSearch } from '../../../../pages/atlas/useGlobePlaceSearch'; +import { useWishAddForm } from '../../../../pages/atlas/useWishAddForm'; import MIconBtn from '../../../components/MIconBtn'; import MProgress from '../../../components/MProgress'; import MSheet from '../../../components/MSheet'; @@ -189,21 +188,8 @@ function WishRow({ s, item, when }: { s: AtlasGlobeState; item: BucketItem; when /** Adding a wish on the phone: where, when and a note, as one sheet with the save at its foot. */ function AddWishSheet({ s }: { s: AtlasGlobeState }): React.ReactElement { - const b = s.bucketState; - const lang = getIntlLanguage(s.language); - const [query, setQuery] = React.useState(''); - const { placeHits, placesLoading } = useGlobePlaceSearch(query, s.placeLang); - const picked = !!b.bucketForm.name; - const thisYear = new Date().getFullYear(); - const year = b.bucketPoiYear; - const month = b.bucketPoiMonth; - const months = Array.from({ length: 12 }, (_, i) => new Date(2000, i).toLocaleString(lang, { month: 'short' })); - const takeName = () => { - const name = query.trim(); - if (!name) return; - b.setBucketForm({ ...b.bucketForm, name, lat: '', lng: '', address: undefined }); - setQuery(''); - }; + const { b, query, setQuery, placeHits, placesLoading, picked, thisYear, year, month, months, takeName } = + useWishAddForm(s); const label = (text: string) => (
{text}
); diff --git a/client/src/pages/atlas/AtlasGlobeTours.tsx b/client/src/pages/atlas/AtlasGlobeTours.tsx index 40c9fff0f..4b0ecde6e 100644 --- a/client/src/pages/atlas/AtlasGlobeTours.tsx +++ b/client/src/pages/atlas/AtlasGlobeTours.tsx @@ -8,13 +8,12 @@ import { } from '../../components/Atlas/tourActivity/tourActivityParts'; import { distanceReading, - heightReading, typeIcon, typeLabel, useDistanceUnit, } from '../../components/Atlas/tourActivity/tourActivityReading'; import { TourActivityStatsDialog } from '../../components/Atlas/tourActivity/TourActivityStatsDialog'; -import { splitMeasure } from '../../components/Atlas/tourActivity/tourActivityStatsModel'; +import { tourHubFigures } from '../../components/Atlas/tourActivity/tourHubFigures'; import { useTourActivityStats } from '../../components/Atlas/tourActivity/useTourActivityStats'; import { fs } from '../../components/shared/DialogShell'; import { InfoDot } from '../../components/Tours/tourParts'; @@ -60,13 +59,7 @@ export default function AtlasGlobeTours({ s }: { s: AtlasGlobeState }): React.Re ); } - const distance = distanceReading(totals, unit); - const hero = distance ? splitMeasure(distance.text) : { value: String(totals.count), unit: '' }; - const ascent = heightReading(totals.ascentM, unit); - const descent = heightReading(totals.descentM, unit); - const types = summary.types; - const maxKm = Math.max(1e-9, ...types.map((x) => x.distanceKm.value ?? 0)); - const uncounted = stats.data?.uncounted ?? { unassigned: 0, undated: 0 }; + const { hero, ascent, descent, types, maxKm, uncounted } = tourHubFigures(summary, stats.data, unit); return (
diff --git a/client/src/pages/atlas/AtlasGlobeWishAdd.tsx b/client/src/pages/atlas/AtlasGlobeWishAdd.tsx index fdd257deb..f3e5a2132 100644 --- a/client/src/pages/atlas/AtlasGlobeWishAdd.tsx +++ b/client/src/pages/atlas/AtlasGlobeWishAdd.tsx @@ -1,10 +1,9 @@ -import { getIntlLanguage } from '@trek/shared'; import { ChevronLeft, ChevronRight, Loader2, MapPin, PenLine, Search, Sparkles, Star, X } from 'lucide-react'; import React from 'react'; import { fs } from '../../components/shared/DialogShell'; import { HUB_CARD, HUB_FIELD, HUB_INSET, HubGhost, HubGlow, HubIconButton, HubPrimary } from './atlasHubKit'; import type { AtlasGlobeState } from './useAtlasGlobe'; -import { useGlobePlaceSearch } from './useGlobePlaceSearch'; +import { useWishAddForm } from './useWishAddForm'; function StepLabel({ n, children }: { n: number; children: React.ReactNode }): React.ReactElement { return ( @@ -26,22 +25,8 @@ function StepLabel({ n, children }: { n: number; children: React.ReactNode }): R * note survives picking a place; cancel clears everything. */ export default function AtlasGlobeWishAdd({ s }: { s: AtlasGlobeState }): React.ReactElement { - const b = s.bucketState; - const lang = getIntlLanguage(s.language); - const [query, setQuery] = React.useState(''); - const { placeHits, placesLoading } = useGlobePlaceSearch(query, s.placeLang); - const picked = !!b.bucketForm.name; - const thisYear = new Date().getFullYear(); - const year = b.bucketPoiYear; - const month = b.bucketPoiMonth; - const months = Array.from({ length: 12 }, (_, i) => new Date(2000, i).toLocaleString(lang, { month: 'short' })); - - const takeName = () => { - const name = query.trim(); - if (!name) return; - b.setBucketForm({ ...b.bucketForm, name, lat: '', lng: '', address: undefined }); - setQuery(''); - }; + const { b, query, setQuery, placeHits, placesLoading, picked, thisYear, year, month, months, takeName } = + useWishAddForm(s); return (
diff --git a/client/src/pages/atlas/useWishAddForm.ts b/client/src/pages/atlas/useWishAddForm.ts new file mode 100644 index 000000000..1b483f7f0 --- /dev/null +++ b/client/src/pages/atlas/useWishAddForm.ts @@ -0,0 +1,38 @@ +import { getIntlLanguage } from '@trek/shared'; +import { useState } from 'react'; +import type { AtlasGlobeState } from './useAtlasGlobe'; +import { useGlobePlaceSearch } from './useGlobePlaceSearch'; + +/** + * The add-a-wish form of the Atlas hub, for the desktop card and the phone + * sheet alike: the place search, the year and month picked, the month names in + * the reader's language, and taking what was typed as a plain name. + */ +export function useWishAddForm(s: AtlasGlobeState) { + const b = s.bucketState; + const lang = getIntlLanguage(s.language); + const [query, setQuery] = useState(''); + const { placeHits, placesLoading } = useGlobePlaceSearch(query, s.placeLang); + const months = Array.from({ length: 12 }, (_, i) => new Date(2000, i).toLocaleString(lang, { month: 'short' })); + + const takeName = () => { + const name = query.trim(); + if (!name) return; + b.setBucketForm({ ...b.bucketForm, name, lat: '', lng: '', address: undefined }); + setQuery(''); + }; + + return { + b, + query, + setQuery, + placeHits, + placesLoading, + picked: !!b.bucketForm.name, + thisYear: new Date().getFullYear(), + year: b.bucketPoiYear, + month: b.bucketPoiMonth, + months, + takeName, + }; +} diff --git a/server/src/nest/atlas/atlas-people.service.ts b/server/src/nest/atlas/atlas-people.service.ts index 55e831169..dcf17e1bb 100644 --- a/server/src/nest/atlas/atlas-people.service.ts +++ b/server/src/nest/atlas/atlas-people.service.ts @@ -99,6 +99,14 @@ export class AtlasPeopleService { }; } + /** + * One traveller's counts and badges, read through the Atlas reads the user's + * own Atlas uses. + * + * @txStandalone its only writes are those reads' region cache refreshes, each + * complete on its own and safe to repeat, so nothing a caller does around + * them needs to share their transaction. + */ private async profile(id: number): Promise<{ person: Omit; badges: Badge[] }> { const stats = await this.atlas.stats(id); const visited = (stats.countries as Array<{ code: string; status?: string; firstVisit?: string | null }>).filter(