Spaces:
Runtime error
Runtime error
Download frontend-react/src/App.jsx from Amitkasar/ai-api-testing: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/frontend-react/src/App.jsx
- Command line
-
hf download hf://spaces/Amitkasar/ai-api-testing/frontend-react/src/App.jsx
-
curl -L -o App.jsx https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/frontend-react/src/App.jsx
10.8 kB
| import React, { useState, useEffect, useRef } from 'react'; | |
| import { BrowserRouter, Routes, Route, Navigate, useNavigate, useLocation } from 'react-router-dom'; | |
| import { | |
| Settings, History, BarChart2, | |
| ChevronRight, ChevronLeft, | |
| User, Layers, FolderOpen, Bot, | |
| Sun, Moon, LogOut, | |
| } from 'lucide-react'; | |
| import { AppProvider, useAppContext } from './components/AppContext'; | |
| import CollectionsPage from './components/CollectionsPage'; | |
| import SpecsPage from './components/SpecsPage'; | |
| import ProfilePage from './components/ProfilePage'; | |
| import HistoryPage from './components/HistoryPage'; | |
| import MediaHubPage from './components/MediaHubPage'; | |
| import SettingsPage from './components/SettingsPage'; | |
| import UsagePage from './components/UsagePage'; | |
| import ChatAssistant from './components/ChatAssistant'; | |
| import { ToastContainer } from './components/Toast'; | |
| import ErrorBoundary from './components/ErrorBoundary'; | |
| import LoginPage from './components/LoginPage'; | |
| import WorkflowPage from './pages/WorkflowPage'; | |
| import { AuthService } from './services/api'; | |
| const WorkflowIcon = ({ size = 15, className = '' }) => ( | |
| <svg width={size} height={size} viewBox="0 0 24 24" fill="none" | |
| stroke="currentColor" strokeWidth={1.6} strokeLinecap="round" strokeLinejoin="round" | |
| className={className} aria-hidden="true"> | |
| <rect x="3" y="3" width="6" height="6" rx="1.2"/> | |
| <rect x="15" y="15" width="6" height="6" rx="1.2"/> | |
| <path d="M9 6h6a3 3 0 0 1 3 3v6"/> | |
| </svg> | |
| ); | |
| function MainApp() { | |
| const { activeSpecId, activeSpecName, toasts, removeToast } = useAppContext(); | |
| const navigate = useNavigate(); | |
| const location = useLocation(); | |
| const [theme, setTheme] = useState('dark'); | |
| const [collapsed, setCollapsed] = useState(false); | |
| const [accountOpen, setAccountOpen] = useState(false); | |
| const accountRef = useRef(null); | |
| // Close account popup when clicking outside | |
| useEffect(() => { | |
| const handler = (e) => { | |
| if (accountRef.current && !accountRef.current.contains(e.target)) { | |
| setAccountOpen(false); | |
| } | |
| }; | |
| document.addEventListener('mousedown', handler); | |
| return () => document.removeEventListener('mousedown', handler); | |
| }, []); | |
| const handleLogout = () => { | |
| AuthService.logout(); | |
| window.dispatchEvent(new Event('auth:logout')); | |
| }; | |
| useEffect(() => { | |
| document.documentElement.setAttribute('data-theme', theme); | |
| }, [theme]); | |
| // Keyboard shortcuts: 1-7 for nav pages, B to toggle sidebar | |
| useEffect(() => { | |
| const handle = (e) => { | |
| if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.isContentEditable) return; | |
| if (e.key === 'b' || e.key === 'B') { setCollapsed(c => !c); return; } | |
| const allNav = [...NAV1, ...NAV2]; | |
| const found = allNav.find(n => n.kbd === e.key); | |
| if (found) navigate(`/${found.id}`); | |
| }; | |
| window.addEventListener('keydown', handle); | |
| return () => window.removeEventListener('keydown', handle); | |
| }, [navigate]); | |
| useEffect(() => { | |
| const handle = (e) => navigate(`/${e.detail}`); | |
| window.addEventListener('nav-change', handle); | |
| return () => window.removeEventListener('nav-change', handle); | |
| }, [navigate]); | |
| const rawPage = location.pathname.replace('/', '') || 'workflow'; | |
| // Treat /collections as /specs for nav highlighting (legacy redirect) | |
| const activePage = rawPage === 'collections' ? 'specs' : rawPage; | |
| const NAV1 = [ | |
| { id: 'workflow', label: 'Workflow', Icon: WorkflowIcon, kbd: '1' }, | |
| { id: 'specs', label: 'Specs', Icon: Layers, kbd: '2' }, | |
| { id: 'profile', label: 'Profile', Icon: User, kbd: '3' }, | |
| ]; | |
| const NAV2 = [ | |
| { id: 'history', label: 'History', Icon: History, kbd: '4' }, | |
| { id: 'media', label: 'Media hub', Icon: FolderOpen, kbd: '5' }, | |
| { id: 'chat', label: 'Assistant', Icon: Bot, kbd: '6' }, | |
| { id: 'usage', label: 'Usage', Icon: BarChart2, kbd: '7' }, | |
| { id: 'settings', label: 'Settings', Icon: Settings, kbd: '8' }, | |
| ]; | |
| return ( | |
| <> | |
| <div className="app"> | |
| {/* ββ Sidebar βββββββββββββββββββββββββββββββββββββββββββ */} | |
| <aside className={`sidebar${collapsed ? ' collapsed' : ''}`}> | |
| <div className="sb-head"> | |
| <div className="sb-mark">A</div> | |
| {!collapsed && ( | |
| <div className="sb-name"> | |
| apitest <span className="dim">/ workspace</span> | |
| </div> | |
| )} | |
| <button | |
| className="sb-toggle" | |
| onClick={() => setCollapsed(c => !c)} | |
| title={collapsed ? 'Expand sidebar' : 'Collapse sidebar'} | |
| > | |
| {collapsed ? <ChevronRight size={13}/> : <ChevronLeft size={13}/>} | |
| </button> | |
| </div> | |
| <div className="sb-section-label">Studio</div> | |
| <div className="sb-nav"> | |
| {NAV1.map(n => ( | |
| <button | |
| key={n.id} | |
| className={`sb-item${activePage === n.id ? ' active' : ''}`} | |
| onClick={() => navigate(`/${n.id}`)} | |
| title={collapsed ? n.label : undefined} | |
| > | |
| <n.Icon size={15} className="ico"/> | |
| <span className="lbl">{n.label}</span> | |
| <span className="kbd">{n.kbd}</span> | |
| </button> | |
| ))} | |
| </div> | |
| <div className="sb-section-label">More</div> | |
| <div className="sb-nav"> | |
| {NAV2.map(n => ( | |
| <button | |
| key={n.id} | |
| className={`sb-item${activePage === n.id ? ' active' : ''}`} | |
| onClick={() => navigate(`/${n.id}`)} | |
| title={collapsed ? n.label : undefined} | |
| > | |
| <n.Icon size={15} className="ico"/> | |
| <span className="lbl">{n.label}</span> | |
| <span className="kbd">{n.kbd}</span> | |
| </button> | |
| ))} | |
| </div> | |
| <div className="sb-foot"> | |
| <span className="dot"/> | |
| {!collapsed && <><span>session live</span><span style={{ flex: 1 }}/></>} | |
| </div> | |
| {/* ββ Account / Logout βββββββββββββββββββββββββββββββ */} | |
| <div className="sb-account" ref={accountRef}> | |
| <button | |
| className="sb-account-btn" | |
| onClick={() => setAccountOpen(o => !o)} | |
| title="Account" | |
| > | |
| <User size={15} className="ico"/> | |
| {!collapsed && <span className="lbl">Account</span>} | |
| </button> | |
| {accountOpen && ( | |
| <div className="sb-account-popup"> | |
| <div className="sb-account-user"> | |
| <User size={13}/> | |
| <span>Signed in</span> | |
| </div> | |
| <div className="sb-account-divider"/> | |
| <button className="sb-account-logout" onClick={handleLogout}> | |
| <LogOut size={13}/> | |
| <span>Logout</span> | |
| </button> | |
| </div> | |
| )} | |
| </div> | |
| </aside> | |
| {/* ββ Main ββββββββββββββββββββββββββββββββββββββββββββββ */} | |
| <main className="main"> | |
| {/* ββ Topbar ββββββββββββββββββββββββββββββββββββββββ */} | |
| <header className="topbar"> | |
| <div className="crumb"> | |
| <span>workspace</span> | |
| <span className="sep">/</span> | |
| <span className="here"> | |
| {activePage === 'workflow' && activeSpecName && activeSpecName !== 'None' | |
| ? activeSpecName | |
| : activePage} | |
| </span> | |
| </div> | |
| <div className="spacer"/> | |
| {/* Theme toggle */} | |
| <div className="theme-toggle"> | |
| <button | |
| className={theme === 'light' ? 'on' : ''} | |
| onClick={() => setTheme('light')} | |
| title="Light theme" | |
| > | |
| <Sun size={12} className="ico"/> light | |
| </button> | |
| <button | |
| className={theme === 'dark' ? 'on' : ''} | |
| onClick={() => setTheme('dark')} | |
| title="Dark theme" | |
| > | |
| <Moon size={12} className="ico"/> dark | |
| </button> | |
| </div> | |
| </header> | |
| {/* ββ Page routes ββββββββββββββββββββββββββββββββββββ */} | |
| <Routes> | |
| <Route path="/" element={<Navigate to="/workflow" replace/>}/> | |
| <Route path="/workflow" element={<WorkflowPage/>}/> | |
| <Route path="/specs" element={ | |
| <SpecsPage onSpecSelected={() => navigate('/workflow')}/> | |
| }/> | |
| <Route path="/collections" element={ | |
| <SpecsPage onSpecSelected={() => navigate('/workflow')}/> | |
| }/> | |
| <Route path="/profile" element={<ProfilePage/>}/> | |
| <Route path="/history" element={<HistoryPage/>}/> | |
| <Route path="/media" element={ | |
| <MediaHubPage activeSpecId={activeSpecId} activeSpecName={activeSpecName}/> | |
| }/> | |
| <Route path="/usage" element={<UsagePage/>}/> | |
| <Route path="/settings" element={<SettingsPage/>}/> | |
| <Route path="/chat" element={ | |
| <ChatAssistant | |
| onOpenInExecutionPanel={() => navigate('/workflow')} | |
| executeStepFromChat={async () => { | |
| navigate('/workflow'); | |
| return {}; | |
| }} | |
| /> | |
| }/> | |
| <Route path="*" element={<Navigate to="/workflow" replace/>}/> | |
| </Routes> | |
| </main> | |
| </div> | |
| <ToastContainer toasts={toasts} removeToast={removeToast}/> | |
| </> | |
| ); | |
| } | |
| export default function App() { | |
| const [isAuthenticated, setIsAuthenticated] = useState(AuthService.isAuthenticated()); | |
| useEffect(() => { | |
| const handle = () => setIsAuthenticated(false); | |
| window.addEventListener('auth:logout', handle); | |
| return () => window.removeEventListener('auth:logout', handle); | |
| }, []); | |
| if (!isAuthenticated) { | |
| return <LoginPage onLogin={() => setIsAuthenticated(true)}/>; | |
| } | |
| return ( | |
| <BrowserRouter> | |
| <AppProvider> | |
| <ErrorBoundary> | |
| <MainApp/> | |
| </ErrorBoundary> | |
| </AppProvider> | |
| </BrowserRouter> | |
| ); | |
| } | |