Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(desktop): auto-dismiss the core-updated toast after 3s, pausing on hover (#21650)

AmAzing-cf36c3229e05
diff --git a/src/features/Electron/updater/UpdateNotification.tsx b/src/features/Electron/updater/UpdateNotification.tsx
index aa15d077e3a..90e05395ae2 100644
--- a/src/features/Electron/updater/UpdateNotification.tsx
+++ b/src/features/Electron/updater/UpdateNotification.tsx
@@ -24,6 +24,9 @@ import { userGeneralSettingsSelectors } from '@/store/user/selectors';
import { applyCoreUpdate } from './applyCoreUpdate';
import { formatCoreVersion } from './coreVersion';
import { selectUpdateInfo } from './selectUpdateInfo';
+import { useAutoDismiss } from './useAutoDismiss';
+
+const APPLIED_TOAST_DURATION = 3000;
const styles = createStaticStyles(({ css, cssVar }) => ({
installLaterCloseButton: css`
@@ -190,6 +193,13 @@ export const UpdateNotification: React.FC = () => {
});
const isDevMode = useUserStore((s) => userGeneralSettingsSelectors.config(s).isDevMode);
+ // The "updated to version" toast only shows when no pending update takes its slot
+ const appliedToastHoverHandlers = useAutoDismiss(
+ updateInfo ? null : appliedVersion,
+ () => setAppliedVersion(null),
+ APPLIED_TOAST_DURATION,
+ );
+
useWatchBroadcast('updateReady', (info) => {
receivedUpdateEvent.current = true;
setUpdateInfo((current) => selectUpdateInfo(current, info));
@@ -255,7 +265,7 @@ export const UpdateNotification: React.FC = () => {
if (!updateInfo)
return appliedVersion ? (
- <div className={styles.installLaterToast}>
+ <div className={styles.installLaterToast} {...appliedToastHoverHandlers}>
<span>
{tElectron('updater.rendererUpdated', { version: formatCoreVersion(appliedVersion) })}
</span>
diff --git a/src/features/Electron/updater/useAutoDismiss.test.ts b/src/features/Electron/updater/useAutoDismiss.test.ts
new file mode 100644
index 00000000000..9478a7e75ad
--- /dev/null
+++ b/src/features/Electron/updater/useAutoDismiss.test.ts
@@ -0,0 +1,87 @@
+import { act, renderHook } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { useAutoDismiss } from './useAutoDismiss';
+
+const renderAutoDismiss = (onDismiss: () => void, key: string | null = '2.2.21') =>
+ renderHook(({ key }) => useAutoDismiss(key, onDismiss, 3000), { initialProps: { key } });
+
+beforeEach(() => {
+ vi.useFakeTimers();
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+});
+
+describe('useAutoDismiss', () => {
+ it('dismisses once the key has been shown for the full duration', () => {
+ const onDismiss = vi.fn();
+ renderAutoDismiss(onDismiss);
+
+ vi.advanceTimersByTime(2999);
+ expect(onDismiss).not.toHaveBeenCalled();
+
+ vi.advanceTimersByTime(1);
+ expect(onDismiss).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not count down while the key is nullish', () => {
+ const onDismiss = vi.fn();
+ renderAutoDismiss(onDismiss, null);
+
+ vi.advanceTimersByTime(10_000);
+ expect(onDismiss).not.toHaveBeenCalled();
+ });
+
+ it('restarts the countdown when the key changes', () => {
+ const onDismiss = vi.fn();
+ const { rerender } = renderAutoDismiss(onDismiss);
+
+ vi.advanceTimersByTime(2000);
+ rerender({ key: '2.2.22' });
+ vi.advanceTimersByTime(2000);
+ expect(onDismiss).not.toHaveBeenCalled();
+
+ vi.advanceTimersByTime(1000);
+ expect(onDismiss).toHaveBeenCalledTimes(1);
+ });
+
+ it('cancels the countdown when the key is cleared', () => {
+ const onDismiss = vi.fn();
+ const { rerender } = renderAutoDismiss(onDismiss);
+
+ vi.advanceTimersByTime(2000);
+ rerender({ key: null });
+ vi.advanceTimersByTime(3000);
+ expect(onDismiss).not.toHaveBeenCalled();
+ });
+
+ it('pauses while hovered and restarts the full duration on leave', () => {
+ const onDismiss = vi.fn();
+ const { result } = renderAutoDismiss(onDismiss);
+
+ vi.advanceTimersByTime(2000);
+ act(() => result.current.onMouseEnter());
+ vi.advanceTimersByTime(10_000);
+ expect(onDismiss).not.toHaveBeenCalled();
+
+ act(() => result.current.onMouseLeave());
+ vi.advanceTimersByTime(2999);
+ expect(onDismiss).not.toHaveBeenCalled();
+ vi.advanceTimersByTime(1);
+ expect(onDismiss).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not keep a new key paused when the hovered toast was replaced', () => {
+ const onDismiss = vi.fn();
+ const { result, rerender } = renderAutoDismiss(onDismiss);
+
+ act(() => result.current.onMouseEnter());
+ rerender({ key: null });
+ rerender({ key: '2.2.22' });
+
+ vi.advanceTimersByTime(3000);
+ expect(onDismiss).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/src/features/Electron/updater/useAutoDismiss.ts b/src/features/Electron/updater/useAutoDismiss.ts
new file mode 100644
index 00000000000..14fb8e0bf4b
--- /dev/null
+++ b/src/features/Electron/updater/useAutoDismiss.ts
@@ -0,0 +1,31 @@
+import { useEffect, useRef, useState } from 'react';
+
+/**
+ * Calls `onDismiss` once `key` has stayed the same for `duration` ms.
+ * A nullish `key` pauses the countdown; a new `key` restarts it.
+ * Spread the returned handlers onto the toast so hovering pauses the countdown
+ * and leaving restarts it in full.
+ */
+export const useAutoDismiss = (
+ key: string | null | undefined,
+ onDismiss: () => void,
+ duration: number,
+) => {
+ const onDismissRef = useRef(onDismiss);
+ onDismissRef.current = onDismiss;
+ // Track which key is hovered so a toast that unmounts under the pointer
+ // (no mouseleave) does not leave the next one paused forever
+ const [hoveredKey, setHoveredKey] = useState<string | null>(null);
+ const paused = !!key && hoveredKey === key;
+
+ useEffect(() => {
+ if (key === null || key === undefined || paused) return;
+ const timer = setTimeout(() => onDismissRef.current(), duration);
+ return () => clearTimeout(timer);
+ }, [key, duration, paused]);
+
+ return {
+ onMouseEnter: () => setHoveredKey(key ?? null),
+ onMouseLeave: () => setHoveredKey(null),
+ };
+};