From b2150181b363b2c0b6087176917d7cfcdc872023 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sat, 28 Oct 2023 21:23:46 -0700 Subject: [PATCH] Persist window paths --- src-web/components/AppRouter.tsx | 8 +++---- .../components/core/Editor/twig/completion.ts | 2 +- .../core/Editor/twig/placeholder.ts | 4 +++- src-web/lib/initPathnamePersistance.ts | 21 +++++++++++++++++++ src-web/main.tsx | 11 ++++++++-- 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 src-web/lib/initPathnamePersistance.ts diff --git a/src-web/components/AppRouter.tsx b/src-web/components/AppRouter.tsx index 06504696..f3d61137 100644 --- a/src-web/components/AppRouter.tsx +++ b/src-web/components/AppRouter.tsx @@ -3,11 +3,11 @@ import { routePaths, useAppRoutes } from '../hooks/useAppRoutes'; import { useRecentRequests } from '../hooks/useRecentRequests'; import { useRequests } from '../hooks/useRequests'; import { GlobalHooks } from './GlobalHooks'; -import RouteError from './RouteError'; import Workspace from './Workspace'; import Workspaces from './Workspaces'; import { DialogProvider } from './DialogContext'; -import { useRecentEnvironments } from '../hooks/useRecentEnvironments'; +import { useActiveEnvironmentId } from '../hooks/useActiveEnvironmentId'; +import RouteError from './RouteError'; const router = createBrowserRouter([ { @@ -48,7 +48,7 @@ export function AppRouter() { function WorkspaceOrRedirect() { const recentRequests = useRecentRequests(); - const recentEnvironments = useRecentEnvironments(); + const activeEnvironmentId = useActiveEnvironmentId(); const requests = useRequests(); const request = requests.find((r) => r.id === recentRequests[0]); const routes = useAppRoutes(); @@ -57,8 +57,8 @@ function WorkspaceOrRedirect() { return ; } - const environmentId = recentEnvironments[0]; const { id: requestId, workspaceId } = request; + const environmentId = activeEnvironmentId ?? undefined; return ( ({ 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/main.tsx b/src-web/main.tsx index 7ffce4b8..aeb05978 100644 --- a/src-web/main.tsx +++ b/src-web/main.tsx @@ -4,10 +4,17 @@ 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'; -setAppearance(await getKeyValue({ key: 'appearance', fallback: getPreferredAppearance() })); +await initPathnamePersistance(); + +setAppearance( + await getKeyValue({ + key: 'appearance', + fallback: getPreferredAppearance(), + }), +); -// root holds our app's root DOM Element: createRoot(document.getElementById('root') as HTMLElement).render(