From 3db084cb915d2ff657803ea3395085fe28a9c01b Mon Sep 17 00:00:00 2001 From: Supertiger Date: Sun, 15 Dec 2024 10:18:24 +0000 Subject: [PATCH] fix error --- src/chat-api/store/useMessages.ts | 86 ++++++++++++++++++------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/src/chat-api/store/useMessages.ts b/src/chat-api/store/useMessages.ts index 46db89f9..37740214 100644 --- a/src/chat-api/store/useMessages.ts +++ b/src/chat-api/store/useMessages.ts @@ -30,7 +30,7 @@ export enum MessageSentStatus { export type Message = RawMessage & { tempId?: string; sentStatus?: MessageSentStatus; - uploadingAttachment?: { file: File; progress: number, speed?: string }; + uploadingAttachment?: { file: File; progress: number; speed?: string }; }; const [messages, setMessages] = createStore< @@ -180,8 +180,14 @@ const sendAndStoreMessage = async (channelId: string, content?: string) => { type: MessageType.CONTENT, ...(!properties?.attachment ? undefined - : { uploadingAttachment: { file: properties.attachment.file, progress: 0 } }), + : { + uploadingAttachment: { + file: properties.attachment.file, + progress: 0, + }, + }), reactions: [], + roleMentions: [], quotedMessages: [], replyMessages: properties?.replyToMessages.map((m) => ({ @@ -207,22 +213,19 @@ const sendAndStoreMessage = async (channelId: string, content?: string) => { (m) => m.tempId === tempMessageId ); if (messageIndex === -1) return; - setMessages( - channelId, - messageIndex, - "uploadingAttachment", - { - progress: percent, - speed - } - ); + setMessages(channelId, messageIndex, "uploadingAttachment", { + progress: percent, + speed, + }); }; const isImage = properties?.attachment?.file.type?.startsWith("image/"); const isMoreThan12MB = file && file.size > 12 * 1024 * 1024; - const shouldUploadToGoogleDrive = properties?.attachment?.uploadTo === "google_drive"; - const shouldUploadToNerimityCdn = properties?.attachment?.uploadTo === "nerimity_cdn"; + const shouldUploadToGoogleDrive = + properties?.attachment?.uploadTo === "google_drive"; + const shouldUploadToNerimityCdn = + properties?.attachment?.uploadTo === "nerimity_cdn"; let googleDriveFileId: string | undefined; if (file && shouldUploadToGoogleDrive) { @@ -235,14 +238,16 @@ const sendAndStoreMessage = async (channelId: string, content?: string) => { ); googleDriveFileId = res.id; } catch (err: any) { - pushFailedMessage(channelId, + pushFailedMessage( + channelId, "Failed to upload file to Google Drive. ```Error\n" + - err.message + - "\nbody: " + - content + - "\nFilename: " + - file.name + - "```"); + err.message + + "\nbody: " + + content + + "\nFilename: " + + file.name + + "```" + ); const index = messages[channelId]?.findIndex( (m) => m.tempId === tempMessageId ); @@ -258,21 +263,26 @@ const sendAndStoreMessage = async (channelId: string, content?: string) => { let nerimityCdnFileId: string | undefined; if (shouldUploadToNerimityCdn && file) { - const data = await uploadAttachment(channelId, { file, onUploadProgress }).catch((err) => { - pushFailedMessage(channelId, + const data = await uploadAttachment(channelId, { + file, + onUploadProgress, + }).catch((err) => { + pushFailedMessage( + channelId, "Failed to upload file. ```Error\n" + - err.message + - "\nbody: " + - content + - "\nFilename: " + - file.name + - "```"); + err.message + + "\nbody: " + + content + + "\nFilename: " + + file.name + + "```" + ); const index = messages[channelId]?.findIndex( (m) => m.tempId === tempMessageId ); setMessages(channelId, index!, "sentStatus", MessageSentStatus.FAILED); return; - }) + }); if (!data) { return; } @@ -302,13 +312,14 @@ const sendAndStoreMessage = async (channelId: string, content?: string) => { }); } } - pushFailedMessage(channelId, + pushFailedMessage( + channelId, "This message couldn't be sent. Try again later. ```Error\n" + - err.message + - "\nbody: " + - content + - "```" - ) + err.message + + "\nbody: " + + content + + "```" + ); }); if (message && channel?.slowModeSeconds) { @@ -357,12 +368,13 @@ const pushFailedMessage = (channelId: string, content: string) => { id: "0", }, reactions: [], + roleMentions: [], quotedMessages: [], id: Math.random().toString(), type: MessageType.CONTENT, - content + content, }); -} +}; const locallyRemoveMessage = (channelId: string, messageId: string) => { const channelMessages = messages[channelId];