-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(setting): implement basic local storage data exporter/importer
refs #56
- Loading branch information
1 parent
fd781cd
commit 34b1cf4
Showing
5 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
deploy-web/src/components/settings/LocalDataManager/LocalDataManager.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, any>; | ||
|
||
interface LocalDataManagerProps { | ||
read: () => LocalData; | ||
write: (data: LocalData) => void; | ||
onDone?: () => void; | ||
} | ||
|
||
export const LocalDataManagerComponent: FC<LocalDataManagerProps> = ({ read, write, onDone }) => { | ||
const ref = useRef<HTMLInputElement>(null); | ||
|
||
const triggerFileUpload = useCallback(() => { | ||
ref.current?.click(); | ||
}, [ref.current]); | ||
|
||
const importLocalData = useCallback((event: React.ChangeEvent<HTMLInputElement>) => { | ||
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 ( | ||
<div className="grid-col-1 mt-4 grid gap-4 md:grid-cols-2"> | ||
<Button onClick={downloadLocalData} size="sm" className="mt-6"> | ||
<Download className="text-sm" /> | ||
Export Local Data | ||
</Button> | ||
|
||
<input onChange={importLocalData} type="file" ref={ref} hidden accept=".json" /> | ||
|
||
<Button onClick={triggerFileUpload} size="sm" className="mt-6"> | ||
<Upload className="text-sm" /> | ||
Import Local Data | ||
</Button> | ||
</div> | ||
); | ||
}; |
18 changes: 18 additions & 0 deletions
18
deploy-web/src/components/settings/LocalDataManager/LocalDataManager.container.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <LocalDataManagerComponent read={readLocalData} write={importLocalData} onDone={reload} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { LocalDataManagerContainer as LocalDataManager } from "./LocalDataManager.container"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters