diff --git a/deploy-web/src/components/settings/LocalDataManager/LocalDataManager.component.tsx b/deploy-web/src/components/settings/LocalDataManager/LocalDataManager.component.tsx new file mode 100644 index 000000000..ce5868ed0 --- /dev/null +++ b/deploy-web/src/components/settings/LocalDataManager/LocalDataManager.component.tsx @@ -0,0 +1,61 @@ +import React, { FC, useCallback, useRef } from "react"; +import { Download, Upload } from "iconoir-react"; +import { Button } from "@src/components/ui/button"; + +export type LocalData = Record; + +interface LocalDataManagerProps { + read: () => LocalData; + write: (data: LocalData) => void; + onDone?: () => void; +} + +export const LocalDataManagerComponent: FC = ({ read, write, onDone }) => { + const ref = useRef(null); + + const triggerFileUpload = useCallback(() => { + ref.current?.click(); + }, [ref.current]); + + const importLocalData = useCallback((event: React.ChangeEvent) => { + const file = event.target.files?.[0]; + if (!file) return; + + const reader = new FileReader(); + reader.onload = () => { + const data = JSON.parse(reader.result as string); + write(data); + + if (typeof onDone === "function") { + onDone(); + } + }; + reader.readAsText(file); + }, []); + + const downloadLocalData = useCallback(() => { + const data = JSON.stringify(read()); + const blob = new Blob([data], { type: "application/json" }); + const url = URL.createObjectURL(blob); + const link = document.createElement("a"); + link.href = url; + link.download = "console.akash.network.data.json"; + link.click(); + }, []); + + return ( +
+ + + + + +
+ ); +}; diff --git a/deploy-web/src/components/settings/LocalDataManager/LocalDataManager.container.tsx b/deploy-web/src/components/settings/LocalDataManager/LocalDataManager.container.tsx new file mode 100644 index 000000000..773f013fc --- /dev/null +++ b/deploy-web/src/components/settings/LocalDataManager/LocalDataManager.container.tsx @@ -0,0 +1,18 @@ +import { LocalData, LocalDataManagerComponent } from "./LocalDataManager.component"; +import React, { useCallback } from "react"; + +export const LocalDataManagerContainer = () => { + const importLocalData = useCallback((data: LocalData) => { + Object.keys(data).forEach(key => { + localStorage.setItem(key, data[key]); + }); + }, []); + + const readLocalData = useCallback(() => { + return localStorage; + }, []); + + const reload = () => window.location.reload(); + + return ; +}; diff --git a/deploy-web/src/components/settings/LocalDataManager/index.ts b/deploy-web/src/components/settings/LocalDataManager/index.ts new file mode 100644 index 000000000..3251e0d7a --- /dev/null +++ b/deploy-web/src/components/settings/LocalDataManager/index.ts @@ -0,0 +1 @@ +export { LocalDataManagerContainer as LocalDataManager } from "./LocalDataManager.container"; diff --git a/deploy-web/src/components/settings/SettingsContainer.tsx b/deploy-web/src/components/settings/SettingsContainer.tsx index b9b1a741b..4c3b3816c 100644 --- a/deploy-web/src/components/settings/SettingsContainer.tsx +++ b/deploy-web/src/components/settings/SettingsContainer.tsx @@ -12,6 +12,7 @@ import { SelectNetworkModal } from "./SelectNetworkModal"; import { SettingsForm } from "./SettingsForm"; import { ColorModeSelect } from "./ColorModeSelect"; import { NextSeo } from "next-seo"; +import { LocalDataManager } from "@src/components/settings/LocalDataManager"; type Props = {}; @@ -48,6 +49,7 @@ export const SettingsContainer: React.FunctionComponent = ({}) => {
+
diff --git a/deploy-web/src/components/settings/SettingsLayout.tsx b/deploy-web/src/components/settings/SettingsLayout.tsx index 2172218b5..b7cc21d74 100644 --- a/deploy-web/src/components/settings/SettingsLayout.tsx +++ b/deploy-web/src/components/settings/SettingsLayout.tsx @@ -46,7 +46,7 @@ export const SettingsLayout: React.FunctionComponent = ({ children, page, -
+
{title} {headerActions}