Skip to content

Commit c6a9480

Browse files
authored
Fix build errors (#32)
### TL;DR Enabled the Benchify code repair API and fixed React dependency issues in the UI builder components. ### What changed? - Uncommented the Benchify fixer API code in `generate/route.ts` to enable automatic code repair functionality - Added proper type definitions for the `FixResult` interface in both `error-display.tsx` and `preview-card.tsx` - Fixed React dependency issues in `code-editor.tsx`: - Added `useCallback` for the `buildFileTree` function - Updated dependency arrays in `useEffect` hooks to properly track dependencies - Fixed quotes in JSX to use proper escaped quotes (`"`) in `error-display.tsx` and `preview-card.tsx` ### How to test? 1. Generate UI components that might contain errors 2. Verify that the Benchify fixer API automatically repairs the code 3. Check that the code editor properly maintains state when files change 4. Confirm that error messages display correctly with proper quotation marks ### Why make this change? This change enables the automatic code repair functionality that was previously disabled, improving the user experience by automatically fixing generated code. The React dependency fixes prevent unnecessary re-renders and potential memory leaks in the UI builder components, while the proper typing enhances code maintainability and type safety.
2 parents cf7bb3f + 05eae9b commit c6a9480

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

app/api/generate/route.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,27 @@ export async function POST(request: NextRequest) {
100100
}
101101

102102
// Repair the generated code using Benchify's API
103-
// const { data } = await benchify.fixer.run({
104-
// files: filesToSandbox.map(file => ({
105-
// path: file.path,
106-
// contents: file.content
107-
// }))
108-
// });
103+
const { data } = await benchify.fixer.run({
104+
files: filesToSandbox.map(file => ({
105+
path: file.path,
106+
contents: file.content
107+
}))
108+
});
109109

110110
let repairedFiles = filesToSandbox;
111-
// if (data) {
112-
// const { success, diff } = data;
113-
114-
// if (success && diff) {
115-
// repairedFiles = filesToSandbox.map(file => {
116-
// const patchResult = applyPatch(file.content, diff);
117-
// return {
118-
// ...file,
119-
// content: typeof patchResult === 'string' ? patchResult : file.content
120-
// };
121-
// });
122-
// }
123-
// }
111+
if (data) {
112+
const { success, diff } = data;
113+
114+
if (success && diff) {
115+
repairedFiles = filesToSandbox.map(file => {
116+
const patchResult = applyPatch(file.content, diff);
117+
return {
118+
...file,
119+
content: typeof patchResult === 'string' ? patchResult : file.content
120+
};
121+
});
122+
}
123+
}
124124

125125
const sandboxResult = await createSandbox({ files: repairedFiles });
126126

components/ui-builder/code-editor.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from "react";
1+
import { useState, useEffect, useCallback } from "react";
22
import { benchifyFileSchema } from "@/lib/schemas";
33
import { z } from "zod";
44
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
@@ -35,7 +35,7 @@ export function CodeEditor({ files = [] }: CodeEditorProps) {
3535
const [openFolders, setOpenFolders] = useState<Set<string>>(new Set());
3636

3737
// Build file tree structure (pure function, no side effects)
38-
const buildFileTree = (files: Array<{ path: string; content: string }>): { tree: FileNode[], allFolders: string[] } => {
38+
const buildFileTree = useCallback((files: Array<{ path: string; content: string }>): { tree: FileNode[], allFolders: string[] } => {
3939
const root: FileNode[] = [];
4040
const folderMap = new Map<string, FileNode>();
4141
const allFolders: string[] = [];
@@ -81,7 +81,7 @@ export function CodeEditor({ files = [] }: CodeEditorProps) {
8181
});
8282

8383
return { tree: root, allFolders };
84-
};
84+
}, []);
8585

8686
const { tree: fileTree, allFolders } = buildFileTree(files);
8787
const selectedFile = files.find(f => f.path === selectedFilePath);
@@ -91,14 +91,14 @@ export function CodeEditor({ files = [] }: CodeEditorProps) {
9191
if (allFolders.length > 0) {
9292
setOpenFolders(new Set(allFolders));
9393
}
94-
}, [files.length]); // Only trigger when files array changes
94+
}, [allFolders]);
9595

9696
// Auto-select first file if none selected (only once when files change)
9797
useEffect(() => {
9898
if (!selectedFilePath && files.length > 0) {
9999
setSelectedFilePath(files[0].path);
100100
}
101-
}, [files.length, selectedFilePath]); // Dependency on selectedFilePath prevents loops
101+
}, [files, selectedFilePath]);
102102

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

components/ui-builder/error-display.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ interface BuildError {
1717
column?: number;
1818
}
1919

20+
interface FixResult {
21+
originalFiles: z.infer<typeof benchifyFileSchema>;
22+
repairedFiles: z.infer<typeof benchifyFileSchema>;
23+
buildOutput: string;
24+
previewUrl: string;
25+
buildErrors?: BuildError[];
26+
hasErrors: boolean;
27+
editInstruction?: string;
28+
}
29+
2030
interface ErrorDisplayProps {
2131
errors: BuildError[];
2232
currentFiles?: z.infer<typeof benchifyFileSchema>;
23-
onFixComplete?: (result: any) => void;
33+
onFixComplete?: (result: FixResult) => void;
2434
}
2535

2636
export function ErrorDisplay({ errors, currentFiles, onFixComplete }: ErrorDisplayProps) {
@@ -196,7 +206,7 @@ Please make the minimal changes necessary to resolve these errors while maintain
196206
<li>• Verify that all props are properly typed</li>
197207
<li>• Make sure all dependencies are correctly installed</li>
198208
<li>• Try regenerating the component with more specific requirements</li>
199-
{currentFiles && <li>• Use the "Fix with AI" button above for automatic error resolution</li>}
209+
{currentFiles && <li>• Use the &quot;Fix with AI&quot; button above for automatic error resolution</li>}
200210
</ul>
201211
</div>
202212
</div>

components/ui-builder/preview-card.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,24 @@ interface BuildError {
4545
column?: number;
4646
}
4747

48+
interface FixResult {
49+
originalFiles: z.infer<typeof benchifyFileSchema>;
50+
repairedFiles: z.infer<typeof benchifyFileSchema>;
51+
buildOutput: string;
52+
previewUrl: string;
53+
buildErrors?: BuildError[];
54+
hasErrors: boolean;
55+
editInstruction?: string;
56+
}
57+
4858
interface PreviewCardProps {
4959
previewUrl?: string;
5060
code: z.infer<typeof benchifyFileSchema>;
5161
isGenerating?: boolean;
5262
prompt?: string;
5363
buildErrors?: BuildError[];
5464
hasErrors?: boolean;
55-
onFixComplete?: (result: any) => void;
65+
onFixComplete?: (result: FixResult) => void;
5666
}
5767

5868
export function PreviewCard({
@@ -110,7 +120,7 @@ export function PreviewCard({
110120
<CardHeader>
111121
<CardTitle>Building Your UI</CardTitle>
112122
<p className="text-sm text-muted-foreground mt-2">
113-
"{prompt.substring(0, 100)}{prompt.length > 100 ? '...' : ''}"
123+
&quot;{prompt.substring(0, 100)}{prompt.length > 100 ? '...' : ''}&quot;
114124
</p>
115125
</CardHeader>
116126
<CardContent className="space-y-4">

0 commit comments

Comments
 (0)