import { useState } from 'react'; import ToolContent from '@components/ToolContent'; import ToolTextInput from '@components/input/ToolTextInput'; import ToolTextResult from '@components/result/ToolTextResult'; import { convertJsonToXml } from './service'; import { CardExampleType } from '@components/examples/ToolExamples'; import { ToolComponentProps } from '@tools/defineTool'; import { Box } from '@mui/material'; import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; import SimpleRadio from '@components/options/SimpleRadio'; type InitialValuesType = { indentationType: 'space' | 'tab' | 'none'; addMetaTag: boolean; }; const initialValues: InitialValuesType = { indentationType: 'space', addMetaTag: false }; const exampleCards: CardExampleType[] = [ { title: 'Basic JSON to XML', description: 'Convert a simple JSON object into an XML format.', sampleText: ` { "users": [ { "name": "John", "age": 30, "city": "New York" }, { "name": "Alice", "age": 25, "city": "London" } ] }`, sampleResult: ` \t \t\tJohn \t\t30 \t\tNew York \t \t \t\tAlice \t\t25 \t\tLondon \t `, sampleOptions: { ...initialValues } } ]; export default function JsonToXml({ title }: ToolComponentProps) { const [input, setInput] = useState(''); const [result, setResult] = useState(''); const compute = (values: InitialValuesType, input: string) => { if (input) { try { const xmlResult = convertJsonToXml(input, values); setResult(xmlResult); } catch (error) { setResult( `Error: ${ error instanceof Error ? error.message : 'Invalid Json format' }` ); } } }; return ( } resultComponent={ } getGroups={({ values, updateField }) => [ { title: 'Output XML Indentation', component: ( updateField('indentationType', 'space')} /> updateField('indentationType', 'tab')} /> updateField('indentationType', 'none')} /> ) }, { title: 'XML Meta Information', component: ( updateField('addMetaTag', value)} title="Add an XML Meta Tag" description="Add a meta tag at the beginning of the XML output." /> ) } ]} /> ); }