Conform more code to strictNullChecks (#10444

* Conform more code to `strictNullChecks`

* Fix tests

* Fix tests
This commit is contained in:
Michael Telatynski 2023-03-27 08:01:09 +01:00 committed by GitHub
parent ba2608ec74
commit c225b8ec29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 85 additions and 75 deletions

View file

@ -24,8 +24,6 @@ import { logger } from "matrix-js-sdk/src/logger";
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
import { isSupportedReceiptType } from "matrix-js-sdk/src/utils";
import { ReadReceipt } from "matrix-js-sdk/src/models/read-receipt";
import { ListenerMap } from "matrix-js-sdk/src/models/typed-event-emitter";
import shouldHideEvent from "../../shouldHideEvent";
import { wantsDateSeparator } from "../../DateUtils";
@ -543,7 +541,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
return null;
}
private collectGhostReadMarker = (node: HTMLElement): void => {
private collectGhostReadMarker = (node: HTMLElement | null): void => {
if (node) {
// now the element has appeared, change the style which will trigger the CSS transition
requestAnimationFrame(() => {
@ -788,13 +786,13 @@ export default class MessagePanel extends React.Component<IProps, IState> {
continuation={continuation}
isRedacted={mxEv.isRedacted()}
replacingEventId={mxEv.replacingEventId()}
editState={isEditing && this.props.editState}
editState={isEditing ? this.props.editState : undefined}
onHeightChanged={this.onHeightChanged}
readReceipts={readReceipts}
readReceiptMap={this.readReceiptMap}
showUrlPreview={this.props.showUrlPreview}
checkUnmounting={this.isUnmounting}
eventSendStatus={mxEv.getAssociatedStatus()}
eventSendStatus={mxEv.getAssociatedStatus() ?? undefined}
isTwelveHour={this.props.isTwelveHour}
permalinkCreator={this.props.permalinkCreator}
last={last}
@ -836,9 +834,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
return null;
}
const receiptDestination: ReadReceipt<string, ListenerMap<string>> = this.context.threadId
? room.getThread(this.context.threadId)
: room;
const receiptDestination = this.context.threadId ? room.getThread(this.context.threadId) : room;
const receipts: IReadReceiptProps[] = [];
@ -1215,7 +1211,7 @@ class CreationGrouper extends BaseGrouper {
let summaryText: string;
const roomId = ev.getRoomId();
const creator = ev.sender ? ev.sender.name : ev.getSender();
const creator = ev.sender?.name ?? ev.getSender();
if (DMRoomMap.shared().getUserIdForRoomId(roomId)) {
summaryText = _t("%(creator)s created this DM.", { creator });
} else {

View file

@ -377,7 +377,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
private roomView = createRef<HTMLElement>();
private searchResultsPanel = createRef<ScrollPanel>();
private messagePanel?: TimelinePanel;
private messagePanel: TimelinePanel | null = null;
private roomViewBody = createRef<HTMLDivElement>();
public static contextType = SDKContext;
@ -611,11 +611,11 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
const newState: Partial<IRoomState> = {
roomId: roomId ?? undefined,
roomAlias: this.context.roomViewStore.getRoomAlias(),
roomAlias: this.context.roomViewStore.getRoomAlias() ?? undefined,
roomLoading: this.context.roomViewStore.isRoomLoading(),
roomLoadError: this.context.roomViewStore.getRoomLoadError(),
roomLoadError: this.context.roomViewStore.getRoomLoadError() ?? undefined,
joining: this.context.roomViewStore.isJoining(),
replyToEvent: this.context.roomViewStore.getQuotingEvent(),
replyToEvent: this.context.roomViewStore.getQuotingEvent() ?? undefined,
// we should only peek once we have a ready client
shouldPeek: this.state.matrixClientIsReady && this.context.roomViewStore.shouldPeek(),
showReadReceipts: SettingsStore.getValue("showReadReceipts", roomId),
@ -654,7 +654,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
// and the root event.
// The rest will be lost for now, until the aggregation API on the server
// becomes available to fetch a whole thread
if (!initialEvent) {
if (!initialEvent && this.context.client) {
initialEvent = (await fetchInitialEvent(this.context.client, roomId, initialEventId)) ?? undefined;
}
@ -848,7 +848,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
isPeeking: true, // this will change to false if peeking fails
});
this.context.client
.peekInRoom(roomId)
?.peekInRoom(roomId)
.then((room) => {
if (this.unmounted) {
return;
@ -883,7 +883,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
});
} else if (room) {
// Stop peeking because we have joined this room previously
this.context.client.stopPeeking();
this.context.client?.stopPeeking();
this.setState({ isPeeking: false });
}
}
@ -909,9 +909,9 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
this.onRoomViewStoreUpdate(true);
const call = this.getCallForRoom();
const callState = call ? call.state : null;
const callState = call?.state;
this.setState({
callState: callState,
callState,
});
this.context.legacyCallHandler.on(LegacyCallHandlerEvent.CallState, this.onCallState);
@ -959,7 +959,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
}
if (this.state.shouldPeek) {
this.context.client.stopPeeking();
this.context.client?.stopPeeking();
}
// stop tracking room changes to format permalinks
@ -1010,7 +1010,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
if (this.viewsLocalRoom) {
// clean up if this was a local room
this.context.client.store.removeRoom(this.state.room.roomId);
this.context.client?.store.removeRoom(this.state.room.roomId);
}
}
@ -1469,7 +1469,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
private updatePermissions(room: Room): void {
if (room) {
const me = this.context.client.getUserId();
const me = this.context.client.getSafeUserId();
const canReact =
room.getMyMembership() === "join" && room.currentState.maySendEvent(EventType.Reaction, me);
const canSendMessages = room.maySendMessage();
@ -1866,7 +1866,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
// this has to be a proper method rather than an unnamed function,
// otherwise react calls it with null on each update.
private gatherTimelinePanelRef = (r?: TimelinePanel): void => {
private gatherTimelinePanelRef = (r: TimelinePanel | null): void => {
this.messagePanel = r;
};

View file

@ -171,7 +171,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
if (payload.event && !payload.event.getThread()) return;
this.setState(
{
editState: payload.event ? new EditorStateTransfer(payload.event) : null,
editState: payload.event ? new EditorStateTransfer(payload.event) : undefined,
},
() => {
if (payload.event) {
@ -213,9 +213,11 @@ export default class ThreadView extends React.Component<IProps, IState> {
};
private get threadLastReply(): MatrixEvent | undefined {
return this.state.thread?.lastReply((ev: MatrixEvent) => {
return ev.isRelation(THREAD_RELATION_TYPE.name) && !ev.status;
});
return (
this.state.thread?.lastReply((ev: MatrixEvent) => {
return ev.isRelation(THREAD_RELATION_TYPE.name) && !ev.status;
}) ?? undefined
);
}
private updateThread = (thread?: Thread): void => {
@ -245,7 +247,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
private setupThreadListeners(thread?: Thread | undefined, oldThread?: Thread | undefined): void {
if (oldThread) {
this.state.thread.off(ThreadEvent.NewReply, this.updateThreadRelation);
this.state.thread?.off(ThreadEvent.NewReply, this.updateThreadRelation);
this.props.room.off(RoomEvent.LocalEchoUpdated, this.updateThreadRelation);
}
if (thread) {
@ -344,7 +346,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
};
public render(): React.ReactNode {
const highlightedEventId = this.props.isInitialEventHighlighted ? this.props.initialEvent?.getId() : null;
const highlightedEventId = this.props.isInitialEventHighlighted ? this.props.initialEvent?.getId() : undefined;
const threadRelation = this.threadRelation;

View file

@ -79,7 +79,7 @@ interface IProps {
// representing. This may or may not have a room, depending on what it's
// a timeline representing. If it has a room, we maintain RRs etc for
// that room.
timelineSet?: EventTimelineSet;
timelineSet: EventTimelineSet;
// overlay events from a second timelineset on the main timeline
// added to support virtual rooms
// events from the overlay timeline set will be added by localTimestamp