Skip to content

Fix code preview bug #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 06-16-move_edit_logic_into_open_ai_file
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions components/ui-builder/code-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useCallback } from "react";
import { useState, useEffect, useCallback, useMemo } from "react";
import { benchifyFileSchema } from "@/lib/schemas";
import { z } from "zod";
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
Expand Down Expand Up @@ -83,7 +83,8 @@ export function CodeEditor({ files = [] }: CodeEditorProps) {
return { tree: root, allFolders };
}, []);

const { tree: fileTree, allFolders } = buildFileTree(files);
// Memoize the file tree calculation to prevent infinite re-renders
const { tree: fileTree, allFolders } = useMemo(() => buildFileTree(files), [files, buildFileTree]);
const selectedFile = files.find(f => f.path === selectedFilePath);

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

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