nandi/oripublic Fork 0
4166f8fba899b222fab287c398dcab9bf6f6e5c9
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * ImageView shows a workspace image (png, jpeg, gif, webp, svg, bmp, ico,
 * avif) at its natural size, capped to the pane width, over a checkerboard
 * that reveals transparency. The bytes stream from GET /api/raw, and the
 * natural dimensions appear once the browser has decoded the image.
 *
 * @example
 * <ImageView path="/work/docs/logo.png" />
 */

import { useState } from "react";
import { rawFileUrl } from "../api";

export function ImageView({ path }: { path: string }) {
	const [size, setSize] = useState<{ width: number; height: number } | null>(
		null,
	);
	const [failed, setFailed] = useState(false);

	if (failed) {
		return <p className="pane-error">the image could not be loaded</p>;
	}
	return (
		<figure className="image-view">
			<div className="image-view-canvas">
				<img
					src={rawFileUrl(path)}
					alt={path.split("/").pop() ?? path}
					onLoad={(event) => {
						const img = event.currentTarget;
						setSize({ width: img.naturalWidth, height: img.naturalHeight });
					}}
					onError={() => setFailed(true)}
				/>
			</div>
			<figcaption className="image-view-meta">
				{size ? `${size.width} × ${size.height} px` : "loading…"}
			</figcaption>
		</figure>
	);
}