feat: image load status in message
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { useState, useEffect, FC } from "react";
|
import { useState, useEffect, FC } from "react";
|
||||||
import { Ping } from '@uiball/loaders';
|
import { Ping, LineWobble } from '@uiball/loaders';
|
||||||
import { getDefaultSize, isMobile } from "../../utils";
|
import { getDefaultSize, isMobile } from "../../utils";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
@@ -19,19 +19,19 @@ const ImageMessage: FC<Props> = ({
|
|||||||
content,
|
content,
|
||||||
properties
|
properties
|
||||||
}) => {
|
}) => {
|
||||||
const [url, setUrl] = useState(thumbnail);
|
const url = thumbnail || content;
|
||||||
|
const [status, setStatus] = useState<"loading" | "error" | "loaded">("loading");
|
||||||
const { width = 0, height = 0 } = getDefaultSize(properties, { min: 200, max: isMobile() ? 300 : 480 });
|
const { width = 0, height = 0 } = getDefaultSize(properties, { min: 200, max: isMobile() ? 300 : 480 });
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const newUrl = thumbnail;
|
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
setUrl(newUrl);
|
setStatus("loaded");
|
||||||
};
|
};
|
||||||
img.onerror = () => {
|
img.onerror = () => {
|
||||||
setUrl("");
|
setStatus("error");
|
||||||
};
|
};
|
||||||
img.src = newUrl;
|
img.src = url;
|
||||||
}, [thumbnail]);
|
}, [url]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`relative`} style={{
|
<div className={`relative`} style={{
|
||||||
@@ -49,17 +49,20 @@ const ImageMessage: FC<Props> = ({
|
|||||||
<span className="text-xs text-gray-500">{progress}%</span>
|
<span className="text-xs text-gray-500">{progress}%</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<img
|
{status == "error" ? <p className="w-full h-full flex-center text-lg text-red-800 bg-red-100">load image error</p> :
|
||||||
className="h-auto w-full cursor-zoom-in object-cover preview"
|
status == "loading" ? <p className="w-full h-full flex-center bg-primary-50/80 dark:bg-primary-900/70">
|
||||||
// style={{
|
<LineWobble />
|
||||||
// width: width ? `${width}px` : "",
|
</p> : <img
|
||||||
// height: height ? `${height}px` : ""
|
className="h-auto w-full cursor-zoom-in object-cover preview"
|
||||||
// }}
|
// style={{
|
||||||
data-meta={JSON.stringify(properties)}
|
// width: width ? `${width}px` : "",
|
||||||
data-origin={content}
|
// height: height ? `${height}px` : ""
|
||||||
data-download={download}
|
// }}
|
||||||
src={url}
|
data-meta={JSON.stringify(properties)}
|
||||||
/>
|
data-origin={content}
|
||||||
|
data-download={download}
|
||||||
|
src={url}
|
||||||
|
/>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -209,6 +209,8 @@ export const normalizeFileMessage = (data: MessagePayload) => {
|
|||||||
const { properties, content, sending = false, content_type } = data;
|
const { properties, content, sending = false, content_type } = data;
|
||||||
const isFile = content_type == ContentTypes.file;
|
const isFile = content_type == ContentTypes.file;
|
||||||
const isPic = isImage(properties?.content_type, properties?.size);
|
const isPic = isImage(properties?.content_type, properties?.size);
|
||||||
|
// gif暂不支持缩略图
|
||||||
|
const isGif = isPic && properties?.content_type == "image/gif";
|
||||||
let res: null | { file_path?: string, content?: string, download?: string, thumbnail: string } = null;
|
let res: null | { file_path?: string, content?: string, download?: string, thumbnail: string } = null;
|
||||||
if (isFile) {
|
if (isFile) {
|
||||||
if (!sending) {
|
if (!sending) {
|
||||||
@@ -220,14 +222,14 @@ export const normalizeFileMessage = (data: MessagePayload) => {
|
|||||||
download: `${BASE_URL}/resource/file?file_path=${encodeURIComponent(
|
download: `${BASE_URL}/resource/file?file_path=${encodeURIComponent(
|
||||||
content
|
content
|
||||||
)}&download=true`,
|
)}&download=true`,
|
||||||
thumbnail: isPic
|
thumbnail: (isPic && !isGif)
|
||||||
? `${BASE_URL}/resource/file?file_path=${encodeURIComponent(
|
? `${BASE_URL}/resource/file?file_path=${encodeURIComponent(
|
||||||
content
|
content
|
||||||
)}&thumbnail=true`
|
)}&thumbnail=true`
|
||||||
: ""
|
: ""
|
||||||
};
|
};
|
||||||
} else if (isPic) {
|
} else if (isPic) {
|
||||||
res = { thumbnail: content };
|
res = { thumbnail: isGif ? "" : content };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
Reference in New Issue
Block a user