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

Commit

Permalink
Conform more of the codebase to strictNullChecks (#10666)
Browse files Browse the repository at this point in the history
* Conform more of the codebase to `strictNullChecks`

* Iterate

* Iterate

* Iterate

* Iterate
  • Loading branch information
t3chguy authored Apr 20, 2023
1 parent 8867f18 commit 93b4ee6
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 114 deletions.
4 changes: 2 additions & 2 deletions src/AddThreepid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export default class AddThreepid {
const authClient = new IdentityAuthClient();
const supportsSeparateAddAndBind = await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind();

let result;
let result: { success: boolean } | MatrixError;
if (this.submitUrl) {
result = await MatrixClientPeg.get().submitMsisdnTokenOtherUrl(
this.submitUrl,
Expand All @@ -311,7 +311,7 @@ export default class AddThreepid {
} else {
throw new UserFriendlyError("The add / bind with MSISDN flow is misconfigured");
}
if (result.errcode) {
if (result instanceof Error) {
throw result;
}

Expand Down
7 changes: 3 additions & 4 deletions src/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { Optional } from "matrix-events-sdk";

import { _t } from "./languageHandler";

function getDaysArray(): string[] {
Expand Down Expand Up @@ -194,10 +196,7 @@ function withinCurrentYear(prevDate: Date, nextDate: Date): boolean {
return prevDate.getFullYear() === nextDate.getFullYear();
}

export function wantsDateSeparator(
prevEventDate: Date | null | undefined,
nextEventDate: Date | null | undefined,
): boolean {
export function wantsDateSeparator(prevEventDate: Optional<Date>, nextEventDate: Optional<Date>): boolean {
if (!nextEventDate || !prevEventDate) {
return false;
}
Expand Down
20 changes: 11 additions & 9 deletions src/LegacyCallHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ export default class LegacyCallHandler extends EventEmitter {
this.play(AudioID.Ring);
}

public isCallSilenced(callId: string): boolean {
return this.isForcedSilent() || this.silencedCalls.has(callId);
public isCallSilenced(callId?: string): boolean {
return this.isForcedSilent() || (!!callId && this.silencedCalls.has(callId));
}

/**
Expand Down Expand Up @@ -395,6 +395,7 @@ export default class LegacyCallHandler extends EventEmitter {
}

const mappedRoomId = LegacyCallHandler.instance.roomIdForCall(call);
if (!mappedRoomId) return;
if (this.getCallForRoom(mappedRoomId)) {
logger.log(
"Got incoming call for room " + mappedRoomId + " but there's already a call for this room: ignoring",
Expand All @@ -411,7 +412,8 @@ export default class LegacyCallHandler extends EventEmitter {
// the call, we'll be ready to send. NB. This is the protocol-level room ID not
// the mapped one: that's where we'll send the events.
const cli = MatrixClientPeg.get();
cli.prepareToEncrypt(cli.getRoom(call.roomId));
const room = cli.getRoom(call.roomId);
if (room) cli.prepareToEncrypt(room);
};

public getCallById(callId: string): MatrixCall | null {
Expand Down Expand Up @@ -505,7 +507,7 @@ export default class LegacyCallHandler extends EventEmitter {
if (this.audioPromises.has(audioId)) {
this.audioPromises.set(
audioId,
this.audioPromises.get(audioId).then(() => {
this.audioPromises.get(audioId)!.then(() => {
audio.load();
return playAudio();
}),
Expand All @@ -531,7 +533,7 @@ export default class LegacyCallHandler extends EventEmitter {
};
if (audio) {
if (this.audioPromises.has(audioId)) {
this.audioPromises.set(audioId, this.audioPromises.get(audioId).then(pauseAudio));
this.audioPromises.set(audioId, this.audioPromises.get(audioId)!.then(pauseAudio));
} else {
pauseAudio();
}
Expand All @@ -546,7 +548,7 @@ export default class LegacyCallHandler extends EventEmitter {
// is the call we consider 'the' call for its room.
const mappedRoomId = this.roomIdForCall(call);

const callForThisRoom = this.getCallForRoom(mappedRoomId);
const callForThisRoom = mappedRoomId ? this.getCallForRoom(mappedRoomId) : null;
return !!callForThisRoom && call.callId === callForThisRoom.callId;
}

Expand Down Expand Up @@ -840,7 +842,7 @@ export default class LegacyCallHandler extends EventEmitter {
cancelButton: _t("OK"),
onFinished: (allow) => {
SettingsStore.setValue("fallbackICEServerAllowed", null, SettingLevel.DEVICE, allow);
cli.setFallbackICEServerAllowed(allow);
cli.setFallbackICEServerAllowed(!!allow);
},
},
undefined,
Expand Down Expand Up @@ -898,7 +900,7 @@ export default class LegacyCallHandler extends EventEmitter {
// previous calls that are probably stale by now, so just cancel them.
if (mappedRoomId !== roomId) {
const mappedRoom = MatrixClientPeg.get().getRoom(mappedRoomId);
if (mappedRoom.getPendingEvents().length > 0) {
if (mappedRoom?.getPendingEvents().length) {
Resend.cancelUnsentEvents(mappedRoom);
}
}
Expand Down Expand Up @@ -933,7 +935,7 @@ export default class LegacyCallHandler extends EventEmitter {
}
}

public async placeCall(roomId: string, type?: CallType, transferee?: MatrixCall): Promise<void> {
public async placeCall(roomId: string, type: CallType, transferee?: MatrixCall): Promise<void> {
// Pause current broadcast, if any
SdkContextClass.instance.voiceBroadcastPlaybacksStore.getCurrent()?.pause();

Expand Down
4 changes: 3 additions & 1 deletion src/components/structures/FilePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ class FilePanel extends React.Component<IProps, IState> {
withoutScrollContainer
ref={this.card}
>
<Measured sensor={this.card.current} onMeasurement={this.onMeasurement} />
{this.card.current && (
<Measured sensor={this.card.current} onMeasurement={this.onMeasurement} />
)}
<SearchWarning isRoomEncrypted={isRoomEncrypted} kind={WarningKind.Files} />
<TimelinePanel
manageReadReceipts={false}
Expand Down
1 change: 1 addition & 0 deletions src/components/structures/LeftPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export default class LeftPanel extends React.Component<IProps, IState> {
}

private doStickyHeaders(list: HTMLDivElement): void {
if (!list.parentElement) return;
const topEdge = list.scrollTop;
const bottomEdge = list.offsetHeight + list.scrollTop;
const sublists = list.querySelectorAll<HTMLDivElement>(".mx_RoomSublist:not(.mx_RoomSublist_hidden)");
Expand Down
4 changes: 2 additions & 2 deletions src/components/structures/LegacyCallEventGrouper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export default class LegacyCallEventGrouper extends EventEmitter {
}

public get duration(): number | null {
if (!this.hangup || !this.selectAnswer) return null;
return this.hangup.getDate().getTime() - this.selectAnswer.getDate().getTime();
if (!this.hangup?.getDate() || !this.selectAnswer?.getDate()) return null;
return this.hangup.getDate()!.getTime() - this.selectAnswer.getDate()!.getTime();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ interface IProps {
// the initial queryParams extracted from the hash-fragment of the URI
startingFragmentQueryParams?: QueryDict;
// called when we have completed a token login
onTokenLoginCompleted?: () => void;
onTokenLoginCompleted: () => void;
// Represents the screen to display as a result of parsing the initial window.location
initialScreenAfterLogin?: IScreen;
// displayname, if any, to set on the device when logging in/registering.
Expand Down Expand Up @@ -1200,7 +1200,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
// We have to manually update the room list because the forgotten room will not
// be notified to us, therefore the room list will have no other way of knowing
// the room is forgotten.
RoomListStore.instance.manualRoomUpdate(room, RoomUpdateCause.RoomRemoved);
if (room) RoomListStore.instance.manualRoomUpdate(room, RoomUpdateCause.RoomRemoved);
})
.catch((err) => {
const errCode = err.errcode || _td("unknown error code");
Expand Down Expand Up @@ -2124,7 +2124,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
onForgotPasswordClick={showPasswordReset ? this.onForgotPasswordClick : undefined}
onServerConfigChange={this.onServerConfigChange}
fragmentAfterLogin={fragmentAfterLogin}
defaultUsername={this.props.startingFragmentQueryParams.defaultUsername as string}
defaultUsername={this.props.startingFragmentQueryParams?.defaultUsername as string | undefined}
{...this.getServerProperties()}
/>
);
Expand Down
19 changes: 9 additions & 10 deletions src/components/structures/MessagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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 { Optional } from "matrix-events-sdk";

import shouldHideEvent from "../../shouldHideEvent";
import { wantsDateSeparator } from "../../DateUtils";
Expand Down Expand Up @@ -436,10 +437,8 @@ export default class MessagePanel extends React.Component<IProps, IState> {
* node (specifically, the bottom of it) will be positioned. If omitted, it
* defaults to 0.
*/
public scrollToEvent(eventId: string, pixelOffset: number, offsetBase: number): void {
if (this.scrollPanel.current) {
this.scrollPanel.current.scrollToToken(eventId, pixelOffset, offsetBase);
}
public scrollToEvent(eventId: string, pixelOffset?: number, offsetBase?: number): void {
this.scrollPanel.current?.scrollToToken(eventId, pixelOffset, offsetBase);
}

public scrollToEventIfNeeded(eventId: string): void {
Expand Down Expand Up @@ -590,16 +589,16 @@ export default class MessagePanel extends React.Component<IProps, IState> {
return { nextEventAndShouldShow, nextTile };
}

private get pendingEditItem(): string | undefined {
private get pendingEditItem(): string | null {
if (!this.props.room) {
return undefined;
return null;
}

try {
return localStorage.getItem(editorRoomKey(this.props.room.roomId, this.context.timelineRenderingType));
} catch (err) {
logger.error(err);
return undefined;
return null;
}
}

Expand Down Expand Up @@ -815,7 +814,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
return ret;
}

public wantsDateSeparator(prevEvent: MatrixEvent | null, nextEventDate: Date): boolean {
public wantsDateSeparator(prevEvent: MatrixEvent | null, nextEventDate: Optional<Date>): boolean {
if (this.context.timelineRenderingType === TimelineRenderingType.ThreadsList) {
return false;
}
Expand Down Expand Up @@ -1174,7 +1173,7 @@ class CreationGrouper extends BaseGrouper {
const ts = createEvent.event.getTs();
ret.push(
<li key={ts + "~"}>
<DateSeparator roomId={createEvent.event.getRoomId()} ts={ts} />
<DateSeparator roomId={createEvent.event.getRoomId()!} ts={ts} />
</li>,
);
}
Expand Down Expand Up @@ -1326,7 +1325,7 @@ class MainGrouper extends BaseGrouper {
const ts = this.events[0].getTs();
ret.push(
<li key={ts + "~"}>
<DateSeparator roomId={this.events[0].getRoomId()} ts={ts} />
<DateSeparator roomId={this.events[0].getRoomId()!} ts={ts} />
</li>,
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/structures/PipContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ interface IState {
secondaryCall: MatrixCall;

// widget candidate to be displayed in the pip view.
persistentWidgetId: string;
persistentRoomId: string;
persistentWidgetId: string | null;
persistentRoomId: string | null;
showWidgetInPip: boolean;
}

Expand Down Expand Up @@ -225,7 +225,7 @@ class PipContainerInner extends React.Component<IProps, IState> {
if (callRoomId ?? this.state.persistentRoomId) {
dis.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: callRoomId ?? this.state.persistentRoomId,
room_id: callRoomId ?? this.state.persistentRoomId ?? undefined,
metricsTrigger: "WebFloatingCallWindow",
});
}
Expand Down Expand Up @@ -318,7 +318,7 @@ class PipContainerInner extends React.Component<IProps, IState> {
pipContent.push(({ onStartMoving }) => (
<WidgetPip
widgetId={this.state.persistentWidgetId}
room={MatrixClientPeg.get().getRoom(this.state.persistentRoomId)!}
room={MatrixClientPeg.get().getRoom(this.state.persistentRoomId ?? undefined)!}
viewingRoom={this.state.viewedRoomId === this.state.persistentRoomId}
onStartMoving={onStartMoving}
movePersistedElement={this.props.movePersistedElement}
Expand Down
74 changes: 41 additions & 33 deletions src/components/structures/RightPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,36 +218,40 @@ export default class RightPanel extends React.Component<IProps, IState> {
}
break;
case RightPanelPhases.Timeline:
card = (
<TimelineCard
classNames="mx_ThreadPanel mx_TimelineCard"
room={this.props.room}
timelineSet={this.props.room.getUnfilteredTimelineSet()}
resizeNotifier={this.props.resizeNotifier}
onClose={this.onClose}
permalinkCreator={this.props.permalinkCreator}
e2eStatus={this.props.e2eStatus}
/>
);
if (this.props.room) {
card = (
<TimelineCard
classNames="mx_ThreadPanel mx_TimelineCard"
room={this.props.room}
timelineSet={this.props.room.getUnfilteredTimelineSet()}
resizeNotifier={this.props.resizeNotifier}
onClose={this.onClose}
permalinkCreator={this.props.permalinkCreator}
e2eStatus={this.props.e2eStatus}
/>
);
}
break;
case RightPanelPhases.FilePanel:
card = <FilePanel roomId={roomId} resizeNotifier={this.props.resizeNotifier} onClose={this.onClose} />;
break;

case RightPanelPhases.ThreadView:
card = (
<ThreadView
room={this.props.room}
resizeNotifier={this.props.resizeNotifier}
onClose={this.onClose}
mxEvent={cardState?.threadHeadEvent}
initialEvent={cardState?.initialEvent}
isInitialEventHighlighted={cardState?.isInitialEventHighlighted}
initialEventScrollIntoView={cardState?.initialEventScrollIntoView}
permalinkCreator={this.props.permalinkCreator}
e2eStatus={this.props.e2eStatus}
/>
);
if (this.props.room) {
card = (
<ThreadView
room={this.props.room}
resizeNotifier={this.props.resizeNotifier}
onClose={this.onClose}
mxEvent={cardState?.threadHeadEvent}
initialEvent={cardState?.initialEvent}
isInitialEventHighlighted={cardState?.isInitialEventHighlighted}
initialEventScrollIntoView={cardState?.initialEventScrollIntoView}
permalinkCreator={this.props.permalinkCreator}
e2eStatus={this.props.e2eStatus}
/>
);
}
break;

case RightPanelPhases.ThreadPanel:
Expand All @@ -262,18 +266,22 @@ export default class RightPanel extends React.Component<IProps, IState> {
break;

case RightPanelPhases.RoomSummary:
card = (
<RoomSummaryCard
room={this.props.room}
onClose={this.onClose}
// whenever RightPanel is passed a room it is passed a permalinkcreator
permalinkCreator={this.props.permalinkCreator!}
/>
);
if (this.props.room) {
card = (
<RoomSummaryCard
room={this.props.room}
onClose={this.onClose}
// whenever RightPanel is passed a room it is passed a permalinkcreator
permalinkCreator={this.props.permalinkCreator!}
/>
);
}
break;

case RightPanelPhases.Widget:
card = <WidgetCard room={this.props.room} widgetId={cardState?.widgetId} onClose={this.onClose} />;
if (this.props.room) {
card = <WidgetCard room={this.props.room} widgetId={cardState?.widgetId} onClose={this.onClose} />;
}
break;
}

Expand Down
Loading

0 comments on commit 93b4ee6

Please sign in to comment.