diff --git a/src/components/ui/RecipeLibrary.tsx b/src/components/ui/RecipeLibrary.tsx index 6464741..c16a6f5 100644 --- a/src/components/ui/RecipeLibrary.tsx +++ b/src/components/ui/RecipeLibrary.tsx @@ -1,5 +1,5 @@ -import { useState } from 'react'; -import { Trash2 } from 'lucide-react'; +import { useRef, useState } from 'react'; +import { Download, Trash2, Upload } from 'lucide-react'; import { useStore } from '../../store/useStore'; import { t } from '../../i18n/en'; import { Button, IconButton } from './primitives'; @@ -10,12 +10,17 @@ export function RecipeLibrary() { const chain = useStore((s) => s.chain); const saved = useStore((s) => s.savedRecipes); const operations = useStore((s) => s.operations); + const savedRecipesReady = useStore((s) => s.savedRecipesReady); const saveCurrentRecipe = useStore((s) => s.saveCurrentRecipe); const loadSavedRecipe = useStore((s) => s.loadSavedRecipe); const removeSavedRecipe = useStore((s) => s.removeSavedRecipe); + const importRecipes = useStore((s) => s.importRecipes); + const exportRecipes = useStore((s) => s.exportRecipes); const setDialog = useStore((s) => s.setDialog); const [name, setName] = useState(''); + const [message, setMessage] = useState(''); + const fileInputRef = useRef(null); const describe = (opIds: string[]) => opIds @@ -32,6 +37,30 @@ export function RecipeLibrary() { setName(''); }; + const onExport = () => { + const blob = new Blob([exportRecipes()], { type: 'application/json' }); + const url = URL.createObjectURL(blob); + const link = document.createElement('a'); + link.href = url; + link.download = 'decodebox-recipes.json'; + link.click(); + URL.revokeObjectURL(url); + }; + + const onImport = async (file: File) => { + if (file.size > 1_000_000) { + setMessage('File is too large.'); + return; + } + try { + const text = await file.text(); + const result = importRecipes(text); + setMessage(result.error ?? `Imported ${result.imported}, skipped ${result.skipped}.`); + } catch { + setMessage('Could not read file.'); + } + }; + return ( +
+ + + { + const file = e.target.files?.[0]; + if (file) void onImport(file); + e.target.value = ''; + }} + /> +
+ + {message &&

{message}

} + {usingDetected && (

{t.library.detectedNote}{' '} @@ -93,6 +150,7 @@ export function RecipeLibrary() {