1
- import React , { useState , useEffect , useRef } from "react" ;
1
+ import React , { useState , useEffect , useRef } from "react" ;
2
2
import { JSONGraph as Graph } from "jsongraph-react" ;
3
3
import { fileExtensions } from "../constants" ;
4
4
const fs = acode . require ( "fsOperation" ) ;
5
5
6
6
const Jsoncrack = ( {
7
7
graphRef,
8
- options : {
9
- theme,
10
- direction,
11
- hideChildrenCount,
12
- hideCollapseButton,
13
- collapseNodes
14
- } ,
8
+ options : { theme, direction, hideChildrenCount, hideCollapseButton } ,
15
9
fileName,
16
10
uri
17
11
} ) => {
18
- const [ fileContent , setFileContent ] = useState ( [ ] ) ;
12
+ const [ fileContent , setFileContent ] = useState ( "" ) ;
19
13
const [ readeState , setReadeState ] = useState ( false ) ;
14
+ const [ isTooLarge , setIsTooLarge ] = useState ( false ) ;
20
15
21
16
const currentFileRef = useRef ( null ) ; // Track the current file being read
22
17
23
18
async function readFileInChunks ( ) {
24
- let chunkSize = 100 * 824 ;
25
19
const file = editorManager . activeFile ;
26
20
const fileContentArray = [ ] ;
27
21
let offset = 0 ;
28
22
29
23
try {
24
+ // Read the entire file
30
25
const content = await fs ( file . uri ) . readFile ( "utf-8" ) ;
26
+ const fileStat = await fs ( uri ) . stat ( ) ;
27
+
28
+ setIsTooLarge ( fileStat . length > 200000 ) ;
29
+
31
30
const fileSize = content . length ;
32
- chunkSize = fileSize / 2 ;
31
+ let chunkSize = fileSize / 5 ;
33
32
34
33
while ( offset < fileSize ) {
35
- if ( file !== currentFileRef . current ) {
34
+ if ( file !== currentFileRef . current || isTooLarge ) {
36
35
console . log ( "File switched, stopping read process..." ) ;
37
36
return ;
38
37
}
@@ -42,7 +41,7 @@ const Jsoncrack = ({
42
41
43
42
offset += chunkSize ;
44
43
45
- setFileContent ( prev => [ ... prev , chunk ] ) ;
44
+ setFileContent ( prev => prev + chunk ) ;
46
45
console . log ( `Processed ${ offset } of ${ fileSize } ` ) ;
47
46
}
48
47
@@ -53,13 +52,12 @@ const Jsoncrack = ({
53
52
}
54
53
55
54
useEffect ( ( ) => {
56
- console . log ( fileName , "...New file detected" ) ;
57
-
58
55
const handleSwitchFile = async ( ) => {
59
56
const { filename } = editorManager . activeFile ;
60
57
61
58
if ( fileExtensions . includes ( filename ?. split ( "." ) . pop ( ) ) ) {
62
59
currentFileRef . current = editorManager . activeFile ; // Set current file reference
60
+ setIsTooLarge ( false ) ;
63
61
setFileContent ( "" ) ; // Reset file content for the new file
64
62
setReadeState ( true ) ;
65
63
await readFileInChunks ( ) ;
@@ -80,24 +78,34 @@ const Jsoncrack = ({
80
78
81
79
return (
82
80
< div className = "w-[99%] h-[94dvh]" >
83
- { readeState || fileContent . length == 0 ? (
84
- < h1 > Loading...</ h1 >
81
+ { readeState ? (
82
+ < div className = "h-full w-full flex items-center justify-center" >
83
+ < h1 > Loading...</ h1 >
84
+ </ div >
85
+ ) : isTooLarge ? (
86
+ < div className = "h-full w-full flex items-center justify-center" >
87
+ < div >
88
+ < h3 className = "text-lg font-bold" > Oppsss!</ h3 >
89
+ < p className = "text-sm" > file is too large</ p >
90
+ </ div >
91
+ </ div >
85
92
) : (
86
- < Graph
87
- json = { fileContent . join ( "" ) }
88
- ref = { graphRef }
89
- style = { {
90
- width : "100%" ,
91
- height : "100%"
92
- } }
93
- layout = { {
94
- theme,
95
- direction,
96
- hideChildrenCount,
97
- hideCollapseButton,
98
- collapseNodes
99
- } }
100
- />
93
+ fileContent && (
94
+ < Graph
95
+ json = { fileContent }
96
+ ref = { graphRef }
97
+ style = { {
98
+ width : "100%" ,
99
+ height : "100%"
100
+ } }
101
+ layout = { {
102
+ theme,
103
+ direction,
104
+ hideChildrenCount,
105
+ hideCollapseButton
106
+ } }
107
+ />
108
+ )
101
109
) }
102
110
</ div >
103
111
) ;
0 commit comments