fix(settings): retry settings load on reconnect so region prefs stop resetting (#1626)

This commit is contained in:
Konstantinos Thermos
2026-07-20 21:02:19 +02:00
committed by GitHub
parent b6a3cced43
commit a3cb783df7
4 changed files with 103 additions and 13 deletions
+23 -10
View File
@@ -52,21 +52,34 @@ export const DEFAULT_SETTINGS: Settings = {
// chosen" (fall back to home + defaults) from an explicitly emptied list.
}
// De-dupe concurrent loads: the reconnection triggers (online / visibility /
// periodic) can all fire a retry at once — collapse them into one in-flight GET.
let _loadInFlight: Promise<void> | null = null
export const useSettingsStore = create<SettingsState>((set, get) => ({
settings: { ...DEFAULT_SETTINGS },
isLoaded: false,
loadSettings: async () => {
try {
const data = await settingsApi.get()
set((state) => ({
settings: { ...state.settings, ...data.settings },
isLoaded: true,
}))
} catch (err: unknown) {
set({ isLoaded: true })
console.error('Failed to load settings:', err)
}
if (_loadInFlight) return _loadInFlight
_loadInFlight = (async () => {
try {
const data = await settingsApi.get()
set((state) => ({
settings: { ...state.settings, ...data.settings },
isLoaded: true,
}))
} catch (err: unknown) {
// Leave isLoaded false so a transient failure — offline at launch, or a
// (docker) server cold-start racing the first request — is retried on
// reconnect instead of stranding the user on built-in defaults (wrong
// currency/units) for the whole session (#1618).
console.error('Failed to load settings:', err)
} finally {
_loadInFlight = null
}
})()
return _loadInFlight
},
updateSetting: async (key: keyof Settings, value: Settings[keyof Settings]) => {
+19
View File
@@ -17,6 +17,8 @@ import { tripSyncManager } from './tripSyncManager'
import { isEffectivelyOnline, onNetworkModeChange } from './networkMode'
import { setPreReconnectHook, setRefetchCallback, getActiveTrips } from '../api/websocket'
import { useTripStore } from '../store/tripStore'
import { useSettingsStore } from '../store/settingsStore'
import { useAuthStore } from '../store/authStore'
const PERIODIC_MS = 30_000
@@ -25,6 +27,20 @@ let _unsubscribeNetworkMode: (() => void) | null = null
let _wasEffectivelyOnline = isEffectivelyOnline()
let _registered = false
/**
* User settings are loaded once on the auth transition (App.tsx), with no retry.
* If that single GET fails — offline at launch, or a server cold-start racing the
* first request — the store stays on built-in DEFAULT_SETTINGS and the user sees
* their currency/units/time-format "reset" for the whole session (#1618). Heal it
* on the same connectivity triggers that re-hydrate trips: retry until it loads.
*/
function ensureSettingsLoaded() {
if (!useAuthStore.getState().isAuthenticated) return
if (!isEffectivelyOnline()) return
if (useSettingsStore.getState().isLoaded) return
useSettingsStore.getState().loadSettings().catch(console.error)
}
/** Pull the latest server state for every open trip into the Zustand store. */
function rehydrateActiveTrips() {
const store = useTripStore.getState()
@@ -44,6 +60,7 @@ function onOnline() {
// edits from the cache/UI. Stay put until the user lifts the switch (which
// routes through onNetworkMode → here with the force flag already cleared).
if (!isEffectivelyOnline()) return
ensureSettingsLoaded()
mutationQueue.flush()
.catch(console.error)
.finally(() => {
@@ -55,6 +72,7 @@ function onOnline() {
/** Tab became visible — flush only; don't trigger a potentially expensive syncAll. */
function onVisibility() {
if (!document.hidden && isEffectivelyOnline()) {
ensureSettingsLoaded()
mutationQueue.flush().catch(console.error)
}
}
@@ -62,6 +80,7 @@ function onVisibility() {
/** Periodic heartbeat — drain any lingering pending mutations. */
function onPeriodic() {
if (isEffectivelyOnline()) {
ensureSettingsLoaded()
mutationQueue.flush().catch(console.error)
}
}