From d2b82d1d37c94d4f90abfeaf0fac15c0281ebf86 Mon Sep 17 00:00:00 2001 From: Mathieu Schimmerling Date: Tue, 5 Mar 2024 14:53:58 +0100 Subject: [PATCH] fix(app): fix initial frameworks selected handling --- src/Index.svelte | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Index.svelte b/src/Index.svelte index 464f39da..e5dab1b9 100644 --- a/src/Index.svelte +++ b/src/Index.svelte @@ -8,7 +8,7 @@ import CodeEditor from "./components/CodeEditor.svelte"; import AppNotificationCenter from "./components/AppNotificationCenter.svelte"; import createLocaleStorage from "./lib/createLocaleStorage.js"; - import { getContext, onMount } from "svelte"; + import { getContext, onDestroy, onMount } from "svelte"; import Header from "./components/Header.svelte"; import Aside from "./components/Aside.svelte"; import GithubIcon from "./components/GithubIcon.svelte"; @@ -48,16 +48,18 @@ let isMounted = false; const siteTitle = "Component Party"; - // -- on route change -- - $: { + const unsubscribeCurrentRoute = currentRoute.subscribe(($currentRoute) => { window.scrollTo(0, 0); isVersusFrameworks = false; document.title = siteTitle; + if ($currentRoute.path === "/") { if (isMounted) { - handleInitialFrameworkIdsSelectedFromStorage(); + handleInitialFrameworkIdsSelectedFromStorage({ useDefaults: false }); } else { - onMountCallbacks.add(handleInitialFrameworkIdsSelectedFromStorage); + onMountCallbacks.add(() => + handleInitialFrameworkIdsSelectedFromStorage({ useDefaults: true }) + ); } } else if ($currentRoute.params?.versus) { const versusFrameworks = handleVersus($currentRoute.params.versus); @@ -74,9 +76,11 @@ } else { navigate("/"); } - } + }); + + onDestroy(unsubscribeCurrentRoute); - function handleInitialFrameworkIdsSelectedFromStorage() { + function handleInitialFrameworkIdsSelectedFromStorage({ useDefaults }) { let frameworkIdsSelectedOnInit = []; const url = new URL(window.location.href); @@ -103,7 +107,7 @@ } } - if (frameworkIdsSelectedOnInit.length === 0) { + if (useDefaults && frameworkIdsSelectedOnInit.length === 0) { frameworkIdsSelectedOnInit = ["react", "svelte4"]; }