import deepEqual from "@gilbarbara/deep-equal"; import { useMutation } from "@tanstack/react-query"; import type { AssertionReport, HttpAssertions, HttpResponse } from "@yaakapp-internal/models"; import { format } from "date-fns"; import { rpc } from "../lib/rpc"; import { Button } from "./core/Button"; import { HttpAssertionReport } from "./HttpAssertionResults"; interface Props { assertions: HttpAssertions; response: HttpResponse | null; } export function HttpAssertionPreview(props: Props) { // A response switch (including a new send) discards results and in-flight UI // state. The old evaluation may finish, but it cannot label the new response. return ; } function ResponsePreview({ assertions, response }: Props) { const preview = useMutation({ mutationFn: (definition: HttpAssertions) => rpc("cmd_preview_http_assertions", { responseId: response?.id, assertions: definition, }), retry: false, gcTime: 0, }); const unavailable = !response ? "Send a request first to get a response to try." : response.state !== "closed" ? "Wait for the response to finish." : !assertions.checks.some((check) => check.enabled) ? "Enable an assertion to try it." : null; const changed = preview.data != null && !deepEqual(preview.data.definition, assertions); return (

{unavailable ?? "Preview against the selected response without sending again."}

{preview.isError && (

Could not preview assertions:{" "} {preview.error instanceof Error ? preview.error.message : String(preview.error)}

)} {preview.data && response && (

Preview

Response from {format(`${response.createdAt}Z`, "MMM d, h:mm:ss a")}. Recorded results are unchanged.

{changed && (

Assertions changed. Try again to update this preview.

)}
)}
); }