diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 72e25a046..b6c677ac2 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -1056,26 +1056,55 @@ export default class EmbeddedChatApi { ) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const form = new FormData(); - if (threadId) { - form.append("tmid", threadId); + if (!userId || !authToken) { + console.error("sendAttachment: User not authenticated"); + return; } + + // Step 1: Upload file to rooms.media endpoint (RC 8.x) + const form = new FormData(); form.append("file", file, fileName); - form.append( - "description", - fileDescription.length !== 0 ? fileDescription : "" + + const uploadResponse = await fetch( + `${this.host}/api/v1/rooms.media/${this.rid}`, + { + method: "POST", + body: form, + headers: { + "X-Auth-Token": authToken, + "X-User-Id": userId, + }, + } ); - const response = fetch(`${this.host}/api/v1/rooms.upload/${this.rid}`, { - method: "POST", - body: form, - headers: { - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - }).then((r) => r.json()); - return response; + + const uploadResult = await uploadResponse.json(); + + if (!uploadResult.success || !uploadResult.file?._id) { + console.error("sendAttachment: Upload failed", uploadResult); + return uploadResult; + } + + // Step 2: Confirm the upload with message details + const confirmResponse = await fetch( + `${this.host}/api/v1/rooms.mediaConfirm/${this.rid}/${uploadResult.file._id}`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-Auth-Token": authToken, + "X-User-Id": userId, + }, + body: JSON.stringify( + threadId + ? { msg: "", description: fileDescription || "", tmid: threadId } + : { msg: "", description: fileDescription || "" } + ), + } + ); + + return await confirmResponse.json(); } catch (err) { - console.log(err); + console.error("sendAttachment error:", err); } } diff --git a/packages/react/src/views/ChatInput/ChatInput.js b/packages/react/src/views/ChatInput/ChatInput.js index e753b689a..0bdebf322 100644 --- a/packages/react/src/views/ChatInput/ChatInput.js +++ b/packages/react/src/views/ChatInput/ChatInput.js @@ -120,9 +120,10 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => { ); const isLoginIn = useLoginStore((state) => state.isLoginIn); - const { toggle, setData } = useAttachmentWindowStore((state) => ({ + const { toggle, setData, data } = useAttachmentWindowStore((state) => ({ toggle: state.toggle, setData: state.setData, + data: state.data, })); const userInfo = { _id: userId, username, name }; @@ -147,7 +148,7 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => { RCInstance.auth.onAuthChange((user) => { if (user) { RCInstance.getCommandsList() - .then((data) => setCommands(data.commands || [])) + .then((response) => setCommands(response.commands || [])) .catch(console.error); RCInstance.getChannelMembers(isChannelPrivate) @@ -184,6 +185,12 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => { } }, [deletedMessage]); + useEffect(() => { + if (data === null && inputRef.current) { + inputRef.current.value = ''; + } + }, [data]); + const getMessageLink = async (id) => { const host = RCInstance.getHost(); const res = await RCInstance.channelInfo();