diff --git a/apps/desktop/src/store/session-pin-sync.test.ts b/apps/desktop/src/store/session-pin-sync.test.ts index 0e470a9ac4..9abfad3c91 100644 --- a/apps/desktop/src/store/session-pin-sync.test.ts +++ b/apps/desktop/src/store/session-pin-sync.test.ts @@ -133,6 +133,25 @@ describe('watchSessionPins remote pull', () => { expect(patch).toHaveBeenCalledWith('cron-pin', false, 'jobs') }) + it('routes a cross-slice unpin to the active profile', async () => { + $activeGatewayProfile.set('work') + + try { + $sessions.set([row('shared', { pinned: true, profile: 'default' })]) + $messagingSessions.set([row('shared', { pinned: true, profile: 'work', source: 'photon' })]) + await flush() + expect($pinnedSessionIds.get()).toEqual(['shared']) + patch.mockClear() + + $pinnedSessionIds.set([]) + await flush() + + expect(patch).toHaveBeenCalledWith('shared', false, 'work') + } finally { + $activeGatewayProfile.set('default') + } + }) + it('adopts a pin another app made', async () => { $sessions.set([row('remote', { pinned: true })]) await flush() diff --git a/apps/desktop/src/store/session-pin-sync.ts b/apps/desktop/src/store/session-pin-sync.ts index 5c9586efcf..95e71acca3 100644 --- a/apps/desktop/src/store/session-pin-sync.ts +++ b/apps/desktop/src/store/session-pin-sync.ts @@ -77,13 +77,26 @@ function publishUnconfirmed(): void { } function profileFor(pinId: string): null | string | undefined { - return loadedSessionRows().find(row => sessionMatchesStoredId(row, pinId))?.profile + return loadedRowFor(pinId)?.profile } function loadedSessionRows(): SessionInfo[] { return [...$sessions.get(), ...$cronSessions.get(), ...$messagingSessions.get()] } +/** + * The row a stored pin id resolves to, across every slice. Same tie-break as + * `rowsByPinId`: when two profiles share the id, the write must target the + * row the pull adopted — the active gateway's — or an unpin PATCHes the other + * profile and the next page re-adopts the pin. + */ +function loadedRowFor(pinId: string): SessionInfo | undefined { + const rows = loadedSessionRows().filter(row => sessionMatchesStoredId(row, pinId)) + const gateway = normalizeProfileKey($activeGatewayProfile.get()) + + return rows.find(row => normalizeProfileKey(row.profile) === gateway) ?? rows[0] +} + /** * One authoritative row per durable pin id. Session ids are only unique inside * a profile, so the cross-profile list can legitimately hold two rows with the @@ -259,7 +272,7 @@ function reconcileInner(): void { // Flush whatever we can resolve now; unresolved ids (row not loaded yet) // retry on the next loaded-session slice change. for (const id of [...pending]) { - const row = loadedSessionRows().find(entry => sessionMatchesStoredId(entry, id)) + const row = loadedRowFor(id) if (!row) { continue