import React, { useEffect, useState } from 'react'; import { useStore } from '@nanostores/react'; import { debugIsOpen, activeTab, logs, type LogEntry } from '../../stores/debugStore'; import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { getDirectusClient } from '@/lib/directus/client'; // Create a client for the devtools if one doesn't exist in context // (Ideally this component is inside the main QueryClientProvider, but we'll see) const queryClient = new QueryClient(); export default function DebugToolbar() { const isOpen = useStore(debugIsOpen); const currentTab = useStore(activeTab); const logEntries = useStore(logs); const [backendStatus, setBackendStatus] = useState<'checking' | 'online' | 'error'>('checking'); const [latency, setLatency] = useState(null); useEffect(() => { if (isOpen && currentTab === 'backend') { checkBackend(); } }, [isOpen, currentTab]); const checkBackend = async () => { setBackendStatus('checking'); const start = performance.now(); try { const client = getDirectusClient(); await client.request(() => ({ path: '/server/ping', method: 'GET' })); setLatency(Math.round(performance.now() - start)); setBackendStatus('online'); } catch (e) { setBackendStatus('error'); } }; if (!isOpen) { return ( ); } return (
{/* Header */}
⚡ Spark Debug
{(['console', 'backend', 'network'] as const).map(tab => ( ))}
{/* Content */}
{/* Console Tab */} {currentTab === 'console' && (
{logEntries.length === 0 && (
No logs captured yet...
)} {logEntries.map((log) => (
[{log.timestamp}] {log.type} {log.messages.join(' ')}
))}
)} {/* Backend Tab */} {currentTab === 'backend' && (
{backendStatus === 'online' ? '● Online' : backendStatus === 'error' ? '✖ Error' : '● Checking...'}

Directus URL: {import.meta.env.PUBLIC_DIRECTUS_URL}

{latency && (

Latency: {latency}ms

)}
)} {/* Network / React Query Tab */} {currentTab === 'network' && (
{/* React Query Devtools needs a QueryClientProvider context. In Astro, components are islands. If this island doesn't share context with the main app (which it likely won't if they are separate roots), we might see empty devtools. However, putting it here is the best attempt. */}

React Query Devtools

(If empty, data fetching might be happening Server-Side or in a different Context)

{/* We force mount devtools panel here if possible */}
)}
); }