Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,26 +1056,53 @@ 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;
}

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;
}

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);
}
}

Expand Down
11 changes: 9 additions & 2 deletions packages/react/src/views/ChatInput/ChatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
Loading