A full-featured download manager.
fix(renderer): stabilize engine restart reminder subscriptions (#2385)
Muhammad Saad Khalid609d365d3a65
diff --git a/src/renderer/hooks/use-engine-restart-required-toast.test.tsx b/src/renderer/hooks/use-engine-restart-required-toast.test.tsxindex 13a82c69..72186c27 100644--- a/src/renderer/hooks/use-engine-restart-required-toast.test.tsx+++ b/src/renderer/hooks/use-engine-restart-required-toast.test.tsx@@ -155,4 +155,18 @@ describe('useEngineRestartRequiredToast', () => {expect.any(Function))})++ it('subscribes once and does not re-subscribe when the translator identity changes', async () => {+ const { i18n } = await import('@renderer/lib/i18n')+ const { rerender } = renderHook(() => useEngineRestartRequiredToast())+ const subscribed = vi.mocked(transport.on).mock.calls.length++ // LanguageSync changeLanguage() bumps `t`'s identity on a normal app start;+ // that must not churn the transport subscriptions.+ await act(async () => {+ await i18n.changeLanguage('zh-CN')+ })+ rerender()+ expect(vi.mocked(transport.on).mock.calls.length).toBe(subscribed)+ })})diff --git a/src/renderer/hooks/use-engine-restart-required-toast.ts b/src/renderer/hooks/use-engine-restart-required-toast.tsindex 5741e7dd..22bdf9e9 100644--- a/src/renderer/hooks/use-engine-restart-required-toast.ts+++ b/src/renderer/hooks/use-engine-restart-required-toast.ts@@ -4,7 +4,7 @@ import { transport } from '@renderer/lib/transport'import { Commands } from '@shared/protocol/commands'import { Events } from '@shared/protocol/events'import { EngineState } from '@shared/types/engine'-import { useEffect } from 'react'+import { useEffect, useRef } from 'react'import { useTranslation } from 'react-i18next'export const ENGINE_RESTART_REQUIRED_TOAST_ID = 'engine-restart-required'@@ -12,6 +12,13 @@ export const ENGINE_RESTART_REQUIRED_TOAST_ID = 'engine-restart-required'/** Show one coalesced, action-bearing reminder until the engine is Ready again. */export function useEngineRestartRequiredToast(): void {const { t } = useTranslation()+ // `LanguageSync` calls `i18n.changeLanguage()` on every app start, which+ // bumps `t`'s identity via react-i18next even for the same language. Thread+ // `t` through a ref and subscribe exactly once (empty deps) so that identity+ // change never tears down + re-attaches the transport listeners (which would+ // also reset `reminderVisible`). Mirrors use-pair-request-prompts.tsx.+ const tRef = useRef(t)+ tRef.current = tuseEffect(() => {let restartPending = false@@ -22,7 +29,9 @@ export function useEngineRestartRequiredToast(): void {restartPending = truetoast.update(ENGINE_RESTART_REQUIRED_TOAST_ID, {actionProps: {- children: t('notification.engineRestartRequired.restartNow'),+ children: tRef.current(+ 'notification.engineRestartRequired.restartNow'+ ),disabled: true,'aria-busy': true,onClick: restartAction,@@ -36,7 +45,9 @@ export function useEngineRestartRequiredToast(): void {if (!reminderVisible) returntoast.update(ENGINE_RESTART_REQUIRED_TOAST_ID, {actionProps: {- children: t('notification.engineRestartRequired.restartNow'),+ children: tRef.current(+ 'notification.engineRestartRequired.restartNow'+ ),disabled: false,onClick: restartAction,},@@ -50,12 +61,14 @@ export function useEngineRestartRequiredToast(): void {toast.close(ENGINE_RESTART_REQUIRED_TOAST_ID)toast.add({id: ENGINE_RESTART_REQUIRED_TOAST_ID,- title: t('notification.engineRestartRequired.title'),- description: t('notification.engineRestartRequired.body'),+ title: tRef.current('notification.engineRestartRequired.title'),+ description: tRef.current('notification.engineRestartRequired.body'),type: 'warning',timeout: 0,actionProps: {- children: t('notification.engineRestartRequired.restartNow'),+ children: tRef.current(+ 'notification.engineRestartRequired.restartNow'+ ),disabled: restartPending,onClick: restartAction,},@@ -80,5 +93,5 @@ export function useEngineRestartRequiredToast(): void {transport.off(Events.EngineRestartRequired, onRestartRequired)transport.off(Events.EngineStateChanged, onEngineStateChanged)}- }, [t])+ }, [])}