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

Commit b6addb4

Browse files
authored
Fix React contexts (#12855)
1 parent 9b77279 commit b6addb4

35 files changed

+100
-108
lines changed

src/components/structures/EmbeddedPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ interface IState {
4545

4646
export default class EmbeddedPage extends React.PureComponent<IProps, IState> {
4747
public static contextType = MatrixClientContext;
48+
public context!: React.ContextType<typeof MatrixClientContext>;
4849
private unmounted = false;
4950
private dispatcherRef: string | null = null;
5051

51-
public constructor(props: IProps, context: typeof MatrixClientContext) {
52+
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
5253
super(props, context);
5354

5455
this.state = {

src/components/structures/FilePanel.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ interface IState {
5959
*/
6060
class FilePanel extends React.Component<IProps, IState> {
6161
public static contextType = RoomContext;
62+
public context!: React.ContextType<typeof RoomContext>;
6263

6364
// This is used to track if a decrypted event was a live event and should be
6465
// added to the timeline.

src/components/structures/NotificationPanel.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ interface IState {
4242
*/
4343
export default class NotificationPanel extends React.PureComponent<IProps, IState> {
4444
public static contextType = RoomContext;
45+
public context!: React.ContextType<typeof RoomContext>;
4546

4647
private card = React.createRef<HTMLDivElement>();
4748

48-
public constructor(props: IProps) {
49-
super(props);
49+
public constructor(props: IProps, context: React.ContextType<typeof RoomContext>) {
50+
super(props, context);
5051

5152
this.state = {
5253
narrow: false,

src/components/structures/RoomStatusBar.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ limitations under the License.
1515
*/
1616

1717
import React, { ReactNode } from "react";
18-
import { EventStatus, MatrixEvent, Room, MatrixError, SyncState, SyncStateData } from "matrix-js-sdk/src/matrix";
18+
import {
19+
ClientEvent,
20+
EventStatus,
21+
MatrixError,
22+
MatrixEvent,
23+
Room,
24+
RoomEvent,
25+
SyncState,
26+
SyncStateData,
27+
} from "matrix-js-sdk/src/matrix";
1928

2029
import { Icon as WarningIcon } from "../../../res/img/feather-customised/warning-triangle.svg";
2130
import { _t, _td } from "../../languageHandler";
@@ -79,18 +88,20 @@ interface IProps {
7988
}
8089

8190
interface IState {
82-
syncState: SyncState;
83-
syncStateData: SyncStateData;
91+
syncState: SyncState | null;
92+
syncStateData: SyncStateData | null;
8493
unsentMessages: MatrixEvent[];
8594
isResending: boolean;
8695
}
8796

8897
export default class RoomStatusBar extends React.PureComponent<IProps, IState> {
8998
private unmounted = false;
9099
public static contextType = MatrixClientContext;
100+
public context!: React.ContextType<typeof MatrixClientContext>;
91101

92-
public constructor(props: IProps, context: typeof MatrixClientContext) {
102+
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
93103
super(props, context);
104+
this.context = context; // XXX: workaround for lack of `declare` support on `public context!:` definition
94105

95106
this.state = {
96107
syncState: this.context.getSyncState(),
@@ -102,8 +113,8 @@ export default class RoomStatusBar extends React.PureComponent<IProps, IState> {
102113

103114
public componentDidMount(): void {
104115
const client = this.context;
105-
client.on("sync", this.onSyncStateChange);
106-
client.on("Room.localEchoUpdated", this.onRoomLocalEchoUpdated);
116+
client.on(ClientEvent.Sync, this.onSyncStateChange);
117+
client.on(RoomEvent.LocalEchoUpdated, this.onRoomLocalEchoUpdated);
107118

108119
this.checkSize();
109120
}
@@ -117,19 +128,19 @@ export default class RoomStatusBar extends React.PureComponent<IProps, IState> {
117128
// we may have entirely lost our client as we're logging out before clicking login on the guest bar...
118129
const client = this.context;
119130
if (client) {
120-
client.removeListener("sync", this.onSyncStateChange);
121-
client.removeListener("Room.localEchoUpdated", this.onRoomLocalEchoUpdated);
131+
client.removeListener(ClientEvent.Sync, this.onSyncStateChange);
132+
client.removeListener(RoomEvent.LocalEchoUpdated, this.onRoomLocalEchoUpdated);
122133
}
123134
}
124135

125-
private onSyncStateChange = (state: SyncState, prevState: SyncState, data: SyncStateData): void => {
136+
private onSyncStateChange = (state: SyncState, prevState: SyncState | null, data?: SyncStateData): void => {
126137
if (state === "SYNCING" && prevState === "SYNCING") {
127138
return;
128139
}
129140
if (this.unmounted) return;
130141
this.setState({
131142
syncState: state,
132-
syncStateData: data,
143+
syncStateData: data ?? null,
133144
});
134145
};
135146

src/components/structures/ThreadView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ export default class ThreadView extends React.Component<IProps, IState> {
9393
// Set by setEventId in ctor.
9494
private eventId!: string;
9595

96-
public constructor(props: IProps) {
97-
super(props);
96+
public constructor(props: IProps, context: React.ContextType<typeof RoomContext>) {
97+
super(props, context);
9898

9999
this.setEventId(this.props.mxEvent);
100100
const thread = this.props.room.getThread(this.eventId) ?? undefined;

src/components/structures/UserView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export default class UserView extends React.Component<IProps, IState> {
4343
public static contextType = MatrixClientContext;
4444
public context!: React.ContextType<typeof MatrixClientContext>;
4545

46-
public constructor(props: IProps) {
47-
super(props);
46+
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
47+
super(props, context);
4848
this.state = {
4949
loading: true,
5050
};

src/components/views/context_menus/MessageContextMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
137137

138138
private reactButtonRef = createRef<any>(); // XXX Ref to a functional component
139139

140-
public constructor(props: IProps) {
141-
super(props);
140+
public constructor(props: IProps, context: React.ContextType<typeof RoomContext>) {
141+
super(props, context);
142142

143143
this.state = {
144144
canRedact: false,

src/components/views/elements/AppTile.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export default class AppTile extends React.Component<IProps, IState> {
143143
private unmounted = false;
144144

145145
public constructor(props: IProps, context: ContextType<typeof MatrixClientContext>) {
146-
super(props);
146+
super(props, context);
147147
this.context = context; // XXX: workaround for lack of `declare` support on `public context!:` definition
148148

149149
// Tiles in miniMode are floating, and therefore not docked

src/components/views/location/LocationPicker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class LocationPicker extends React.Component<ILocationPickerProps, IState> {
5555
private geolocate?: maplibregl.GeolocateControl;
5656
private marker?: maplibregl.Marker;
5757

58-
public constructor(props: ILocationPickerProps) {
59-
super(props);
58+
public constructor(props: ILocationPickerProps, context: React.ContextType<typeof MatrixClientContext>) {
59+
super(props, context);
6060

6161
this.state = {
6262
position: undefined,

src/components/views/messages/EditHistoryMessage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class EditHistoryMessage extends React.PureComponent<IProps, ISta
5959
private tooltips: Element[] = [];
6060

6161
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
62-
super(props);
62+
super(props, context);
6363
this.context = context;
6464

6565
const cli = this.context;

0 commit comments

Comments
 (0)