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.

Tabs: the tab that signed in or signed up joins the other tabs' saves at once

Duarte Santos22206004921a
diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js
index 2f89f961b..7549a4d2e 100644
--- a/frontend/src/store/useStore.js
+++ b/frontend/src/store/useStore.js
@@ -783,6 +783,7 @@ export const useStore = create((set, get) => {
persist(freshState(), false)
localStorage.removeItem('gym_owner_name')
localStorage.removeItem('gym_owner')
+ seenOwner = null // a guest's tab now: it joins what other signed-out tabs save (as setUser)
setSync({ offline: false, auth: false, lastError: null, pending: false, lastSynced: 0 })
// On a phone the file is wiped with it, now rather than after the usual wait: a start in
// between would open in local mode on the signed-out account's data, since that boot takes
@@ -1358,6 +1359,10 @@ export const useStore = create((set, get) => {
if (m && m.uid !== u.id) writeAdopt(null)
adoptHold = !!m && m.uid === u.id
}
+ // What another tab of this browser saved under the copy's owner so far is this copy's
+ // too: the question below is about it, and from here on this tab joins only the
+ // account's saves (seenOwner below).
+ syncFromSaved()
if (other) {
keepForPrevious(owner)
forgetSync()
@@ -1370,6 +1375,10 @@ export const useStore = create((set, get) => {
if (adopt) writeAdopt({ uid: u.id, rejoined, alwaysAsk: !!adopt.alwaysAsk, pre: preOf(get().S) })
localStorage.setItem('gym_owner_name', u.name || '')
localStorage.setItem('gym_owner', u.id)
+ // This tab now joins the account's saves like any tab opened on it: one opened after the
+ // sign-in saves under the new owner, and a tab still comparing against the old one took
+ // no part until it had saved itself, then wrote its copy over theirs (joinSaved).
+ seenOwner = u.id
localStorage.setItem('gym_user', JSON.stringify(u)); localStorage.removeItem('gym_guest')
logoutOwed(false) // this sign-in's cookie replaced the one a failed sign-out left behind
// The file follows at once, as the new account's: until then it holds the previous one's.
diff --git a/frontend/src/store/useStore.tabs-join.test.jsx b/frontend/src/store/useStore.tabs-join.test.jsx
index 6bbdd3bde..70199d7b8 100644
--- a/frontend/src/store/useStore.tabs-join.test.jsx
+++ b/frontend/src/store/useStore.tabs-join.test.jsx
@@ -141,4 +141,64 @@ describe('two tabs of one browser keep each other\'s changes', () => {
expect(r.owed).toBe(false)
expect(ids(srv.doc)).toEqual(['w1', 'w2'])
})
+ // QA retest 2026-10-06: the tab that signed in (or signed up) still compared against the owner it
+ // had loaded with, so it took no part in the join until it had saved once itself, and that save
+ // wrote its copy over a weigh-in and a running workout another tab had logged.
+ it('a tab that just signed in sees the other tab\'s saves and keeps them, running workout included', async () => {
+ localStorage.removeItem('gym_owner')
+ localStorage.setItem('gym_state_v1', JSON.stringify({ ...clone(DEF), _ts: 100, workouts: [w('g1')] }))
+ const srv = server(null)
+ const A = await openTab(null)
+ A.getState().setUser({ id: OWNER, name: 'Tess' }) // signs up here; nothing saved since
+ const B = await openTab() // opened afterwards, on the account
+ srv.offline = true
+ B.getState().update(s => {
+ s.bodyweight = [{ d: '2026-09-05', w: 66.6, t: Date.now() }]
+ s.active = { id: 'run2', start: Date.now(), entries: [{ id: 'bench', sets: [{ w: 60, r: 5, done: true }] }] }
+ })
+ announce()
+ expect(A.getState().S.bodyweight.map(e => e.w)).toEqual([66.6])
+ expect(A.getState().S.active?.id).toBe('run2')
+ A.getState().update(s => { s.restSec = 135 })
+ expect(saved().bodyweight.map(e => e.w)).toEqual([66.6])
+ expect(saved().active?.id).toBe('run2')
+ expect(saved().restSec).toBe(135)
+ })
+
+ it('a tab that just signed in joins a closed tab\'s save on its next change', async () => {
+ localStorage.removeItem('gym_owner')
+ localStorage.setItem('gym_state_v1', JSON.stringify({ ...clone(DEF), _ts: 100, workouts: [w('g1')] }))
+ const srv = server(null)
+ const A = await openTab(null)
+ A.getState().setUser({ id: OWNER, name: 'Tess' })
+ const B = await openTab()
+ srv.offline = true
+ B.getState().update(s => { s.workouts.push(w('w2', '2026-09-02')); s.bodyweight = [{ d: '2026-09-05', w: 66.6, t: Date.now() }] })
+ B.setState({ user: null, ready: false }) // closed before the browser told A
+ A.getState().update(s => { s.restSec = 135 })
+ expect(ids(saved())).toEqual(['g1', 'w2'])
+ expect(saved().bodyweight.map(e => e.w)).toEqual([66.6])
+ })
+
+ it('a tab that signed out joins what another signed-out tab saves next', async () => {
+ const srv = server({ ...clone(DEF), _ts: 100, workouts: [w('w1')], _rev: 1 })
+ savedCopy(srv.doc, 1)
+ const A = await openTab()
+ await A.getState().signOut()
+ const B = await openTab(null) // a guest tab opened after the sign-out
+ B.getState().update(s => { s.workouts.push(w('g9', '2026-09-09')) })
+ A.getState().update(s => { s.restSec = 50 })
+ expect(ids(saved())).toEqual(['g9'])
+ expect(saved().restSec).toBe(50)
+ })
+ it('a sign-in asks about what another guest tab saved too, and then joins only the account\'s saves', async () => {
+ localStorage.removeItem('gym_owner')
+ localStorage.setItem('gym_state_v1', JSON.stringify({ ...clone(DEF), _ts: 100, workouts: [w('g1')] }))
+ server(null)
+ const A = await openTab(null)
+ const B = await openTab(null)
+ B.getState().update(s => { s.workouts.push(w('g2', '2026-09-02')) }) // A not told yet
+ A.getState().setUser({ id: OWNER, name: 'Tess' }, { adopt: true })
+ expect(JSON.parse(localStorage.getItem('gym_adopt')).pre.workouts.sort()).toEqual(['g1', 'g2'])
+ })
})