'use client'; import { Loader2, Send, Terminal, WifiOff } from 'lucide-react'; import { useEffect, useState } from 'react'; import { usePlaygroundContext } from '../../../context/PlaygroundContext'; import { EmptyState, ScrollArea } from '../ui'; import { PreviewView } from './PreviewView'; import { PrettyView } from './PrettyView'; import { RawView } from './RawView'; import { StatusBar } from './StatusBar'; import type { ViewMode } from './types'; import { useResponseView } from './useResponseView'; import { ViewTabs } from './ViewTabs'; /** Response panel used by both the sticky ``SlideInPlayground`` and the * modal ``TryItSheet``. Responsibilities: * - Empty / loading / network-error gates * - Three view modes (Pretty / Raw / Preview) with a tab bar * - Auto-pick the best default view per response type (JSON → Pretty, * HTML → Preview, everything else → Pretty as the widest-purpose) * * View mode is local component state rather than context state — it * should reset when the selected endpoint changes and not leak between * endpoints. */ export function ResponsePanel() { const { state } = usePlaygroundContext(); const { response, loading, selectedEndpoint } = state; const { treeData, rawText, detected } = useResponseView(response?.data, response?.headers); const showPreview = detected.kind === 'html'; // Default view heuristic: show HTML pages preview-first so the // reader sees the actual rendered server page immediately. // Everything else lands on Pretty. const [mode, setMode] = useState(showPreview ? 'preview' : 'pretty'); // Reset the mode when the endpoint or response changes so a // previously-selected tab from a different shape doesn't stick // around (e.g. stuck on Preview when switching from HTML→JSON). useEffect(() => { setMode(showPreview ? 'preview' : 'pretty'); }, [selectedEndpoint, response, showPreview]); if (loading) { return (
Sending…
); } if (!selectedEndpoint) return ; if (!response) return ; const hasError = Boolean(response.error); const hasStatus = response.status != null; // Pure network error (no HTTP response at all — CORS, offline, timeout) if (hasError && !hasStatus) { return ( ); } return ( <> {/* HTTP-level error body (4xx/5xx — has status but also error flag) */} {hasError && (

{response.error}

)} {mode === 'pretty' && ( )} {mode === 'raw' && } {mode === 'preview' && } ); }