From 9ee6b9a6c9da1ee3835f85da8b6662de5fb49987 Mon Sep 17 00:00:00 2001 From: ayang <473033518@qq.com> Date: Fri, 16 May 2025 14:32:11 +0800 Subject: [PATCH] feat: add file upload failure handling and alert message --- src/components/Assistant/FileList.tsx | 89 ++++++++++++++++++--------- src/components/Common/Tooltip2.tsx | 42 +++++++++++++ src/components/Search/InputExtra.tsx | 7 ++- src/locales/en/translation.json | 4 ++ src/locales/zh/translation.json | 4 ++ src/stores/chatStore.ts | 4 +- 6 files changed, 118 insertions(+), 32 deletions(-) create mode 100644 src/components/Common/Tooltip2.tsx diff --git a/src/components/Assistant/FileList.tsx b/src/components/Assistant/FileList.tsx index 7335158a..8bdebe07 100644 --- a/src/components/Assistant/FileList.tsx +++ b/src/components/Assistant/FileList.tsx @@ -4,10 +4,11 @@ import { X } from "lucide-react"; import { useAsyncEffect } from "ahooks"; import { useTranslation } from "react-i18next"; -import { useChatStore } from "@/stores/chatStore"; +import { UploadFile, useChatStore } from "@/stores/chatStore"; import { useConnectStore } from "@/stores/connectStore"; import FileIcon from "../Common/Icons/FileIcon"; import platformAdapter from "@/utils/platformAdapter"; +import Tooltip2 from "../Common/Tooltip2"; interface FileListProps { sessionId: string; @@ -39,29 +40,42 @@ const FileList = (props: FileListProps) => { if (uploaded) continue; - const attachmentIds: any = await platformAdapter.commands( - "upload_attachment", - { - serverId, - sessionId, - filePaths: [path], + try { + const attachmentIds: any = await platformAdapter.commands( + "upload_attachment", + { + serverId, + sessionId, + filePaths: [path], + } + ); + + if (!attachmentIds) { + throw new Error("Failed to get attachment id"); + } else { + Object.assign(item, { + uploaded: true, + attachmentId: attachmentIds[0], + }); } - ); - if (!attachmentIds) continue; - - Object.assign(item, { - uploaded: true, - attachmentId: attachmentIds[0], - }); - - setUploadFiles(uploadFiles); + setUploadFiles(uploadFiles); + } catch (error) { + Object.assign(item, { + uploadFailed: true, + failedMessage: String(error), + }); + } } }, [uploadFiles]); - const deleteFile = async (id: string, attachmentId: string) => { + const deleteFile = async (file: UploadFile) => { + const { id, uploadFailed, attachmentId } = file; + setUploadFiles(uploadFiles.filter((file) => file.id !== id)); + if (uploadFailed) return; + platformAdapter.commands("delete_attachment", { serverId, id: attachmentId, @@ -71,16 +85,25 @@ const FileList = (props: FileListProps) => { return (