diff --git a/apps/app/src/state/authStore.ts b/apps/app/src/state/authStore.ts index 7669c1fe..99a155ab 100644 --- a/apps/app/src/state/authStore.ts +++ b/apps/app/src/state/authStore.ts @@ -116,10 +116,13 @@ async function loadWorkspace( user: User, profile: Profile | null, set: Set, + get: Get, ) { if (!user.email_confirmed_at) return set({ workspace: { status: 'unconfirmed' } }) if (profile && !profile.active) return set({ workspace: { status: 'inactive' } }) - set({ workspace: { status: 'loading' } }) + // A re-read (USER_UPDATED, retry) keeps a ready workspace on screen until the + // answer, so the routes under it, an open form included, stay mounted. + if (get().workspace.status !== 'ready') set({ workspace: { status: 'loading' } }) try { const id = await fetchPersonalWorkspaceId(client, user.id) set({ workspace: id ? { status: 'ready', id } : { status: 'missing' } }) @@ -136,7 +139,7 @@ async function loadAccount(client: FaunosteoClient, user: User, set: Set, get: G ]) setOwn({ profile, roles }) await Promise.all([ - loadWorkspace(client, user, profile, setOwn), + loadWorkspace(client, user, profile, setOwn, get), syncAvatar(client, user, setOwn, get).catch(() => setOwn({ avatar: null })), ]) } @@ -242,7 +245,7 @@ export const useAuthStore = create((set, get) => ({ refreshWorkspace: async () => { const user = requireUser(get().user) - await loadWorkspace(requireClient(), user, get().profile, setForUser(user, set, get)) + await loadWorkspace(requireClient(), user, get().profile, setForUser(user, set, get), get) }, refreshAvatar: async () => { diff --git a/apps/app/tests/unit/state.authStore.test.ts b/apps/app/tests/unit/state.authStore.test.ts index 54d47bda..7a9bec6e 100644 --- a/apps/app/tests/unit/state.authStore.test.ts +++ b/apps/app/tests/unit/state.authStore.test.ts @@ -298,6 +298,22 @@ describe('authStore', () => { }) }) + it('keeps a ready workspace on screen while the same account is read again', async () => { + fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }] + stop = useAuthStore.getState().start() + fake.emit('SIGNED_IN', fakeSession()) + await flush() + await flush() + const seen: string[] = [] + const unsubscribe = useAuthStore.subscribe((s) => seen.push(s.workspace.status)) + fake.emit('USER_UPDATED', fakeSession()) + await flush() + await flush() + unsubscribe() + expect(seen).not.toContain('loading') + expect(useAuthStore.getState().workspace).toEqual({ status: 'ready', id: 'ws-1' }) + }) + it('forgets the workspace on sign-out', async () => { fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }] stop = useAuthStore.getState().start()