diff --git a/index.html b/index.html index 8f9f5c30..b8f93e38 100644 --- a/index.html +++ b/index.html @@ -4,20 +4,20 @@ Yaak App - + diff --git a/src-web/components/GlobalHooks.tsx b/src-web/components/GlobalHooks.tsx index 6bce9e4c..a5a99567 100644 --- a/src-web/components/GlobalHooks.tsx +++ b/src-web/components/GlobalHooks.tsx @@ -1,7 +1,7 @@ import { useQueryClient } from '@tanstack/react-query'; import { getCurrent } from '@tauri-apps/api/webviewWindow'; import { useEffect } from 'react'; -import { useActiveWorkspace } from '../hooks/useActiveWorkspace'; +import { useAtiveWorkspaceChangedToast } from '../hooks/useAtiveWorkspaceChangedToast'; import { useClipboardText } from '../hooks/useClipboardText'; import { useCommandPalette } from '../hooks/useCommandPalette'; import { cookieJarsQueryKey } from '../hooks/useCookieJars'; @@ -33,12 +33,8 @@ import { monokaiProDefault } from '../lib/theme/themes/monokai-pro'; import { rosePineDefault } from '../lib/theme/themes/rose-pine'; import { yaakDark } from '../lib/theme/themes/yaak'; import { getThemeCSS } from '../lib/theme/window'; -import { InlineCode } from './core/InlineCode'; -import { useToast } from './ToastContext'; export function GlobalHooks() { - const toast = useToast(); - // Include here so they always update, even if no component references them useRecentWorkspaces(); useRecentEnvironments(); @@ -48,21 +44,7 @@ export function GlobalHooks() { useSyncThemeToDocument(); useCommandPalette(); useNotificationToast(); - - const activeWorkspace = useActiveWorkspace(); - - useEffect(() => { - if (activeWorkspace == null) return; - toast.show({ - id: 'workspace-changed', - timeout: 3000, - message: ( - <> - Switched workspace to {activeWorkspace.name} - - ), - }); - }, [activeWorkspace, toast]); + useAtiveWorkspaceChangedToast(); const queryClient = useQueryClient(); const { wasUpdatedExternally } = useRequestUpdateKey(null); diff --git a/src-web/hooks/useAtiveWorkspaceChangedToast.tsx b/src-web/hooks/useAtiveWorkspaceChangedToast.tsx new file mode 100644 index 00000000..2cb9d749 --- /dev/null +++ b/src-web/hooks/useAtiveWorkspaceChangedToast.tsx @@ -0,0 +1,30 @@ +import { useEffect, useState } from 'react'; +import { InlineCode } from '../components/core/InlineCode'; +import { useToast } from '../components/ToastContext'; +import { useActiveWorkspace } from './useActiveWorkspace'; + +export function useAtiveWorkspaceChangedToast() { + const toast = useToast(); + const activeWorkspace = useActiveWorkspace(); + const [id, setId] = useState(activeWorkspace?.id ?? null); + + useEffect(() => { + // Early return if same or invalid active workspace + if (id === activeWorkspace?.id || activeWorkspace == null) return; + + setId(activeWorkspace?.id ?? null); + + // Don't notify on first load + if (id === null) return; + + toast.show({ + id: 'workspace-changed', + timeout: 3000, + message: ( + <> + Switched workspace to {activeWorkspace.name} + + ), + }); + }, [activeWorkspace, id, toast]); +}