1
1
"use client" ;
2
2
3
- import { useState } from "react" ;
3
+ import { useState , useEffect } from "react" ;
4
4
import { useExplorer } from "@/hooks/use-explorer" ;
5
5
import { Editor } from "../editor" ;
6
6
import type { FC } from "react" ;
@@ -25,33 +25,44 @@ export const CodePath: FC = () => {
25
25
const [ extracted , setExtracted ] = useState < ParsedResponse | null > ( null ) ;
26
26
const [ error , setError ] = useState < string | null > ( null ) ;
27
27
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
+
28
63
useDebouncedEffect (
29
64
( ) => {
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 ( ) ;
55
66
} ,
56
67
500 ,
57
68
[ javascript , esVersion , sourceType , index , indexes ] ,
0 commit comments