add local echo for reaction sent from emojiboard
This commit is contained in:
parent
691fa34c4c
commit
a116058362
4 changed files with 104 additions and 28 deletions
|
|
@ -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)) && (
|
||||
<Reactions
|
||||
style={{ marginTop: config.space.S200 }}
|
||||
roomId={roomId}
|
||||
reactions={reactionEvents}
|
||||
reactions={reactionEvents ?? []}
|
||||
reactionSender={reactionSenderTarget ? reactionSender : undefined}
|
||||
eventId={event.event_id}
|
||||
canSendReaction={canSendReaction}
|
||||
canRedactReaction={canRedact || canDeleteOwn}
|
||||
|
|
@ -434,7 +439,7 @@ export function RoomTimeline({ eventId, roomInputRef, editor, timeline }: RoomTi
|
|||
onUsernameClick={handleUsernameClick}
|
||||
onReplyClick={handleReplyClick}
|
||||
onThreadClick={handleThreadClick}
|
||||
onReactionToggle={handleReactionToggle}
|
||||
onReactionToggle={handleAddReaction}
|
||||
onEditId={handleEdit}
|
||||
redacted={redacted.has(event.event_id)}
|
||||
hideReadReceipts={hideActivity}
|
||||
|
|
|
|||
|
|
@ -1,32 +1,38 @@
|
|||
import { useCallback } from 'react';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { MessageEventWithoutRoomId } from 'neurone/lib/api/types';
|
||||
import { ReactionContent, RelationType } from '../../../../types/matrix/common';
|
||||
import { MessageEvent } from '../../../../types/matrix/room';
|
||||
import { useClient } from '../../../hooks/useClient';
|
||||
import {
|
||||
EventSenderError,
|
||||
EventSenderIdle,
|
||||
LocalEvent,
|
||||
LocalEventSender,
|
||||
LocalMessage,
|
||||
} from '../../../../client/room/local-event';
|
||||
import { useRoom } from '../../../hooks/useRoom';
|
||||
import { useTimelineView } from './useTimeline';
|
||||
|
||||
export const useSendReaction = () => {
|
||||
export const useSendReaction = (
|
||||
reactions: Map<string, MessageEventWithoutRoomId<ReactionContent>[]>,
|
||||
redacted: Set<string>
|
||||
) => {
|
||||
const ct = useClient();
|
||||
const roomId = useRoom();
|
||||
const timeline = useTimelineView();
|
||||
const [reactionSender, setReactionSender] = useState<LocalEventSender<ReactionContent>>();
|
||||
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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<HTMLButtonElement>;
|
||||
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<LocalEventSender<ReactionContent>>();
|
||||
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({
|
|||
<Reaction
|
||||
ref={targetRef}
|
||||
data-reaction-key={reaction.key}
|
||||
aria-pressed={!!sender}
|
||||
aria-pressed={adding}
|
||||
reaction={reaction.key}
|
||||
count={sender ? reaction.users.size + 1 : reaction.users.size}
|
||||
adding={!!sender}
|
||||
count={adding ? reaction.users.size + 1 : reaction.users.size}
|
||||
adding={adding}
|
||||
onClick={disabled ? undefined : handleToggleReaction}
|
||||
onContextMenu={onViewReaction}
|
||||
aria-disabled={disabled}
|
||||
|
|
@ -221,14 +232,26 @@ export type ReactionsProps = {
|
|||
canSendReaction?: boolean;
|
||||
canRedactReaction?: boolean;
|
||||
reactions: MessageEventWithoutRoomId<ReactionContent>[];
|
||||
reactionSender?: LocalEventSender<ReactionContent>;
|
||||
};
|
||||
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<boolean | string>(false);
|
||||
const reactionSenderState = useObservableValueOptionally(reactionSender?.state);
|
||||
const senderKey = reactionSender?.localEvent.original.content['m.relates_to'].key;
|
||||
|
||||
const handleViewReaction: MouseEventHandler<HTMLButtonElement> = (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 (
|
||||
<Box
|
||||
|
|
@ -267,6 +306,7 @@ export const Reactions = as<'div', ReactionsProps>(
|
|||
eventId={eventId}
|
||||
reaction={reaction}
|
||||
onViewReaction={handleViewReaction}
|
||||
reactionSenderState={senderKey === reaction.key ? reactionSenderState : undefined}
|
||||
disabled={!canSendReaction}
|
||||
/>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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)) && (
|
||||
<Reactions
|
||||
style={{ marginTop: config.space.S200 }}
|
||||
roomId={roomId}
|
||||
reactions={reactionEvents}
|
||||
reactions={reactionEvents ?? []}
|
||||
reactionSender={reactionSenderTarget ? reactionSender : undefined}
|
||||
eventId={event.event_id}
|
||||
canSendReaction={canSendReaction}
|
||||
canRedactReaction={canRedact || canDeleteOwn}
|
||||
|
|
@ -338,7 +343,7 @@ export function TimelineView({ threadTimeline, threadInputRef }: TimelineViewPro
|
|||
onUserClick={handleUserClick}
|
||||
onUsernameClick={handleUsernameClick}
|
||||
onReplyClick={handleReplyClick}
|
||||
onReactionToggle={handleReactionToggle}
|
||||
onReactionToggle={handleAddReaction}
|
||||
onEditId={handleEdit}
|
||||
redacted={redacted.has(event.event_id)}
|
||||
hideReadReceipts={hideActivity}
|
||||
|
|
|
|||
Loading…
Reference in a new issue