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
35 changed files with 99 additions and 107 deletions

View File

@@ -45,10 +45,11 @@ interface IState {
export default class EmbeddedPage extends React.PureComponent<IProps, IState> {
public static contextType = MatrixClientContext;
public context!: React.ContextType<typeof MatrixClientContext>;
private unmounted = false;
private dispatcherRef: string | null = null;
public constructor(props: IProps, context: typeof MatrixClientContext) {
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
super(props, context);
this.state = {

View File

@@ -59,6 +59,7 @@ interface IState {
*/
class FilePanel extends React.Component<IProps, IState> {
public static contextType = RoomContext;
public context!: React.ContextType<typeof RoomContext>;
// This is used to track if a decrypted event was a live event and should be
// added to the timeline.

View File

@@ -42,11 +42,12 @@ interface IState {
*/
export default class NotificationPanel extends React.PureComponent<IProps, IState> {
public static contextType = RoomContext;
public context!: React.ContextType<typeof RoomContext>;
private card = React.createRef<HTMLDivElement>();
public constructor(props: IProps) {
super(props);
public constructor(props: IProps, context: React.ContextType<typeof RoomContext>) {
super(props, context);
this.state = {
narrow: false,

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,
});
};

View File

@@ -93,8 +93,8 @@ export default class ThreadView extends React.Component<IProps, IState> {
// Set by setEventId in ctor.
private eventId!: string;
public constructor(props: IProps) {
super(props);
public constructor(props: IProps, context: React.ContextType<typeof RoomContext>) {
super(props, context);
this.setEventId(this.props.mxEvent);
const thread = this.props.room.getThread(this.eventId) ?? undefined;

View File

@@ -43,8 +43,8 @@ export default class UserView extends React.Component<IProps, IState> {
public static contextType = MatrixClientContext;
public context!: React.ContextType<typeof MatrixClientContext>;
public constructor(props: IProps) {
super(props);
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
super(props, context);
this.state = {
loading: true,
};