diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 2ac09799..a3c1dd69 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -313,7 +313,7 @@ export function RoomTimeline({ eventId, roomInputRef, editor, timeline }: RoomTi const handleUsernameClick = useUsernameClickHandler(editor); const [replyDraft, handleReplyClick] = useReplyClickHandler(editor); - const handleReactionToggle = useSendReaction(); + const { reactionSender, handleAddReaction } = useSendReaction(reactions, redacted); const handleEdit = useCallback( (editEvtId?: string) => { @@ -345,15 +345,20 @@ export function RoomTimeline({ eventId, roomInputRef, editor, timeline }: RoomTi let threadSeries: MessageEventWithoutRoomId[] = []; const renderMatrixEvent = (event: MessageEventWithoutRoomId, item: number, collapse: boolean) => { const reactionEvents = reactions.get(event.event_id)?.filter((r) => !redacted.has(r.event_id)); + const reactionSenderTarget = + reactionSender?.localEvent.original.content['m.relates_to'].event_id === event.event_id; + const editEvents = edits.get(event.event_id); const editedEvent = getLatestEditEvent(event, editEvents, redacted); const highlighted = focusItem?.index === item && focusItem.highlight; - const reactionJSX = reactionEvents && reactionEvents?.length > 0 && ( + const reactionJSX = (reactionSenderTarget || + (reactionEvents && reactionEvents?.length > 0)) && ( { +export const useSendReaction = ( + reactions: Map[]>, + redacted: Set +) => { const ct = useClient(); const roomId = useRoom(); - const timeline = useTimelineView(); + const [reactionSender, setReactionSender] = useState>(); + + const hasExistingReaction = useCallback( + (targetEventId: string, key: string) => { + const eventReactions = reactions.get(targetEventId)?.filter((r) => !redacted.has(r.event_id)); + return eventReactions?.some( + (evt) => evt.sender === ct.session.userId && evt.content['m.relates_to'].key === key + ); + }, + [reactions, redacted, ct] + ); const handleAddReaction = useCallback( (targetEventId: string, key: string, shortcode?: string) => { - const redacted = timeline.relations.redacted.read; - const reactions = timeline.relations.reactions.eventToRelations.read - .get(targetEventId) - ?.filter((r) => !redacted.has(r.event_id)); - if ( - reactions?.some( - (evt) => evt.sender === ct.session.userId && evt.content['m.relates_to'].key === key - ) - ) { + if (hasExistingReaction(targetEventId, key)) { // Already reacted with this emoji return; } @@ -48,9 +54,29 @@ export const useSendReaction = () => { if (eventSender.state.read instanceof EventSenderIdle) { eventSender.state.read.send(); } + setReactionSender(eventSender); }, - [ct, roomId, timeline] + [ct, roomId, hasExistingReaction] ); - return handleAddReaction; + useEffect(() => { + const unsub = reactionSender?.state.subscribe((state) => { + if (state instanceof EventSenderError) { + setReactionSender(undefined); + } + }); + return () => { + unsub?.(); + }; + }, [reactionSender]); + + const sendingRel = reactionSender?.localEvent.original.content['m.relates_to']; + + return { + reactionSender: + sendingRel && hasExistingReaction(sendingRel.event_id, sendingRel.key) + ? undefined + : reactionSender, + handleAddReaction, + }; }; diff --git a/src/app/features/room/message/Reactions.tsx b/src/app/features/room/message/Reactions.tsx index aa361f5c..5cbb7218 100644 --- a/src/app/features/room/message/Reactions.tsx +++ b/src/app/features/room/message/Reactions.tsx @@ -29,6 +29,7 @@ import { EventSenderPending, LocalEvent, LocalEventSender, + LocalEventSenderState, LocalMessage, } from '../../../../client/room/local-event'; import { MessageEvent } from '../../../../types/matrix/room'; @@ -40,6 +41,7 @@ type ReactionUnpressedProps = { eventId: string; reaction: ReactionGroup; onViewReaction: MouseEventHandler; + reactionSenderState?: LocalEventSenderState; disabled?: boolean; }; function ReactionUnpressed({ @@ -47,11 +49,13 @@ function ReactionUnpressed({ eventId, reaction, onViewReaction, + reactionSenderState, disabled, }: ReactionUnpressedProps) { const ct = useClient(); const [sender, setSender] = useState>(); + const adding = !!sender || !!reactionSenderState; const senderState = useObservableValueOptionally(sender?.state); @@ -62,6 +66,13 @@ function ReactionUnpressed({ }, [senderState]); const handleToggleReaction = () => { + if (reactionSenderState) { + if (reactionSenderState instanceof EventSenderPending) { + reactionSenderState.abort(); + } + return; + } + if (senderState instanceof EventSenderPending) { senderState.abort(); setSender(undefined); @@ -113,10 +124,10 @@ function ReactionUnpressed({ []; + reactionSender?: LocalEventSender; }; export const Reactions = as<'div', ReactionsProps>( ( - { className, reactions, roomId, eventId, canSendReaction, canRedactReaction, ...props }, + { + className, + reactions, + reactionSender, + roomId, + eventId, + canSendReaction, + canRedactReaction, + ...props + }, ref ) => { const ct = useClient(); const [viewer, setViewer] = useState(false); + const reactionSenderState = useObservableValueOptionally(reactionSender?.state); + const senderKey = reactionSender?.localEvent.original.content['m.relates_to'].key; const handleViewReaction: MouseEventHandler = (evt) => { evt.stopPropagation(); @@ -238,7 +261,23 @@ export const Reactions = as<'div', ReactionsProps>( else setViewer(key); }; - const reactionGroups = useMemo(() => getReactionGroups(reactions), [reactions]); + const reactionGroups = useMemo(() => { + const groups = getReactionGroups(reactions); + if (!reactionSender) { + return groups; + } + + const existingGroup = groups.some((g) => g.key === senderKey); + if (!existingGroup && senderKey) { + groups.push({ + key: senderKey, + events: [], + users: new Set([]), + firstTs: Date.now(), + }); + } + return groups; + }, [reactions, reactionSender, senderKey]); return ( ( eventId={eventId} reaction={reaction} onViewReaction={handleViewReaction} + reactionSenderState={senderKey === reaction.key ? reactionSenderState : undefined} disabled={!canSendReaction} /> ); diff --git a/src/app/features/room/thread-view/TimelineView.tsx b/src/app/features/room/thread-view/TimelineView.tsx index a81f096c..53f82f73 100644 --- a/src/app/features/room/thread-view/TimelineView.tsx +++ b/src/app/features/room/thread-view/TimelineView.tsx @@ -266,7 +266,7 @@ export function TimelineView({ threadTimeline, threadInputRef }: TimelineViewPro const handleUsernameClick = useUsernameClickHandler(editor); const [replyDraft, handleReplyClick] = useReplyClickHandler(editor, threadTimeline.threadId); - const handleReactionToggle = useSendReaction(); + const { reactionSender, handleAddReaction } = useSendReaction(reactions, redacted); const handleEdit = useCallback( (editEvtId?: string) => { @@ -284,6 +284,9 @@ export function TimelineView({ threadTimeline, threadInputRef }: TimelineViewPro const renderMatrixEvent = (event: MessageEventWithoutRoomId, item: number, collapse: boolean) => { const reactionEvents = reactions.get(event.event_id)?.filter((r) => !redacted.has(r.event_id)); + const reactionSenderTarget = + reactionSender?.localEvent.original.content['m.relates_to'].event_id === event.event_id; + const editEvents = edits.get(event.event_id); const editedEvent = getLatestEditEvent(event, editEvents, redacted); const highlighted = focusItem?.index === item && focusItem.highlight; @@ -305,11 +308,13 @@ export function TimelineView({ threadTimeline, threadInputRef }: TimelineViewPro /> ) : null; - const reactionJSX = reactionEvents && reactionEvents?.length > 0 && ( + const reactionJSX = (reactionSenderTarget || + (reactionEvents && reactionEvents?.length > 0)) && (