import { useQuery } from "@tanstack/react-query"; import { formatSize } from "@yaakapp-internal/lib/formatSize"; import type { Flow, FlowBody } from "@yaakapp-internal/proxy-lib"; import { Banner, CsvViewer, ImageViewer, languageFromContentType, LoadingIcon, MultipartViewer, SvgViewer, TextViewer, WebPageViewer, } from "@yaakapp-internal/ui"; import type { CodeEditorProps } from "@yaakapp-internal/ui/editor"; import type { ReactNode } from "react"; import { useCallback, useEffect, useState } from "react"; import { base64ToBytes, bytesToText, filterText, findHeader, formatText, mimeEssence, } from "../lib/body"; import { rpc } from "../lib/rpc"; interface Props { flow: Flow; which: FlowBody; pretty: boolean; } export function useFlowBody(flow: Flow, which: FlowBody, enabled = true) { return useQuery({ enabled, queryKey: ["get_flow_body", flow.id, which, flow.updatedAt], queryFn: async () => { const res = await rpc("get_flow_body", { flowId: flow.id, which }); return { bytes: base64ToBytes(res.data), truncated: res.truncated, storedSize: res.storedSize, }; }, }); } export function FlowBodyViewer({ flow, which, pretty }: Props) { const body = useFlowBody(flow, which); if (body.isPending) { return ( ); } if (body.error) { return {body.error.message}; } const { bytes, truncated, storedSize } = body.data; if (bytes.length === 0) { return {which === "request" ? "No request body" : "Empty"}; } const headers = which === "request" ? flow.requestHeaders : flow.responseHeaders; const contentType = findHeader(headers, "content-type"); const mime = mimeEssence(contentType) ?? ""; const wireSize = which === "request" ? flow.requestSize : flow.responseSize; const wireTruncated = which === "request" ? flow.requestTruncated : flow.responseTruncated; let viewer: ReactNode; if (mime.startsWith("image/svg")) { viewer = ; } else if (mime.startsWith("image/")) { viewer = ; } else if (mime.startsWith("multipart/") && pretty) { const boundary = contentType?.match(/;\s*boundary="?([^";]+)"?/i)?.[1] ?? "unknown"; viewer = ( ); } else if (/csv|tab-separated/.test(mime) && pretty) { viewer = ; } else { const text = bytesToText(bytes, truncated); const language = languageFromContentType(contentType, text); if (text == null) { viewer = Binary body, {formatSize(bytes.length)}; } else if (language === "html" && pretty && which === "response") { viewer = ; } else { viewer = ( ); } } if (!truncated) return viewer; return (
{wireTruncated ? `Showing the first ${formatSize(storedSize)} of ${formatSize(wireSize)}, the capture limit. The app received all of it.` : `Showing the first ${formatSize(bytes.length)} of the decoded body, the capture limit. The app received all of it.`}
{viewer}
); } /** A text body, filtered with the syntax for the language detected from it, not just its header */ function FlowTextViewer({ flow, which, text, language, pretty, }: Props & { text: string; language: CodeEditorProps["language"] }) { const runFilter = useCallback( (expression: string) => filterText(flow.id, which, expression, language), [flow.id, which, language], ); return ( ); } function ImageBody({ bytes, mime }: { bytes: Uint8Array; mime: string }) { const [size, setSize] = useState<{ width: number; height: number } | null>(null); useEffect(() => { let cancelled = false; createImageBitmap(new Blob([bytes], { type: mime })).then( (bitmap) => { if (!cancelled) setSize({ width: bitmap.width, height: bitmap.height }); bitmap.close(); }, () => { if (!cancelled) setSize(null); }, ); return () => { cancelled = true; }; }, [bytes, mime]); return (
{size != null && `${size.width} × ${size.height} • `} {mime} • {formatSize(bytes.length)}
); } export function EmptyState({ children }: { children: ReactNode }) { return (
{children}
); }