mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
feat(mobile): say in a trip's menu whether it is on the device
Whether a trip was kept for offline use was only to be found in the offline settings. The trip's own menu now has an Offline entry that says it right there: saved on this device as of the last sync, not saved yet, or not kept here (switched off, or a finished trip nobody pinned). A tap opens the offline settings, where everything else about it is. Addresses discussion #1580.
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
// FE-SETTINGS-TRIPOFFLINE-001: where a trip stands on this device, for its menu (#1580).
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { tripOfflineState } from './useTripOfflineStatus';
|
||||
|
||||
describe('tripOfflineState', () => {
|
||||
it('FE-SETTINGS-TRIPOFFLINE-001: off when switched off or not kept, saved as of the last sync, else pending', () => {
|
||||
expect(tripOfflineState(false, true, 1000)).toEqual({ kind: 'off' });
|
||||
expect(tripOfflineState(true, false, 1000)).toEqual({ kind: 'off' });
|
||||
expect(tripOfflineState(true, true, 1000)).toEqual({ kind: 'saved', at: 1000 });
|
||||
expect(tripOfflineState(true, true, null)).toEqual({ kind: 'pending' });
|
||||
expect(tripOfflineState(true, true, undefined)).toEqual({ kind: 'pending' });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { offlineDb } from '../../db/offlineDb';
|
||||
import { isTripOfflineEnabled, isTripPinned } from '../../sync/offlinePrefs';
|
||||
import { isDateEligible } from '../../sync/tripSyncManager';
|
||||
import type { Trip } from '../../types';
|
||||
|
||||
/** Where a trip stands on this device (#1580), as the trip's own menu says it. */
|
||||
export type TripOfflineState = { kind: 'saved'; at: number } | { kind: 'pending' } | { kind: 'off' };
|
||||
|
||||
/**
|
||||
* The trip's state from what decides it: switched off, or kept by neither the
|
||||
* date rule nor a pin, it is not on the device; synced before, it is, as of then;
|
||||
* otherwise the next sync brings it.
|
||||
*/
|
||||
export function tripOfflineState(
|
||||
enabled: boolean,
|
||||
kept: boolean,
|
||||
lastSyncedAt: number | null | undefined
|
||||
): TripOfflineState {
|
||||
if (!enabled || !kept) return { kind: 'off' };
|
||||
if (lastSyncedAt) return { kind: 'saved', at: lastSyncedAt };
|
||||
return { kind: 'pending' };
|
||||
}
|
||||
|
||||
/**
|
||||
* The offline state of one trip, read again each time `active` turns on (the
|
||||
* menu opening), so it shows the last sync rather than the one before it.
|
||||
*/
|
||||
export function useTripOfflineStatus(trip: Trip | null | undefined, active: boolean): TripOfflineState | null {
|
||||
const [state, setState] = useState<TripOfflineState | null>(null);
|
||||
useEffect(() => {
|
||||
if (!active || !trip) return;
|
||||
let cancelled = false;
|
||||
const enabled = isTripOfflineEnabled(trip.id);
|
||||
const kept = isTripPinned(trip.id) || isDateEligible(trip);
|
||||
void offlineDb.syncMeta.get(trip.id).then(
|
||||
(meta) => {
|
||||
if (!cancelled) setState(tripOfflineState(enabled, kept, meta?.lastSyncedAt));
|
||||
},
|
||||
// An unreadable store says nothing about the trip: the line stays away.
|
||||
() => {
|
||||
if (!cancelled) setState(null);
|
||||
}
|
||||
);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [trip, active]);
|
||||
return state;
|
||||
}
|
||||
Reference in New Issue
Block a user