import React, { useRef, useState } from 'react'; import { Database, Upload, Trash2, LayoutTemplate, Search, RefreshCw, Plus, Check } from 'lucide-react'; import { useAppContext } from './AppContext'; import { SpecService } from '../services/api'; const CollectionsPage = ({ onCollectionSelected }) => { const { collections, activeSpecId, loadSpec, deleteSpec, fetchCollections, isLoading, addToast, } = useAppContext(); const fileInputRef = useRef(null); const [isUploading, setIsUploading] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const handleFileUpload = async (e) => { const file = e.target.files?.[0]; if (!file) return; setIsUploading(true); try { await SpecService.uploadSpec(file); await fetchCollections(); addToast('success', `"${file.name}" uploaded successfully.`); } catch (err) { console.error('Upload failed', err); addToast('error', err.message || 'Failed to upload spec.'); } finally { setIsUploading(false); if (fileInputRef.current) fileInputRef.current.value = ''; } }; const filteredSpecs = collections.filter(spec => spec.name.toLowerCase().includes(searchQuery.toLowerCase()) ); return (
{/* Header */}
Studio

API Collections

{/* Toolbar */}
setSearchQuery(e.target.value)} /> /
{filteredSpecs.length} {filteredSpecs.length === 1 ? 'collection' : 'collections'}
{/* Content */} {isLoading ? (

Loading collections…

) : filteredSpecs.length === 0 ? (
{searchQuery ? 'No collections match your search' : 'No collections yet'}

{searchQuery ? 'Try a different search term or clear the filter.' : 'Upload a Swagger / OpenAPI spec (.json, .yaml) to get started.'}

{!searchQuery && ( )}
) : (
{filteredSpecs.map(spec => (
{ loadSpec(spec.id); onCollectionSelected(); }} > {/* Active badge */} {activeSpecId === spec.id && (
Active
)}

{spec.name}

API Specification
ID: {spec.id.slice(0, 8)}…
))}
)}
); }; export default CollectionsPage;