From 954b5d3430f6ad87e45869a8454f23c3411bf7f3 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Wed, 24 Sep 2025 21:44:52 +0800 Subject: [PATCH] =?UTF-8?q?ui:=20=E7=B4=B0=E7=AF=80=E5=84=AA=E5=8C=96:=20?= =?UTF-8?q?=E7=99=BC=E9=80=81=E6=B6=88=E6=81=AF=E6=99=82,=20=E8=BD=89?= =?UTF-8?q?=E5=9C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/ui/chat/ChatFragment.tsx | 56 ++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/client/ui/chat/ChatFragment.tsx b/client/ui/chat/ChatFragment.tsx index bcb4a7c..2420216 100644 --- a/client/ui/chat/ChatFragment.tsx +++ b/client/ui/chat/ChatFragment.tsx @@ -116,30 +116,42 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC const [showLoadingMoreMessagesTip, setShowLoadingMoreMessagesTip] = React.useState(false) const [showNoMoreMessagesTip, setShowNoMoreMessagesTip] = React.useState(false) + const [isMessageSending, setIsMessageSending] = React.useState(false) + const cachedFiles = React.useRef({} as { [fileName: string]: ArrayBuffer }) async function sendMessage() { - let text = inputRef.current!.value - for (const fileName of Object.keys(cachedFiles.current)) { - if (text.indexOf(fileName) != -1) { - const re = await Client.invoke("Chat.uploadFile", { - token: data.access_token, - file_name: fileName, - target, - data: cachedFiles.current[fileName], - }, 5000) - if (checkApiSuccessOrSncakbar(re, `文件[${fileName}] 上傳失敗`)) return - text = text.replaceAll(fileName, re.data!.file_path as string) + try { + let text = inputRef.current!.value + if (text.trim() == '') return + setIsMessageSending(true) + for (const fileName of Object.keys(cachedFiles.current)) { + if (text.indexOf(fileName) != -1) { + const re = await Client.invoke("Chat.uploadFile", { + token: data.access_token, + file_name: fileName, + target, + data: cachedFiles.current[fileName], + }, 5000) + if (checkApiSuccessOrSncakbar(re, `文件[${fileName}] 上傳失敗`)) return + text = text.replaceAll(fileName, re.data!.file_path as string) + } } - } - const re = await Client.invoke("Chat.sendMessage", { - token: data.access_token, - target, - text, - }, 5000) - if (checkApiSuccessOrSncakbar(re, "發送失敗")) return - inputRef.current!.value = '' - cachedFiles.current = {} + const re = await Client.invoke("Chat.sendMessage", { + token: data.access_token, + target, + text, + }, 5000) + if (checkApiSuccessOrSncakbar(re, "發送失敗")) return + inputRef.current!.value = '' + cachedFiles.current = {} + } catch (e) { + snackbar({ + message: '發送失敗: ' + (e as Error).message, + placement: 'top', + }) + } + setIsMessageSending(false) } return ( @@ -160,7 +172,7 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC showReturnButton && } { @@ -302,7 +314,7 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC }}> sendMessage()}> + }} onClick={() => sendMessage()} loading={isMessageSending}>