import React, { useState, useCallback, useMemo } from 'react'; import { Tabs, Breadcrumbs, Group, Box, Text, Flex } from '@mantine/core'; import { IconCode, IconEye, IconPointFilled } from '@tabler/icons-react'; import ContentView from '../editor/ContentView'; import CreateFileModal from '../modals/file/CreateFileModal'; import DeleteFileModal from '../modals/file/DeleteFileModal'; import RenameFileModal from '../modals/file/RenameFileModal'; import CommitMessageModal from '../modals/git/CommitMessageModal'; import { useFileContent } from '../../hooks/useFileContent'; import { useFileOperations } from '../../hooks/useFileOperations'; import { useGitOperations } from '../../hooks/useGitOperations'; import { useModalContext } from '../../contexts/ModalContext'; import type { FileNode } from '../../types/models'; type ViewTab = 'source' | 'preview'; interface MainContentProps { selectedFile: string | null; handleFileSelect: (filePath: string | null) => Promise; loadFileList: () => Promise; files: FileNode[]; } const MainContent: React.FC = ({ selectedFile, handleFileSelect, loadFileList, files, }) => { const [activeTab, setActiveTab] = useState('source'); const { content, hasUnsavedChanges, setHasUnsavedChanges, handleContentChange, } = useFileContent(selectedFile); const { handleSave, handleCreate, handleDelete, handleRename } = useFileOperations(); const { handleCommitAndPush } = useGitOperations(); const { setRenameFileModalVisible } = useModalContext(); const handleTabChange = useCallback((value: string | null): void => { if (value) { setActiveTab(value as ViewTab); } }, []); const handleSaveFile = useCallback( async (filePath: string, fileContent: string): Promise => { const success = await handleSave(filePath, fileContent); if (success) { setHasUnsavedChanges(false); } return success; }, [handleSave, setHasUnsavedChanges] ); const handleCreateFile = useCallback( async (fileName: string): Promise => { const success = await handleCreate(fileName); if (success) { await loadFileList(); await handleFileSelect(fileName); } }, [handleCreate, handleFileSelect, loadFileList] ); const handleDeleteFile = useCallback( async (filePath: string): Promise => { const success = await handleDelete(filePath); if (success) { await loadFileList(); await handleFileSelect(null); } }, [handleDelete, handleFileSelect, loadFileList] ); const handleRenameFile = useCallback( async (oldPath: string, newPath: string): Promise => { const success = await handleRename(oldPath, newPath); if (success) { await loadFileList(); // If we renamed the currently selected file, update the selection if (selectedFile === oldPath) { await handleFileSelect(newPath); } } }, [handleRename, handleFileSelect, loadFileList, selectedFile] ); const handleBreadcrumbClick = useCallback(() => { if (selectedFile) { setRenameFileModalVisible(true); } }, [selectedFile, setRenameFileModalVisible]); const renderBreadcrumbs = useMemo(() => { if (!selectedFile) return null; const pathParts = selectedFile.split('/'); const items = pathParts.map((part, index) => { // Make the filename (last part) clickable for rename const isFileName = index === pathParts.length - 1; return ( {part} ); }); return ( {items} {hasUnsavedChanges && ( )} ); }, [selectedFile, hasUnsavedChanges, handleBreadcrumbClick]); return ( {renderBreadcrumbs} } /> } /> ); }; export default MainContent;