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

Commit be5928c

Browse files
authored
Conform more of the codebase to strictNullChecks (#10672)
* Conform more of the codebase to `strictNullChecks` * Iterate * Iterate * Iterate * Iterate * Conform more of the codebase to `strictNullChecks` * Iterate * Update record key
1 parent 792a39a commit be5928c

34 files changed

+143
-115
lines changed

src/Avatar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { isLocalRoom } from "./utils/localRoom/isLocalRoom";
2626

2727
// Not to be used for BaseAvatar urls as that has similar default avatar fallback already
2828
export function avatarUrlForMember(
29-
member: RoomMember,
29+
member: RoomMember | undefined,
3030
width: number,
3131
height: number,
3232
resizeMethod: ResizeMethod,

src/Searching.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@ async function localSearch(
176176
searchArgs.room_id = roomId;
177177
}
178178

179-
const localResult = await eventIndex.search(searchArgs);
179+
const localResult = await eventIndex!.search(searchArgs);
180+
if (!localResult) {
181+
throw new Error("Local search failed");
182+
}
180183

181184
searchArgs.next_batch = localResult.next_batch;
182185

@@ -225,7 +228,11 @@ async function localPagination(searchResult: ISeshatSearchResults): Promise<ISes
225228

226229
const searchArgs = searchResult.seshatQuery;
227230

228-
const localResult = await eventIndex.search(searchArgs);
231+
const localResult = await eventIndex!.search(searchArgs);
232+
if (!localResult) {
233+
throw new Error("Local search pagination failed");
234+
}
235+
229236
searchResult.seshatQuery.next_batch = localResult.next_batch;
230237

231238
// We only need to restore the encryption state for the new results, so
@@ -477,7 +484,7 @@ function combineResponses(
477484
// Set the response next batch token to one of the tokens from the sources,
478485
// this makes sure that if we exhaust one of the sources we continue with
479486
// the other one.
480-
if (previousSearchResult.seshatQuery.next_batch) {
487+
if (previousSearchResult.seshatQuery?.next_batch) {
481488
response.next_batch = previousSearchResult.seshatQuery.next_batch;
482489
} else if (previousSearchResult.serverSideNextBatch) {
483490
response.next_batch = previousSearchResult.serverSideNextBatch;
@@ -535,13 +542,13 @@ async function combinedPagination(searchResult: ISeshatSearchResults): Promise<I
535542
const searchArgs = searchResult.seshatQuery;
536543
const oldestEventFrom = searchResult.oldestEventFrom;
537544

538-
let localResult: IResultRoomEvents;
539-
let serverSideResult: ISearchResponse;
545+
let localResult: IResultRoomEvents | undefined;
546+
let serverSideResult: ISearchResponse | undefined;
540547

541548
// Fetch events from the local index if we have a token for it and if it's
542549
// the local indexes turn or the server has exhausted its results.
543-
if (searchArgs.next_batch && (!searchResult.serverSideNextBatch || oldestEventFrom === "server")) {
544-
localResult = await eventIndex.search(searchArgs);
550+
if (searchArgs?.next_batch && (!searchResult.serverSideNextBatch || oldestEventFrom === "server")) {
551+
localResult = await eventIndex!.search(searchArgs);
545552
}
546553

547554
// Fetch events from the server if we have a token for it and if it's the
@@ -551,7 +558,7 @@ async function combinedPagination(searchResult: ISeshatSearchResults): Promise<I
551558
serverSideResult = await client.search(body);
552559
}
553560

554-
let serverEvents: IResultRoomEvents;
561+
let serverEvents: IResultRoomEvents | undefined;
555562

556563
if (serverSideResult) {
557564
serverEvents = serverSideResult.search_categories.room_events;

src/actions/RoomListActions.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ export default class RoomListActions {
5151
room: Room,
5252
oldTag: TagID | null,
5353
newTag: TagID | null,
54-
oldIndex?: number,
55-
newIndex?: number,
54+
newIndex: number,
5655
): AsyncActionPayload {
5756
let metaData: Parameters<MatrixClient["setRoomTag"]>[2] | null = null;
5857

@@ -63,12 +62,8 @@ export default class RoomListActions {
6362

6463
newList.sort((a, b) => a.tags[newTag].order - b.tags[newTag].order);
6564

66-
// If the room was moved "down" (increasing index) in the same list we
67-
// need to use the orders of the tiles with indices shifted by +1
68-
const offset = newTag === oldTag && oldIndex < newIndex ? 1 : 0;
69-
70-
const indexBefore = offset + newIndex - 1;
71-
const indexAfter = offset + newIndex;
65+
const indexBefore = newIndex - 1;
66+
const indexAfter = newIndex;
7267

7368
const prevOrder = indexBefore <= 0 ? 0 : newList[indexBefore].tags[newTag].order;
7469
const nextOrder = indexAfter >= newList.length ? 1 : newList[indexAfter].tags[newTag].order;

src/components/structures/MessagePanel.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
883883

884884
const existingReceipts = receiptsByEvent.get(lastShownEventId) || [];
885885
const newReceipts = this.getReadReceiptsForEvent(event);
886+
if (!newReceipts) continue;
886887
receiptsByEvent.set(lastShownEventId, existingReceipts.concat(newReceipts));
887888

888889
// Record these receipts along with their last shown event ID for
@@ -1218,7 +1219,7 @@ class CreationGrouper extends BaseGrouper {
12181219
key="roomcreationsummary"
12191220
events={this.events}
12201221
onToggle={panel.onHeightChanged} // Update scroll state
1221-
summaryMembers={[ev.sender]}
1222+
summaryMembers={ev.sender ? [ev.sender] : undefined}
12221223
summaryText={summaryText}
12231224
layout={this.panel.props.layout}
12241225
>

src/components/structures/RoomView.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
627627
mainSplitContentType: room ? this.getMainSplitContentType(room) : undefined,
628628
initialEventId: undefined, // default to clearing this, will get set later in the method if needed
629629
showRightPanel: this.context.rightPanelStore.isOpenForRoom(roomId),
630-
activeCall: CallStore.instance.getActiveCall(roomId),
630+
activeCall: roomId ? CallStore.instance.getActiveCall(roomId) : null,
631631
};
632632

633633
if (
@@ -1071,6 +1071,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
10711071
};
10721072

10731073
private onAction = async (payload: ActionPayload): Promise<void> => {
1074+
if (!this.context.client) return;
10741075
switch (payload.action) {
10751076
case "message_sent":
10761077
this.checkDesktopNotifications();
@@ -1228,7 +1229,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
12281229
this.handleEffects(ev);
12291230
}
12301231

1231-
if (ev.getSender() !== this.context.client.getSafeUserId()) {
1232+
if (this.context.client && ev.getSender() !== this.context.client.getSafeUserId()) {
12321233
// update unread count when scrolled up
12331234
if (!this.state.search && this.state.atEndOfLiveTimeline) {
12341235
// no change
@@ -1469,7 +1470,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
14691470
};
14701471

14711472
private updatePermissions(room: Room): void {
1472-
if (room) {
1473+
if (room && this.context.client) {
14731474
const me = this.context.client.getSafeUserId();
14741475
const canReact =
14751476
room.getMyMembership() === "join" && room.currentState.maySendEvent(EventType.Reaction, me);
@@ -1956,6 +1957,8 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
19561957
}
19571958

19581959
public render(): React.ReactNode {
1960+
if (!this.context.client) return null;
1961+
19591962
if (this.state.room instanceof LocalRoom) {
19601963
if (this.state.room.state === LocalRoomState.CREATING) {
19611964
return this.renderLocalRoomCreateLoader(this.state.room);
@@ -2064,7 +2067,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
20642067
const inviteEvent = myMember ? myMember.events.member : null;
20652068
let inviterName = _t("Unknown");
20662069
if (inviteEvent) {
2067-
inviterName = inviteEvent.sender?.name ?? inviteEvent.getSender();
2070+
inviterName = inviteEvent.sender?.name ?? inviteEvent.getSender()!;
20682071
}
20692072

20702073
// We deliberately don't try to peek into invites, even if we have permission to peek

src/components/structures/ScrollPanel.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ export default class ScrollPanel extends React.Component<IProps> {
786786
const scrollState = this.scrollState;
787787
const trackedNode = scrollState.trackedNode;
788788

789-
if (!trackedNode?.parentElement) {
789+
if (!trackedNode?.parentElement && this.itemlist.current) {
790790
let node: HTMLElement | undefined = undefined;
791791
const messages = this.itemlist.current.children;
792792
const scrollToken = scrollState.trackedScrollToken;
@@ -890,7 +890,7 @@ export default class ScrollPanel extends React.Component<IProps> {
890890
public clearPreventShrinking = (): void => {
891891
const messageList = this.itemlist.current;
892892
const balanceElement = messageList && messageList.parentElement;
893-
if (balanceElement) balanceElement.style.paddingBottom = null;
893+
if (balanceElement) balanceElement.style.removeProperty("paddingBottom");
894894
this.preventShrinkingState = null;
895895
debuglog("prevent shrinking cleared");
896896
};
@@ -904,7 +904,7 @@ export default class ScrollPanel extends React.Component<IProps> {
904904
what it was when marking.
905905
*/
906906
public updatePreventShrinking = (): void => {
907-
if (this.preventShrinkingState) {
907+
if (this.preventShrinkingState && this.itemlist.current) {
908908
const sn = this.getScrollNode();
909909
const scrollState = this.scrollState;
910910
const messageList = this.itemlist.current;
@@ -922,7 +922,7 @@ export default class ScrollPanel extends React.Component<IProps> {
922922
if (!shouldClear) {
923923
const currentOffset = messageList.clientHeight - (offsetNode.offsetTop + offsetNode.clientHeight);
924924
const offsetDiff = offsetFromBottom - currentOffset;
925-
if (offsetDiff > 0) {
925+
if (offsetDiff > 0 && balanceElement) {
926926
balanceElement.style.paddingBottom = `${offsetDiff}px`;
927927
debuglog("update prevent shrinking ", offsetDiff, "px from bottom");
928928
} else if (offsetDiff < 0) {

src/components/structures/UserView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export default class UserView extends React.Component<IProps, IState> {
7979
return;
8080
}
8181
const fakeEvent = new MatrixEvent({ type: "m.room.member", content: profileInfo });
82-
const member = new RoomMember(null, this.props.userId);
82+
// We pass an empty string room ID here, this is slight abuse of the class to simplify code
83+
const member = new RoomMember("", this.props.userId);
8384
member.setMembershipEvent(fakeEvent);
8485
this.setState({ member, loading: false });
8586
}

src/components/views/context_menus/RoomContextMenu.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ const RoomContextMenu: React.FC<IProps> = ({ room, onFinished, ...props }) => {
169169
);
170170

171171
const echoChamber = EchoChamber.forRoom(room);
172-
let notificationLabel: string;
173-
let iconClassName: string;
172+
let notificationLabel: string | undefined;
173+
let iconClassName: string | undefined;
174174
switch (echoChamber.notificationVolume) {
175175
case RoomNotifState.AllMessages:
176176
notificationLabel = _t("Default");
@@ -337,7 +337,7 @@ const RoomContextMenu: React.FC<IProps> = ({ room, onFinished, ...props }) => {
337337
const isApplied = RoomListStore.instance.getTagsForRoom(room).includes(tagId);
338338
const removeTag = isApplied ? tagId : inverseTag;
339339
const addTag = isApplied ? null : tagId;
340-
dis.dispatch(RoomListActions.tagRoom(cli, room, removeTag, addTag, undefined, 0));
340+
dis.dispatch(RoomListActions.tagRoom(cli, room, removeTag, addTag, 0));
341341
} else {
342342
logger.warn(`Unexpected tag ${tagId} applied to ${room.roomId}`);
343343
}

src/components/views/context_menus/RoomGeneralContextMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const RoomGeneralContextMenu: React.FC<RoomGeneralContextMenuProps> = ({
100100
const isApplied = RoomListStore.instance.getTagsForRoom(room).includes(tagId);
101101
const removeTag = isApplied ? tagId : inverseTag;
102102
const addTag = isApplied ? null : tagId;
103-
dis.dispatch(RoomListActions.tagRoom(cli, room, removeTag, addTag, undefined, 0));
103+
dis.dispatch(RoomListActions.tagRoom(cli, room, removeTag, addTag, 0));
104104
} else {
105105
logger.warn(`Unexpected tag ${tagId} applied to ${room.roomId}`);
106106
}

src/components/views/dialogs/ReportEventDialog.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export default class ReportEventDialog extends React.Component<IProps, IState> {
260260

261261
// if the user should also be ignored, do that
262262
if (this.state.ignoreUserToo) {
263-
await client.setIgnoredUsers([...client.getIgnoredUsers(), ev.getSender()]);
263+
await client.setIgnoredUsers([...client.getIgnoredUsers(), ev.getSender()!]);
264264
}
265265

266266
this.props.onFinished(true);
@@ -309,8 +309,8 @@ export default class ReportEventDialog extends React.Component<IProps, IState> {
309309
// Display report-to-moderator dialog.
310310
// We let the user pick a nature.
311311
const client = MatrixClientPeg.get();
312-
const homeServerName = SdkConfig.get("validated_server_config").hsName;
313-
let subtitle;
312+
const homeServerName = SdkConfig.get("validated_server_config")!.hsName;
313+
let subtitle: string;
314314
switch (this.state.nature) {
315315
case Nature.Disagreement:
316316
subtitle = _t(

0 commit comments

Comments
 (0)