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

Commit bc60a9b

Browse files
authored
Conform more of the codebase to strictNullChecks (#10504
* Conform more of the codebase to `strictNullChecks` * Iterate
1 parent 6db0c7a commit bc60a9b

File tree

18 files changed

+60
-54
lines changed

18 files changed

+60
-54
lines changed

src/LegacyCallHandler.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ export default class LegacyCallHandler extends EventEmitter {
905905

906906
const timeUntilTurnCresExpire = MatrixClientPeg.get().getTurnServersExpiry() - Date.now();
907907
logger.log("Current turn creds expire in " + timeUntilTurnCresExpire + " ms");
908-
const call = MatrixClientPeg.get().createCall(mappedRoomId);
908+
const call = MatrixClientPeg.get().createCall(mappedRoomId)!;
909909

910910
try {
911911
this.addCallForRoom(roomId, call);

src/components/views/dialogs/AddExistingToSpaceDialog.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import React, { ReactElement, ReactNode, useContext, useMemo, useRef, useState } from "react";
17+
import React, { ReactElement, ReactNode, RefObject, useContext, useMemo, useRef, useState } from "react";
1818
import classNames from "classnames";
1919
import { Room } from "matrix-js-sdk/src/models/room";
2020
import { sleep } from "matrix-js-sdk/src/utils";
@@ -140,11 +140,12 @@ export const AddExistingToSpace: React.FC<IAddExistingToSpaceProps> = ({
140140
const cli = useContext(MatrixClientContext);
141141
const msc3946ProcessDynamicPredecessor = useSettingValue<boolean>("feature_dynamic_room_predecessors");
142142
const visibleRooms = useMemo(
143-
() => cli.getVisibleRooms(msc3946ProcessDynamicPredecessor).filter((r) => r.getMyMembership() === "join"),
143+
() =>
144+
cli?.getVisibleRooms(msc3946ProcessDynamicPredecessor).filter((r) => r.getMyMembership() === "join") ?? [],
144145
[cli, msc3946ProcessDynamicPredecessor],
145146
);
146147

147-
const scrollRef = useRef<AutoHideScrollbar<"div">>();
148+
const scrollRef = useRef() as RefObject<AutoHideScrollbar<"div">>;
148149
const [scrollState, setScrollState] = useState<IScrollState>({
149150
// these are estimates which update as soon as it mounts
150151
scrollTop: 0,
@@ -212,7 +213,7 @@ export const AddExistingToSpace: React.FC<IAddExistingToSpaceProps> = ({
212213

213214
throw e;
214215
});
215-
setProgress((i) => i + 1);
216+
setProgress((i) => (i ?? 0) + 1);
216217
} catch (e) {
217218
logger.error("Failed to add rooms to space", e);
218219
error = e;
@@ -305,13 +306,15 @@ export const AddExistingToSpace: React.FC<IAddExistingToSpaceProps> = ({
305306

306307
const onScroll = (): void => {
307308
const body = scrollRef.current?.containerRef.current;
309+
if (!body) return;
308310
setScrollState({
309311
scrollTop: body.scrollTop,
310312
height: body.clientHeight,
311313
});
312314
};
313315

314-
const wrappedRef = (body: HTMLDivElement): void => {
316+
const wrappedRef = (body: HTMLDivElement | null): void => {
317+
if (!body) return;
315318
setScrollState({
316319
scrollTop: body.scrollTop,
317320
height: body.clientHeight,

src/components/views/dialogs/ConfirmAndWaitRedactDialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface IProps {
2929

3030
interface IState {
3131
isRedacting: boolean;
32-
redactionErrorCode: string | number;
32+
redactionErrorCode: string | number | null;
3333
}
3434

3535
/*

src/components/views/dialogs/ExportDialog.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import React, { useRef, useState, Dispatch, SetStateAction } from "react";
17+
import React, { useRef, useState, Dispatch, SetStateAction, RefObject } from "react";
1818
import { Room } from "matrix-js-sdk/src/matrix";
1919
import { logger } from "matrix-js-sdk/src/logger";
2020

@@ -104,8 +104,8 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
104104
} = useExportFormState();
105105

106106
const [isExporting, setExporting] = useState(false);
107-
const sizeLimitRef = useRef<Field>();
108-
const messageCountRef = useRef<Field>();
107+
const sizeLimitRef = useRef() as RefObject<Field>;
108+
const messageCountRef = useRef() as RefObject<Field>;
109109
const [exportProgressText, setExportProgressText] = useState(_t("Processing…"));
110110
const [displayCancel, setCancelWarning] = useState(false);
111111
const [exportCancelled, setExportCancelled] = useState(false);
@@ -144,18 +144,18 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
144144
const onExportClick = async (): Promise<void> => {
145145
const isValidSize =
146146
!setSizeLimit ||
147-
(await sizeLimitRef.current.validate({
147+
(await sizeLimitRef.current?.validate({
148148
focused: false,
149149
}));
150150

151151
if (!isValidSize) {
152-
sizeLimitRef.current.validate({ focused: true });
152+
sizeLimitRef.current?.validate({ focused: true });
153153
return;
154154
}
155155
if (exportType === ExportType.LastNMessages) {
156-
const isValidNumberOfMessages = await messageCountRef.current.validate({ focused: false });
156+
const isValidNumberOfMessages = await messageCountRef.current?.validate({ focused: false });
157157
if (!isValidNumberOfMessages) {
158-
messageCountRef.current.validate({ focused: true });
158+
messageCountRef.current?.validate({ focused: true });
159159
return;
160160
}
161161
}

src/components/views/dialogs/IncomingSasDialog.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import React from "react";
17+
import React, { ReactNode } from "react";
1818
import { IGeneratedSas, ISasEvent, SasEvent } from "matrix-js-sdk/src/crypto/verification/SAS";
1919
import { VerificationBase, VerificationEvent } from "matrix-js-sdk/src/crypto/verification/Base";
2020
import { logger } from "matrix-js-sdk/src/logger";
@@ -48,13 +48,13 @@ interface IState {
4848
// eslint-disable-next-line camelcase
4949
avatar_url?: string;
5050
displayname?: string;
51-
};
52-
opponentProfileError: Error;
53-
sas: IGeneratedSas;
51+
} | null;
52+
opponentProfileError: Error | null;
53+
sas: IGeneratedSas | null;
5454
}
5555

5656
export default class IncomingSasDialog extends React.Component<IProps, IState> {
57-
private showSasEvent: ISasEvent;
57+
private showSasEvent: ISasEvent | null;
5858

5959
public constructor(props: IProps) {
6060
super(props);
@@ -93,7 +93,7 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {
9393
});
9494
} catch (e) {
9595
this.setState({
96-
opponentProfileError: e,
96+
opponentProfileError: e as Error,
9797
});
9898
}
9999
}
@@ -133,7 +133,7 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {
133133
};
134134

135135
private onSasMatchesClick = (): void => {
136-
this.showSasEvent.confirm();
136+
this.showSasEvent?.confirm();
137137
this.setState({
138138
phase: PHASE_WAIT_FOR_PARTNER_TO_CONFIRM,
139139
});
@@ -143,7 +143,7 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {
143143
this.props.onFinished(true);
144144
};
145145

146-
private renderPhaseStart(): JSX.Element {
146+
private renderPhaseStart(): ReactNode {
147147
const isSelf = this.props.verifier.userId === MatrixClientPeg.get().getUserId();
148148

149149
let profile;
@@ -227,7 +227,8 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {
227227
);
228228
}
229229

230-
private renderPhaseShowSas(): JSX.Element {
230+
private renderPhaseShowSas(): ReactNode {
231+
if (!this.showSasEvent) return null;
231232
return (
232233
<VerificationShowSas
233234
sas={this.showSasEvent.sas}
@@ -239,7 +240,7 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {
239240
);
240241
}
241242

242-
private renderPhaseWaitForPartnerToConfirm(): JSX.Element {
243+
private renderPhaseWaitForPartnerToConfirm(): ReactNode {
243244
return (
244245
<div>
245246
<Spinner />
@@ -248,15 +249,15 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {
248249
);
249250
}
250251

251-
private renderPhaseVerified(): JSX.Element {
252+
private renderPhaseVerified(): ReactNode {
252253
return <VerificationComplete onDone={this.onVerifiedDoneClick} />;
253254
}
254255

255-
private renderPhaseCancelled(): JSX.Element {
256+
private renderPhaseCancelled(): ReactNode {
256257
return <VerificationCancelled onDone={this.onCancelClick} />;
257258
}
258259

259-
public render(): React.ReactNode {
260+
public render(): ReactNode {
260261
let body;
261262
switch (this.state.phase) {
262263
case PHASE_START:

src/components/views/dialogs/LeaveSpaceDialog.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ import DialogButtons from "../elements/DialogButtons";
2323
import BaseDialog from "../dialogs/BaseDialog";
2424
import SpaceStore from "../../../stores/spaces/SpaceStore";
2525
import SpaceChildrenPicker from "../spaces/SpaceChildrenPicker";
26+
import { filterBoolean } from "../../../utils/arrays";
2627

2728
interface IProps {
2829
space: Room;
2930
onFinished(leave: boolean, rooms?: Room[]): void;
3031
}
3132

3233
const isOnlyAdmin = (room: Room): boolean => {
33-
const userId = room.client.getUserId();
34-
if (room.getMember(userId).powerLevelNorm !== 100) {
34+
const userId = room.client.getSafeUserId();
35+
if (room.getMember(userId)?.powerLevelNorm !== 100) {
3536
return false; // user is not an admin
3637
}
3738
return room.getJoinedMembers().every((member) => {
@@ -51,9 +52,7 @@ const LeaveSpaceDialog: React.FC<IProps> = ({ space, onFinished }) => {
5152
},
5253
false,
5354
);
54-
return Array.from(roomSet)
55-
.map((roomId) => space.client.getRoom(roomId))
56-
.filter(Boolean);
55+
return filterBoolean(Array.from(roomSet).map((roomId) => space.client.getRoom(roomId)));
5756
}, [space]);
5857
const [roomsToLeave, setRoomsToLeave] = useState<Room[]>([]);
5958
const selectedRooms = useMemo(() => new Set(roomsToLeave), [roomsToLeave]);

src/components/views/dialogs/security/CreateCrossSigningDialog.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ import InteractiveAuthDialog from "../InteractiveAuthDialog";
3232
interface IProps {
3333
accountPassword?: string;
3434
tokenLogin?: boolean;
35-
onFinished?: (success?: boolean) => void;
35+
onFinished: (success?: boolean) => void;
3636
}
3737

3838
interface IState {
3939
error: Error | null;
40-
canUploadKeysWithPasswordOnly?: boolean;
40+
canUploadKeysWithPasswordOnly: boolean | null;
4141
accountPassword: string;
4242
}
4343

@@ -73,7 +73,7 @@ export default class CreateCrossSigningDialog extends React.PureComponent<IProps
7373

7474
private async queryKeyUploadAuth(): Promise<void> {
7575
try {
76-
await MatrixClientPeg.get().uploadDeviceSigningKeys(null, {} as CrossSigningKeys);
76+
await MatrixClientPeg.get().uploadDeviceSigningKeys(undefined, {} as CrossSigningKeys);
7777
// We should never get here: the server should always require
7878
// UI auth to upload device signing keys. If we do, we upload
7979
// no keys which would be a no-op.

src/components/views/messages/MPollBody.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export default class MPollBody extends React.Component<IBodyProps, IState> {
236236
* @returns userId -> UserVote
237237
*/
238238
private collectUserVotes(): Map<string, UserVote> {
239-
if (!this.state.voteRelations) {
239+
if (!this.state.voteRelations || !this.context) {
240240
return new Map<string, UserVote>();
241241
}
242242
return collectUserVotes(allVotes(this.state.voteRelations), this.context.getUserId(), this.state.selected);

src/components/views/right_panel/PinnedMessagesCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const PinnedMessagesCard: React.FC<IProps> = ({ room, onClose, permalinkCreator
106106
const newlyRead = pinnedEventIds.filter((id) => !readPinnedEvents.has(id));
107107
if (newlyRead.length > 0) {
108108
// clear out any read pinned events which no longer are pinned
109-
cli.setRoomAccountData(room.roomId, ReadPinsEventId, {
109+
cli?.setRoomAccountData(room.roomId, ReadPinsEventId, {
110110
event_ids: pinnedEventIds,
111111
});
112112
}

src/components/views/rooms/MemberList.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class MemberList extends React.Component<IProps, IState> {
7777
public constructor(props: IProps, context: React.ContextType<typeof SDKContext>) {
7878
super(props);
7979
this.state = this.getMembersState([], []);
80-
this.showPresence = context.memberListStore.isPresenceEnabled();
80+
this.showPresence = context?.memberListStore.isPresenceEnabled() ?? true;
8181
this.mounted = true;
8282
this.listenForMembersChanges();
8383
}
@@ -278,7 +278,7 @@ export default class MemberList extends React.Component<IProps, IState> {
278278
});
279279
};
280280

281-
private getPending3PidInvites(): MatrixEvent[] | undefined {
281+
private getPending3PidInvites(): MatrixEvent[] {
282282
// include 3pid invites (m.room.third_party_invite) state events.
283283
// The HS may have already converted these into m.room.member invites so
284284
// we shouldn't add them if the 3pid invite state key (token) is in the
@@ -291,11 +291,13 @@ export default class MemberList extends React.Component<IProps, IState> {
291291

292292
// discard all invites which have a m.room.member event since we've
293293
// already added them.
294-
const memberEvent = room.currentState.getInviteForThreePidToken(e.getStateKey());
294+
const memberEvent = room.currentState.getInviteForThreePidToken(e.getStateKey()!);
295295
if (memberEvent) return false;
296296
return true;
297297
});
298298
}
299+
300+
return [];
299301
}
300302

301303
private makeMemberTiles(members: Array<RoomMember | MatrixEvent>): JSX.Element[] {

0 commit comments

Comments
 (0)