1
- import { useState , useEffect , useCallback } from "react" ;
1
+ import { useState , useEffect , useCallback , useMemo } from "react" ;
2
2
import { benchifyFileSchema } from "@/lib/schemas" ;
3
3
import { z } from "zod" ;
4
4
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' ;
@@ -83,7 +83,8 @@ export function CodeEditor({ files = [] }: CodeEditorProps) {
83
83
return { tree : root , allFolders } ;
84
84
} , [ ] ) ;
85
85
86
- const { tree : fileTree , allFolders } = buildFileTree ( files ) ;
86
+ // Memoize the file tree calculation to prevent infinite re-renders
87
+ const { tree : fileTree , allFolders } = useMemo ( ( ) => buildFileTree ( files ) , [ files , buildFileTree ] ) ;
87
88
const selectedFile = files . find ( f => f . path === selectedFilePath ) ;
88
89
89
90
// Open all folders by default (only once when files change)
@@ -98,7 +99,7 @@ export function CodeEditor({ files = [] }: CodeEditorProps) {
98
99
if ( ! selectedFilePath && files . length > 0 ) {
99
100
setSelectedFilePath ( files [ 0 ] . path ) ;
100
101
}
101
- } , [ files , selectedFilePath ] ) ;
102
+ } , [ files . length , selectedFilePath ] ) ; // Use files.length instead of files array
102
103
103
104
// Get file icon based on extension
104
105
const getFileIcon = ( path : string ) => {
0 commit comments