Skip to content

Commit 8c34d4a

Browse files
committed
fix pmp ignoring edit events
1 parent d0db174 commit 8c34d4a

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

.changeset/fix-pmp-edit-render.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
Fix per-message profiles not updating avatar/name if edit events are recieved.

src/app/features/room/message/Message.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
Username,
5050
UsernameBold,
5151
} from '$components/message';
52-
import { canEditEvent, getEventEdits, getMemberAvatarMxc } from '$utils/room';
52+
import { canEditEvent, getEditedEvent, getEventEdits, getMemberAvatarMxc } from '$utils/room';
5353
import { mxcUrlToHttp } from '$utils/matrix';
5454
import { getSettings, MessageLayout, MessageSpacing, settingsAtom } from '$state/settings';
5555
import { nicknamesAtom, setNicknameAtom } from '$state/nicknames';
@@ -380,18 +380,38 @@ function MessageInternal(
380380
const mx = useMatrixClient();
381381
const useAuthentication = useMediaAuthentication();
382382

383+
const [editVersion, setEditVersion] = useState(0);
384+
385+
useEffect(() => {
386+
const onReplaced = () => setEditVersion((v) => v + 1);
387+
mEvent.on('Event.replaced' as any, onReplaced);
388+
return () => {
389+
mEvent.off('Event.replaced' as any, onReplaced);
390+
};
391+
}, [mEvent]);
392+
383393
/**
384394
* We read the per-message profile from the event content here.
385395
* We have to do this in the message component because the per-message profile can be different for each message, and we need to read it for each message individually.
386396
* We also want to avoid reading and parsing the per-message profile in a parent component like the timeline, because that would be inefficient and would cause unnecessary re-renders of the entire timeline whenever a per-message profile changes.
387397
*/
388-
const pmp: PerMessageProfileBeeperFormat | undefined = useMemo(
389-
() =>
390-
mEvent.getContent()?.['com.beeper.per_message_profile'] as
391-
| PerMessageProfileBeeperFormat
392-
| undefined,
393-
[mEvent]
394-
);
398+
const pmp: PerMessageProfileBeeperFormat | undefined = useMemo(() => {
399+
const evtId = mEvent.getId();
400+
const evtTimeline = evtId ? room.getTimelineForEvent(evtId) : undefined;
401+
const editedEvent =
402+
evtTimeline && evtId
403+
? getEditedEvent(evtId, mEvent, evtTimeline.getTimelineSet())
404+
: undefined;
405+
406+
const resolvedContent = editedEvent
407+
? editedEvent.getContent()['m.new_content']
408+
: mEvent.getContent();
409+
410+
return resolvedContent?.['com.beeper.per_message_profile'] as
411+
| PerMessageProfileBeeperFormat
412+
| undefined;
413+
// eslint-disable-next-line react-hooks/exhaustive-deps
414+
}, [mEvent, room, editVersion]);
395415

396416
/**
397417
* We convert the per-message profile from the Beeper format to our internal format here in the message component

0 commit comments

Comments
 (0)