import { useMemo, useState } from "react"; import { Button, Collapse, CopyButton, Group, Paper, ScrollArea, Stack, Text, Textarea } from "@mantine/core"; import { IconCheck, IconCopy, IconExternalLink } from "@tabler/icons-react"; import { ResponseTree } from "./response-tree"; export interface PreviewResponseMessages { empty: string; sampleHint: string; editSample: string; addSample: string; copied: string; copy: string; sampleLabel: string; sampleDescription: string; invalidSample: string; cancelSample: string; applySample: string; copyPath: string; pathCopied: string; insertPath: string; openRaw: string; } export interface PreviewResponsePanelProps { value: unknown; rawResponse?: string | null; onInsertDataPath?(path: string): void; onSampleDataChange?(value: unknown): void; messages: PreviewResponseMessages; } function openRawResponse(rawResponse: string) { const url = URL.createObjectURL(new Blob([rawResponse], { type: "application/json" })); window.open(url); window.setTimeout(() => URL.revokeObjectURL(url), 60_000); } export function PreviewResponsePanel({ value, rawResponse, onInsertDataPath, onSampleDataChange, messages, }: PreviewResponsePanelProps) { const responseJson = value == null ? "" : JSON.stringify(value, null, 2); const [draft, setDraft] = useState(responseJson); const [editing, setEditing] = useState(false); const error = useMemo(() => { if (!draft.trim()) return null; try { JSON.parse(draft); return null; } catch { return messages.invalidSample; } }, [draft, messages.invalidSample]); const edit = () => { setDraft(responseJson || JSON.stringify({ name: "Sample service", status: "online", value: 42 }, null, 2)); setEditing(true); }; const apply = () => { if (!draft.trim() || error) return; onSampleDataChange?.(JSON.parse(draft) as unknown); setEditing(false); }; return ( {responseJson ? messages.sampleHint : messages.empty} {onSampleDataChange && ( )} {responseJson && ( {({ copied, copy }) => ( )} )}