nandi/oripublic Fork 0
76d62ac0da1600e1c208017584c3fa22e54feaa3
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

ImageView.tsx · 41 lines · 1.2 KBTypeScript Blame HistoryRaw
✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme 76d62ac k33g yesterday1/**
2 * ImageView shows a workspace image (png, jpeg, gif, webp, svg, bmp, ico,
3 * avif) at its natural size, capped to the pane width, over a checkerboard
4 * that reveals transparency. The bytes stream from GET /api/raw, and the
5 * natural dimensions appear once the browser has decoded the image.
6 *
7 * @example
8 * <ImageView path="/work/docs/logo.png" />
9 */
10
11import { useState } from "react";
12import { rawFileUrl } from "../api";
13
14export function ImageView({ path }: { path: string }) {
15 const [size, setSize] = useState<{ width: number; height: number } | null>(
16 null,
17 );
18 const [failed, setFailed] = useState(false);
19
20 if (failed) {
21 return <p className="pane-error">the image could not be loaded</p>;
22 }
23 return (
24 <figure className="image-view">
25 <div className="image-view-canvas">
26 <img
27 src={rawFileUrl(path)}
28 alt={path.split("/").pop() ?? path}
29 onLoad={(event) => {
30 const img = event.currentTarget;
31 setSize({ width: img.naturalWidth, height: img.naturalHeight });
32 }}
33 onError={() => setFailed(true)}
34 />
35 </div>
36 <figcaption className="image-view-meta">
37 {size ? `${size.width} × ${size.height} px` : "loading…"}
38 </figcaption>
39 </figure>
40 );
41}