Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Update thread summary when latest event gets decrypted #8564

Merged
merged 1 commit into from
May 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/components/views/rooms/ThreadSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { useContext } from "react";
import React, { useContext, useState } from "react";
import { Thread, ThreadEvent } from "matrix-js-sdk/src/models/thread";
import { MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/models/event";
import { IContent, MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/models/event";

import { _t } from "../../../languageHandler";
import { CardContext } from "../right_panel/context";
import AccessibleButton, { ButtonEvent } from "../elements/AccessibleButton";
import { showThread } from "../../../dispatcher/dispatch-actions/threads";
import PosthogTrackers from "../../../PosthogTrackers";
import { useTypedEventEmitterState } from "../../../hooks/useEventEmitter";
import { useTypedEventEmitter, useTypedEventEmitterState } from "../../../hooks/useEventEmitter";
import RoomContext from "../../../contexts/RoomContext";
import { MessagePreviewStore } from "../../../stores/room-list/MessagePreviewStore";
import MemberAvatar from "../avatars/MemberAvatar";
Expand Down Expand Up @@ -76,18 +76,21 @@ export const ThreadMessagePreview = ({ thread, showDisplayname = false }: IPrevi
const cli = useContext(MatrixClientContext);

const lastReply = useTypedEventEmitterState(thread, ThreadEvent.Update, () => thread.replyToEvent);
// track the replacing event id as a means to regenerate the thread message preview
const replacingEventId = useTypedEventEmitterState(
lastReply,
MatrixEventEvent.Replaced,
() => lastReply?.replacingEventId(),
);
// track the content as a means to regenerate the thread message preview upon edits & decryption
const [content, setContent] = useState<IContent>(lastReply?.getContent());
useTypedEventEmitter(lastReply, MatrixEventEvent.Replaced, () => {
setContent(lastReply.getContent());
});
const awaitDecryption = lastReply?.shouldAttemptDecryption() || lastReply?.isBeingDecrypted();
useTypedEventEmitter(awaitDecryption ? lastReply : null, MatrixEventEvent.Decrypted, () => {
setContent(lastReply.getContent());
});

const preview = useAsyncMemo(async () => {
if (!lastReply) return;
await cli.decryptEventIfNeeded(lastReply);
return MessagePreviewStore.instance.generatePreviewForEvent(lastReply);
}, [lastReply, replacingEventId]);
}, [lastReply, content]);
if (!preview) return null;

return <>
Expand Down