import React, { useState, useEffect, useRef } from 'react'; import JSZip from 'jszip'; import CopyIcon from './icons/CopyIcon'; import DownloadIcon from './icons/DownloadIcon'; import { AppStatus, AppType } from '../types'; interface CodeDisplayProps { appType: AppType; pythonCode?: string; cppFiles?: Record | null; status: AppStatus; } const PythonCodeView: React.FC<{ code: string }> = ({ code }) => { const [copyText, setCopyText] = useState('Copy Code'); const codeRef = useRef(null); const finalCode = code.replace(/\n```$/, '').trim(); useEffect(() => { // @ts-ignore if (window.hljs && codeRef.current) { // @ts-ignore window.hljs.highlightElement(codeRef.current); } }, [finalCode]); const handleCopy = () => { navigator.clipboard.writeText(finalCode).then(() => { setCopyText('Copied!'); setTimeout(() => setCopyText('Copy Code'), 2000); }); }; const handleDownload = () => { const blob = new Blob([finalCode], { type: 'text/python' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'generated_app.py'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; return (
        
          {finalCode}
        
      
); }; type BuildSystem = 'makefile' | 'cmake' | 'meson'; const CppCodeView: React.FC<{ files: Record }> = ({ files }) => { const [activeFile, setActiveFile] = useState(Object.keys(files)[0] || ''); const [buildSystem, setBuildSystem] = useState('cmake'); const [isDownloading, setIsDownloading] = useState(false); const codeRef = useRef(null); useEffect(() => { // @ts-ignore if (window.hljs && codeRef.current) { // @ts-ignore window.hljs.highlightElement(codeRef.current); } }, [activeFile, files]); const getBuildFileContent = (): { filename: string, content: string } => { const targetName = "MyApp"; const sources = Object.keys(files).filter(f => f.endsWith('.cpp')).join(' '); const headers = Object.keys(files).filter(f => f.endsWith('.h')).join(' '); switch(buildSystem) { case 'makefile': return { filename: 'Makefile', content: ` CXX = g++ CXXFLAGS = -std=c++17 -fPIC \`pkg-config --cflags Qt6Widgets Qt6Gui Qt6Core Qt6WebEngineWidgets\` LDFLAGS = \`pkg-config --libs Qt6Widgets Qt6Gui Qt6Core Qt6WebEngineWidgets\` TARGET = ${targetName} SOURCES = ${sources} OBJECTS = $(SOURCES:.cpp=.o) .PHONY: all clean all: $(TARGET) $(TARGET): $(OBJECTS) \t$(CXX) $(OBJECTS) -o $(TARGET) $(LDFLAGS) %.o: %.cpp ${headers} \t$(CXX) $(CXXFLAGS) -c $< -o $@ clean: \trm -f $(OBJECTS) $(TARGET) `.trim() }; case 'cmake': return { filename: 'CMakeLists.txt', content: ` cmake_minimum_required(VERSION 3.16) project(${targetName} VERSION 1.0) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) find_package(Qt6 COMPONENTS Widgets Gui Core WebEngineWidgets REQUIRED) add_executable(\${PROJECT_NAME} ${sources.split(' ').join('\n ')} ${headers.split(' ').join('\n ')} ) target_link_libraries(\${PROJECT_NAME} PRIVATE Qt6::Widgets Qt6::Gui Qt6::Core Qt6::WebEngineWidgets) `.trim() }; case 'meson': return { filename: 'meson.build', content: ` project('${targetName}', 'cpp', version : '1.0', default_options : ['cpp_std=c++17']) qt6 = dependency('qt6', modules: ['Core', 'Gui', 'Widgets', 'WebEngineWidgets'], required: true) executable( '${targetName}', files(${sources.split(' ').map(s=>`'${s}'`).join(', ')}), dependencies: [qt6], install: true ) `.trim() }; } } const handleDownloadZip = async () => { setIsDownloading(true); const zip = new JSZip(); Object.entries(files).forEach(([name, content]) => { zip.file(name, content); }); const buildFile = getBuildFileContent(); zip.file(buildFile.filename, buildFile.content); try { const content = await zip.generateAsync({type:"blob"}); const url = URL.createObjectURL(content); const a = document.createElement('a'); a.href = url; a.download = 'qt_app.zip'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); } catch (e) { console.error("Failed to generate ZIP", e); } finally { setIsDownloading(false); } }; if (!activeFile) return
No files were generated.
return (
{Object.keys(files).map(name => ( ))}
                  
                    {files[activeFile]}
                  
                
); }; const CodeDisplay: React.FC = ({ appType, pythonCode, cppFiles, status }) => { if (status !== AppStatus.SUCCESS) { return
Generating...
; } return (
{appType === 'python' && pythonCode ? ( ) : appType === 'c++' && cppFiles ? ( ) : (
Code generation complete, but no output received.
)}
); }; export default CodeDisplay;