-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Listen to events so that encryption icon updates when status changes #28407
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,21 +6,41 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only | |
Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix"; | ||
import { useEffect, useState } from "react"; | ||
import { CryptoEvent, MatrixClient, Room, RoomStateEvent } from "matrix-js-sdk/src/matrix"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { throttle } from "lodash"; | ||
|
||
import { E2EStatus, shieldStatusForRoom } from "../utils/ShieldUtils"; | ||
import { useTypedEventEmitter } from "./useEventEmitter"; | ||
|
||
export function useEncryptionStatus(client: MatrixClient, room: Room): E2EStatus | null { | ||
const [e2eStatus, setE2eStatus] = useState<E2EStatus | null>(null); | ||
|
||
useEffect(() => { | ||
if (client.getCrypto()) { | ||
shieldStatusForRoom(client, room).then((e2eStatus) => { | ||
setE2eStatus(e2eStatus); | ||
}); | ||
} | ||
}, [client, room]); | ||
const updateEncryptionStatus = useMemo( | ||
() => | ||
throttle( | ||
() => { | ||
if (client.getCrypto()) { | ||
console.log("getting status"); | ||
shieldStatusForRoom(client, room).then((e2eStatus) => { | ||
setE2eStatus(e2eStatus); | ||
}); | ||
} | ||
}, | ||
250, | ||
{ leading: true, trailing: true }, | ||
), | ||
[client, room], | ||
); | ||
|
||
useEffect(updateEncryptionStatus, [updateEncryptionStatus]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not familiar with that, the second arg are dependencies? So is this not a cyclic dependencies? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the dependencies specify when |
||
|
||
// shieldStatusForRoom depends on the room membership, each member's trust | ||
// status for each member, and each member's devices, so we update the | ||
// status whenever any of those changes. | ||
useTypedEventEmitter(room, RoomStateEvent.Members, updateEncryptionStatus); | ||
useTypedEventEmitter(client, CryptoEvent.UserTrustStatusChanged, updateEncryptionStatus); | ||
useTypedEventEmitter(client, CryptoEvent.DevicesUpdated, updateEncryptionStatus); | ||
|
||
return e2eStatus; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a helpful console message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh. I thought I removed all my debugging messages, but I forgot that one.