Spaces:
Runtime error
Runtime error
File size: 7,754 Bytes
1e24842 f1c3c6d 1e24842 f1c3c6d 1e24842 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | import React, { useRef, useState } from 'react';
import { Database, Upload, Trash2, LayoutTemplate, Search, RefreshCw, Plus, Check } from 'lucide-react';
import { useAppContext } from './AppContext';
import { SpecService, api } from '../services/api';
const SpecsPage = ({ onSpecSelected }) => {
const {
specs, activeSpecId, loadSpec, deleteSpec,
fetchSpecs, 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 {
const result = await SpecService.uploadSpec(file);
// If the spec declares a server URL, save it immediately so loadSpec picks it up
if (result.spec_id && result.suggested_base_url) {
await SpecService.updateBaseUrl(result.spec_id, result.suggested_base_url);
}
await fetchSpecs();
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 = specs.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 Specs
</h1>
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<button
className="btn"
onClick={() => fetchSpecs()}
disabled={isLoading}
title="Refresh specs"
>
<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 specs…"
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 ? 'spec' : 'specs'}
</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 specs…</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 specs match your search' : 'No specs 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); onSpecSelected(); }}
>
{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 spec"
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 SpecsPage;
|