import type { HttpResponse, HttpResponseEvent } from "@yaakapp-internal/models"; import { CountBadge, type ExtraPaneTab, type HostBodyViewerArgs, HttpResponsePaneView, Icon, type ResponseTextViewerExtras, type TextFilterApi, Tooltip, VStack, } from "@yaakapp-internal/ui"; import type { CSSProperties } from "react"; import { useCallback, useMemo } from "react"; import { useCancelHttpResponse } from "../hooks/useCancelHttpResponse"; import { useCopyHttpResponse } from "../hooks/useCopyHttpResponse"; import { useRequestBodyLoader, useResponseBodyLoader } from "../hooks/useHttpBodyLoaders"; import { useHttpResponseEvents } from "../hooks/useHttpResponseEvents"; import { usePinnedHttpResponse } from "../hooks/usePinnedHttpResponse"; import { useRecentFilters } from "../hooks/useRecentFilters"; import { useResponseViewMode } from "../hooks/useResponseViewMode"; import { useSaveResponse } from "../hooks/useSaveResponse"; import { useTimelineViewMode } from "../hooks/useTimelineViewMode"; import { httpResponseToView } from "../lib/httpViews"; import { getCookieCounts } from "../lib/model_util"; import { CopyButton } from "./CopyButton"; import { HotkeyList } from "./core/HotkeyList"; import { IconButton } from "./core/IconButton"; import { PillButton } from "./core/PillButton"; import { HttpResponseTimeline } from "./HttpResponseTimeline"; import { RecentHttpResponsesDropdown } from "./RecentHttpResponsesDropdown"; import { EventStreamViewer } from "./responseViewers/EventStreamViewer"; import { RecentFiltersDropdown } from "./responseViewers/RecentFiltersDropdown"; interface Props { style?: CSSProperties; className?: string; activeRequestId: string; } const TAB_TIMELINE = "timeline"; export type TimelineViewMode = "timeline" | "text"; interface RedirectDropWarning { droppedBodyCount: number; droppedHeaders: string[]; } /** The shared response pane, bound to the pinned response of the active request */ export function HttpResponsePane({ style, className, activeRequestId }: Props) { const { activeResponse, setPinnedResponseId, responses } = usePinnedHttpResponse(activeRequestId); const [viewMode, setViewMode] = useResponseViewMode(activeResponse?.requestId); const [timelineViewMode, setTimelineViewMode] = useTimelineViewMode(); const responseEvents = useHttpResponseEvents(activeResponse); const redirectDropWarning = useMemo( () => getRedirectDropWarning(responseEvents.data), [responseEvents.data], ); const shouldShowRedirectDropWarning = activeResponse?.state === "closed" && redirectDropWarning != null; const cookieCounts = useMemo(() => getCookieCounts(responseEvents.data), [responseEvents.data]); const saveResponse = useSaveResponse(activeResponse ?? null); const copyResponse = useCopyHttpResponse(activeResponse ?? null); const cancel = useCancelHttpResponse(activeResponse?.id ?? null); const body = useResponseBodyLoader(activeResponse ?? null); const requestBody = useRequestBodyLoader(activeResponse ?? null); const textViewer = useResponseTextViewerExtras(activeResponse ?? null); const responseView = useMemo( () => (activeResponse == null ? null : httpResponseToView(activeResponse, responseEvents.data)), [activeResponse, responseEvents.data], ); const extraTabs = useMemo( () => activeResponse == null ? [] : [ { tab: { value: TAB_TIMELINE, rightSlot: , options: { value: timelineViewMode, onChange: (v) => setTimelineViewMode((v as TimelineViewMode) ?? "timeline"), items: [ { label: "Timeline", value: "timeline" }, { label: "Timeline (Text)", shortLabel: "Timeline", value: "text" }, ], }, }, content: ( ), }, ], [activeResponse, responseEvents.data?.length, setTimelineViewMode, timelineViewMode], ); const renderBodyViewer = useCallback( ({ mimeType, viewMode }: HostBodyViewerArgs) => activeResponse != null && mimeType?.match(/^text\/event-stream/i) && viewMode === "pretty" ? ( ) : null, [activeResponse], ); return ( } viewMode={viewMode === "raw" ? "raw" : "pretty"} onViewModeChange={setViewMode} activeTabKey={activeRequestId} onSaveBody={saveResponse.mutate} onCopyBody={copyResponse.mutate} onCancel={cancel.mutate} cookieCounts={cookieCounts} extraTabs={extraTabs} textViewer={textViewer} renderBodyViewer={renderBodyViewer} renderCopyButton={renderCopyButton} statusLineCenter={ shouldShowRedirectDropWarning ? ( Redirect changed this request {redirectDropWarning.droppedBodyCount > 0 && ( Body dropped on {redirectDropWarning.droppedBodyCount}{" "} {redirectDropWarning.droppedBodyCount === 1 ? "redirect hop" : "redirect hops"} )} {redirectDropWarning.droppedHeaders.length > 0 && ( Headers dropped:{" "} {redirectDropWarning.droppedHeaders.join(", ")} )} See Timeline for details. } > } > {getRedirectWarningLabel(redirectDropWarning)} ) : null } statusLineRight={ activeResponse != null && ( ) } /> ); } /** The client's additions to the response text viewer: filter history, save and copy */ function useResponseTextViewerExtras( response: HttpResponse | null, ): ResponseTextViewerExtras | undefined { const filterStateKey = `response.body.${response?.requestId ?? ""}`; const { recentFilters, addFilter, removeFilter, togglePin, clearFilters } = useRecentFilters(filterStateKey); const saveResponse = useSaveResponse(response); const copyResponse = useCopyHttpResponse(response); const filterLeftSlot = useCallback( (filter: TextFilterApi) => (
), [recentFilters, removeFilter, togglePin, clearFilters], ); if (response == null) return undefined; const actionsDisabled = response.state !== "closed" && response.status >= 100; return { filterStateKey, onFilterApplied: addFilter, filterLeftSlot, footerActions: [ saveResponse.mutate()} className="border !border-border-subtle" />, copyResponse.mutate()} className="border !border-border-subtle" />, ], }; } function renderCopyButton(getText: () => Promise) { return ; } function getRedirectDropWarning( events: HttpResponseEvent[] | undefined, ): RedirectDropWarning | null { if (events == null || events.length === 0) return null; let droppedBodyCount = 0; const droppedHeaders = new Set(); for (const e of events) { const event = e.event; if (event.type !== "redirect") { continue; } if (event.dropped_body) { droppedBodyCount += 1; } for (const headerName of event.dropped_headers ?? []) { pushHeaderName(droppedHeaders, headerName); } } if (droppedBodyCount === 0 && droppedHeaders.size === 0) { return null; } return { droppedBodyCount, droppedHeaders: Array.from(droppedHeaders).sort(), }; } function pushHeaderName(headers: Set, headerName: string): void { const existing = Array.from(headers).find((h) => h.toLowerCase() === headerName.toLowerCase()); if (existing == null) { headers.add(headerName); } } function getRedirectWarningLabel(warning: RedirectDropWarning): string { if (warning.droppedBodyCount > 0 && warning.droppedHeaders.length > 0) { return "Dropped body and headers"; } if (warning.droppedBodyCount > 0) { return "Dropped body"; } return "Dropped headers"; }