/** * "Export as recipe": choose which IDs of a Blockly script become inputs and download the recipe. */ import React, { useEffect, useMemo, useState } from 'react'; import { Alert, Box, Button, Checkbox, Chip, Dialog, DialogActions, DialogContent, DialogTitle, LinearProgress, Link, Table, TableBody, TableCell, TableHead, TableRow, TextField, Typography, } from '@mui/material'; import { Cancel as IconCancel, Download as IconDownload } from '@mui/icons-material'; import { type AdminConnection, I18n } from '@iobroker/gui-components'; import './i18n'; import { loadCatalog } from './catalog'; import { loadObjects } from './devices'; import { buildRecipe, checkItems, hasBraces, recipeId, suggestItems, type ExportItem } from './exportRecipe'; interface ExportDialogProps { socket: AdminConnection; instanceId: string; /** The Blockly XML of the script */ xml: string; scriptName: string; onClose: () => void; } export default function ExportDialog(props: ExportDialogProps): React.JSX.Element { const lang = I18n.getLanguage(); const [items, setItems] = useState(null); const [error, setError] = useState(''); const [version, setVersion] = useState('8.0.0'); const [titleEn, setTitleEn] = useState(props.scriptName); const [titleDe, setTitleDe] = useState(props.scriptName); const [id, setId] = useState(''); const [descriptionEn, setDescriptionEn] = useState(''); const [descriptionDe, setDescriptionDe] = useState(''); const [author, setAuthor] = useState(''); useEffect(() => { loadObjects(props.socket) .then(objects => setItems(suggestItems(props.xml, objects, lang))) .catch((e: unknown) => setError(String(e))); void loadCatalog(props.socket, props.instanceId).then(answer => answer.version && setVersion(answer.version)); }, [props.socket, props.instanceId, props.xml, lang]); const recipeIdValue = id || recipeId(titleEn); const problems = useMemo(() => { const list = items ? checkItems(items) : []; if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(recipeIdValue)) { list.push(I18n.t('recipe_export_bad_id')); } if (!titleEn.trim() || !titleDe.trim() || !descriptionEn.trim() || !descriptionDe.trim()) { list.push(I18n.t('recipe_export_texts')); } if (!/^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/.test(author)) { list.push(I18n.t('recipe_export_author')); } return list; }, [items, recipeIdValue, titleEn, titleDe, descriptionEn, descriptionDe, author]); const change = (index: number, patch: Partial): void => setItems(items!.map((item, i) => (i === index ? { ...item, ...patch } : item))); const download = (): void => { const recipe = buildRecipe(props.xml, items || [], { id: recipeIdValue, title: { en: titleEn.trim(), de: titleDe.trim() }, description: { en: descriptionEn.trim(), de: descriptionDe.trim() }, author, minJavascript: version.replace(/-.*$/, ''), }); const blob = new Blob([`${JSON.stringify(recipe, null, 4)}\n`], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${recipeIdValue}.json`; document.body.appendChild(a); a.click(); a.remove(); setTimeout(() => URL.revokeObjectURL(url), 1000); }; return ( false} maxWidth="lg" fullWidth > {I18n.t('recipe_export_title')} {I18n.t('recipe_export_info')}{' '} CONTRIBUTING.md {hasBraces(props.xml) ? {I18n.t('recipe_export_braces')} : null} setTitleEn(e.target.value)} /> setTitleDe(e.target.value)} /> setDescriptionEn(e.target.value)} /> setDescriptionDe(e.target.value)} /> setId(e.target.value)} helperText={`recipes/${recipeIdValue}.json`} /> setAuthor(e.target.value.trim())} /> {I18n.t('recipe_export_ids')} {error ? {error} : null} {!items && !error ? : null} {items && !items.length ? ( {I18n.t('recipe_export_no_ids')} ) : null} {items?.length ? ( {I18n.t('recipe_export_input')} {I18n.t('recipe_export_state')} {I18n.t('recipe_export_becomes')} {I18n.t('recipe_export_input_name')} {items.map((item, index) => ( change(index, { use: e.target.checked })} />
{item.name}
{item.id}
{item.device ? ( change(index, { kind: 'device' })} /> change(index, { kind: 'state' })} /> ) : ( )} change(index, { input: e.target.value.trim() })} />
))}
) : null} {problems.length ? ( {problems.map(problem => (
{problem}
))}
) : null}
); }