feat(threads): add thread reply input handling

RoomInput:
- Seed thread reply draft when opening drawer from notification
- Handle thread relation in message composition
- Support isolated draft state per thread
- Auto-populate replyTo when replying in thread
- Send messages with thread relation metadata
- Clear thread draft after sending reply
This commit is contained in:
Evie Gauthier committed 2026-03-13 23:30:01 -04:00
1 parent 04495680a1
commit 516cd3d51e
1 file changed
+102 -18
+102 -18
View File
@@ -164,15 +164,30 @@ const getReplyContent = (replyDraft: IReplyDraft | undefined): IEventRelation =>
const relatesTo: IEventRelation = {};
relatesTo['m.in_reply_to'] = {
event_id: replyDraft.eventId,
};
// If this is a thread relation
if (replyDraft.relation?.rel_type === RelationType.Thread) {
relatesTo.event_id = replyDraft.relation.event_id;
relatesTo.rel_type = RelationType.Thread;
relatesTo.is_falling_back = false;
// Check if this is a reply to a specific message in the thread
// (replyDraft.body being empty means it's just a seeded thread draft)
if (replyDraft.body && replyDraft.eventId !== replyDraft.relation.event_id) {
// This is a reply to a message within the thread
relatesTo['m.in_reply_to'] = {
event_id: replyDraft.eventId,
};
relatesTo.is_falling_back = false;
} else {
// This is just a regular thread message
relatesTo.is_falling_back = true;
}
} else {
// Regular reply (not in a thread)
relatesTo['m.in_reply_to'] = {
event_id: replyDraft.eventId,
};
}
return relatesTo;
};
@@ -187,9 +202,13 @@ interface RoomInputProps {
fileDropContainerRef: RefObject<HTMLElement>;
roomId: string;
room: Room;
threadRootId?: string;
}
export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
({ editor, fileDropContainerRef, roomId, room }, ref) => {
({ editor, fileDropContainerRef, roomId, room, threadRootId }, ref) => {
// When in thread mode, isolate drafts by thread root ID so thread replies
// don't clobber the main room draft (and vice versa).
const draftKey = threadRootId ?? roomId;
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const [enterForNewline] = useSetting(settingsAtom, 'enterForNewline');
@@ -205,8 +224,8 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
const permissions = useRoomPermissions(creators, powerLevels);
const canSendReaction = permissions.event(MessageEvent.Reaction, mx.getSafeUserId());
const [msgDraft, setMsgDraft] = useAtom(roomIdToMsgDraftAtomFamily(roomId));
const [replyDraft, setReplyDraft] = useAtom(roomIdToReplyDraftAtomFamily(roomId));
const [msgDraft, setMsgDraft] = useAtom(roomIdToMsgDraftAtomFamily(draftKey));
const [replyDraft, setReplyDraft] = useAtom(roomIdToReplyDraftAtomFamily(draftKey));
const replyUserID = replyDraft?.userId;
const { color: replyUsernameColor, font: replyUsernameFont } = useSableCosmetics(
@@ -215,7 +234,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
);
const [uploadBoard, setUploadBoard] = useState(true);
const [selectedFiles, setSelectedFiles] = useAtom(roomIdToUploadItemsAtomFamily(roomId));
const [selectedFiles, setSelectedFiles] = useAtom(roomIdToUploadItemsAtomFamily(draftKey));
const uploadFamilyObserverAtom = createUploadFamilyObserverAtom(
roomUploadAtomFamily,
selectedFiles.map((f) => f.file)
@@ -330,6 +349,26 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
replyBodyJSX = scaleSystemEmoji(strippedBody);
}
// Seed the reply draft with the thread relation whenever we're in thread
// mode (e.g. on first render or when the thread root changes). We use the
// current user's ID as userId so that the mention logic skips it.
useEffect(() => {
if (!threadRootId) return;
setReplyDraft((prev) => {
if (
prev?.relation?.rel_type === RelationType.Thread &&
prev.relation.event_id === threadRootId
)
return prev;
return {
userId: mx.getUserId() ?? '',
eventId: threadRootId,
body: '',
relation: { rel_type: RelationType.Thread, event_id: threadRootId },
};
});
}, [threadRootId, setReplyDraft, mx]);
useEffect(() => {
Transforms.insertFragment(editor, msgDraft);
}, [editor, msgDraft]);
@@ -345,7 +384,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
resetEditor(editor);
resetEditorHistory(editor);
},
[roomId, editor, setMsgDraft]
[draftKey, editor, setMsgDraft]
);
useEffect(() => {
@@ -419,13 +458,22 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
if (contents.length > 0) {
const replyContent = plainText?.length === 0 ? getReplyContent(replyDraft) : undefined;
if (replyContent) contents[0]['m.relates_to'] = replyContent;
setReplyDraft(undefined);
if (threadRootId) {
setReplyDraft({
userId: mx.getUserId() ?? '',
eventId: threadRootId,
body: '',
relation: { rel_type: RelationType.Thread, event_id: threadRootId },
});
} else {
setReplyDraft(undefined);
}
}
await Promise.all(
contents.map((content) =>
mx
.sendMessage(roomId, content as any)
.sendMessage(roomId, threadRootId ?? null, content as any)
.then((res) => {
debugLog.info('message', 'Uploaded file message sent', {
roomId,
@@ -561,7 +609,17 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
resetEditor(editor);
resetEditorHistory(editor);
setInputKey((prev) => prev + 1);
setReplyDraft(undefined);
if (threadRootId) {
// Re-seed the thread reply draft so the next message also goes to the thread.
setReplyDraft({
userId: mx.getUserId() ?? '',
eventId: threadRootId,
body: '',
relation: { rel_type: RelationType.Thread, event_id: threadRootId },
});
} else {
setReplyDraft(undefined);
}
sendTypingStatus(false);
};
if (scheduledTime) {
@@ -589,7 +647,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
roomId,
scheduledDelayId: editingScheduledDelayId,
});
const res = await mx.sendMessage(roomId, content as any);
const res = await mx.sendMessage(roomId, threadRootId ?? null, content as any);
debugLog.info('message', 'Message sent successfully', { roomId, eventId: res.event_id });
invalidate();
setEditingScheduledDelayId(null);
@@ -604,7 +662,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
} else {
resetInput();
debugLog.info('message', 'Sending message', { roomId, msgtype: (content as any).msgtype });
mx.sendMessage(roomId, content as any)
mx.sendMessage(roomId, threadRootId ?? null, content as any)
.then((res) => {
debugLog.info('message', 'Message sent successfully', {
roomId,
@@ -618,6 +676,11 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
});
log.error('failed to send message', { roomId }, error);
});
=======
mx.sendMessage(roomId, threadRootId ?? null, content as any).catch((error: unknown) => {
log.error('failed to send message', { roomId }, error);
});
>>>>>>> af4c338d (feat(threads): add thread reply input handling)
}
}, [
editor,
@@ -625,6 +688,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
canSendReaction,
mx,
roomId,
threadRootId,
replyDraft,
silentReply,
scheduledTime,
@@ -729,7 +793,16 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
};
if (replyDraft) {
content['m.relates_to'] = getReplyContent(replyDraft);
setReplyDraft(undefined);
if (threadRootId) {
setReplyDraft({
userId: mx.getUserId() ?? '',
eventId: threadRootId,
body: '',
relation: { rel_type: RelationType.Thread, event_id: threadRootId },
});
} else {
setReplyDraft(undefined);
}
}
mx.sendEvent(roomId, EventType.Sticker, content);
};
@@ -888,7 +961,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
</Box>
</div>
)}
{replyDraft && (
{replyDraft && (!threadRootId || replyDraft.body) && (
<div>
<Box
alignItems="Center"
@@ -896,7 +969,18 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
style={{ padding: `${config.space.S200} ${config.space.S300} 0` }}
>
<IconButton
onClick={() => setReplyDraft(undefined)}
onClick={() => {
if (threadRootId) {
setReplyDraft({
userId: mx.getUserId() ?? '',
eventId: threadRootId,
body: '',
relation: { rel_type: RelationType.Thread, event_id: threadRootId },
});
} else {
setReplyDraft(undefined);
}
}}
variant="SurfaceVariant"
size="300"
radii="300"