From 360bea8bf45303083185c71d0a3428f87d4e5cf0 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 6 Dec 2026 17:24:26 +0100 Subject: [PATCH] fix(atlas): refresh the open globe after offline region removal Use the same queued Atlas projection for an open globe and for an offline restart. --- .../atlas/useAtlasCountryActions.test.ts | 34 ++++++++++++++++++- .../src/pages/atlas/useAtlasCountryActions.ts | 9 ++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/client/src/pages/atlas/useAtlasCountryActions.test.ts b/client/src/pages/atlas/useAtlasCountryActions.test.ts index 45e396676..fc6d435e2 100644 --- a/client/src/pages/atlas/useAtlasCountryActions.test.ts +++ b/client/src/pages/atlas/useAtlasCountryActions.test.ts @@ -1,4 +1,4 @@ -// FE-PAGE-ATLASACTIONS-001 to FE-PAGE-ATLASACTIONS-015: the country / region popup logic +// FE-PAGE-ATLASACTIONS-001 to FE-PAGE-ATLASACTIONS-016: the country / region popup logic // behind both the desktop dialog and the phone sheet. import { act, renderHook } from '@testing-library/react'; import { http, HttpResponse } from 'msw'; @@ -7,6 +7,8 @@ import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vite import { buildAtlasData, buildBucketItem } from '../../../tests/helpers/atlas'; import { server } from '../../../tests/helpers/msw/server'; +import { atlasRepo } from '../../repo/atlasRepo'; +import { isForcedOffline, setForcedOffline } from '../../sync/networkMode'; import type { AtlasData, BucketItem } from './atlasModel'; import { bucketMonthTarget, @@ -205,6 +207,36 @@ describe('useAtlasCountryActions', () => { expect(result.current.confirmAction).toBeNull(); }); + it('FE-PAGE-ATLASACTIONS-016: offline region removal refreshes the open globe from projected stats', async () => { + const alreadyForced = isForcedOffline(); + const projected = buildAtlasData({ countries: [] }); + const unmark = vi.spyOn(atlasRepo, 'unmarkRegion').mockResolvedValue(undefined); + const stats = vi.spyOn(atlasRepo, 'stats').mockResolvedValue(projected); + const countryDetail = vi.spyOn(atlasRepo, 'countryDetail'); + try { + setForcedOffline(true); + const { result } = setup({ + confirmAction: { type: 'unmark-region', code: 'ES', name: 'Galicia', regionCode: 'ES-GA' }, + data: buildAtlasData({ + countries: [{ code: 'ES', tripCount: 0, placeCount: 0, firstVisit: null, lastVisit: null }], + }), + visitedRegions: { ES: [{ code: 'ES-GA', name: 'Galicia', placeCount: 0 }] }, + }); + await act(() => result.current.actions.unmarkRegion()); + expect(unmark).toHaveBeenCalledWith('ES-GA'); + expect(stats).toHaveBeenCalledOnce(); + expect(countryDetail).not.toHaveBeenCalled(); + expect(result.current.data).toBe(projected); + expect(result.current.visitedRegions).toEqual({}); + expect(result.current.confirmAction).toBeNull(); + } finally { + setForcedOffline(alreadyForced); + unmark.mockRestore(); + stats.mockRestore(); + countryDetail.mockRestore(); + } + }); + it('FE-PAGE-ATLASACTIONS-015: a country the server still holds as marked stays when its last region goes (#2597)', async () => { const init = () => ({ confirmAction: { type: 'unmark-region', code: 'ES', name: 'Galicia', regionCode: 'ES-GA' } as AtlasConfirmAction, diff --git a/client/src/pages/atlas/useAtlasCountryActions.ts b/client/src/pages/atlas/useAtlasCountryActions.ts index f07353b9c..ca0ea5a34 100644 --- a/client/src/pages/atlas/useAtlasCountryActions.ts +++ b/client/src/pages/atlas/useAtlasCountryActions.ts @@ -4,6 +4,7 @@ import { useEffect, useState } from 'react'; import { useToast } from '../../components/shared/Toast'; import { atlasRepo } from '../../repo/atlasRepo'; +import { isEffectivelyOffline } from '../../sync/networkMode'; import { getApiErrorMessage, type TranslationFn } from '../../types'; import { findBucketDuplicate, @@ -172,7 +173,13 @@ export function useAtlasCountryActions({ try { await atlasRepo.unmarkRegion(regionCode); setVisitedRegions((prev) => withRegionUnmarked(prev, countryCode, regionCode)); - if (!(await isCountryStillMarked(countryCode))) { + if (isEffectivelyOffline()) { + try { + setData((await atlasRepo.stats()) as AtlasData); + } catch { + // A missing snapshot does not undo the queued write. Keep the current countries until the next load. + } + } else if (!(await isCountryStillMarked(countryCode))) { setData((prev) => withCountryDroppedAfterRegionUnmark(prev, countryCode, regionCode, visitedRegions)); } } catch (err) {