ai-api-testing / frontend-react /src /components /CollectionsPage.jsx
Amitkasar's picture
changed the project structure and ui , updated security and enhanced the project
3f78860
Raw History Blame Contribute Delete
7.64 kB
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 (
<div className="page">
<div className="page-inner">
{/* Header */}
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 4 }}>
<div>
<div className="h-eyebrow">Studio</div>
<h1 style={{ fontSize: 18, fontWeight: 600, color: 'var(--fg-0)', letterSpacing: '-0.01em', margin: '4px 0 0', display: 'flex', alignItems: 'center', gap: 8 }}>
<Database size={18} style={{ color: 'var(--accent)' }} />
API Collections
</h1>
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<button
className="btn"
onClick={() => fetchCollections()}
disabled={isLoading}
title="Refresh collections"
>
<RefreshCw size={13} style={isLoading ? { animation: 'spin 1s linear infinite' } : {}} />
Refresh
</button>
<button
className="btn btn-primary"
onClick={() => fileInputRef.current?.click()}
disabled={isUploading}
>
{isUploading
? <><RefreshCw size={13} style={{ animation: 'spin 1s linear infinite' }} /> Uploading…</>
: <><Plus size={13} /> Upload Spec</>}
</button>
<input
type="file"
ref={fileInputRef}
style={{ display: 'none' }}
accept=".json,.yaml,.yml"
onChange={handleFileUpload}
/>
</div>
</div>
{/* Toolbar */}
<div className="col-toolbar">
<div className="search">
<Search size={13} style={{ color: 'var(--fg-3)', flexShrink: 0 }} />
<input
type="text"
placeholder="Search collections…"
value={searchQuery}
onChange={e => setSearchQuery(e.target.value)}
/>
<span className="kbd">/</span>
</div>
<span style={{ fontSize: 11, color: 'var(--fg-3)' }}>
{filteredSpecs.length} {filteredSpecs.length === 1 ? 'collection' : 'collections'}
</span>
</div>
{/* Content */}
{isLoading ? (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: 240, gap: 12 }}>
<RefreshCw size={28} style={{ color: 'var(--fg-4)', animation: 'spin 1s linear infinite' }} />
<p style={{ fontSize: 12, color: 'var(--fg-3)', margin: 0 }}>Loading collections…</p>
</div>
) : filteredSpecs.length === 0 ? (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: 260, gap: 14, textAlign: 'center' }}>
<div style={{ padding: 18, borderRadius: 10, background: 'var(--bg-2)', border: '1px solid var(--line-2)' }}>
<Database size={32} style={{ color: 'var(--fg-4)' }} strokeWidth={1.5} />
</div>
<div>
<div style={{ fontSize: 13, fontWeight: 600, color: 'var(--fg-1)', marginBottom: 4 }}>
{searchQuery ? 'No collections match your search' : 'No collections yet'}
</div>
<p style={{ fontSize: 12, color: 'var(--fg-3)', margin: '0 0 16px', maxWidth: 360, lineHeight: 1.6 }}>
{searchQuery
? 'Try a different search term or clear the filter.'
: 'Upload a Swagger / OpenAPI spec (.json, .yaml) to get started.'}
</p>
{!searchQuery && (
<button className="btn btn-primary" onClick={() => fileInputRef.current?.click()}>
<Upload size={13} /> Upload your first spec
</button>
)}
</div>
</div>
) : (
<div className="col-grid">
{filteredSpecs.map(spec => (
<div
key={spec.id}
className={`col-card${activeSpecId === spec.id ? ' active' : ''}`}
onClick={() => { loadSpec(spec.id); onCollectionSelected(); }}
>
{/* Active badge */}
{activeSpecId === spec.id && (
<div style={{
position: 'absolute', top: 10, right: 10,
display: 'inline-flex', alignItems: 'center', gap: 4,
background: 'var(--accent)', color: '#fff',
fontSize: 9, fontWeight: 700, letterSpacing: '0.1em',
textTransform: 'uppercase', padding: '2px 7px', borderRadius: 3,
}}>
<Check size={8} /> Active
</div>
)}
<div className="top">
<div style={{
width: 36, height: 36, display: 'grid', placeItems: 'center',
borderRadius: 8, background: 'var(--bg-2)', border: '1px solid var(--line-2)',
flexShrink: 0,
}}>
<LayoutTemplate size={18} style={{ color: activeSpecId === spec.id ? 'var(--accent)' : 'var(--fg-2)' }} />
</div>
<button
onClick={(e) => { e.stopPropagation(); deleteSpec(spec.id); }}
className="ico-btn"
title="Delete collection"
style={{ width: 26, height: 26, color: 'var(--fg-3)' }}
>
<Trash2 size={13} />
</button>
</div>
<h3 style={{ margin: '0 0 4px', fontSize: 13, fontWeight: 600, color: 'var(--fg-0)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
{spec.name}
</h3>
<div style={{ fontSize: 10, textTransform: 'uppercase', letterSpacing: '0.1em', color: 'var(--fg-3)', fontWeight: 700, marginBottom: 2 }}>
API Specification
</div>
<div className="meta">
<span className="id">ID: {spec.id.slice(0, 8)}…</span>
</div>
</div>
))}
</div>
)}
</div>
</div>
);
};
export default CollectionsPage;