8
8
InputLeftElement ,
9
9
useColorMode ,
10
10
} from "@chakra-ui/react" ;
11
+ import { EJSON } from "bson" ;
11
12
import { t } from "i18next" ;
12
13
import { throttle } from "lodash" ;
13
14
@@ -33,6 +34,19 @@ import "./index.css";
33
34
34
35
import useGlobalStore from "@/pages/globalStore" ;
35
36
37
+ function stringifyEJSONAndFormat ( data : any ) {
38
+ // get stringified EJSON
39
+ const ejson = EJSON . stringify ( data ) ;
40
+
41
+ // parse ejson string to json object
42
+ const parsed = JSON . parse ( ejson ) ;
43
+
44
+ // format json object
45
+ const formatted = JSON . stringify ( parsed , null , 2 ) ;
46
+
47
+ return formatted ;
48
+ }
49
+
36
50
export default function DataPanel ( ) {
37
51
const [ currentData , setCurrentData ] = useState < any > ( { data : undefined , record : "{}" } ) ;
38
52
const globalStore = useGlobalStore ( ) ;
@@ -133,7 +147,7 @@ export default function DataPanel() {
133
147
const handleData = async ( ) => {
134
148
let params = { } ;
135
149
try {
136
- params = JSON . parse ( currentData . record ) ;
150
+ params = EJSON . parse ( currentData . record ) as any ;
137
151
if ( Object . keys ( params ) . length === 0 ) {
138
152
globalStore . showError ( t ( "DataEntry.CreateError" ) ) ;
139
153
return ;
@@ -273,21 +287,18 @@ export default function DataPanel() {
273
287
} }
274
288
deleteRuleMutation = { deleteDataMutation }
275
289
component = { ( item : any ) => {
276
- return (
277
- < JSONViewer
278
- colorMode = { colorMode }
279
- code = { JSON . stringify ( item , null , 2 ) }
280
- className = "dataList"
281
- />
282
- ) ;
290
+ const code = JSON . stringify ( item , null , 2 ) ;
291
+ return < JSONViewer colorMode = { colorMode } code = { code } className = "dataList" /> ;
283
292
} }
284
293
toolComponent = { ( item : any ) => {
285
294
const newData = { ...item } ;
286
295
delete newData . _id ;
296
+
297
+ const text = stringifyEJSONAndFormat ( newData ) ;
287
298
return (
288
299
< CopyText
289
300
hideToolTip
290
- text = { JSON . stringify ( newData , null , 2 ) }
301
+ text = { text }
291
302
tip = { String ( t ( "Copied" ) ) }
292
303
className = "ml-2 hover:bg-gray-200"
293
304
>
@@ -312,7 +323,7 @@ export default function DataPanel() {
312
323
< div className = "mb-4 flex-1 rounded" >
313
324
< JSONEditor
314
325
colorMode = { colorMode }
315
- value = { JSON . stringify ( currentData . data || { } , null , 2 ) }
326
+ value = { stringifyEJSONAndFormat ( currentData . data || { } ) }
316
327
onChange = { ( values ) => {
317
328
setCurrentData ( ( pre : any ) => {
318
329
return {
0 commit comments