From 1547c3646521e45018b26391bf5343348beb265a Mon Sep 17 00:00:00 2001 From: Gregory Gridin Date: Fri, 16 Aug 2024 10:29:48 -0700 Subject: [PATCH] Fix GetLink-Tutorial-playground-state issue 1558 --- playground/src/editor.tsx | 12 ++++++------ playground/src/main.tsx | 11 ++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/playground/src/editor.tsx b/playground/src/editor.tsx index 8f530a653f..360580530d 100644 --- a/playground/src/editor.tsx +++ b/playground/src/editor.tsx @@ -362,15 +362,15 @@ export function Editor(props: { try { const encodedCode = await codeToCompressedBase64(code); const escapedCode = encodeURIComponent(encodedCode); - // Get current URL without query parameters to use as the base URL - const newUrl = `${ - window.location.href.split("?")[0] - }?code=${escapedCode}&profile=${profile}`; + // Update or add the current URL parameters 'code' and 'profile' + const newURL = new URL(window.location.href); + newURL.searchParams.set("code", escapedCode); + newURL.searchParams.set("profile", profile); // Copy link to clipboard and update url without reloading the page - navigator.clipboard.writeText(newUrl); + navigator.clipboard.writeText(newURL.toString()); - window.history.pushState({}, "", newUrl); + window.history.pushState({}, "", newURL.toString()); messageText = "Link was copied to the clipboard"; } finally { const popup = document.getElementById("popup") as HTMLDivElement; diff --git a/playground/src/main.tsx b/playground/src/main.tsx index 0d6066e792..5a464c975d 100644 --- a/playground/src/main.tsx +++ b/playground/src/main.tsx @@ -141,11 +141,12 @@ function App(props: { katas: Kata[]; linkedCode?: string }) { function onNavItemSelected(name: string) { // If there was a ?code link on the URL before, clear it out - const params = new URLSearchParams(window.location.search); - if (params.get("code")) { - // Get current URL without query parameters to use as the URL - const newUrl = `${window.location.href.split("?")[0]}`; - window.history.pushState({}, "", newUrl); + const newURL = new URL(window.location.href); + if (newURL.searchParams.get("code")) { + newURL.searchParams.delete("code"); + newURL.searchParams.delete("profile"); + window.history.pushState({}, "", newURL.toString()); + props.linkedCode = undefined; } setCurrentNavItem(name); }