Spaces:
Runtime error
Runtime error
Download frontend-react/src/components/SpecsPage.jsx from Amitkasar/ai-api-testing: direct link, hf CLI and curl.
- Browser
- Download file 7.75 kB
-
https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/frontend-react/src/components/SpecsPage.jsx
- Command line
-
hf download hf://spaces/Amitkasar/ai-api-testing/frontend-react/src/components/SpecsPage.jsx
-
curl -L -o SpecsPage.jsx https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/frontend-react/src/components/SpecsPage.jsx
7.75 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, 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; | |