Skip to content

Commit

Permalink
fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Dec 15, 2024
1 parent 79d890a commit 3db084c
Showing 1 changed file with 49 additions and 37 deletions.
86 changes: 49 additions & 37 deletions src/chat-api/store/useMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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) => ({
Expand All @@ -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) {
Expand All @@ -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
);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 3db084c

Please sign in to comment.