From cfffc7fd4e271985f1bf1c56781f4b8192814ce4 Mon Sep 17 00:00:00 2001 From: Redm4x <2829180+Redm4x@users.noreply.github.com> Date: Tue, 14 May 2024 15:20:57 -0400 Subject: [PATCH] feat(deploy-web): add localstorage export page --- deploy-web/src/pages/_app.tsx | 7 ++++- .../pages/standalone/localstorage-export.tsx | 30 +++++++++++++++++++ deploy-web/src/utils/localStorage.ts | 11 ++++++- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 deploy-web/src/pages/standalone/localstorage-export.tsx diff --git a/deploy-web/src/pages/_app.tsx b/deploy-web/src/pages/_app.tsx index e4c212274..3e10089e0 100644 --- a/deploy-web/src/pages/_app.tsx +++ b/deploy-web/src/pages/_app.tsx @@ -46,7 +46,7 @@ Router.events.on("routeChangeError", () => NProgress.done()); // Client-side cache, shared for the whole session of the user in the browser. const clientSideEmotionCache = createEmotionCache(); -const App: React.FunctionComponent = ({ Component, pageProps, emotionCache = clientSideEmotionCache }) => { +const App: React.FunctionComponent = ({ Component, pageProps, emotionCache = clientSideEmotionCache, router }) => { usePreviousRoute(); useEffect(() => { @@ -63,6 +63,11 @@ const App: React.FunctionComponent = ({ Component, pageProps, emotionCach }; }, []); + // Do not wrap with layout if standalone page + if (router.pathname.startsWith("/standalone/")) { + return ; + } + return ( <> diff --git a/deploy-web/src/pages/standalone/localstorage-export.tsx b/deploy-web/src/pages/standalone/localstorage-export.tsx new file mode 100644 index 000000000..cf0f64d7a --- /dev/null +++ b/deploy-web/src/pages/standalone/localstorage-export.tsx @@ -0,0 +1,30 @@ +"use client"; + +import { extractLocalStorageData } from "@src/utils/localStorage"; +import { useEffect } from "react"; + +const validExternalDomain = "https://console.akash.network"; + +export default function Page() { + useEffect(() => { + exportLocalStorageData(); + }, []); + + function exportLocalStorageData() { + if (window.parent === window) { + console.log(`${window.location.origin} => No parent window found`); + return; + } + + const data = extractLocalStorageData(); + + console.log(`${window.location.origin} => Sending localstorage data to ${validExternalDomain}`); + window.parent.postMessage(data, { targetOrigin: validExternalDomain }); + } + + return ( +
+

Exporting local storage data to {validExternalDomain}

+
+ ); +} diff --git a/deploy-web/src/utils/localStorage.ts b/deploy-web/src/utils/localStorage.ts index 5bd66f90c..be736b137 100644 --- a/deploy-web/src/utils/localStorage.ts +++ b/deploy-web/src/utils/localStorage.ts @@ -1,6 +1,6 @@ import getConfig from "next/config"; import { gt, neq } from "semver"; -import { mainnetId } from "./constants"; +import { mainnetId, sandboxId, testnetId } from "./constants"; const { publicRuntimeConfig } = getConfig(); const migrations = { @@ -44,3 +44,12 @@ export const migrateLocalStorage = () => { // Update the latestUpdatedVersion localStorage.setItem("latestUpdatedVersion", currentVersion); }; + +export function extractLocalStorageData() { + const networks = [mainnetId, testnetId, sandboxId]; + + const allKeys = Object.keys(localStorage); + const filteredKeys = allKeys.filter(key => networks.some(network => key.startsWith(network + "/"))); + + return filteredKeys.reduce((acc, key) => ({ ...acc, [key]: localStorage.getItem(key) }), {} as { [key: string]: string }); +}