Skip to content

Commit 450b77b

Browse files
committed
add logic to handle large file
1 parent 1d4f8af commit 450b77b

File tree

8 files changed

+57
-102
lines changed

8 files changed

+57
-102
lines changed

icon.png

-10.8 KB
Loading

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"name": "@chaos-19/json-graph-acode", "version": "1.0.1",
2+
"name": "@chaos-19/json-graph-acode",
3+
"version": "1.0.1",
34
"description": "A JSON to Graph Visualizer plugin for Acode, built with React. This plugin enables users to visualize JSON data as an interactive graph for easy exploration of complex structures.",
45
"main": "dist/main.js",
56
"author": "kalkidan Getachew",

src/App.jsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React, { useState, useEffect, useRef } from "react";
2-
import PageContextProvider, { PageContext } from "./PageContext";
32

43
import Jsoncrack from "./components/Jsoncrack";
54
import { fileExtensions, jsonIcon } from "./constants";
65
import DropDown from "./components/Dropdown";
76

7+
const fs = acode.require("fsOperation");
8+
89
const App = ({ $page }) => {
910
const [pageState, setPageState] = useState({
1011
theme: "light",
@@ -77,10 +78,14 @@ const App = ({ $page }) => {
7778
};
7879

7980
useEffect(() => {
80-
const handleSwitchFile = () => {
81+
const handleSwitchFile = async () => {
8182
const { filename, uri } = editorManager.activeFile;
82-
83-
if (fileExtensions.includes(filename?.split(".").pop())) {
83+
84+
const isFileExist = await fs(uri).exists();
85+
if (
86+
isFileExist &&
87+
fileExtensions.includes(filename?.split(".").pop())
88+
) {
8489
setActiveFile({
8590
filename,
8691
uri

src/PageContext.jsx

-24
This file was deleted.

src/components/Dropdown.jsx

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const DropDown = ({ $page, pageState, handlePageState, graphRef }) => {
1+
const DropDown = ({ $page, pageState, handlePageState }) => {
22
const toggleMenu = false;
33

44
const dropdownMenu = tag("span", {
@@ -34,21 +34,12 @@ const DropDown = ({ $page, pageState, handlePageState, graphRef }) => {
3434
<li data-theme="light">
3535
<span class="dropdown-item theme">theme</span>
3636
</li>
37-
<li data-value="false" data-collapseNodes="false">
38-
<span class="dropdown-item collapseNodes">collapseNodes</span>
39-
</li>
40-
<li data-value="false" data-compactNodes="false">
41-
<span class="dropdown-item compactNodes">compactNodes</span>
42-
</li>
4337
<li data-value="false" data-hideChildrenCount="false">
4438
<span class="dropdown-item hideChildrenCount">hideChildrenCount</span>
4539
</li>
4640
<li data-value="false" data-hideCollapseButton="false">
4741
<span class="dropdown-item hideCollapseButton">hideCollapseButton</span>
4842
</li>
49-
<li data-value="false" data-disableImagePreview="false">
50-
<span class="dropdown-item">disableImagePreview</span>
51-
</li>
5243
</ul>
5344
`;
5445

@@ -98,21 +89,13 @@ const DropDown = ({ $page, pageState, handlePageState, graphRef }) => {
9889

9990
const keys = [
10091
// "collapseNodes",
101-
"compactNodes",
92+
//"compactNodes",
10293
"hideChildrenCount",
103-
"hideCollapseButton",
104-
"disableImagePreview"
94+
"hideCollapseButton"
95+
//"disableImagePreview"
10596
];
106-
list[2].addEventListener("click", e => {
107-
const collapseNodes = list[1].dataset.value;
108-
e.target.textContent = collapsGraph ? "collapseNodes" : "expandNodes";
109-
list[1].dataset.value = !collapseNodes;
110-
console.log(JSON.stringify(graphRef.current));
111-
if (!collapseNodes) graphRef.current?.collapsGraph();
112-
else graphRef.current.expandGraph();
113-
});
11497

115-
for (let i = 3; i < list.length; i++)
98+
for (let i = 2; i < list.length; i++)
11699
list[i].addEventListener("click", e => {
117100
e.preventDefault();
118101
//console.log(list[i].dataset.value, i);

src/components/Jsoncrack.jsx

+40-32
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
import React, { useState, useEffect ,useRef} from "react";
1+
import React, { useState, useEffect, useRef } from "react";
22
import { JSONGraph as Graph } from "jsongraph-react";
33
import { fileExtensions } from "../constants";
44
const fs = acode.require("fsOperation");
55

66
const Jsoncrack = ({
77
graphRef,
8-
options: {
9-
theme,
10-
direction,
11-
hideChildrenCount,
12-
hideCollapseButton,
13-
collapseNodes
14-
},
8+
options: { theme, direction, hideChildrenCount, hideCollapseButton },
159
fileName,
1610
uri
1711
}) => {
18-
const [fileContent, setFileContent] = useState([]);
12+
const [fileContent, setFileContent] = useState("");
1913
const [readeState, setReadeState] = useState(false);
14+
const [isTooLarge, setIsTooLarge] = useState(false);
2015

2116
const currentFileRef = useRef(null); // Track the current file being read
2217

2318
async function readFileInChunks() {
24-
let chunkSize = 100 * 824;
2519
const file = editorManager.activeFile;
2620
const fileContentArray = [];
2721
let offset = 0;
2822

2923
try {
24+
// Read the entire file
3025
const content = await fs(file.uri).readFile("utf-8");
26+
const fileStat = await fs(uri).stat();
27+
28+
setIsTooLarge(fileStat.length > 200000);
29+
3130
const fileSize = content.length;
32-
chunkSize = fileSize / 2;
31+
let chunkSize = fileSize / 5;
3332

3433
while (offset < fileSize) {
35-
if (file !== currentFileRef.current) {
34+
if (file !== currentFileRef.current || isTooLarge) {
3635
console.log("File switched, stopping read process...");
3736
return;
3837
}
@@ -42,7 +41,7 @@ const Jsoncrack = ({
4241

4342
offset += chunkSize;
4443

45-
setFileContent(prev => [...prev, chunk]);
44+
setFileContent(prev => prev + chunk);
4645
console.log(`Processed ${offset} of ${fileSize}`);
4746
}
4847

@@ -53,13 +52,12 @@ const Jsoncrack = ({
5352
}
5453

5554
useEffect(() => {
56-
console.log(fileName, "...New file detected");
57-
5855
const handleSwitchFile = async () => {
5956
const { filename } = editorManager.activeFile;
6057

6158
if (fileExtensions.includes(filename?.split(".").pop())) {
6259
currentFileRef.current = editorManager.activeFile; // Set current file reference
60+
setIsTooLarge(false);
6361
setFileContent(""); // Reset file content for the new file
6462
setReadeState(true);
6563
await readFileInChunks();
@@ -80,24 +78,34 @@ const Jsoncrack = ({
8078

8179
return (
8280
<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>
8592
) : (
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+
)
101109
)}
102110
</div>
103111
);

src/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import plugin from "../plugin.json";
22
import React from "react";
33
import ReactDOM from "react-dom/client";
44
import "./index.css"; // Import TailwindCSS
5-
import PageContextProvider from "./PageContext";
5+
66
import ErrorBoundary from "./components/ErrorBoundary";
77
import App from "./App";
88

src/workers/fileReaderWorker.js

-18
This file was deleted.

0 commit comments

Comments
 (0)