Skip to content

Commit d751c90

Browse files
authored
fix: code path rendering issue (#57)
1 parent d127480 commit d751c90

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

src/components/path/index.tsx

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { useState } from "react";
3+
import { useState, useEffect } from "react";
44
import { useExplorer } from "@/hooks/use-explorer";
55
import { Editor } from "../editor";
66
import type { FC } from "react";
@@ -25,33 +25,44 @@ export const CodePath: FC = () => {
2525
const [extracted, setExtracted] = useState<ParsedResponse | null>(null);
2626
const [error, setError] = useState<string | null>(null);
2727

28+
const fetchCodePath = async () => {
29+
try {
30+
const response = await generateCodePath(
31+
javascript,
32+
esVersion,
33+
sourceType,
34+
);
35+
if ("error" in response) {
36+
throw new Error(response.error);
37+
}
38+
const newExtracted = JSON.parse(
39+
response.response,
40+
) as ParsedResponse;
41+
if (newExtracted.codePathList.length < indexes) {
42+
setPathIndex({
43+
index: 0,
44+
indexes: newExtracted.codePathList.length,
45+
});
46+
} else {
47+
setPathIndex({
48+
...pathIndex,
49+
indexes: newExtracted.codePathList.length,
50+
});
51+
}
52+
setError(null);
53+
setExtracted(newExtracted);
54+
} catch (newError) {
55+
setError(parseError(newError));
56+
}
57+
};
58+
59+
useEffect(() => {
60+
fetchCodePath();
61+
}, []);
62+
2863
useDebouncedEffect(
2964
() => {
30-
generateCodePath(javascript, esVersion, sourceType)
31-
.then(response => {
32-
if ("error" in response) {
33-
throw new Error(response.error);
34-
}
35-
36-
return JSON.parse(response.response) as ParsedResponse;
37-
})
38-
.then(newExtracted => {
39-
if (newExtracted.codePathList.length < indexes) {
40-
setPathIndex({
41-
index: 0,
42-
indexes: newExtracted.codePathList.length,
43-
});
44-
} else {
45-
setPathIndex({
46-
...pathIndex,
47-
indexes: newExtracted.codePathList.length,
48-
});
49-
}
50-
setError(null);
51-
return newExtracted;
52-
})
53-
.then(setExtracted)
54-
.catch(newError => setError(parseError(newError)));
65+
fetchCodePath();
5566
},
5667
500,
5768
[javascript, esVersion, sourceType, index, indexes],

0 commit comments

Comments
 (0)