diff --git a/src-web/components/GlobalHooks.tsx b/src-web/components/GlobalHooks.tsx index 1cc57451..effdbd1c 100644 --- a/src-web/components/GlobalHooks.tsx +++ b/src-web/components/GlobalHooks.tsx @@ -13,15 +13,23 @@ import { modelsEq } from '../lib/models'; import { useRecentRequests } from '../hooks/useRecentRequests'; import { useRecentWorkspaces } from '../hooks/useRecentWorkspaces'; import { useRecentEnvironments } from '../hooks/useRecentEnvironments'; +import { useLocation } from 'react-router-dom'; +import { useEffect } from 'react'; +import { setPathname } from '../lib/persistPathname'; export function GlobalHooks() { useRecentWorkspaces(); useRecentEnvironments(); useRecentRequests(); - const queryClient = useQueryClient(); const { wasUpdatedExternally } = useRequestUpdateKey(null); + // Listen for location changes and update the pathname + const location = useLocation(); + useEffect(() => { + setPathname(location.pathname); + }, [location.pathname]); + useListenToTauriEvent('created_model', ({ payload, windowLabel }) => { if (shouldIgnoreEvent(payload, windowLabel)) return; @@ -71,8 +79,9 @@ export function GlobalHooks() { } if (!shouldIgnoreModel(payload)) { - queryClient.setQueryData(queryKey, (values) => - values?.map((v) => (modelsEq(v, payload) ? payload : v)), + queryClient.setQueryData( + queryKey, + (values) => values?.map((v) => (modelsEq(v, payload) ? payload : v)), ); } }); diff --git a/src-web/lib/initPathnamePersistance.ts b/src-web/lib/initPathnamePersistance.ts deleted file mode 100644 index fbe4d922..00000000 --- a/src-web/lib/initPathnamePersistance.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { appWindow } from '@tauri-apps/api/window'; -import { NAMESPACE_NO_SYNC, getKeyValue, setKeyValue } from './keyValueStore'; - -const key = ['window_pathname', appWindow.label]; -const namespace = NAMESPACE_NO_SYNC; -const fallback = undefined; - -export async function initPathnamePersistance() { - if (window.location.pathname === '/') { - const pathname = await getKeyValue({ key, namespace, fallback }); - if (pathname != null) { - window.location.replace(pathname); - } - return; - } - - window.addEventListener('pushstate', async () => { - const { pathname: value } = window.location; - await setKeyValue({ key, namespace, value }); - }); -} diff --git a/src-web/lib/keyValueStore.ts b/src-web/lib/keyValueStore.ts index 0db31dc2..f34c6145 100644 --- a/src-web/lib/keyValueStore.ts +++ b/src-web/lib/keyValueStore.ts @@ -36,7 +36,7 @@ export async function getKeyValue({ return extractKeyValueOrFallback(kv, fallback); } -export function extractKeyValue(kv: KeyValue | null): T | undefined { +function extractKeyValue(kv: KeyValue | null): T | undefined { if (kv === null) return undefined; try { return JSON.parse(kv.value) as T; @@ -45,7 +45,7 @@ export function extractKeyValue(kv: KeyValue | null): T | undefined { } } -export function extractKeyValueOrFallback(kv: KeyValue | null, fallback: T): T { +function extractKeyValueOrFallback(kv: KeyValue | null, fallback: T): T { const v = extractKeyValue(kv); if (v === undefined) return fallback; return v; diff --git a/src-web/lib/persistPathname.ts b/src-web/lib/persistPathname.ts new file mode 100644 index 00000000..533195cc --- /dev/null +++ b/src-web/lib/persistPathname.ts @@ -0,0 +1,21 @@ +import { appWindow } from '@tauri-apps/api/window'; +import { NAMESPACE_NO_SYNC, getKeyValue, setKeyValue } from './keyValueStore'; + +const key = ['window_pathname', appWindow.label]; +const namespace = NAMESPACE_NO_SYNC; +const fallback = undefined; + +export async function setPathname(value: string) { + await setKeyValue({ key, namespace, value }); +} + +export async function maybeRestorePathname() { + if (window.location.pathname !== '/') { + return; + } + + const pathname = await getKeyValue({ key, namespace, fallback }); + if (pathname != null) { + window.location.replace(pathname); + } +} diff --git a/src-web/main.tsx b/src-web/main.tsx index aeb05978..2290a98d 100644 --- a/src-web/main.tsx +++ b/src-web/main.tsx @@ -4,9 +4,9 @@ import { App } from './components/App'; import { getKeyValue } from './lib/keyValueStore'; import { getPreferredAppearance, setAppearance } from './lib/theme/window'; import './main.css'; -import { initPathnamePersistance } from './lib/initPathnamePersistance'; +import { maybeRestorePathname } from './lib/persistPathname'; -await initPathnamePersistance(); +await maybeRestorePathname(); setAppearance( await getKeyValue({