refactor: port import/export functionality

This commit is contained in:
jamesgeorge007
2024-02-19 13:27:26 +05:30
parent 8483339005
commit 6ed9c09f06
11 changed files with 331 additions and 72 deletions

View File

@@ -1,32 +1,31 @@
import * as E from "fp-ts/Either"
import { platform } from "~/platform"
/**
* Create a downloadable file from a collection and prompts the user to download it.
* @param collectionJSON - JSON string of the collection
* @param name - Name of the collection set as the file name
*/
export const initializeDownloadCollection = (
export const initializeDownloadFile = async (
collectionJSON: string,
name: string | null
) => {
const file = new Blob([collectionJSON], { type: "application/json" })
const a = document.createElement("a")
const url = URL.createObjectURL(file)
a.href = url
const result = await platform.io.saveFileWithDialog({
data: collectionJSON,
contentType: "application/json",
suggestedFilename: `${name ?? "collection"}.json`,
filters: [
{
name: "Hoppscotch Collection/Environment JSON file",
extensions: ["json"],
},
],
})
if (name) {
a.download = `${name}.json`
} else {
a.download = `${url.split("/").pop()!.split("#")[0].split("?")[0]}.json`
if (result.type === "unknown" || result.type === "saved") {
return E.right("state.download_started")
}
document.body.appendChild(a)
a.click()
setTimeout(() => {
document.body.removeChild(a)
URL.revokeObjectURL(url)
}, 1000)
return E.right("state.download_started")
return E.left("state.download_failed")
}

View File

@@ -1,5 +0,0 @@
import { HoppCollection } from "@hoppscotch/data"
export const myCollectionsExporter = (myCollections: HoppCollection[]) => {
return JSON.stringify(myCollections, null, 2)
}