"use client"; import * as React from "react"; import { RotateCcwIcon, ZoomInIcon, ZoomOutIcon } from "lucide-react"; import ReactMarkdown, { type Components, defaultUrlTransform } from "react-markdown"; import remarkGfm from "remark-gfm"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent } from "@/components/ui/dialog"; import { type UITranslate, useUITranslation } from "@/i18n/use-ui-translation"; import { apiFetch } from "@/lib/api"; import { auth } from "@/lib/auth"; function AuthenticatedImage({ src, alt, ...props }: React.ImgHTMLAttributes) { const { ui, liveUI } = useUITranslation(); const [objectURL, setObjectURL] = React.useState(null); const [captureTimestamp, setCaptureTimestamp] = React.useState(null); const [zoomed, setZoomed] = React.useState(false); const [zoomScale, setZoomScale] = React.useState(1); const [pan, setPan] = React.useState({ x: 0, y: 0 }); const dragRef = React.useRef<{ x: number; y: number; panX: number; panY: number } | null>(null); const imageSrc = typeof src === "string" ? src : undefined; React.useEffect(() => { let cancelled = false; let url: string | null = null; if (!imageSrc || !imageSrc.startsWith("/api/workspace/preview")) { setObjectURL(null); setCaptureTimestamp(null); setZoomed(false); setZoomScale(1); setPan({ x: 0, y: 0 }); return () => { cancelled = true; }; } const token = auth.getToken(); setCaptureTimestamp(null); apiFetch(imageSrc, { credentials: "same-origin", headers: token ? { Authorization: `Bearer ${token}` } : undefined, }) .then((response) => { if (!response.ok) throw new Error(liveUI("screen.3cb4ca5e3686db", { v0: response.status })); setCaptureTimestamp(response.headers.get("X-Report-Screenshot-Timestamp")); return response.blob(); }) .then((blob) => { if (cancelled) return; url = URL.createObjectURL(blob); setObjectURL(url); }) .catch(() => { if (!cancelled) setObjectURL(null); }); return () => { cancelled = true; if (url) URL.revokeObjectURL(url); }; }, [imageSrc, liveUI]); const renderedSrc = objectURL ?? imageSrc; const timestamp = captureTimestamp ? ( {captureTimestamp} ) : null; return ( <> { setZoomed(next); if (!next) { setZoomScale(1); setPan({ x: 0, y: 0 }); } }} >
{ if (zoomScale <= 1 || event.button !== 0) return; event.currentTarget.setPointerCapture(event.pointerId); dragRef.current = { x: event.clientX, y: event.clientY, panX: pan.x, panY: pan.y }; }} onPointerMove={(event) => { const drag = dragRef.current; if (!drag) return; setPan({ x: drag.panX + event.clientX - drag.x, y: drag.panY + event.clientY - drag.y }); }} onPointerUp={(event) => { dragRef.current = null; event.currentTarget.releasePointerCapture?.(event.pointerId); }} onPointerCancel={() => { dragRef.current = null; }} style={{ cursor: zoomScale > 1 ? (dragRef.current ? "grabbing" : "grab") : "default" }} >
{alt event.preventDefault()} className="max-h-[90dvh] max-w-[90vw] rounded-md object-contain" /> {timestamp}
{Math.round(zoomScale * 100)}%
); } // Tailwind-styled element overrides (no typography plugin in this project, so we // style each element). `node` is stripped — it's not a valid DOM attribute. const components = (ui: UITranslate): Components => ({ h1: ({ node, ...p }) =>
, h2: ({ node, ...p }) =>
, h3: ({ node, ...p }) =>
, h4: ({ node, ...p }) =>
, p: ({ node, ...p }) =>

, ul: ({ node, ...p }) =>