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.
This commit is contained in:
Maurice
2026-12-06 17:24:26 +01:00
parent e3922b76d8
commit 360bea8bf4
2 changed files with 41 additions and 2 deletions
@@ -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,
@@ -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) {