diff options
author | crupest <crupest@outlook.com> | 2023-08-19 02:13:26 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-08-19 02:13:26 +0800 |
commit | d6c1c9f2c9eddd7d6e4e91b2a9de71cfd9db6b73 (patch) | |
tree | 4b546fe67049a8211b3265a5d3316ae3947ac6e7 /FrontEnd/src/views/common/BlobImage.tsx | |
parent | eec2e74a928f6448a0503e003d8afa693730b365 (diff) | |
download | timeline-d6c1c9f2c9eddd7d6e4e91b2a9de71cfd9db6b73.tar.gz timeline-d6c1c9f2c9eddd7d6e4e91b2a9de71cfd9db6b73.tar.bz2 timeline-d6c1c9f2c9eddd7d6e4e91b2a9de71cfd9db6b73.zip |
...
Diffstat (limited to 'FrontEnd/src/views/common/BlobImage.tsx')
-rw-r--r-- | FrontEnd/src/views/common/BlobImage.tsx | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/FrontEnd/src/views/common/BlobImage.tsx b/FrontEnd/src/views/common/BlobImage.tsx index 5e050ebe..259c2210 100644 --- a/FrontEnd/src/views/common/BlobImage.tsx +++ b/FrontEnd/src/views/common/BlobImage.tsx @@ -1,27 +1,26 @@ -import * as React from "react"; +import { ComponentPropsWithoutRef, useState, useEffect } from "react"; -const BlobImage: React.FC< - Omit<React.ImgHTMLAttributes<HTMLImageElement>, "src"> & { - blob?: Blob | unknown; - } -> = (props) => { - const { blob, ...otherProps } = props; +type BlobImageProps = Omit<ComponentPropsWithoutRef<"img">, "src"> & { + imgRef?: React.Ref<HTMLImageElement>; + src?: Blob | string | null; +}; + +export default function BlobImage(props: BlobImageProps) { + const { imgRef, src, ...otherProps } = props; - const [url, setUrl] = React.useState<string | undefined>(undefined); + const [url, setUrl] = useState<string | null | undefined>(undefined); - React.useEffect(() => { - if (blob instanceof Blob) { - const url = URL.createObjectURL(blob); + useEffect(() => { + if (src instanceof Blob) { + const url = URL.createObjectURL(src); setUrl(url); return () => { URL.revokeObjectURL(url); }; } else { - setUrl(undefined); + setUrl(src); } - }, [blob]); - - return <img {...otherProps} src={url} />; -}; + }, [src]); -export default BlobImage; + return <img ref={imgRef} {...otherProps} src={url ?? undefined} />; +} |