Skip to content

Commit

Permalink
Add Image.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Nov 24, 2024
1 parent 5f08593 commit 19e68ea
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/viser/client/src/components/Image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useEffect, useState } from "react";
import { GuiImageMessage } from "../WebsocketMessages";
import { Box, Text } from "@mantine/core";

function ImageComponent({ props }: GuiImageMessage) {
if (!props.visible) return <></>;

const [imageUrl, setImageUrl] = useState<string | null>(null);

useEffect(() => {
if (props.data === null) {
setImageUrl(null);
} else {
const image_url = URL.createObjectURL(
new Blob([props.data], { type: props.media_type }),
);
setImageUrl(image_url);
return () => {
URL.revokeObjectURL(image_url);
};
}
}, [props.data, props.media_type]);

return imageUrl === null ? null : (
<Box px="xs">
{props.label === null ? null : (
<Text fz="sm" display="block">
{props.label}
</Text>
)}
<img
src={imageUrl}
style={{
maxWidth: "100%",
height: "auto",
}}
/>
</Box>
);
}

export default ImageComponent;

0 comments on commit 19e68ea

Please sign in to comment.