-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataFormatConverter.tsx
More file actions
386 lines (358 loc) · 13 KB
/
DataFormatConverter.tsx
File metadata and controls
386 lines (358 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
'use client';
import { useToolState } from '@/components/providers/ToolStateProvider';
import { Button } from '@/components/ui/button';
import { CodePanel } from '@/components/ui/code-panel';
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import {
DEFAULT_MULTI_FORMAT_OPTIONS,
FORMAT_OPTIONS,
INDENT_OPTIONS,
INPUT_FORMAT_OPTIONS,
MULTI_FORMAT_EXAMPLES,
PYTHON_QUOTE_STYLE_OPTIONS,
type MultiFormatOptions
} from '@/config/data-format-converter-config';
import { useCodeEditorTheme } from '@/hooks/useCodeEditorTheme';
import {
convertFormat,
getConversionStats,
type FormatType,
type MultiFormatConversionResult
} from '@/libs/data-format-converter';
import { cn } from '@/libs/utils';
import { ArrowPathIcon, ChevronDownIcon } from '@heroicons/react/24/outline';
import { useEffect, useState } from 'react';
interface DataFormatConverterProps {
className?: string;
instanceId: string;
}
export function DataFormatConverter({ className, instanceId }: DataFormatConverterProps) {
const { toolState, updateToolState } = useToolState('data-format-converter', instanceId);
// Initialize with defaults to avoid hydration mismatch
const [options, setOptions] = useState<MultiFormatOptions>(DEFAULT_MULTI_FORMAT_OPTIONS);
const [input, setInput] = useState<string>('');
const [output, setOutput] = useState<string>('');
const [isConverting, setIsConverting] = useState(false);
const [error, setError] = useState<string>('');
const [stats, setStats] = useState<{
inputSize: number;
outputSize: number;
inputLines: number;
outputLines: number;
format: FormatType;
} | null>(null);
const [isHydrated, setIsHydrated] = useState(false);
// Editor settings
const [theme] = useCodeEditorTheme('basicDark');
const [inputWrapText, setInputWrapText] = useState(true);
const [outputWrapText, setOutputWrapText] = useState(true);
// Hydrate state from toolState after mount (client-side only)
useEffect(() => {
setIsHydrated(true);
if (toolState) {
if (toolState.options) setOptions(toolState.options as MultiFormatOptions);
if (toolState.input) setInput(toolState.input as string);
if (toolState.output) setOutput(toolState.output as string);
if (toolState.error) setError(toolState.error as string);
if (toolState.stats) setStats(toolState.stats as typeof stats);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Update persistent state whenever local state changes
useEffect(() => {
if (isHydrated) {
updateToolState({
options,
input,
output,
error,
stats: stats || undefined
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [options, input, output, error, stats, isHydrated]);
// Reset local state when tool state is cleared
useEffect(() => {
if (isHydrated && (!toolState || Object.keys(toolState).length === 0)) {
setOptions(DEFAULT_MULTI_FORMAT_OPTIONS);
setInput('');
setOutput('');
setError('');
setStats(null);
}
}, [toolState, isHydrated]);
const handleConvert = async () => {
if (!input.trim()) {
setError('Please enter content to convert');
setOutput('');
setStats(null);
return;
}
setIsConverting(true);
setError('');
try {
await new Promise(resolve => setTimeout(resolve, 300));
const result: MultiFormatConversionResult = convertFormat(
input,
options.inputFormat,
options.outputFormat,
options.indentSize,
options.pythonQuoteStyle
);
if (result.success) {
setOutput(result.output);
setStats(getConversionStats(input, result.output, result.format));
} else {
setError(result.error || 'Conversion failed');
setOutput('');
setStats(null);
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Conversion failed');
setOutput('');
setStats(null);
} finally {
setIsConverting(false);
}
};
const handleLoadExample = (example: typeof MULTI_FORMAT_EXAMPLES[0]) => {
// Load example based on input format
let exampleInput = '';
if (options.inputFormat === 'json') {
exampleInput = example.json;
} else if (options.inputFormat === 'yaml') {
exampleInput = example.yaml;
} else if (options.inputFormat === 'python') {
exampleInput = example.python;
} else if (options.inputFormat === 'typescript') {
exampleInput = example.typescript;
} else if (options.inputFormat === 'xml') {
exampleInput = example.xml || example.json;
} else {
// Default to JSON
exampleInput = example.json;
}
setInput(exampleInput);
setError('');
setOutput('');
setStats(null);
};
const getCharacterCount = (text: string): number => {
return text.length;
};
const getLineCount = (text: string): number => {
if (!text) return 0;
return text.split('\n').length;
};
// Get language for syntax highlighting
const getInputLanguage = (): string => {
if (options.inputFormat === 'python') {
return 'python';
}
if (options.inputFormat === 'typescript') {
return 'typescript';
}
if (options.inputFormat === 'xml') {
return 'xml';
}
return options.inputFormat;
};
const getOutputLanguage = (): string => {
if (options.outputFormat === 'python') {
return 'python';
}
if (options.outputFormat === 'typescript') {
return 'typescript';
}
if (options.outputFormat === 'xml') {
return 'xml';
}
return options.outputFormat;
};
return (
<div className={cn('flex flex-col h-full', className)}>
{/* Header Section */}
<div className="bg-background px-[28px] pt-[36px] pb-[20px]">
<h1 className="text-[32px] font-normal leading-6 tracking-normal text-neutral-900 dark:text-neutral-100 mb-3">
Data Format Converter
</h1>
<p className="text-sm leading-5 tracking-normal text-neutral-900 dark:text-neutral-100">
Convert between JSON, YAML, Python Dictionary, TypeScript Map, and XML formats. JSON is used as the common intermediate format.
</p>
</div>
{/* Body Section */}
<div className="flex-1 flex flex-col bg-background px-[24px] pt-6 pb-10 min-h-0 overflow-y-auto">
<div className="flex-1 flex flex-col gap-4 min-h-0">
{/* Controls */}
<div className="flex flex-col gap-4">
{/* Main Controls Row */}
<div className="flex items-center gap-3 flex-wrap">
{/* Input Format Select */}
<Select
value={options.inputFormat}
onValueChange={(value: FormatType) =>
setOptions(prev => ({ ...prev, inputFormat: value }))
}
>
<SelectTrigger label="Input Format:" className="min-w-[200px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
{INPUT_FORMAT_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
{/* Output Format Select */}
<Select
value={options.outputFormat}
onValueChange={(value: FormatType) =>
setOptions(prev => ({ ...prev, outputFormat: value }))
}
>
<SelectTrigger label="Output Format:" className="min-w-[200px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
{FORMAT_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
{/* Indent Size */}
<Select
value={options.indentSize.toString()}
onValueChange={(value) =>
setOptions(prev => ({ ...prev, indentSize: parseInt(value) }))
}
>
<SelectTrigger label="Indent Size:" className="min-w-[200px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
{INDENT_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value.toString()}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
{/* Python Quote Style - Only show when output format is Python */}
{options.outputFormat === 'python' && (
<Select
value={options.pythonQuoteStyle}
onValueChange={(value: 'single' | 'double') =>
setOptions(prev => ({ ...prev, pythonQuoteStyle: value }))
}
>
<SelectTrigger label="Quote Style:" className="min-w-[200px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
{PYTHON_QUOTE_STYLE_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
)}
</div>
</div>
{/* Side-by-side Editor Panels */}
<div className="flex-1 grid grid-cols-1 lg:grid-cols-2 gap-4 min-h-0">
{/* Input Panel */}
<CodePanel fillHeight={true}
title={`Input (${options.inputFormat.toUpperCase()})`}
value={input}
onChange={setInput}
language={getInputLanguage()}
height="500px"
theme={theme}
wrapText={inputWrapText}
onWrapTextChange={setInputWrapText}
showCopyButton={false}
showClearButton={true}
headerActions={
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
className="h-8 px-3 text-xs"
>
Load Examples
<ChevronDownIcon className="h-3 w-3 ml-1" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="max-h-[300px] overflow-y-auto">
{MULTI_FORMAT_EXAMPLES.map((example, index) => (
<DropdownMenuItem
key={index}
onClick={() => handleLoadExample(example)}
>
{example.name}
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
}
footerLeftContent={
<span>{getCharacterCount(input)} characters</span>
}
footerRightContent={
<Button
onClick={handleConvert}
disabled={!input.trim() || isConverting}
variant="default"
size="sm"
className="h-8 px-4"
>
{isConverting ? (
<>
<ArrowPathIcon className="h-4 w-4 animate-spin mr-2" />
Converting...
</>
) : (
'Convert'
)}
</Button>
}
/>
{/* Output Panel */}
<CodePanel fillHeight={true}
title={`Output (${options.outputFormat.toUpperCase()})`}
value={output}
language={getOutputLanguage()}
height="500px"
theme={theme}
wrapText={outputWrapText}
onWrapTextChange={setOutputWrapText}
footerLeftContent={
output && (
<>
<span>{getCharacterCount(output)} characters</span>
<span>{getLineCount(output)} lines</span>
</>
)
}
/>
</div>
{/* Error Display */}
{error && (
<div className="flex items-center space-x-2 p-4 bg-red-50 dark:bg-red-950/20 border border-red-200 dark:border-red-800 rounded-lg">
<div className="text-sm text-red-700 dark:text-red-300">
{error}
</div>
</div>
)}
</div>
</div>
</div>
);
}