diff --git a/app/src/hooks/useFileContent.js b/app/src/hooks/useFileContent.ts similarity index 59% rename from app/src/hooks/useFileContent.js rename to app/src/hooks/useFileContent.ts index b524a12..3bdd43c 100644 --- a/app/src/hooks/useFileContent.js +++ b/app/src/hooks/useFileContent.ts @@ -1,25 +1,38 @@ import { useState, useCallback, useEffect } from 'react'; -import { fetchFileContent } from '../api/git'; import { isImageFile } from '../utils/fileHelpers'; -import { DEFAULT_FILE } from '../utils/constants'; import { useWorkspace } from '../contexts/WorkspaceContext'; +import { DEFAULT_FILE } from '@/types/file'; +import { getFileContent } from '@/api/file'; -export const useFileContent = (selectedFile) => { +interface UseFileContentResult { + content: string; + setContent: React.Dispatch>; + hasUnsavedChanges: boolean; + setHasUnsavedChanges: React.Dispatch>; + loadFileContent: (filePath: string) => Promise; + handleContentChange: (newContent: string) => void; +} + +export const useFileContent = ( + selectedFile: string | null +): UseFileContentResult => { const { currentWorkspace } = useWorkspace(); - const [content, setContent] = useState(DEFAULT_FILE.content); - const [originalContent, setOriginalContent] = useState(DEFAULT_FILE.content); - const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false); + const [content, setContent] = useState(DEFAULT_FILE.content); + const [originalContent, setOriginalContent] = useState( + DEFAULT_FILE.content + ); + const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false); const loadFileContent = useCallback( - async (filePath) => { + async (filePath: string) => { if (!currentWorkspace) return; try { - let newContent; + let newContent: string; if (filePath === DEFAULT_FILE.path) { newContent = DEFAULT_FILE.content; } else if (!isImageFile(filePath)) { - newContent = await fetchFileContent(currentWorkspace.name, filePath); + newContent = await getFileContent(currentWorkspace.name, filePath); } else { newContent = ''; // Set empty content for image files } @@ -43,7 +56,7 @@ export const useFileContent = (selectedFile) => { }, [selectedFile, currentWorkspace, loadFileContent]); const handleContentChange = useCallback( - (newContent) => { + (newContent: string) => { setContent(newContent); setHasUnsavedChanges(newContent !== originalContent); },