From bde2ad2ca8a8f7e9bc36dad6a5d74e3e72c5f4f8 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 20 Oct 2026 13:43:12 +0200 Subject: [PATCH] feat(atlas): sum up the tours you have done, by period and type (STA-02) With the Tours addon on, the Atlas panel gets a third tab, Tours, beside Stats and Bucket List: what you have done on your tours across all your trips. A period (all time, this year, the last 12 months or an earlier year) and a tour type narrow it; the tab shows the distance large with the number of tours at its corner, the ascent and descent, and the types with their distance. "Show tours" opens every tour of the period in a dialog with the same filters, the sums, the split by type and one row per tour. What counts is said, never guessed. A tour counts once the day it is on is over, with the values of the planning, badged "Planned" and explained as not measured. A linked actual track (ACT-01) replaces those values for its execution, badged "Recorded" and named, and is never added on top nor counted as a tour of its own. A tour on several days counts once, a whole route does not count beside the stages planned from it, a paddle tour sums no heights, and a value nobody knows stays out of the sum, which then reads "at least". Only the user's own part counts: a day names them among its participants, or names nobody on a trip of their own; being able to open somebody else's trip makes none of its tours theirs. Tours still ahead, on no day or only on undated days are named as not counting. A tour that fell through can be left out of the sums ("Leave out"): it stays in the plan and in the list, dimmed, and "Count again" brings it back. Only the user's own numbers change. Server: GET /api/tours/activity-stats answers the user's activity entries across every trip they can open, and PUT /api/tours/activity-stats/exclusions leaves a tour out or counts it again (the tour must be in such a trip, else 404). A tour_activity_exclusions table keeps the exclusions per user and tour. The sums are added up by a shared summary in @trek/shared, so the Atlas and the assistant say the same numbers. MCP: get_tour_activity_stats and set_tour_activity_excluded over the same service. The tab reads live and needs a connection, like the rest of the Atlas statistics. --- client/src/api/tourActivityStats.ts | 10 + .../AtlasTourActivityPane.test.tsx | 149 ++++++++++ .../tourActivity/AtlasTourActivityPane.tsx | 234 +++++++++++++++ .../tourActivity/TourActivityEntryRow.tsx | 158 ++++++++++ .../TourActivityStatsDialog.test.tsx | 176 ++++++++++++ .../tourActivity/TourActivityStatsDialog.tsx | 192 +++++++++++++ .../Atlas/tourActivity/tourActivityParts.tsx | 269 ++++++++++++++++++ .../tourActivityStatsModel.test.ts | 91 ++++++ .../tourActivity/tourActivityStatsModel.ts | 90 ++++++ .../tourActivity/useTourActivityStats.test.ts | 155 ++++++++++ .../tourActivity/useTourActivityStats.ts | 139 +++++++++ client/src/pages/AtlasPage.test.tsx | 28 ++ client/src/pages/AtlasPage.tsx | 30 +- client/src/pages/atlas/useAtlas.ts | 2 +- client/src/repo/tourActivityStatsRepo.test.ts | 94 ++++++ client/src/repo/tourActivityStatsRepo.ts | 40 +++ server/scripts/repository-owners.json | 1 + .../entities/TourActivityExclusions.entity.ts | 29 ++ server/src/db/entities/Tours.entity.ts | 3 + server/src/db/entities/Users.entity.ts | 3 + server/src/db/entities/index.ts | 2 + .../db/kysely/TourActivityExclusions.table.ts | 10 + server/src/db/kysely/db.ts | 2 + ...tour_can_be_left_out_of_ones_activities.ts | 25 ++ .../repositories/TourActivities.repository.ts | 114 +++++++- .../TourActivityExclusions.repository.ts | 46 +++ .../nest/tours/dto/tour-activity-stats.dto.ts | 5 + .../tours/tour-activity-stats.controller.ts | 37 +++ .../nest/tours/tour-activity-stats.helpers.ts | 184 ++++++++++++ .../src/nest/tours/tour-activity-stats.mcp.ts | 89 ++++++ .../nest/tours/tour-activity-stats.service.ts | 52 ++++ server/src/nest/tours/tours.module.ts | 11 + .../e2e/tours-activity-stats.e2e.test.ts | 216 ++++++++++++++ server/tests/unit/db/entities-index.test.ts | 7 +- .../tests/unit/db/entity-class-fields.test.ts | 3 +- server/tests/unit/db/legacy-baseline.test.ts | 1 + .../TourActivities.stats.repository.test.ts | 130 +++++++++ .../TourActivityExclusions.repository.test.ts | 89 ++++++ .../tour-activity-stats.controller.test.ts | 64 +++++ .../nest/tour-activity-stats.helpers.test.ts | 223 +++++++++++++++ .../unit/nest/tour-activity-stats.mcp.test.ts | 121 ++++++++ .../nest/tour-activity-stats.service.test.ts | 152 ++++++++++ shared/src/i18n/ar/tours.ts | 32 +++ shared/src/i18n/az/tours.ts | 32 +++ shared/src/i18n/br/tours.ts | 32 +++ shared/src/i18n/ca/tours.ts | 32 +++ shared/src/i18n/cs/tours.ts | 32 +++ shared/src/i18n/de/tours.ts | 32 +++ shared/src/i18n/en/tours.ts | 32 +++ shared/src/i18n/es/tours.ts | 32 +++ shared/src/i18n/et/tours.ts | 32 +++ shared/src/i18n/fr/tours.ts | 32 +++ shared/src/i18n/gr/tours.ts | 32 +++ shared/src/i18n/hu/tours.ts | 32 +++ shared/src/i18n/id/tours.ts | 32 +++ shared/src/i18n/it/tours.ts | 32 +++ shared/src/i18n/ja/tours.ts | 32 +++ shared/src/i18n/ko/tours.ts | 32 +++ shared/src/i18n/nl/tours.ts | 33 +++ shared/src/i18n/pl/tours.ts | 32 +++ shared/src/i18n/ru/tours.ts | 32 +++ shared/src/i18n/sk/tours.ts | 32 +++ shared/src/i18n/sv/tours.ts | 32 +++ shared/src/i18n/th/tours.ts | 32 +++ shared/src/i18n/tours-locales.spec.ts | 2 +- shared/src/i18n/tr/tours.ts | 32 +++ shared/src/i18n/uk/tours.ts | 32 +++ shared/src/i18n/vi/tours.ts | 32 +++ shared/src/i18n/zh-TW/tours.ts | 31 ++ shared/src/i18n/zh/tours.ts | 31 ++ shared/src/index.ts | 2 + .../src/tours/tour-activity-stats.schema.ts | 89 ++++++ shared/src/tours/tour-activity-stats.spec.ts | 186 ++++++++++++ shared/src/tours/tour-activity-stats.ts | 99 +++++++ wiki/Atlas.md | 12 + wiki/MCP-Addon-Tools.md | 4 +- 76 files changed, 4717 insertions(+), 16 deletions(-) create mode 100644 client/src/api/tourActivityStats.ts create mode 100644 client/src/components/Atlas/tourActivity/AtlasTourActivityPane.test.tsx create mode 100644 client/src/components/Atlas/tourActivity/AtlasTourActivityPane.tsx create mode 100644 client/src/components/Atlas/tourActivity/TourActivityEntryRow.tsx create mode 100644 client/src/components/Atlas/tourActivity/TourActivityStatsDialog.test.tsx create mode 100644 client/src/components/Atlas/tourActivity/TourActivityStatsDialog.tsx create mode 100644 client/src/components/Atlas/tourActivity/tourActivityParts.tsx create mode 100644 client/src/components/Atlas/tourActivity/tourActivityStatsModel.test.ts create mode 100644 client/src/components/Atlas/tourActivity/tourActivityStatsModel.ts create mode 100644 client/src/components/Atlas/tourActivity/useTourActivityStats.test.ts create mode 100644 client/src/components/Atlas/tourActivity/useTourActivityStats.ts create mode 100644 client/src/repo/tourActivityStatsRepo.test.ts create mode 100644 client/src/repo/tourActivityStatsRepo.ts create mode 100644 server/src/db/entities/TourActivityExclusions.entity.ts create mode 100644 server/src/db/kysely/TourActivityExclusions.table.ts create mode 100644 server/src/db/migrations/Migration20200101045910_a_tour_can_be_left_out_of_ones_activities.ts create mode 100644 server/src/db/repositories/TourActivityExclusions.repository.ts create mode 100644 server/src/nest/tours/dto/tour-activity-stats.dto.ts create mode 100644 server/src/nest/tours/tour-activity-stats.controller.ts create mode 100644 server/src/nest/tours/tour-activity-stats.helpers.ts create mode 100644 server/src/nest/tours/tour-activity-stats.mcp.ts create mode 100644 server/src/nest/tours/tour-activity-stats.service.ts create mode 100644 server/tests/e2e/tours-activity-stats.e2e.test.ts create mode 100644 server/tests/unit/db/repositories/TourActivities.stats.repository.test.ts create mode 100644 server/tests/unit/db/repositories/TourActivityExclusions.repository.test.ts create mode 100644 server/tests/unit/nest/tour-activity-stats.controller.test.ts create mode 100644 server/tests/unit/nest/tour-activity-stats.helpers.test.ts create mode 100644 server/tests/unit/nest/tour-activity-stats.mcp.test.ts create mode 100644 server/tests/unit/nest/tour-activity-stats.service.test.ts create mode 100644 shared/src/tours/tour-activity-stats.schema.ts create mode 100644 shared/src/tours/tour-activity-stats.spec.ts create mode 100644 shared/src/tours/tour-activity-stats.ts diff --git a/client/src/api/tourActivityStats.ts b/client/src/api/tourActivityStats.ts new file mode 100644 index 000000000..53675e525 --- /dev/null +++ b/client/src/api/tourActivityStats.ts @@ -0,0 +1,10 @@ +import type { TourActivityExclusionRequest, TourActivityStatsResponse } from '@trek/shared'; +import apiClient from './client'; + +/** STA-02: the user's own tour activities across their trips, and leaving one out of them; both answer the sums. */ +export const tourActivityStatsApi = { + get: (signal?: AbortSignal): Promise => + apiClient.get('/tours/activity-stats', { signal }).then((r) => r.data), + setExcluded: (body: TourActivityExclusionRequest): Promise => + apiClient.put('/tours/activity-stats/exclusions', body).then((r) => r.data), +}; diff --git a/client/src/components/Atlas/tourActivity/AtlasTourActivityPane.test.tsx b/client/src/components/Atlas/tourActivity/AtlasTourActivityPane.test.tsx new file mode 100644 index 000000000..86e212184 --- /dev/null +++ b/client/src/components/Atlas/tourActivity/AtlasTourActivityPane.test.tsx @@ -0,0 +1,149 @@ +// FE-STA2-PANE-001 to FE-STA2-PANE-006 +// STA-02: the Atlas tab of the user's own tour activities: the distance large +// with how many tours at its corner, the climb, the types, what the numbers +// rest on, and the full list in a dialog. +import userEvent from '@testing-library/user-event'; +import { summarizeTourActivities, type TourActivityStatsEntry, type TourActivityStatsResponse } from '@trek/shared'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { render, screen } from '../../../../tests/helpers/render'; +import { buildSettings } from '../../../../tests/helpers/factories'; +import { resetAllStores, seedStore } from '../../../../tests/helpers/store'; +import { useSettingsStore } from '../../../store/settingsStore'; +import { AtlasTourActivityPane } from './AtlasTourActivityPane'; +import type { TourActivityStats } from './useTourActivityStats'; + +const hook = vi.hoisted(() => ({ current: null as unknown, active: [] as boolean[] })); +vi.mock('./useTourActivityStats', () => ({ + useTourActivityStats: (active: boolean) => { + hook.active.push(active); + return hook.current; + }, +})); + +const TODAY = '2026-10-20'; + +function entry(overrides: Partial = {}): TourActivityStatsEntry { + return { + place_id: 1, + name: 'Ridge', + tour_type: 'hike', + trip: { id: 5, title: 'Alps' }, + date: '2026-07-02', + basis: 'plan', + track: null, + day_count: 1, + distance_km: 12, + ascent_m: 800, + descent_m: 790, + excluded: false, + ...overrides, + }; +} + +function statsOf(data: TourActivityStatsResponse | null, overrides: Partial = {}) { + const stats: TourActivityStats = { + status: data ? 'ready' : 'loading', + data, + summary: data ? summarizeTourActivities(data.entries, { today: TODAY }) : null, + period: 'all', + setPeriod: vi.fn(), + periods: ['all', 'thisYear', 'last12'], + type: 'all', + setType: vi.fn(), + types: ['all', 'hike', 'kayak'], + pending: new Set(), + setExcluded: vi.fn().mockResolvedValue(undefined), + retry: vi.fn(), + ...overrides, + }; + hook.current = stats; + return stats; +} + +const data: TourActivityStatsResponse = { + entries: [ + entry(), + entry({ place_id: 2, name: 'Summit', date: '2026-08-01', distance_km: 20.5, ascent_m: 1200, descent_m: 1200 }), + entry({ + place_id: 3, + name: 'Lake', + tour_type: 'kayak', + basis: 'track', + track: { place_id: 9, name: 'Watch' }, + distance_km: 8, + ascent_m: null, + descent_m: null, + }), + ], + uncounted: { unassigned: 0, undated: 1 }, +}; + +beforeEach(() => { + resetAllStores(); + vi.clearAllMocks(); + hook.active = []; + seedStore(useSettingsStore, { settings: buildSettings({ distance_unit: 'metric' }) }); +}); + +describe('AtlasTourActivityPane', () => { + it('FE-STA2-PANE-001: follows whether the tab is shown, silent before, loading until the read is there', () => { + statsOf(null, { status: 'idle' }); + const { rerender } = render(); + expect(hook.active).toEqual([false]); + expect(screen.queryByRole('status')).not.toBeInTheDocument(); + statsOf(null); + rerender(); + expect(hook.active).toEqual([false, true]); + expect(screen.getByRole('status')).toHaveTextContent('Loading'); + }); + + it('FE-STA2-PANE-002: shows the distance large with the tour count at its corner, and the climb', () => { + statsOf(data); + render(); + expect(screen.getByTestId('atlas-tour-activity-distance')).toHaveTextContent('40.5km'); + expect(screen.getByTestId('atlas-tour-activity-count')).toHaveTextContent('3'); + expect(screen.getByTestId('atlas-tour-activity-ascent')).toHaveTextContent('2000m'); + expect(screen.getByTestId('atlas-tour-activity-type-hike')).toHaveTextContent('2'); + expect(screen.getByTestId('atlas-tour-activity-type-kayak')).toHaveTextContent('8 km'); + }); + + it('FE-STA2-PANE-003: says what the numbers rest on and what does not count', () => { + statsOf(data); + render(); + expect(screen.getByTestId('tour-activity-basis-plan')).toHaveTextContent('Planned2'); + expect(screen.getByTestId('tour-activity-basis-track')).toHaveTextContent('Recorded1'); + expect(screen.getByTestId('tour-activity-undated')).toHaveTextContent('1'); + }); + + it('FE-STA2-PANE-004: one type chosen leaves the type columns out; nothing done says so', () => { + statsOf(data, { type: 'hike', summary: summarizeTourActivities(data.entries, { today: TODAY, type: 'hike' }) }); + const { rerender } = render(); + expect(screen.queryByTestId('atlas-tour-activity-type-hike')).not.toBeInTheDocument(); + + statsOf(data, { summary: summarizeTourActivities(data.entries, { today: TODAY, from: '2027-01-01' }) }); + rerender(); + expect(screen.getByText('No completed tours in this period')).toBeInTheDocument(); + expect(screen.queryByRole('button', { name: 'Show tours' })).not.toBeInTheDocument(); + }); + + it('FE-STA2-PANE-005: the full list opens in the dialog', async () => { + statsOf(data); + const user = userEvent.setup(); + render(); + await user.click(screen.getByRole('button', { name: 'Show tours' })); + expect(screen.getByRole('heading', { name: 'My tour activities' })).toBeInTheDocument(); + expect(screen.getByTestId('tour-activity-list')).toBeInTheDocument(); + }); + + it('FE-STA2-PANE-006: offline and failed reads say so, a failed one with a retry', async () => { + statsOf(null, { status: 'offline' }); + const { rerender } = render(); + expect(screen.getByText(/needs a connection/i)).toBeInTheDocument(); + + const failed = statsOf(null, { status: 'error' }); + rerender(); + expect(screen.getByRole('alert')).toHaveTextContent('Tour activities could not be loaded'); + await userEvent.setup().click(screen.getByRole('button', { name: 'Try again' })); + expect(failed.retry).toHaveBeenCalled(); + }); +}); diff --git a/client/src/components/Atlas/tourActivity/AtlasTourActivityPane.tsx b/client/src/components/Atlas/tourActivity/AtlasTourActivityPane.tsx new file mode 100644 index 000000000..8a7befaf5 --- /dev/null +++ b/client/src/components/Atlas/tourActivity/AtlasTourActivityPane.tsx @@ -0,0 +1,234 @@ +import type { TourActivitySummary } from '@trek/shared'; +import { Footprints, ListOrdered } from 'lucide-react'; +import { useState } from 'react'; +import { useTranslation } from '../../../i18n'; +import type { DistanceUnit } from '../../../types'; +import { fs } from '../../shared/DialogShell'; +import { Tooltip } from '../../shared/Tooltip'; +import { CardAction, InfoDot } from '../../Tours/tourParts'; +import { TourActivityStatsDialog } from './TourActivityStatsDialog'; +import { splitMeasure } from './tourActivityStatsModel'; +import { + ActivityFilters, + ActivityStatusLine, + BasisBadges, + distanceReading, + heightReading, + typeIcon, + typeLabel, + UncountedBadges, + useDistanceUnit, +} from './tourActivityParts'; +import { useTourActivityStats, type TourActivityStats } from './useTourActivityStats'; + +/** The wash the hero tile sits on, as the Atlas numbers do: the text colour, faintly, over the glass. */ +const HERO_TINT = 'color-mix(in srgb, var(--text-primary) 7%, transparent)'; +/** How many types the strip names before the dialog takes over. */ +const STRIP_TYPES = 3; +const EYEBROW = 'mt-1.5 whitespace-nowrap font-semibold uppercase tracking-wide text-content-faint'; + +function Divider() { + return