Can send text messages in conversations

This commit is contained in:
2025-11-27 19:13:16 +01:00
parent bda47a2770
commit 9f83a6fb66
3 changed files with 76 additions and 1 deletions

View File

@@ -80,4 +80,15 @@ export class MatrixApiEvent {
room.id
}/event/${event_id}/file?thumbnail=${thumbnail}`;
}
/**
* Send text message
*/
static async SendTextMessage(room: Room, content: string): Promise<void> {
await APIClient.exec({
method: "POST",
uri: `/matrix/room/${room.id}/send_text_message`,
jsonData: { content },
});
}
}

View File

@@ -5,6 +5,7 @@ import type { Room } from "../../api/matrix/MatrixApiRoom";
import { RoomEventsManager } from "../../utils/RoomEventsManager";
import { AsyncWidget } from "../AsyncWidget";
import { RoomMessagesList } from "./RoomMessagesList";
import { SendMessageForm } from "./SendMessageForm";
export function RoomWidget(p: {
room: Room;
@@ -28,7 +29,7 @@ export function RoomWidget(p: {
build={() => (
<div style={{ display: "flex", flexDirection: "column", flex: 1 }}>
<RoomMessagesList mgr={roomMgr!} {...p} />
<div>Send message form</div>
<SendMessageForm {...p} />
</div>
)}
/>

View File

@@ -0,0 +1,63 @@
import SendIcon from "@mui/icons-material/Send";
import { IconButton, TextField } from "@mui/material";
import React, { type FormEvent } from "react";
import { MatrixApiEvent } from "../../api/matrix/MatrixApiEvent";
import type { Room } from "../../api/matrix/MatrixApiRoom";
import { useAlert } from "../../hooks/contexts_provider/AlertDialogProvider";
import { useLoadingMessage } from "../../hooks/contexts_provider/LoadingMessageProvider";
export function SendMessageForm(p: { room: Room }): React.ReactElement {
const loadingMessage = useLoadingMessage();
const alert = useAlert();
const [text, setText] = React.useState("");
const handleTextSubmit = async (e: FormEvent) => {
e.preventDefault();
if (text === "") return;
loadingMessage.show("Sending message...");
try {
await MatrixApiEvent.SendTextMessage(p.room, text);
setText("");
} catch (e) {
console.error(`Failed to send message! ${e}`);
alert(`Failed to send message! ${e}`);
} finally {
loadingMessage.hide();
}
};
return (
<form onSubmit={handleTextSubmit}>
<div
style={{
padding: "10px",
paddingLeft: "20px",
display: "flex",
flexDirection: "row",
}}
>
<TextField
placeholder="Send a message..."
variant="standard"
fullWidth
style={{}}
slotProps={{ input: { disableUnderline: true } }}
value={text}
onChange={(e) => setText(e.target.value)}
/>
<IconButton
size="small"
style={{ visibility: text === "" ? "hidden" : "visible" }}
onClick={handleTextSubmit}
>
<SendIcon />
</IconButton>
</div>
</form>
);
}