diff --git a/src/components/views/rooms/SendMessageComposer.tsx b/src/components/views/rooms/SendMessageComposer.tsx index 252957c2c77..ea49e9fcbe3 100644 --- a/src/components/views/rooms/SendMessageComposer.tsx +++ b/src/components/views/rooms/SendMessageComposer.tsx @@ -243,7 +243,7 @@ export class SendMessageComposer extends React.Component; - private readonly prepareToEncrypt?: DebouncedFunc<() => void>; + private prepareToEncrypt?: DebouncedFunc<() => void>; private readonly editorRef = createRef(); private model: EditorModel; private currentlyComposedEditorState: SerializedPart[] | null = null; @@ -253,25 +253,25 @@ export class SendMessageComposer extends React.Component) { super(props, context); - if (this.props.mxClient.getCrypto() && this.props.mxClient.isRoomEncrypted(this.props.room.roomId)) { - this.prepareToEncrypt = throttle( - () => { - this.props.mxClient.getCrypto()?.prepareToEncrypt(this.props.room); - }, - 60000, - { leading: true, trailing: false }, - ); - } - const partCreator = new CommandPartCreator(this.props.room, this.props.mxClient); const parts = this.restoreStoredEditorState(partCreator) || []; this.model = new EditorModel(parts, partCreator); this.sendHistoryManager = new SendHistoryManager(this.props.room.roomId, "mx_cider_history_"); } - public componentDidMount(): void { + public async componentDidMount(): Promise { window.addEventListener("beforeunload", this.saveStoredEditorState); this.dispatcherRef = dis.register(this.onAction); + + if (await this.props.mxClient.getCrypto()?.isEncryptionEnabledInRoom(this.props.room.roomId)) { + this.prepareToEncrypt = throttle( + () => { + this.props.mxClient.getCrypto()?.prepareToEncrypt(this.props.room); + }, + 60000, + { leading: true, trailing: false }, + ); + } } public componentDidUpdate(prevProps: ISendMessageComposerProps): void { diff --git a/test/unit-tests/components/views/rooms/SendMessageComposer-test.tsx b/test/unit-tests/components/views/rooms/SendMessageComposer-test.tsx index e423d03ea9f..aa45525c4f0 100644 --- a/test/unit-tests/components/views/rooms/SendMessageComposer-test.tsx +++ b/test/unit-tests/components/views/rooms/SendMessageComposer-test.tsx @@ -605,7 +605,7 @@ describe("", () => { it("should call prepareToEncrypt when the user is typing", async () => { const cli = stubClient(); - cli.isRoomEncrypted = jest.fn().mockReturnValue(true); + jest.spyOn(cli.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true); const room = mkStubRoom("!roomId:server", "Room", cli); expect(cli.getCrypto()!.prepareToEncrypt).not.toHaveBeenCalled();