Fix React contexts (#12855)

This commit is contained in:
Michael Telatynski 2024-08-01 13:01:05 +01:00 committed by GitHub
parent 9b77279b3e
commit b6addb4118
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 99 additions and 107 deletions

View file

@ -15,7 +15,16 @@ limitations under the License.
*/
import React, { ReactNode } from "react";
import { EventStatus, MatrixEvent, Room, MatrixError, SyncState, SyncStateData } from "matrix-js-sdk/src/matrix";
import {
ClientEvent,
EventStatus,
MatrixError,
MatrixEvent,
Room,
RoomEvent,
SyncState,
SyncStateData,
} from "matrix-js-sdk/src/matrix";
import { Icon as WarningIcon } from "../../../res/img/feather-customised/warning-triangle.svg";
import { _t, _td } from "../../languageHandler";
@ -79,8 +88,8 @@ interface IProps {
}
interface IState {
syncState: SyncState;
syncStateData: SyncStateData;
syncState: SyncState | null;
syncStateData: SyncStateData | null;
unsentMessages: MatrixEvent[];
isResending: boolean;
}
@ -88,9 +97,11 @@ interface IState {
export default class RoomStatusBar extends React.PureComponent<IProps, IState> {
private unmounted = false;
public static contextType = MatrixClientContext;
public context!: React.ContextType<typeof MatrixClientContext>;
public constructor(props: IProps, context: typeof MatrixClientContext) {
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
super(props, context);
this.context = context; // XXX: workaround for lack of `declare` support on `public context!:` definition
this.state = {
syncState: this.context.getSyncState(),
@ -102,8 +113,8 @@ export default class RoomStatusBar extends React.PureComponent<IProps, IState> {
public componentDidMount(): void {
const client = this.context;
client.on("sync", this.onSyncStateChange);
client.on("Room.localEchoUpdated", this.onRoomLocalEchoUpdated);
client.on(ClientEvent.Sync, this.onSyncStateChange);
client.on(RoomEvent.LocalEchoUpdated, this.onRoomLocalEchoUpdated);
this.checkSize();
}
@ -117,19 +128,19 @@ export default class RoomStatusBar extends React.PureComponent<IProps, IState> {
// we may have entirely lost our client as we're logging out before clicking login on the guest bar...
const client = this.context;
if (client) {
client.removeListener("sync", this.onSyncStateChange);
client.removeListener("Room.localEchoUpdated", this.onRoomLocalEchoUpdated);
client.removeListener(ClientEvent.Sync, this.onSyncStateChange);
client.removeListener(RoomEvent.LocalEchoUpdated, this.onRoomLocalEchoUpdated);
}
}
private onSyncStateChange = (state: SyncState, prevState: SyncState, data: SyncStateData): void => {
private onSyncStateChange = (state: SyncState, prevState: SyncState | null, data?: SyncStateData): void => {
if (state === "SYNCING" && prevState === "SYNCING") {
return;
}
if (this.unmounted) return;
this.setState({
syncState: state,
syncStateData: data,
syncStateData: data ?? null,
});
};