import { useRef } from 'react'; import { Upload, Download, FilePlus, File } from 'lucide-react'; import { Button } from '../components/ui'; import { Config } from '../models/Config'; import { ConfigReader } from '../models/ConfigReader'; import { ConfigBuilder } from '../builders/ConfigBuilder'; import { Env } from '../models/Env'; interface FileChooserProps { onSelected: (config: Config) => void; config?: Config; } export function FileChooser({ onSelected, config }: FileChooserProps) { const fileInputRef = useRef(null); async function handleFileChange(event: React.ChangeEvent) { const file = event.target.files?.[0]; if (!file) return; console.log(file.name, file.type, file.size, 'supported:', ConfigReader.isSupportedFormat(file)); const reader = new ConfigReader(); const cfg = await reader.parseFromFile(file); if (cfg !== null) { onSelected(cfg); } // Reset input if (fileInputRef.current) { fileInputRef.current.value = ''; } } function handleNew() { const cfg = new Config(); cfg.setEnvs([new Env(0, 'DEFAULT', [])]); cfg.setTemplate('{}'); onSelected(cfg); } function handleDownload() { if (!config) { alert('No configuration loaded'); return; } const xmlContent = ConfigBuilder.buildFullXml(config); const filename = ConfigBuilder.generateFilename(); const blob = new Blob([xmlContent], { type: 'text/xml' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); } const hasConfig = !!config && !config.isEmpty(); return (
{/* Logo/Brand */}
Configucci
{/* Action Buttons */}
or {/* File Upload */}
); }