Spaces:
Runtime error
Runtime error
Download frontend-react/src/components/HistoryPage.jsx from Amitkasar/ai-api-testing: direct link, hf CLI and curl.
- Browser
- Download file 23.4 kB
-
https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/frontend-react/src/components/HistoryPage.jsx
- Command line
-
hf download hf://spaces/Amitkasar/ai-api-testing/frontend-react/src/components/HistoryPage.jsx
-
curl -L -o HistoryPage.jsx https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/frontend-react/src/components/HistoryPage.jsx
23.4 kB
| import React, { useState, useEffect } from 'react'; | |
| import { History, Clock, CheckCircle, XCircle, ChevronRight, Activity, Search, Filter, Trash2, ArrowRight, Zap, Download } from 'lucide-react'; | |
| import { HistoryService } from '../services/api'; | |
| import { useAppContext } from './AppContext'; | |
| const HistoryPage = () => { | |
| const { importSession, addToast } = useAppContext(); | |
| const [history, setHistory] = useState([]); | |
| const [isLoading, setIsLoading] = useState(true); | |
| const [searchQuery, setSearchQuery] = useState(''); | |
| const [expandedSessionId, setExpandedSessionId] = useState(null); | |
| const [expandedStepId, setExpandedStepId] = useState(null); | |
| useEffect(() => { | |
| fetchHistory(); | |
| }, []); | |
| const handleImport = (session) => { | |
| importSession(session); | |
| // Navigate to testing page | |
| const event = new CustomEvent('nav-change', { detail: 'testing' }); | |
| window.dispatchEvent(event); | |
| }; | |
| const fetchHistory = async () => { | |
| setIsLoading(true); | |
| try { | |
| const data = await HistoryService.getHistory(); | |
| setHistory(data); | |
| } catch (e) { | |
| console.error("Failed to fetch history", e); | |
| } finally { | |
| setIsLoading(false); | |
| } | |
| }; | |
| const handleExportSession = async (sessionId) => { | |
| try { | |
| await HistoryService.exportSession(sessionId); | |
| } catch (e) { | |
| console.error("Export failed", e); | |
| addToast('error', "Failed to export session."); | |
| } | |
| }; | |
| const handleDeleteSession = async (sessionId) => { | |
| if (!window.confirm("Are you sure you want to delete this test session?")) return; | |
| try { | |
| await HistoryService.deleteHistorySession(sessionId); | |
| setHistory(prev => prev.filter(item => item.session_id !== sessionId)); | |
| if (expandedSessionId === sessionId) setExpandedSessionId(null); | |
| } catch (e) { | |
| console.error("Delete failed", e); | |
| addToast('error', "Failed to delete session."); | |
| } | |
| }; | |
| const handleClearAll = async () => { | |
| if (!window.confirm("CRITICAL: This will permanently wipe ALL test history. Proceed?")) return; | |
| try { | |
| await HistoryService.clearAllHistory(); | |
| setHistory([]); | |
| setExpandedSessionId(null); | |
| } catch (e) { | |
| console.error("Clear failed", e); | |
| addToast('error', "Failed to clear history."); | |
| } | |
| }; | |
| // Grouping logic: Create a map of sessions, then convert to array | |
| const sessionsMap = (history || []).reduce((acc, curr) => { | |
| if (!curr) return acc; | |
| const sid = curr.session_id || 'unknown'; | |
| if (!acc[sid]) { | |
| acc[sid] = { | |
| session_id: sid, | |
| spec_id: curr.spec_id, | |
| spec_name: curr.spec_name || 'Unknown Spec', | |
| scenario: curr.scenario || 'No scenario text', | |
| timestamp: curr.timestamp, | |
| steps: [], | |
| status: 'success' | |
| }; | |
| } | |
| acc[sid].steps.push(curr); | |
| if (curr.analysis?.status === 'failure') acc[sid].status = 'failure'; | |
| // Capture context variables from the session metadata | |
| if (curr.context_variables && Object.keys(curr.context_variables).length > 0) { | |
| if (!acc[sid].context_variables) acc[sid].context_variables = {}; | |
| Object.assign(acc[sid].context_variables, curr.context_variables); | |
| } | |
| // Capture step definition if it exists (for restoration) | |
| if (curr.step_definition) { | |
| if (!acc[sid].plan_steps) acc[sid].plan_steps = {}; | |
| acc[sid].plan_steps[curr.step] = curr.step_definition; | |
| } | |
| // Keep the most recent timestamp as session timestamp | |
| if (new Date(curr.timestamp) > new Date(acc[sid].timestamp)) { | |
| acc[sid].timestamp = curr.timestamp; | |
| } | |
| return acc; | |
| }, {}); | |
| // Sort steps within sessions and convert map to sorted array | |
| const groupedSessions = Object.values(sessionsMap) | |
| .map(s => { | |
| // Reconstruct the plan from plan_steps or fallback to minimal info from steps | |
| const stepsForImport = Object.keys(s.plan_steps || {}).length > 0 | |
| ? Object.values(s.plan_steps).sort((a,b) => a.step - b.step) | |
| : s.steps.map(st => ({ | |
| step: st.step, | |
| endpoint: st.request.method + ' ' + st.request.url.split('/').pop(), | |
| purpose: st.analysis?.response_summary?.slice(0, 50) || 'Step execution' | |
| })).sort((a,b) => a.step - b.step); | |
| return { | |
| ...s, | |
| stepsForImport, | |
| steps: s.steps.sort((a, b) => a.step - b.step) | |
| }; | |
| }) | |
| .sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)); | |
| const filteredSessions = groupedSessions.filter(s => | |
| s.spec_name.toLowerCase().includes(searchQuery.toLowerCase()) || | |
| s.scenario.toLowerCase().includes(searchQuery.toLowerCase()) || | |
| s.steps.some(st => (st.analysis?.response_summary || '').toLowerCase().includes(searchQuery.toLowerCase())) | |
| ); | |
| const formatDate = (dateStr) => { | |
| const d = new Date(dateStr); | |
| return d.toLocaleString('en-US', { | |
| month: 'short', | |
| day: 'numeric', | |
| hour: '2-digit', | |
| minute: '2-digit' | |
| }); | |
| }; | |
| return ( | |
| <div className="flex-1 flex flex-col bg-[var(--bg-primary)] overflow-hidden animate-in fade-in slide-in-from-bottom-2 duration-300"> | |
| {/* Header */} | |
| <div className="h-14 border-b border-[var(--border-subtle)] flex items-center justify-between px-6 bg-[var(--bg-secondary)] shrink-0"> | |
| <div className="flex items-center space-x-4"> | |
| <h1 className="text-lg font-medium text-[var(--text-primary)] flex items-center space-x-2"> | |
| <History size={20} className="text-[var(--accent-primary)]" /> | |
| <span>Execution History</span> | |
| </h1> | |
| <span className="text-xs bg-[var(--bg-tertiary)] text-[var(--text-secondary)] px-2 py-0.5 rounded-full font-mono border border-[var(--border-subtle)]"> | |
| {groupedSessions.length} sessions | |
| </span> | |
| </div> | |
| <div className="flex items-center space-x-3"> | |
| <div className="relative"> | |
| <Search className="absolute left-3 top-1/2 -translate-y-1/2 text-[var(--text-secondary)]" size={14} /> | |
| <input | |
| type="text" | |
| placeholder="Search specs, scenarios..." | |
| value={searchQuery} | |
| onChange={e => setSearchQuery(e.target.value)} | |
| className="bg-[var(--bg-tertiary)] border border-[var(--border-subtle)] text-[var(--text-primary)] pl-9 pr-4 py-1.5 text-xs rounded-lg outline-none focus:border-[var(--accent-primary)] w-64 transition-all" | |
| /> | |
| </div> | |
| <button | |
| onClick={fetchHistory} | |
| className="p-2 text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] rounded-lg transition-colors" | |
| title="Refresh History" | |
| > | |
| <Activity size={18} /> | |
| </button> | |
| {groupedSessions.length > 0 && ( | |
| <button | |
| onClick={handleClearAll} | |
| className="px-3 py-1.5 bg-red-500/10 hover:bg-red-500/20 text-red-500 text-[10px] font-bold uppercase tracking-wider rounded-lg border border-red-500/20 transition-all flex items-center space-x-2" | |
| title="Clear All History" | |
| > | |
| <Trash2 size={14} /> | |
| <span>Clear All</span> | |
| </button> | |
| )} | |
| </div> | |
| </div> | |
| {isLoading ? ( | |
| <div className="flex-1 flex flex-col items-center justify-center space-y-4 opacity-50"> | |
| <Activity size={32} className="text-[var(--accent-primary)] animate-pulse" /> | |
| <p className="text-sm font-light text-[var(--text-secondary)]">Loading history records...</p> | |
| </div> | |
| ) : filteredSessions.length === 0 ? ( | |
| <div className="flex-1 flex flex-col items-center justify-center p-12 text-center"> | |
| <div className="w-16 h-16 bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded-2xl flex items-center justify-center mb-6"> | |
| <History size={32} className="text-[var(--text-secondary)] opacity-30" /> | |
| </div> | |
| <h3 className="text-lg font-light text-[var(--text-primary)]">No History Found</h3> | |
| <p className="text-sm text-[var(--text-secondary)] max-w-xs mt-2"> | |
| Run some test scenarios on the Workflow page to see execution logs appear here. | |
| </p> | |
| </div> | |
| ) : ( | |
| <div className="flex-1 overflow-y-auto p-6"> | |
| <div className="max-w-5xl mx-auto space-y-4"> | |
| {filteredSessions.map((session) => ( | |
| <div | |
| key={session.session_id} | |
| className={`group border transition-all duration-200 overflow-hidden rounded-xl ${ | |
| expandedSessionId === session.session_id | |
| ? 'bg-[var(--bg-secondary)] border-[var(--accent-primary)]/50 shadow-2xl' | |
| : 'bg-[var(--bg-secondary)]/50 border-[var(--border-subtle)] hover:border-[var(--text-secondary)]/30' | |
| }`} | |
| > | |
| {/* Session Header (Collapsible) */} | |
| <div | |
| onClick={() => setExpandedSessionId(expandedSessionId === session.session_id ? null : session.session_id)} | |
| className="p-4 flex items-center cursor-pointer select-none" | |
| > | |
| <div className={`mr-4 p-2 rounded-lg ${ | |
| session.status === 'success' ? 'bg-emerald-500/10' : 'bg-red-500/10' | |
| }`}> | |
| {session.status === 'success' | |
| ? <Zap size={20} className="text-emerald-500" /> | |
| : <XCircle size={20} className="text-red-500" /> | |
| } | |
| </div> | |
| <div className="flex-1 min-w-0"> | |
| <div className="flex items-center space-x-2 text-[10px] font-bold uppercase tracking-wider text-[var(--text-secondary)] mb-1"> | |
| <span className="text-[var(--accent-primary)]">{session.spec_name}</span> | |
| <span>•</span> | |
| <span>{session.steps.length} steps</span> | |
| </div> | |
| <div className="text-sm text-[var(--text-primary)] font-medium truncate"> | |
| {session.scenario} | |
| </div> | |
| </div> | |
| <div className="flex items-center shrink-0 ml-6 space-x-4"> | |
| <button | |
| onClick={(e) => { e.stopPropagation(); handleDeleteSession(session.session_id); }} | |
| className="p-2 text-[var(--text-secondary)] hover:text-red-500 hover:bg-red-500/10 rounded-lg transition-all" | |
| title="Delete Session" | |
| > | |
| <Trash2 size={16} /> | |
| </button> | |
| <button | |
| onClick={(e) => { e.stopPropagation(); handleExportSession(session.session_id); }} | |
| className="p-2 text-[var(--text-secondary)] hover:text-[var(--accent-secondary)] hover:bg-[var(--bg-tertiary)] rounded-lg transition-all flex items-center space-x-2 group/btn" | |
| title="Export Session as JSON" | |
| > | |
| <Download size={16} className="group-hover/btn:-translate-y-0.5 transition-transform" /> | |
| <span className="text-[10px] uppercase font-bold tracking-tight hidden group-hover/btn:inline">Export</span> | |
| </button> | |
| <button | |
| onClick={(e) => { e.stopPropagation(); handleImport(session); }} | |
| className="p-2 text-[var(--text-secondary)] hover:text-[var(--accent-primary)] hover:bg-[var(--bg-tertiary)] rounded-lg transition-all flex items-center space-x-2 group/btn" | |
| title="Import to Testing Workspace" | |
| > | |
| <ArrowRight size={16} className="group-hover/btn:translate-x-0.5 transition-transform" /> | |
| <span className="text-[10px] uppercase font-bold tracking-tight hidden group-hover/btn:inline">Import</span> | |
| </button> | |
| <div className="flex flex-col items-end"> | |
| <div className="flex items-center text-xs text-[var(--text-secondary)] font-mono mb-1"> | |
| <Clock size={12} className="mr-1" /> | |
| {formatDate(session.timestamp)} | |
| </div> | |
| <ChevronRight | |
| size={16} | |
| className={`text-[var(--text-secondary)] transition-transform duration-200 ${expandedSessionId === session.session_id ? 'rotate-90' : ''}`} | |
| /> | |
| </div> | |
| </div> | |
| </div> | |
| {/* Session Steps (Expanded View) */} | |
| {expandedSessionId === session.session_id && ( | |
| <div className="px-4 pb-6 overflow-hidden animate-in slide-in-from-top-2 duration-200"> | |
| {/* Session Extraction History */} | |
| {session.context_variables && Object.keys(session.context_variables).length > 0 && ( | |
| <div className="mb-6 bg-[var(--bg-primary)] border border-[var(--border-subtle)] rounded-lg p-4"> | |
| <h4 className="text-[10px] font-bold uppercase text-[var(--text-secondary)] mb-3 tracking-wider flex items-center"> | |
| <Activity size={12} className="mr-2 text-[var(--accent-primary)]" /> | |
| Session Extraction History | |
| </h4> | |
| <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-3"> | |
| {Object.entries(session.context_variables).map(([k, v]) => ( | |
| <div key={k} className="flex flex-col space-y-1 bg-[var(--bg-tertiary)] p-2 rounded border border-[var(--border-subtle)]"> | |
| <span className="text-[9px] text-[var(--accent-primary)] font-mono">{k}</span> | |
| <span className="text-xs text-[var(--accent-secondary)] truncate">"{String(v)}"</span> | |
| </div> | |
| ))} | |
| </div> | |
| </div> | |
| )} | |
| <div className="pt-4 border-t border-[var(--border-subtle)] space-y-3"> | |
| {session.steps.map((item) => ( | |
| <div key={item.id} className="border border-[var(--border-subtle)] rounded-lg overflow-hidden bg-[var(--bg-primary)]"> | |
| <div | |
| onClick={() => setExpandedStepId(expandedStepId === item.id ? null : item.id)} | |
| className="p-3 flex items-center justify-between cursor-pointer hover:bg-[var(--bg-tertiary)] transition-colors" | |
| > | |
| <div className="flex items-center space-x-3"> | |
| <span className={`px-2 py-0.5 rounded text-[10px] font-mono ${ | |
| item.analysis?.status === 'success' ? 'bg-green-500/20 text-green-400' : 'bg-red-500/20 text-red-400' | |
| }`}> | |
| Step {item.step} | |
| </span> | |
| <span className="text-xs text-[var(--text-primary)] font-medium">{item.request.method} {item.request.url.split('/').pop()}</span> | |
| </div> | |
| <ChevronRight | |
| size={14} | |
| className={`text-[var(--text-secondary)] transition-transform duration-200 ${expandedStepId === item.id ? 'rotate-90' : ''}`} | |
| /> | |
| </div> | |
| {/* Step Details */} | |
| {expandedStepId === item.id && ( | |
| <div className="p-4 bg-[var(--bg-primary)] border-t border-[var(--border-subtle)] space-y-4 animate-in fade-in duration-200"> | |
| {/* AI Analysis Result */} | |
| <div className={`p-4 rounded-lg border-l-4 ${ | |
| item.analysis?.status === 'success' ? 'border-emerald-500 bg-emerald-500/5' : 'border-red-500 bg-red-500/5' | |
| }`}> | |
| <div className="text-[10px] font-bold uppercase text-[var(--text-secondary)] mb-2">AI Step Analysis</div> | |
| <p className="text-xs text-[var(--text-primary)] leading-relaxed"> | |
| {item.analysis?.response_summary || "Summary not available."} | |
| </p> | |
| {item.analysis?.extracted_variables && Object.keys(item.analysis.extracted_variables).length > 0 && ( | |
| <div className="mt-3 flex flex-wrap gap-2"> | |
| {Object.entries(item.analysis.extracted_variables).map(([k, v]) => ( | |
| <div key={k} className="px-2 py-1 bg-[var(--bg-tertiary)] rounded text-[9px] text-[var(--accent-primary)] font-mono border border-[var(--border-subtle)]"> | |
| {k}: <span className="text-[var(--accent-secondary)]">"{String(v)}"</span> | |
| </div> | |
| ))} | |
| </div> | |
| )} | |
| </div> | |
| {/* Request/Response Grid */} | |
| <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> | |
| <div className="space-y-2"> | |
| <div className="text-[9px] uppercase font-bold text-[var(--text-secondary)] flex items-center"> | |
| <ArrowRight size={10} className="mr-1" /> | |
| Request Details | |
| </div> | |
| <div className="bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded p-3 text-[10px] font-mono overflow-auto max-h-48 space-y-3"> | |
| <div className="text-[var(--accent-primary)] font-bold mb-2 break-all">{item.request.method} {item.request.url}</div> | |
| {item.request.headers && Object.keys(item.request.headers).length > 0 && ( | |
| <div> | |
| <div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Headers</div> | |
| {Object.entries(item.request.headers).map(([k,v]) => ( | |
| <div key={k} className="text-[var(--accent-primary)]"><span className="text-[var(--text-secondary)]">{k}:</span> {v}</div> | |
| ))} | |
| </div> | |
| )} | |
| {item.request.query_params && Object.keys(item.request.query_params).length > 0 && ( | |
| <div> | |
| <div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Query Params</div> | |
| {Object.entries(item.request.query_params).map(([k,v]) => ( | |
| <div key={k} className="text-[var(--accent-primary)]"><span className="text-[var(--text-secondary)]">{k}=</span>{v}</div> | |
| ))} | |
| </div> | |
| )} | |
| {item.request.body && <div> | |
| <div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Body</div> | |
| <pre className="text-[var(--text-secondary)]">{JSON.stringify(item.request.body, null, 2)}</pre> | |
| </div>} | |
| {item.request.form_data && Object.keys(item.request.form_data).length > 0 && ( | |
| <div> | |
| <div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Form Data</div> | |
| <pre className="text-[var(--text-secondary)]">{JSON.stringify(item.request.form_data, null, 2)}</pre> | |
| </div> | |
| )} | |
| {item.request.files && Object.keys(item.request.files).length > 0 && ( | |
| <div> | |
| <div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Files</div> | |
| {Object.entries(item.request.files).map(([k,v]) => ( | |
| <div key={k} className="text-[var(--accent-secondary)]"><span className="text-[var(--text-secondary)]">{k}:</span> {v}</div> | |
| ))} | |
| </div> | |
| )} | |
| </div> | |
| </div> | |
| <div className="space-y-2"> | |
| <div className="text-[9px] uppercase font-bold text-[var(--text-secondary)] flex items-center"> | |
| <Zap size={10} className="mr-1" /> | |
| Response Body ({item.response.status_code}) | |
| </div> | |
| <div className="bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded p-3 text-[10px] font-mono overflow-auto max-h-48"> | |
| <pre className="text-[var(--accent-secondary)]"> | |
| {(() => { | |
| try { return JSON.stringify(JSON.parse(item.response.body), null, 2); } | |
| catch { return item.response.body; } | |
| })()} | |
| </pre> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| ))} | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| ))} | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| }; | |
| export default HistoryPage; | |