import React, { useState } from 'react'; import { Box } from '@mui/material'; import ToolContent from '@components/ToolContent'; import ToolTextInput from '@components/input/ToolTextInput'; import ToolTextResult from '@components/result/ToolTextResult'; import { SplitOperatorType, wrapList } from './service'; import { CardExampleType } from '@components/examples/ToolExamples'; import { ToolComponentProps } from '@tools/defineTool'; import { GetGroupsType } from '@components/options/ToolOptions'; import TextFieldWithDesc from '@components/options/TextFieldWithDesc'; import SimpleRadio from '@components/options/SimpleRadio'; import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; import * as Yup from 'yup'; import { useTranslation } from 'react-i18next'; interface InitialValuesType { splitOperatorType: SplitOperatorType; splitSeparator: string; joinSeparator: string; deleteEmptyItems: boolean; left: string; right: string; } const initialValues: InitialValuesType = { splitOperatorType: 'symbol', splitSeparator: ',', joinSeparator: ',', deleteEmptyItems: true, left: '"', right: '"' }; const validationSchema = Yup.object({ splitSeparator: Yup.string().required('The separator is required'), joinSeparator: Yup.string().required('The join separator is required') }); const exampleCards: CardExampleType[] = [ { title: 'Wrap list items with quotes', description: 'This example shows how to wrap each item in a list with quotes.', sampleText: 'apple,banana,orange', sampleResult: '"apple","banana","orange"', sampleOptions: { splitOperatorType: 'symbol', splitSeparator: ',', joinSeparator: ',', deleteEmptyItems: true, left: '"', right: '"' } }, { title: 'Wrap list items with brackets', description: 'This example shows how to wrap each item in a list with brackets.', sampleText: 'item1,item2,item3', sampleResult: '[item1],[item2],[item3]', sampleOptions: { splitOperatorType: 'symbol', splitSeparator: ',', joinSeparator: ',', deleteEmptyItems: true, left: '[', right: ']' } }, { title: 'Wrap list items with custom text', description: 'This example shows how to wrap each item with different text on each side.', sampleText: 'apple,banana,orange', sampleResult: 'prefix-apple-suffix,prefix-banana-suffix,prefix-orange-suffix', sampleOptions: { splitOperatorType: 'symbol', splitSeparator: ',', joinSeparator: ',', deleteEmptyItems: true, left: 'prefix-', right: '-suffix' } } ]; export default function Wrap({ title }: ToolComponentProps) { const { t } = useTranslation(); const [input, setInput] = useState(''); const [result, setResult] = useState(''); const compute = (optionsValues: InitialValuesType, input: string) => { if (input) { try { setResult( wrapList( optionsValues.splitOperatorType, input, optionsValues.splitSeparator, optionsValues.joinSeparator, optionsValues.deleteEmptyItems, optionsValues.left, optionsValues.right ) ); } catch (error) { if (error instanceof Error) { setResult(`Error: ${error.message}`); } else { setResult('An unknown error occurred'); } } } }; const getGroups: GetGroupsType = ({ values, updateField }) => [ { title: t('list.wrap.splitOptions'), component: ( updateField('splitOperatorType', 'symbol')} checked={values.splitOperatorType === 'symbol'} title={t('list.wrap.splitBySymbol')} /> updateField('splitOperatorType', 'regex')} checked={values.splitOperatorType === 'regex'} title={t('list.wrap.splitByRegex')} /> updateField('splitSeparator', val)} description={t('list.wrap.splitSeparatorDescription')} /> updateField('joinSeparator', val)} description={t('list.wrap.joinSeparatorDescription')} /> updateField('deleteEmptyItems', checked)} title={t('list.wrap.removeEmptyItems')} /> ) }, { title: t('list.wrap.wrapOptions'), component: ( updateField('left', val)} description={t('list.wrap.leftTextDescription')} /> updateField('right', val)} description={t('list.wrap.rightTextDescription')} /> ) } ]; return ( } resultComponent={ } initialValues={initialValues} getGroups={getGroups} validationSchema={validationSchema} toolInfo={{ title: t('list.wrap.toolInfo.title'), description: t('list.wrap.toolInfo.description') }} exampleCards={exampleCards} input={input} setInput={setInput} compute={compute} /> ); }