MCPcopy Create free account
hub / github.com/Linen-dev/linen.dev / Component

Function Component

apps/web/ui/Image/index.tsx:20–122  ·  view source on GitHub ↗
({
  className,
  src,
  height: initialHeight,
  width: initialWidth,
  alt,
  onLoad,
  onClick,
  isBot,
}: Props)

Source from the content-addressed store, hash-verified

18}
19
20export default function Component({
21 className,
22 src,
23 height: initialHeight,
24 width: initialWidth,
25 alt,
26 onLoad,
27 onClick,
28 isBot,
29}: Props) {
30 const [width, setWidth] = useState(initialWidth || 0);
31 const [height, setHeight] = useState(initialHeight || 0);
32 const [loaded, setLoaded] = useState(false);
33 const [preview, setPreview] = useState(false);
34 const [error, setError] = useState(null);
35
36 useEffect(() => {
37 let mounted = true;
38 setLoaded(false);
39 preload(src)
40 .then((image: HTMLImageElement) => {
41 if (mounted) {
42 setWidth((width) => width || image.naturalWidth);
43 setHeight((height) => height || image.naturalHeight);
44 setLoaded(true);
45 }
46 })
47 .catch((exception) => {
48 if (mounted) {
49 setError(exception);
50 setWidth(0);
51 setHeight(0);
52 setLoaded(false);
53 }
54 });
55 return () => {
56 mounted = false;
57 };
58 }, [src]);
59
60 useEffect(() => {
61 loaded && onLoad?.();
62 }, [loaded]);
63
64 if (isBot) {
65 return <img className={className} src={src} alt={alt || src} />;
66 }
67
68 if (loaded) {
69 return (
70 <div>
71 <img
72 className={classNames(styles.image, className)}
73 src={src}
74 alt={alt || src}
75 width={width}
76 height={height}
77 style={{ height, width }}

Callers

nothing calls this directly

Calls 2

preloadFunction · 0.85
onClickFunction · 0.50

Tested by

no test coverage detected