fix(offline): archived trips on the device follow the eviction rules again

Keeping archived trips stopped the sync from wiping them, but left nothing
that ever took one off the device. The trip sync never refreshes an archived
trip, and the 7-day rule and the per-trip switch only looked at the active
list, so an archived trip stayed until Clear cache however long ago it ended.

The stale and switched-off rules now also run over the archived trips that
are stored here, with the same eviction as an active trip: its rows go, its
parked changes stay. The offline settings list the stored trips the server's
list leaves out, so an archived trip gets a switch the user can turn off;
before, the switches came from the active list only and could not reach it.
This commit is contained in:
Maurice
2026-10-08 11:40:09 +02:00
parent e132d1529f
commit b61a55f037
5 changed files with 104 additions and 20 deletions
@@ -1,4 +1,4 @@
// FE-COMP-OFFLINETAB-001 to FE-COMP-OFFLINETAB-028
// FE-COMP-OFFLINETAB-001 to FE-COMP-OFFLINETAB-031
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
@@ -500,6 +500,22 @@ describe('OfflineTab', () => {
expect(screen.queryByText('No trips cached yet. Connect to the internet to sync.')).not.toBeInTheDocument();
});
it('FE-COMP-OFFLINETAB-031: an archived trip still stored here gets a switch that takes it off', async () => {
const user = userEvent.setup();
const soon = new Date(Date.now() + 7 * 86_400_000).toISOString().slice(0, 10);
const archived = buildTrip({ id: 9, title: 'Rome', start_date: '2025-01-01', end_date: soon, is_archived: 1 });
// The server's list leaves archived trips out; the device still holds Rome.
h.tripsToArray.mockResolvedValue([archived, paris]);
render(<OfflineTab />);
const toggle = await screen.findByRole('button', { name: 'Rome' });
expect(toggle).toHaveAttribute('aria-pressed', 'true');
expect(screen.getAllByRole('button', { name: 'Paris' })).toHaveLength(1);
await user.click(toggle);
expect(h.clearTripData).toHaveBeenCalledWith(9);
});
it('FE-COMP-OFFLINETAB-020: the per-trip section disappears when there are no trips at all', async () => {
server.use(http.get('/api/trips', () => HttpResponse.json({ trips: [] })));
render(<OfflineTab />);
@@ -26,6 +26,21 @@ import { useNetworkMode } from '../../hooks/useNetworkMode'
import type { SyncMeta, QueuedMutation } from '../../db/offlineDb'
import type { Trip } from '../../types'
/**
* The trips the per-trip switches cover: the server's list plus the trips still
* stored here that it leaves out. Those are archived trips (a deleted one goes
* with the next sync), which the trip sync keeps but never refreshes, so this
* switch is the user's way to take one off the device before the date rule does.
*/
async function switchableTrips(): Promise<Trip[]> {
if (isEffectivelyOffline()) return offlineDb.trips.toArray()
const listed = await tripsApi.list().then(r => (r as { trips: Trip[] }).trips).catch(() => null)
if (!listed) return offlineDb.trips.toArray()
const ids = new Set(listed.map(t => t.id))
const stored = await offlineDb.trips.toArray().catch(() => [] as Trip[])
return [...listed, ...stored.filter(t => t.id > 0 && !ids.has(t.id))]
}
export interface CachedTripRow {
trip: Trip
meta: SyncMeta
@@ -128,9 +143,7 @@ export function useOfflineSettings() {
// The per-trip storage toggles are driven by the FULL trip list, not just
// the cached ones, so a trip turned off stays visible and re-enableable.
try {
const trips = isEffectivelyOffline()
? await offlineDb.trips.toArray()
: await tripsApi.list().then(r => (r as { trips: Trip[] }).trips).catch(() => offlineDb.trips.toArray())
const trips = await switchableTrips()
trips.sort((a, b) => (a.start_date ?? '').localeCompare(b.start_date ?? ''))
setAllTrips(trips)
} catch {