Skip to content

Commit 1fac15d

Browse files
Juan Castañojuancastano
authored andcommitted
Fix code preview bug
1 parent fe1b3e2 commit 1fac15d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

components/ui-builder/code-editor.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useCallback } from "react";
1+
import { useState, useEffect, useCallback, useMemo } from "react";
22
import { benchifyFileSchema } from "@/lib/schemas";
33
import { z } from "zod";
44
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
@@ -83,7 +83,8 @@ export function CodeEditor({ files = [] }: CodeEditorProps) {
8383
return { tree: root, allFolders };
8484
}, []);
8585

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]);
8788
const selectedFile = files.find(f => f.path === selectedFilePath);
8889

8990
// Open all folders by default (only once when files change)
@@ -98,7 +99,7 @@ export function CodeEditor({ files = [] }: CodeEditorProps) {
9899
if (!selectedFilePath && files.length > 0) {
99100
setSelectedFilePath(files[0].path);
100101
}
101-
}, [files, selectedFilePath]);
102+
}, [files.length, selectedFilePath]); // Use files.length instead of files array
102103

103104
// Get file icon based on extension
104105
const getFileIcon = (path: string) => {

0 commit comments

Comments
 (0)