The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useGT } from "gt-react"; |
| 2 | import { useLayoutEffect, useRef, useState } from "react"; |
| 3 | import { MaximizeIcon, MinusIcon, PlusIcon, ScanIcon } from "lucide-react"; |
| 4 | import { ExpandedImageDialog } from "~/components/chat/ExpandedImageDialog"; |
| 5 | import { observeResize } from "~/lib/observeResize"; |
| 6 | import { AttachmentImage } from "../media/AttachmentImage"; |
| 7 | import { FileSurfaceAction, FileSurfaceFailure } from "./fileSurfaceChrome"; |
| 8 | |
| 9 | export default function PreviewImageContent(props: { |
| 10 | readonly src: string; |
| 11 | readonly name: string; |
| 12 | readonly mimeType?: string; |
| 13 | readonly onError?: () => void; |
| 14 | }) { |
| 15 | const gt = useGT(); |
| 16 | const [failed, setFailed] = useState(false); |
| 17 | const [expanded, setExpanded] = useState(false); |
| 18 | const [natural, setNatural] = useState({ width: 0, height: 0 }); |
| 19 | const [viewport, setViewport] = useState({ width: 0, height: 0 }); |
| 20 | const [zoom, setZoom] = useState<number | null>(null); |
| 21 | const ref = useRef<HTMLDivElement>(null); |
| 22 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Measure the image viewport before paint and observe resizes to keep fit-to-view scaling accurate. |
| 23 | useLayoutEffect(() => { |
| 24 | const node = ref.current; |
| 25 | if (!node) return; |
| 26 | const update = () => setViewport({ width: node.clientWidth, height: node.clientHeight }); |
| 27 | update(); |
| 28 | return observeResize(node, update); |
| 29 | }, []); |
| 30 | const fit = natural.width |
| 31 | ? Math.min( |
| 32 | 1, |
| 33 | Math.max(1, viewport.width - 32) / natural.width, |
| 34 | Math.max(1, viewport.height - 32) / natural.height, |
| 35 | ) |
| 36 | : 1; |
| 37 | const scale = zoom ?? fit; |
| 38 | const changeZoom = (factor: number) => setZoom(Math.max(0.05, Math.min(8, scale * factor))); |
| 39 | if (failed) return <FileSurfaceFailure message={gt("Unable to load image.")} />; |
| 40 | return ( |
| 41 | <> |
| 42 | <div className="flex min-h-10 shrink-0 flex-wrap items-center justify-between gap-2 border-b border-border/60 px-3 py-1 text-xs text-muted-foreground"> |
| 43 | <span className="tabular-nums"> |
| 44 | {natural.width |
| 45 | ? `${natural.width.toLocaleString()} × ${natural.height.toLocaleString()}` |
| 46 | : gt("Loading image…")} |
| 47 | </span> |
| 48 | <div className="flex items-center gap-1"> |
| 49 | <FileSurfaceAction |
| 50 | label={gt("Zoom out")} |
| 51 | disabled={!natural.width || scale <= 0.05} |
| 52 | onPress={() => changeZoom(1 / 1.25)} |
| 53 | > |
| 54 | <MinusIcon className="size-3.5" /> |
| 55 | </FileSurfaceAction> |
| 56 | <span className="min-w-10 text-center tabular-nums">{Math.round(scale * 100)}%</span> |
| 57 | <FileSurfaceAction |
| 58 | label={gt("Zoom in")} |
| 59 | disabled={!natural.width || scale >= 8} |
| 60 | onPress={() => changeZoom(1.25)} |
| 61 | > |
| 62 | <PlusIcon className="size-3.5" /> |
| 63 | </FileSurfaceAction> |
| 64 | <FileSurfaceAction |
| 65 | label={gt("Fit image")} |
| 66 | pressed={zoom === null} |
| 67 | onPress={() => setZoom(null)} |
| 68 | > |
| 69 | <ScanIcon className="size-3.5" /> |
| 70 | </FileSurfaceAction> |
| 71 | <FileSurfaceAction |
| 72 | label={gt("Actual image size")} |
| 73 | pressed={zoom === 1} |
| 74 | onPress={() => setZoom(1)} |
| 75 | > |
| 76 | <MaximizeIcon className="size-3.5" /> |
| 77 | </FileSurfaceAction> |
| 78 | </div> |
| 79 | </div> |
| 80 | <div ref={ref} className="min-h-0 flex-1 overflow-auto"> |
| 81 | <div |
| 82 | className="flex min-h-full min-w-full items-center justify-center p-4" |
| 83 | style={{ |
| 84 | width: natural.width ? natural.width * scale + 32 : undefined, |
| 85 | height: natural.height ? natural.height * scale + 32 : undefined, |
| 86 | }} |
| 87 | > |
| 88 | <button |
| 89 | type="button" |
| 90 | aria-label={gt("Zoom {name}", { name: props.name })} |
| 91 | className="shrink-0 cursor-zoom-in" |
| 92 | onClick={() => setExpanded(true)} |
| 93 | > |
| 94 | <AttachmentImage |
| 95 | name={props.name} |
| 96 | mimeType={props.mimeType} |
| 97 | src={props.src} |
| 98 | alt={props.name} |
| 99 | className="block max-w-none" |
| 100 | style={ |
| 101 | natural.width |
| 102 | ? { width: natural.width * scale, height: natural.height * scale } |
| 103 | : { maxWidth: "100%" } |
| 104 | } |
| 105 | onLoad={(event) => |
| 106 | setNatural({ |
| 107 | width: event.currentTarget.naturalWidth, |
| 108 | height: event.currentTarget.naturalHeight, |
| 109 | }) |
| 110 | } |
| 111 | onError={() => { |
| 112 | setFailed(true); |
| 113 | props.onError?.(); |
| 114 | }} |
| 115 | /> |
| 116 | </button> |
| 117 | </div> |
| 118 | </div> |
| 119 | {expanded ? ( |
| 120 | <ExpandedImageDialog |
| 121 | preview={{ |
| 122 | images: [ |
| 123 | { |
| 124 | src: props.src, |
| 125 | name: props.name, |
| 126 | ...(props.mimeType ? { mimeType: props.mimeType } : {}), |
| 127 | }, |
| 128 | ], |
| 129 | index: 0, |
| 130 | }} |
| 131 | onClose={() => setExpanded(false)} |
| 132 | /> |
| 133 | ) : null} |
| 134 | </> |
| 135 | ); |
| 136 | } |