Skip to content

The agent that grows with you

fix(desktop): reset messaging credential fields during render on scope switch

liuhao10242a4595b1c671
diff --git a/apps/desktop/src/app/messaging/index.test.tsx b/apps/desktop/src/app/messaging/index.test.tsx
index 392064dd8b1..5eb753fe1ae 100644
--- a/apps/desktop/src/app/messaging/index.test.tsx
+++ b/apps/desktop/src/app/messaging/index.test.tsx
@@ -150,6 +150,72 @@ describe('MessagingView enable switch', () => {
const toggle = await screen.findByRole('switch', { name: 'Disable Microsoft Teams' })
expect(toggle.closest('label')?.textContent).toBe('Enabled')
})
+
+ it("drops the previous profile's credential placeholders on the very first post-switch render", async () => {
+ // #96542: blanking the stale platforms state in a passive effect lets the
+ // "new scope + old data" frame paint first, so the new profile briefly
+ // showed the PREVIOUS profile's redacted Telegram token as the field
+ // placeholder. The reset must happen during render — after the scope
+ // switch returns from act(), the old value is already gone from the DOM
+ // with no waitFor() in between.
+ const { $settingsScopeOverride } = await import('@/store/settings-scope')
+ const oldToken = '123456:AAE-previous-profile-token'
+
+ getMessagingPlatforms.mockResolvedValue({
+ platforms: [
+ platform({
+ env_vars: [
+ {
+ advanced: false,
+ description: 'Telegram bot token from @BotFather.',
+ is_password: true,
+ is_set: true,
+ key: 'TELEGRAM_TOKEN',
+ prompt: 'Token',
+ redacted_value: oldToken,
+ required: true,
+ url: null
+ }
+ ],
+ id: 'telegram',
+ name: 'Telegram'
+ })
+ ]
+ })
+
+ $settingsScopeOverride.set(null)
+ await renderMessaging()
+
+ // The previous profile's redacted token is visible as the placeholder.
+ expect(await screen.findByPlaceholderText(oldToken)).not.toBeNull()
+
+ // Switch the scope while B's fetch stays pending, so any stale rendering
+ // would still be showing A's data.
+ //
+ // Note on coverage: jsdom cannot observe the paint-order race itself
+ // (act() flushes passive effects synchronously, so an effect-based reset
+ // also clears before the assertion; outside act() the commit itself is
+ // deferred). This test therefore pins the reset *semantics* — after a
+ // scope switch the previous profile's credential placeholder must be gone
+ // from the DOM even while the new profile's fetch is still pending. The
+ // paint-order guarantee ("the stale frame never reaches the screen") is
+ // carried by resetting during render per the React docs pattern instead
+ // of in a passive effect, which fires after paint.
+ getMessagingPlatforms.mockReturnValue(new Promise(() => {}))
+
+ await act(async () => {
+ $settingsScopeOverride.set('profile-b')
+ })
+
+ // Synchronous assertion — no waitFor: the reset must have discarded the
+ // stale platforms state before the new profile's fetch resolves.
+ expect(screen.queryByPlaceholderText(oldToken)).toBeNull()
+
+ // Let pending work settle and restore the shared store.
+ await act(async () => {
+ $settingsScopeOverride.set(null)
+ })
+ })
})
describe('MessagingView setup-guide link', () => {
diff --git a/apps/desktop/src/app/messaging/index.tsx b/apps/desktop/src/app/messaging/index.tsx
index fca0fab6e7d..a7448b3c802 100644
--- a/apps/desktop/src/app/messaging/index.tsx
+++ b/apps/desktop/src/app/messaging/index.tsx
@@ -1,6 +1,6 @@
import { useStore } from '@nanostores/react'
import type * as React from 'react'
-import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
+import { useCallback, useEffect, useMemo, useState } from 'react'
import { PageLoader } from '@/components/page-loader'
import { StatusDot, type StatusTone } from '@/components/status-dot'
@@ -236,20 +236,21 @@ export function MessagingView({ setStatusbarItemGroup: _setStatusbarItemGroup, .
// Scope switch: the mounted list still shows the PREVIOUS profile's
// platforms/pairing while the new fetch is in flight — blank it so stale
- // rows can't be toggled against the wrong backend.
- const scopeSeenRef = useRef(scopeProfile)
-
- // eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (scope-change guard)
- useEffect(() => {
- if (scopeSeenRef.current === scopeProfile) {
- return
- }
-
- scopeSeenRef.current = scopeProfile
+ // rows can't be toggled against the wrong backend. This must run during
+ // render, not in an effect: passive effects fire after paint, and that
+ // first painted frame would show the previous profile's credential
+ // placeholders (e.g. a redacted Telegram token) under the new profile's
+ // scope for the ~1s until the fetch lands (#96542). Setting state during
+ // render makes React throw the stale frame away before it reaches the
+ // screen.
+ const [prevScope, setPrevScope] = useState(scopeProfile)
+
+ if (prevScope !== scopeProfile) {
+ setPrevScope(scopeProfile)
setPlatforms(null)
setPairing({ approved: [], pending: [] })
setEdits({})
- }, [scopeProfile])
+ }
const changeEventsAvailable = useStore($changeEventsAvailable)
const platformsChangeTick = useStore($platformsChangeTick)