diff --git a/src/components/ButtonImport.tsx b/src/components/ButtonImport.tsx index f3cd285..ee1d454 100644 --- a/src/components/ButtonImport.tsx +++ b/src/components/ButtonImport.tsx @@ -9,12 +9,11 @@ import { DialogContent, Button, } from "@fluentui/react-components"; -import { TooltipButton } from "./TooltipButton"; -import { ArrowImportRegular } from "@fluentui/react-icons"; import { makeStyles, tokens, useId, Label, Textarea } from "@fluentui/react-components"; import { LogTag, log } from "../core/log"; -import { isValidSnipExportJson } from "../core/Snip"; +import { SnipWithSource, completeSnip, getExportSnipFromExportJson, isValidSnipExportJson } from "../core/Snip"; +import { saveSnip } from "../core/snipStorage"; const useStyles = makeStyles({ base: { @@ -103,30 +102,61 @@ async function getImportSnip(value: string): Promise { return undefined; } -export function ButtonImport({ setImport }: { setImport: (value: string) => void }) { +async function importSnip(value: string): Promise { + console.log("Import snip"); + console.log(value); + const newSnip = getExportSnipFromExportJson(value); + console.log(newSnip); + if (newSnip) { + // create a new snip with the imported snip + const complete = completeSnip(newSnip); + const source = "local"; + complete.modified = Date.now(); + const importSnip: SnipWithSource = { ...complete, source }; + + const saved = await saveSnip(importSnip); + return saved; + } else { + console.error("import failed - invalid snip"); + } + return undefined; +} + +export function ButtonImport({ + openSnip, + children, +}: { + openSnip: (openSnip: SnipWithSource) => void; + children: JSX.Element; +}) { const textareaId = useId("import-textarea"); const styles = useStyles(); + async function doImport(value: string) { + const snip = await importSnip(value); + if (snip) { + openSnip(snip); + } + } + async function onClickImport(event: React.FormEvent) { event.preventDefault(); log(LogTag.ButtonImport, "import"); const value = (document.getElementById(textareaId) as HTMLTextAreaElement).value; const snipText = await getImportSnip(value); if (snipText) { - setImport(snipText); + doImport(snipText); } // otherwise invalid. } return ( - - } /> - + {children}
- Import Snip Json + Import Snip