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:
Maurice
2026-11-27 23:23:50 +01:00
parent 57fb2cf47b
commit 8d5d34fbb3
31 changed files with 186 additions and 4 deletions
@@ -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;
}
@@ -1,6 +1,7 @@
import { ChevronRight, FileDown, Settings, Settings2, Sparkles } from 'lucide-react'
import { ChevronRight, CloudDownload, FileDown, Settings, Settings2, Sparkles } from 'lucide-react'
import type { LucideIcon } from 'lucide-react'
import { useState } from 'react'
import { useNavigate } from 'react-router'
import MSheet from '../../../components/MSheet'
import { edgeFadeClass, useEdgeFade } from '../../../components/useEdgeFade'
import type { MTripSheetsProps } from '../MTripShell'
@@ -9,6 +10,8 @@ import { useIsWide } from '../../../../hooks/sizeClass'
import { useTranslation } from '../../../../i18n'
import { useAssistantAsk } from '../../../../components/Assistant/useAssistantAsk'
import { useAssistantHints } from '../../../../components/Assistant/useAssistantHints'
import { useTripOfflineStatus, type TripOfflineState } from '../../../../components/Settings/useTripOfflineStatus'
import { useSettingsStore } from '../../../../store/settingsStore'
/** Demo tile tints per legacy tab id; plugins share the files neutral. */
const TILE_COLORS: Record<string, string> = {
@@ -27,9 +30,25 @@ const TILE_COLORS: Record<string, string> = {
* transparent scrim. Grid of the trip sections that did not fit the dock
* (files/collab/plugins, with short stats) plus the share/export/settings rows.
*/
/** The offline line under the trip's own menu entry (#1580): saved as of when, or why not. */
function offlineLine(state: TripOfflineState | null, t: (key: string, params?: Record<string, string>) => string, language: string, is12h: boolean): string | undefined {
if (!state) return undefined
if (state.kind === 'off') return t('mobileTrip.offlineOff')
if (state.kind === 'pending') return t('mobileTrip.offlineNotSaved')
const at = new Date(state.at)
const today = new Date().toDateString() === at.toDateString()
const time = today
? at.toLocaleTimeString(language, { hour: 'numeric', minute: '2-digit', hour12: is12h })
: at.toLocaleDateString(language, { day: 'numeric', month: 'short' })
return t('mobileTrip.offlineSaved', { time })
}
export default function MMehrSheet({ planner, shell }: MTripSheetsProps) {
const { t } = useTranslation()
const { t, language } = useTranslation()
const navigate = useNavigate()
const open = shell.sheet?.id === 'mehr'
const offline = useTripOfflineStatus(planner.trip, open)
const is12h = useSettingsStore(s => s.settings.time_format === '12h')
const canEditTrip = planner.can('trip_edit', planner.trip)
const assistant = useAssistantAsk(planner.tripId)
// Warnings not shown yet turn the tile's line into a count; the dock gets no dot.
@@ -61,9 +80,20 @@ export default function MMehrSheet({ planner, shell }: MTripSheetsProps) {
shell.setTrTab(id)
}
const actions: { id: string; icon: LucideIcon; label: string; go: () => void }[] = [
const actions: { id: string; icon: LucideIcon; label: string; sub?: string; go: () => void }[] = [
{ id: 'members', icon: Settings2, label: t('trip.settings.title'), go: () => shell.openSheet('members') },
{ id: 'export', icon: FileDown, label: t('mobileTrip.export'), go: () => shell.openSheet('export') },
// Whether this trip is on the device, without a trip to the settings to find out (#1580).
{
id: 'offline',
icon: CloudDownload,
label: t('settings.tabs.offline'),
sub: offlineLine(offline, t, language, is12h),
go: () => {
shell.closeSheet()
void navigate('/settings?tab=offline')
},
},
...(canEditTrip
? [{ id: 'tripedit', icon: Settings, label: t('dashboard.editTrip'), go: () => shell.openSheet('tripedit') }]
: []),
@@ -139,7 +169,10 @@ export default function MMehrSheet({ planner, shell }: MTripSheetsProps) {
<div className="flex h-[34px] w-[34px] flex-none items-center justify-center rounded-[10px] bg-[color:var(--m-ic)]">
<action.icon size={17} strokeWidth={1.9} />
</div>
<span className="min-w-0 flex-1 truncate text-[0.875rem] font-semibold">{action.label}</span>
<span className="min-w-0 flex-1">
<span className="block truncate text-[0.875rem] font-semibold">{action.label}</span>
{action.sub && <span className="block truncate text-caption text-m-muted">{action.sub}</span>}
</span>
<ChevronRight size={16} strokeWidth={2} className="flex-none text-m-faint" />
</button>
))}