/**
* Offline settings tab (#1135) — controls for:
* - Offline mode: a force-offline switch that first downloads everything, then
* routes the app to the cache + mutation queue.
* - Prepare for offline: an awaited, progress-tracked full download.
* - What to store: a map-tiles toggle plus a per-trip on/off.
* - Sync conflicts: a keep-mine / keep-theirs resolver and a default strategy.
* - Cache stats + clear.
*
* All of the logic lives in `useOfflineSettings`, shared with the phone twin
* `MSettingsOffline`; this file is the desktop markup over it.
*/
import React, { useState } from 'react'
import { RefreshCw, Trash2, Database, CloudOff, Download, Check, GitMerge, Map as MapIcon, AlertTriangle } from 'lucide-react'
import Section from './Section'
import ToggleSwitch from './ToggleSwitch'
import { SettingRow, SettingRows, SettingsHint, StatusPill, SETTINGS_BUTTON, SETTINGS_BUTTON_DANGER } from './settingsKit'
import { useOfflineSettings, offlineNoticeKey, isOfflineNoticeWarning, offlineProgressLabel, offlineRowMeta } from './useOfflineSettings'
import { useTranslation } from '../../i18n'
import CustomSelect from '../shared/CustomSelect'
import ConfirmDialog from '../shared/ConfirmDialog'
import { fs } from '../shared/DialogShell'
import type { ConflictStrategy } from '../../sync/offlinePrefs'
import type { QueuedMutation } from '../../db/offlineDb'
const EYEBROW = 'm-0 mb-2 font-geist font-bold uppercase tracking-[.08em] text-content-faint'
function conflictName(m: QueuedMutation): string {
const body = (m.body ?? {}) as { name?: unknown }
const server = (m.conflictServer ?? {}) as { name?: unknown }
return (typeof body.name === 'string' && body.name)
|| (typeof server.name === 'string' && server.name)
|| `#${m.entityId ?? ''}`
}
export default function OfflineTab(): React.ReactElement {
const { t, locale } = useTranslation()
const {
offline, forced,
rows, allTrips, pendingCount, failedCount, conflicts,
syncing, clearing, loading, preparing, progress, notice, prefs, canClear,
runPrepare, handleToggleForce, handleResync, handleClear,
handleToggleTiles, tripStorageState, handleToggleTrip, resolveConflict,
handleConflictStrategy, retryFailed, discardFailed,
} = useOfflineSettings()
const [confirmClear, setConfirmClear] = useState(false)
const progressLabel = offlineProgressLabel(progress, t)
return (
{/* Offline mode + prepare */}
}
>
{forced && (
{t('settings.offline.mode.active')}
)}
{preparing
?
: }
{preparing ? t('settings.offline.prepare.running') : t('settings.offline.prepare.button')}
{syncing ? t('settings.offline.resyncing') : t('settings.offline.resync')}
{preparing && progress && (
)}
{!preparing && notice && notice.kind !== 'load-failed' && (
{isOfflineNoticeWarning(notice) ?
:
}
{t(offlineNoticeKey(notice), notice.kind === 'stored' ? { count: notice.trips } : undefined)}
)}
{/* Conflicts (only when there are any) */}
{conflicts.length > 0 && (
{conflicts.length}}
>
{t('settings.offline.conflicts.hint')}
{conflicts.map(c => (
{t('settings.offline.conflicts.item', { name: conflictName(c) })}
resolveConflict(c.id, true)} className={SETTINGS_BUTTON} style={fs(12.5, 'body')}>
{t('settings.offline.conflicts.keepMine')}
resolveConflict(c.id, false)} className={SETTINGS_BUTTON} style={fs(12.5, 'body')}>
{t('settings.offline.conflicts.keepServer')}
))}
handleConflictStrategy(v as ConflictStrategy)}
options={[
{ value: 'ask', label: t('settings.offline.conflicts.strategy.ask') },
{ value: 'mine', label: t('settings.offline.conflicts.strategy.mine') },
{ value: 'server', label: t('settings.offline.conflicts.strategy.server') },
]}
menuFit="content"
style={{ minWidth: 240 }}
/>
}
/>
)}
{/* What to store offline */}
}
/>
{allTrips.length > 0 && (
{t('settings.offline.storage.tripsTitle')}
{allTrips.map((trip) => {
const { on, dateEligible } = tripStorageState(trip)
return (
{trip.title}
{on ? t('settings.offline.storage.tripOn') : t('settings.offline.storage.tripOff')}
{!dateEligible && `. ${t('settings.offline.storage.tripFinished')}`}
handleToggleTrip(trip)} label={trip.title} />
)
})}
)}
{/* Cache stats + list + clear */}
setConfirmClear(true)}
disabled={clearing || !canClear}
className={SETTINGS_BUTTON_DANGER}
style={fs(12.5, 'body')}
>
{t('settings.offline.clear')}
}
>
{conflicts.length > 0 && }
{failedCount > 0 && }
{failedCount > 0 && (
{t('settings.offline.failed.hint')}
void retryFailed()} disabled={offline} className={SETTINGS_BUTTON} style={fs(12.5, 'body')}>
{t('settings.offline.failed.retry')}
void discardFailed()} className={SETTINGS_BUTTON_DANGER} style={fs(12.5, 'body')}>
{t('settings.offline.failed.discard')}
)}
{loading ? (
{t('settings.offline.loading')}
) : rows.length === 0 ? (
{t(notice?.kind === 'load-failed' ? 'settings.offline.notice.loadFailed' : 'settings.offline.empty')}
) : (
{rows.map(({ trip, meta, placeCount, fileCount }) => (
{trip.title}
{offlineRowMeta({ trip, placeCount, fileCount }, t, locale)}
{meta.lastSyncedAt
? new Date(meta.lastSyncedAt).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })
: '—'}
))}
)}
setConfirmClear(false)}
onConfirm={() => { void handleClear() }}
title={t('settings.offline.clear')}
message={t('settings.offline.clearConfirm')}
confirmLabel={t('settings.offline.clear')}
danger
/>
)
}
/** One counter tile: the number big, what it counts under it. */
function Stat({ label, value, danger }: { label: string; value: number; danger?: boolean }) {
return (
)
}