import { useState, useEffect } from 'react' import { Dialog, DialogContent } from '@/components/ui/dialog' import { FilePreview } from './FilePreview' import { Loader2, FileText } from 'lucide-react' import { API_BASE_URL } from '@/config' import type { FileInfo } from '@/types/files' interface FilePreviewDialogProps { isOpen: boolean onClose: () => void filePath: string repoBasePath?: string onFileSaved?: () => void initialLineNumber?: number } export function FilePreviewDialog({ isOpen, onClose, filePath, repoBasePath, onFileSaved, initialLineNumber }: FilePreviewDialogProps) { const [file, setFile] = useState(null) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { if (!isOpen || !filePath) { setFile(null) setError(null) return } const fetchFile = async () => { setIsLoading(true) setError(null) try { const fullPath = repoBasePath ? `${repoBasePath}/${filePath}` : filePath const response = await fetch(`${API_BASE_URL}/api/files/${fullPath}`) if (!response.ok) { throw new Error(`Failed to load file: ${response.statusText}`) } const data = await response.json() setFile(data) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load file') } finally { setIsLoading(false) } } fetchFile() }, [isOpen, filePath, repoBasePath]) const handleOpenChange = (open: boolean) => { if (!open) { onClose() } } return (
{isLoading ? (
) : error ? (

Failed to load file

{error}

) : file ? ( ) : null}
) }