From 3738de3e3e6fcdfcfbe6faf601aa5bf4f6d07687 Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Mon, 22 Jan 2024 11:11:56 +0100 Subject: [PATCH] feat: adding router state persistence Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> --- .../backend/src/registries/RouterRegistry.ts | 36 +++++++++++++++++++ packages/backend/src/studio-api-impl.ts | 10 ++++++ packages/backend/src/studio.ts | 3 ++ packages/frontend/src/App.svelte | 8 +++++ packages/frontend/src/Route.svelte | 6 ++++ packages/shared/src/StudioAPI.ts | 4 +++ packages/shared/src/models/IRouterState.ts | 21 +++++++++++ 7 files changed, 88 insertions(+) create mode 100644 packages/backend/src/registries/RouterRegistry.ts create mode 100644 packages/shared/src/models/IRouterState.ts diff --git a/packages/backend/src/registries/RouterRegistry.ts b/packages/backend/src/registries/RouterRegistry.ts new file mode 100644 index 000000000..a97baad27 --- /dev/null +++ b/packages/backend/src/registries/RouterRegistry.ts @@ -0,0 +1,36 @@ +/********************************************************************** + * Copyright (C) 2024 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ***********************************************************************/ +import type { RouterState } from '@shared/src/models/IRouterState'; + +export class RouterRegistry { + private state: RouterState; + + constructor() { + this.state = { + url: '/', + }; + } + + getRouterState(): RouterState { + return this.state; + } + + setRouterState(state: RouterState) { + this.state = state; + } +} diff --git a/packages/backend/src/studio-api-impl.ts b/packages/backend/src/studio-api-impl.ts index a36f268e5..1d3c75e6d 100644 --- a/packages/backend/src/studio-api-impl.ts +++ b/packages/backend/src/studio-api-impl.ts @@ -31,6 +31,8 @@ import * as path from 'node:path'; import type { CatalogManager } from './managers/catalogManager'; import type { Catalog } from '@shared/src/models/ICatalog'; import type { PlaygroundState } from '@shared/src/models/IPlaygroundState'; +import type { RouterState } from '@shared/src/models/IRouterState'; +import type { RouterRegistry } from './registries/RouterRegistry'; export class StudioApiImpl implements StudioAPI { constructor( @@ -39,6 +41,7 @@ export class StudioApiImpl implements StudioAPI { private taskRegistry: TaskRegistry, private playgroundManager: PlayGroundManager, private catalogManager: CatalogManager, + private routerRegistry: RouterRegistry, ) {} async ping(): Promise { @@ -123,4 +126,11 @@ export class StudioApiImpl implements StudioAPI { async getCatalog(): Promise { return this.catalogManager.getCatalog(); } + + async saveRouterState(state: RouterState): Promise { + this.routerRegistry.setRouterState(state); + } + async getRouterState(): Promise { + return this.routerRegistry.getRouterState(); + } } diff --git a/packages/backend/src/studio.ts b/packages/backend/src/studio.ts index 15db733c0..86a3a1071 100644 --- a/packages/backend/src/studio.ts +++ b/packages/backend/src/studio.ts @@ -28,6 +28,7 @@ import * as fs from 'node:fs'; import { TaskRegistry } from './registries/TaskRegistry'; import { PlayGroundManager } from './managers/playground'; import { CatalogManager } from './managers/catalogManager'; +import { RouterRegistry } from './registries/RouterRegistry'; export class Studio { readonly #extensionContext: ExtensionContext; @@ -97,6 +98,7 @@ export class Studio { this.playgroundManager = new PlayGroundManager(this.#panel.webview); // Create catalog manager, responsible for loading the catalog files and watching for changes this.catalogManager = new CatalogManager(applicationManager.appUserDirectory, this.#panel.webview); + const routerRegister = new RouterRegistry(); // Creating StudioApiImpl this.studioApi = new StudioApiImpl( @@ -105,6 +107,7 @@ export class Studio { taskRegistry, this.playgroundManager, this.catalogManager, + routerRegister, ); await this.catalogManager.loadCatalog(); diff --git a/packages/frontend/src/App.svelte b/packages/frontend/src/App.svelte index 58ef74a43..9780b97bd 100644 --- a/packages/frontend/src/App.svelte +++ b/packages/frontend/src/App.svelte @@ -12,8 +12,16 @@ import Registries from '/@/pages/Registries.svelte'; import Models from '/@/pages/Models.svelte'; import Recipe from '/@/pages/Recipe.svelte'; import Model from './pages/Model.svelte'; +import { onMount } from 'svelte'; +import { studioClient } from '/@/utils/client'; router.mode.hash(); + +onMount(async () => { + // Load router state on application startup + const state = await studioClient.getRouterState(); + router.goto(state.url); +}); diff --git a/packages/frontend/src/Route.svelte b/packages/frontend/src/Route.svelte index 09f5dcbc9..d174c1ac0 100644 --- a/packages/frontend/src/Route.svelte +++ b/packages/frontend/src/Route.svelte @@ -1,6 +1,7 @@