From 1032b331c9538a49ec37e6b44c2014501846158c Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Thu, 9 Mar 2023 20:15:46 +0800 Subject: [PATCH] feat: image load status in message --- .../component/FileMessage/ImageMessage.tsx | 39 ++++++++++--------- src/common/utils.tsx | 6 ++- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/common/component/FileMessage/ImageMessage.tsx b/src/common/component/FileMessage/ImageMessage.tsx index 3f8bef0b..fcd07d5f 100644 --- a/src/common/component/FileMessage/ImageMessage.tsx +++ b/src/common/component/FileMessage/ImageMessage.tsx @@ -1,5 +1,5 @@ import { useState, useEffect, FC } from "react"; -import { Ping } from '@uiball/loaders'; +import { Ping, LineWobble } from '@uiball/loaders'; import { getDefaultSize, isMobile } from "../../utils"; type Props = { @@ -19,19 +19,19 @@ const ImageMessage: FC = ({ content, 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 }); useEffect(() => { - const newUrl = thumbnail; const img = new Image(); img.onload = () => { - setUrl(newUrl); + setStatus("loaded"); }; img.onerror = () => { - setUrl(""); + setStatus("error"); }; - img.src = newUrl; - }, [thumbnail]); + img.src = url; + }, [url]); return (
= ({ {progress}%
)} - + {status == "error" ?

load image error

: + status == "loading" ?

+ +

: } ); }; diff --git a/src/common/utils.tsx b/src/common/utils.tsx index a668fc91..8482dffa 100644 --- a/src/common/utils.tsx +++ b/src/common/utils.tsx @@ -209,6 +209,8 @@ export const normalizeFileMessage = (data: MessagePayload) => { const { properties, content, sending = false, content_type } = data; const isFile = content_type == ContentTypes.file; 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; if (isFile) { if (!sending) { @@ -220,14 +222,14 @@ export const normalizeFileMessage = (data: MessagePayload) => { download: `${BASE_URL}/resource/file?file_path=${encodeURIComponent( content )}&download=true`, - thumbnail: isPic + thumbnail: (isPic && !isGif) ? `${BASE_URL}/resource/file?file_path=${encodeURIComponent( content )}&thumbnail=true` : "" }; } else if (isPic) { - res = { thumbnail: content }; + res = { thumbnail: isGif ? "" : content }; } } return res;