1
- import { useEffect } from "react" ;
1
+ import { useEffect , useMemo } from "react" ;
2
2
import { JsonViewer } from "@textea/json-viewer" ;
3
3
import { Sidebar } from "./components/sidebar" ;
4
4
import { Input } from "@/components/ui/input" ;
@@ -20,21 +20,17 @@ export default function App() {
20
20
if ( apiVersion === "v1" ) Y . applyUpdate ( ydoc , buf ) ;
21
21
if ( apiVersion === "v2" ) Y . applyUpdateV2 ( ydoc , buf ) ;
22
22
}
23
- displayOptionStore . setState ( { display : ydoc } ) ;
23
+ displayOptionStore . setState ( { display : ydoc , displayError : null } ) ;
24
24
// @ts -expect-error expose ydoc for advance debug
25
25
window . currentDoc = ydoc ;
26
26
} catch ( e ) {
27
- displayOptionStore . setState ( { display : e } ) ;
27
+ // @ts -expect-error expose ydoc for advance debug
28
+ window . currentDoc = null ;
29
+ ydoc . destroy ( ) ;
30
+ displayOptionStore . setState ( { display : null , displayError : e } ) ;
28
31
}
29
32
30
- if (
31
- typeof prev === "object" &&
32
- prev &&
33
- "destroy" in prev &&
34
- typeof prev . destroy === "function"
35
- ) {
36
- prev . destroy ( ) ;
37
- }
33
+ if ( prev && prev . destroy ) prev . destroy ( ) ;
38
34
} , [ inputBuffer , apiVersion ] ) ;
39
35
40
36
return (
@@ -50,17 +46,19 @@ export default function App() {
50
46
}
51
47
className = "mb-4 bg-gray-800 text-white border-gray-700"
52
48
/>
53
- < div className = "flex-1 overflow-auto bg-gray-800 rounded-lg p-4 [&>div]:px-4 [&>div]:py-2" >
54
- < Viewer />
49
+ < div className = "flex-1 gap-4 grid grid-cols-2 overflow-auto bg-gray-800 rounded-lg p-4 [&>div]:px-4 [&>div]:py-2" >
50
+ < MainViewer />
51
+ < SideViewer />
55
52
</ div >
56
53
</ main >
57
54
</ div >
58
55
) ;
59
56
}
60
57
61
- function Viewer ( ) {
58
+ function MainViewer ( ) {
62
59
const {
63
60
display,
61
+ displayError,
64
62
indentWidth,
65
63
enableClipboard,
66
64
displaySize,
@@ -69,7 +67,46 @@ function Viewer() {
69
67
return (
70
68
< JsonViewer
71
69
theme = "dark"
72
- value = { display }
70
+ value = { displayError ?? display }
71
+ indentWidth = { indentWidth }
72
+ enableClipboard = { enableClipboard }
73
+ displayDataTypes = { displayDataTypes }
74
+ displaySize = { displaySize }
75
+ defaultInspectDepth = { 3 }
76
+ />
77
+ ) ;
78
+ }
79
+
80
+ function SideViewer ( ) {
81
+ const {
82
+ display,
83
+ displayError,
84
+ indentWidth,
85
+ enableClipboard,
86
+ displaySize,
87
+ displayDataTypes,
88
+ } = useStore ( displayOptionStore ) ;
89
+ const getApiType = useStore ( yDocOptionStore , ( s ) => s . getApiType ) ;
90
+ const contentDisplay = useMemo ( ( ) => {
91
+ if ( ! display ) return null ;
92
+ try {
93
+ const ydoc = new Y . Doc ( ) ;
94
+ Y . applyUpdateV2 ( ydoc , Y . encodeStateAsUpdateV2 ( display ) ) ;
95
+ const displayObj : Record < string , unknown > = { } ;
96
+ for ( const key of ydoc . share . keys ( ) ) {
97
+ displayObj [ key ] = ydoc [ getApiType ] ( key ) ;
98
+ }
99
+ ydoc . destroy ( ) ;
100
+ return displayObj ;
101
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
102
+ } catch ( e ) {
103
+ return null ;
104
+ }
105
+ } , [ display , getApiType ] ) ;
106
+ return (
107
+ < JsonViewer
108
+ theme = "dark"
109
+ value = { displayError ?? contentDisplay }
73
110
indentWidth = { indentWidth }
74
111
enableClipboard = { enableClipboard }
75
112
displayDataTypes = { displayDataTypes }
0 commit comments