-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-main.tsx
More file actions
110 lines (95 loc) · 3.21 KB
/
Copy pathjson-main.tsx
File metadata and controls
110 lines (95 loc) · 3.21 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
"use client"
import { useState, useCallback, useMemo, useRef, useEffect } from "react"
import { Footer } from "@/components/footer"
import { JsonInput } from "@/components/json-input"
import { JsonViewer } from "@/components/json-viewer"
import { Panel, PanelGroup, PanelResizeHandle } from "react-resizable-panels"
import { useDebounce } from "@/hooks/use-debounce"
export function JsonMain() {
const [mounted, setMounted] = useState(false)
const [input, setInput] = useState("")
const [searchQuery, setSearchQuery] = useState("")
const [viewMode, setViewMode] = useState<"tree" | "raw">("tree")
const [error, setError] = useState<string | null>(null)
const inputRef = useRef<HTMLTextAreaElement>(null)
const outputTreeRef = useRef<HTMLDivElement>(null)
const outputRawRef = useRef<HTMLPreElement>(null)
const debouncedInput = useDebounce(input, 300)
// Process JSON - simple parsing only
const processJson = useCallback((text: string) => {
try {
setError(null)
if (!text.trim()) {
return null
}
// Try to parse as valid JSON
return JSON.parse(text)
} catch (error) {
setError("Invalid JSON")
return null
}
}, [])
const parsedJson = useMemo(() => {
return processJson(debouncedInput)
}, [debouncedInput, processJson])
const handleCopyToClipboard = useCallback(() => {
if (parsedJson) {
navigator.clipboard.writeText(
viewMode === "raw" ? JSON.stringify(parsedJson) : JSON.stringify(parsedJson, null, 2),
)
}
}, [parsedJson, viewMode])
useEffect(() => {
setMounted(true)
}, [])
if (!mounted) {
return null
}
return (
<div className="flex-grow flex-col bg-background rounded-none">
{/* Main content area with better spacing */}
<div className="flex-1">
<PanelGroup direction="horizontal" className="h-full bg-card rounded-lg shadow-sm">
<Panel defaultSize={50} minSize={20}>
<div className="h-full">
<JsonInput
input={input}
setInput={setInput}
error={error}
inputRef={inputRef}
/>
</div>
</Panel>
<PanelResizeHandle className="w-0.5 bg-border hover:bg-primary transition-colors mx-4" />
<Panel minSize={30}>
<div className="h-full">
<JsonViewer
parsedJson={parsedJson}
viewMode={viewMode}
setViewMode={setViewMode}
searchQuery={searchQuery}
setSearchQuery={setSearchQuery}
handleCopyToClipboard={handleCopyToClipboard}
error={error}
outputTreeRef={outputTreeRef}
outputRawRef={outputRawRef}
/>
</div>
</Panel>
</PanelGroup>
</div>
{/* Footer at bottom */}
<div className="mt-4">
<Footer
mounted={mounted}
parsedJson={parsedJson}
viewMode={viewMode}
setViewMode={setViewMode}
searchQuery={searchQuery}
setSearchQuery={setSearchQuery}
handleCopyToClipboard={handleCopyToClipboard}
/>
</div>
</div>
)
}