Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

The "back online" toast fires on the real reconnect path: the pull that runs first cleared the offline flag before the owed push looked at it

Duarte Santos14c454f5c131
diff --git a/frontend/src/store/useStore.adopt.test.jsx b/frontend/src/store/useStore.adopt.test.jsx
index 02eabce04..4fd9730b0 100644
--- a/frontend/src/store/useStore.adopt.test.jsx
+++ b/frontend/src/store/useStore.adopt.test.jsx
@@ -92,9 +92,12 @@ describe('offline and unsynced flags', () => {
await useStore.getState().pushState()
expect(useStore.getState().sync).toMatchObject({ offline: true, pending: true })
expect(localStorage.getItem('gym_dirty')).toBe('1')
- // back online: the push lands, the flags clear, and the user hears about it once
+ // back online: the `online` event checks with the server first (the pull clears the offline
+ // flag), then the owed push lands, the flags clear, and the user hears about it once
+ api.mockResolvedValueOnce({ state: { ...clone(DEF), _ts: 100, workouts: [workout('w1')], _rev: 1 }, rev: 1 })
api.mockResolvedValueOnce({ ok: true, rev: 2 })
- await useStore.getState().pushState()
+ window.dispatchEvent(new Event('online'))
+ await new Promise(r => setTimeout(r, 20))
expect(useStore.getState().sync).toMatchObject({ offline: false, pending: false })
expect(localStorage.getItem('gym_dirty')).toBeNull()
await new Promise(r => setTimeout(r, 0)) // the toast goes through a lazy import of useUI
diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js
index 08fc35342..18e6cc32d 100644
--- a/frontend/src/store/useStore.js
+++ b/frontend/src/store/useStore.js
@@ -109,6 +109,7 @@ export const useStore = create((set, get) => {
let forceNext = false // the next push replaces the server copy outright (import, reset)
let lastCheck = 0
let pollTm = null
+ let offlineChanges = false // a push failed for lack of network — the next one that lands says so
const readSync = () => { try { return JSON.parse(localStorage.getItem(SYNC_KEY)) || null } catch { return null } }
const writeSync = (rev, ts) => localStorage.setItem(SYNC_KEY, JSON.stringify({ rev, ts: ts || 0 }))
@@ -223,15 +224,15 @@ export const useStore = create((set, get) => {
toldTooLarge = false
// Back from offline with changes that were waiting: say so once — the banner that promised
// "syncs when you're back online" has just kept its word.
- const was = get().sync
setSync({ offline: false, pending: false, lastSynced: Date.now() })
- if (was.offline && was.pending) {
+ if (offlineChanges) {
+ offlineChanges = false
import('./useUI.js').then(({ useUI }) => useUI.getState().toast(t('Back online — synced with the server.'))).catch(() => {})
}
} catch (e) {
// A session that is gone is boot's business (/api/me); the copy stays owed to the server.
if (e.status === 401) { localStorage.setItem('gym_dirty', '1'); return }
- if (isNetworkError(e)) { localStorage.setItem('gym_dirty', '1'); setSync({ offline: true, pending: true }); return }
+ if (isNetworkError(e)) { localStorage.setItem('gym_dirty', '1'); offlineChanges = true; setSync({ offline: true, pending: true }); return }
if (e.status === 409 && e.data && attempt < 2) {
// Another device wrote since this one last read. The server sent its document along;
// merge and push once more against that revision. A second refusal in a row leaves the