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

Conform more of the codebase to strictNullChecks #10607

Merged
merged 5 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/autocomplete/Autocompleter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface ICompletion {
type?: "at-room" | "command" | "community" | "room" | "user";
completion: string;
completionId?: string;
component?: ReactElement;
component: ReactElement;
range: ISelectionRange;
command?: string;
suffix?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/components/structures/LoggedInView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class LoggedInView extends React.Component<IProps, IState> {
};

private createResizer(): Resizer {
let panelSize: number;
let panelSize: number | null;
let panelCollapsed: boolean;
const collapseConfig: ICollapseConfig = {
// TODO decrease this once Spaces launches as it'll no longer need to include the 56px Community Panel
Expand Down
23 changes: 15 additions & 8 deletions src/components/structures/MessagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ export default class MessagePanel extends React.Component<IProps, IState> {
}

private shouldHideSender(): boolean {
return this.props.room?.getInvitedAndJoinedMemberCount() <= 2 && this.props.layout === Layout.Bubble;
return (
!!this.props.room &&
this.props.room.getInvitedAndJoinedMemberCount() <= 2 &&
this.props.layout === Layout.Bubble
);
}

private calculateRoomMembersCount = (): void => {
Expand Down Expand Up @@ -465,7 +469,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
}
}

if (MatrixClientPeg.get().isUserIgnored(mxEv.getSender())) {
if (MatrixClientPeg.get().isUserIgnored(mxEv.getSender()!)) {
return false; // ignored = no show (only happens if the ignore happens after an event was received)
}

Expand Down Expand Up @@ -647,7 +651,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
for (let i = 0; i < events.length; i++) {
const eventAndShouldShow = events[i];
const { event, shouldShow } = eventAndShouldShow;
const eventId = event.getId();
const eventId = event.getId()!;
const last = event === lastShownEvent;
const { nextEventAndShouldShow, nextTile } = this.getNextEventInfo(events, i);

Expand Down Expand Up @@ -745,7 +749,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
!wantsDateSeparator &&
shouldFormContinuation(prevEvent, mxEv, this.showHiddenEvents, this.context.timelineRenderingType);

const eventId = mxEv.getId();
const eventId = mxEv.getId()!;
const highlight = eventId === this.props.highlightedEventId;

const readReceipts = this.readReceiptsByEvent.get(eventId);
Expand Down Expand Up @@ -1075,7 +1079,7 @@ abstract class BaseGrouper {
public readonly nextEventTile?: MatrixEvent | null,
) {
this.readMarker = panel.readMarkerForEvent(
firstEventAndShouldShow.event.getId(),
firstEventAndShouldShow.event.getId()!,
firstEventAndShouldShow.event === lastShownEvent,
);
}
Expand Down Expand Up @@ -1143,7 +1147,7 @@ class CreationGrouper extends BaseGrouper {

public add({ event: ev, shouldShow }: EventAndShouldShow): void {
const panel = this.panel;
this.readMarker = this.readMarker || panel.readMarkerForEvent(ev.getId(), ev === this.lastShownEvent);
this.readMarker = this.readMarker || panel.readMarkerForEvent(ev.getId()!, ev === this.lastShownEvent);
if (!shouldShow) {
return;
}
Expand Down Expand Up @@ -1295,7 +1299,7 @@ class MainGrouper extends BaseGrouper {
// We can ignore any events that don't actually have a message to display
if (!hasText(ev, this.panel.showHiddenEvents)) return;
}
this.readMarker = this.readMarker || this.panel.readMarkerForEvent(ev.getId(), ev === this.lastShownEvent);
this.readMarker = this.readMarker || this.panel.readMarkerForEvent(ev.getId()!, ev === this.lastShownEvent);
if (!this.panel.showHiddenEvents && !shouldShow) {
// absorb hidden events to not split the summary
return;
Expand Down Expand Up @@ -1331,7 +1335,10 @@ class MainGrouper extends BaseGrouper {
// This will prevent it from being re-created unnecessarily, and instead will allow new props to be provided.
// In turn, the shouldComponentUpdate method on ELS can be used to prevent unnecessary renderings.
const keyEvent = this.events.find((e) => this.panel.grouperKeyMap.get(e));
const key = keyEvent ? this.panel.grouperKeyMap.get(keyEvent) : this.generateKey();
const key =
keyEvent && this.panel.grouperKeyMap.has(keyEvent)
? this.panel.grouperKeyMap.get(keyEvent)!
: this.generateKey();
if (!keyEvent) {
// Populate the weak map with the key.
// Note that we only set the key on the specific event it refers to, since this group might get
Expand Down
1 change: 1 addition & 0 deletions src/components/views/avatars/RoomAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default class RoomAvatar extends React.Component<IProps, IState> {

private onRoomAvatarClick = (): void => {
const avatarUrl = Avatar.avatarUrlForRoom(this.props.room ?? null, undefined, undefined, undefined);
if (!avatarUrl) return;
const params = {
src: avatarUrl,
name: this.props.room?.name,
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/dialogs/CreateSubspaceDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { RefObject, useRef, useState } from "react";
import React, { useRef, useState } from "react";
import { Room } from "matrix-js-sdk/src/models/room";
import { JoinRule } from "matrix-js-sdk/src/@types/partials";
import { logger } from "matrix-js-sdk/src/logger";
Expand All @@ -41,9 +41,9 @@ const CreateSubspaceDialog: React.FC<IProps> = ({ space, onAddExistingSpaceClick

const [busy, setBusy] = useState<boolean>(false);
const [name, setName] = useState("");
const spaceNameField = useRef() as RefObject<Field>;
const spaceNameField = useRef<Field>(null);
const [alias, setAlias] = useState("");
const spaceAliasField = useRef() as RefObject<RoomAliasField>;
const spaceAliasField = useRef<RoomAliasField>(null);
const [avatar, setAvatar] = useState<File | undefined>();
const [topic, setTopic] = useState<string>("");

Expand Down
10 changes: 5 additions & 5 deletions src/components/views/dialogs/ExportDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

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

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

const [isExporting, setExporting] = useState(false);
const sizeLimitRef = useRef() as RefObject<Field>;
const messageCountRef = useRef() as RefObject<Field>;
const sizeLimitRef = useRef<Field>(null);
const messageCountRef = useRef<Field>(null);
const [exportProgressText, setExportProgressText] = useState(_t("Processing…"));
const [displayCancel, setCancelWarning] = useState(false);
const [exportCancelled, setExportCancelled] = useState(false);
Expand Down Expand Up @@ -182,7 +182,7 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
{
key: "number",
test: ({ value }) => {
const parsedSize = parseInt(value, 10);
const parsedSize = parseInt(value!, 10);
return validateNumberInRange(1, 2000)(parsedSize);
},
invalid: () => {
Expand Down Expand Up @@ -218,7 +218,7 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
{
key: "number",
test: ({ value }) => {
const parsedSize = parseInt(value, 10);
const parsedSize = parseInt(value!, 10);
return validateNumberInRange(1, 10 ** 8)(parsedSize);
},
invalid: () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/dialogs/FeedbackDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface IProps {
}

const FeedbackDialog: React.FC<IProps> = (props: IProps) => {
const feedbackRef = useRef<Field>();
const feedbackRef = useRef<Field>(null);
const [comment, setComment] = useState<string>("");
const [canContact, toggleCanContact] = useStateToggle(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import * as React from "react";
import { RefObject, SyntheticEvent, useRef, useState } from "react";
import { SyntheticEvent, useRef, useState } from "react";

import { _t, _td } from "../../../languageHandler";
import Field from "../elements/Field";
Expand All @@ -30,7 +30,7 @@ interface IProps {

const RegistrationEmailPromptDialog: React.FC<IProps> = ({ onFinished }) => {
const [email, setEmail] = useState("");
const fieldRef = useRef() as RefObject<Field>;
const fieldRef = useRef<Field>(null);

const onSubmit = async (e: SyntheticEvent): Promise<void> => {
e.preventDefault();
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/dialogs/ReportEventDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export default class ReportEventDialog extends React.Component<IProps, IState> {
});
} else {
// Report to homeserver admin through the dedicated Matrix API.
await client.reportEvent(ev.getRoomId(), ev.getId(), -100, this.state.reason.trim());
await client.reportEvent(ev.getRoomId()!, ev.getId()!, -100, this.state.reason.trim());
}

// if the user should also be ignored, do that
Expand Down Expand Up @@ -340,7 +340,7 @@ export default class ReportEventDialog extends React.Component<IProps, IState> {
);
break;
case NonStandardValue.Admin:
if (client.isRoomEncrypted(this.props.mxEvent.getRoomId())) {
if (client.isRoomEncrypted(this.props.mxEvent.getRoomId()!)) {
subtitle = _t(
"This room is dedicated to illegal or toxic content " +
"or the moderators fail to moderate illegal or toxic content.\n" +
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/dialogs/spotlight/SpotlightDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ interface IDirectoryOpts {
}

const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = null, onFinished }) => {
const inputRef = useRef() as RefObject<HTMLInputElement>;
const scrollContainerRef = useRef() as RefObject<HTMLDivElement>;
const inputRef = useRef<HTMLInputElement>(null);
const scrollContainerRef = useRef<HTMLDivElement>(null);
const cli = MatrixClientPeg.get();
const rovingContext = useContext(RovingTabIndexContext);
const [query, _setQuery] = useState(initialText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { SettingLevel } from "../../../settings/SettingLevel";

interface IProps {
// Current room
roomId: string;
roomId: string | null;
minWidth: number;
maxWidth: number;
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/elements/MiniAvatarUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import classNames from "classnames";
import { EventType } from "matrix-js-sdk/src/@types/event";
import React, { useContext, useRef, useState, MouseEvent, ReactNode, RefObject } from "react";
import React, { useContext, useRef, useState, MouseEvent, ReactNode } from "react";

import MatrixClientContext from "../../../contexts/MatrixClientContext";
import RoomContext from "../../../contexts/RoomContext";
Expand Down Expand Up @@ -59,7 +59,7 @@ const MiniAvatarUploader: React.FC<IProps> = ({
setShow(false);
}, 13000); // hide after being shown for 10 seconds

const uploadRef = useRef() as RefObject<HTMLInputElement>;
const uploadRef = useRef<HTMLInputElement>(null);

const label = hasAvatar || busy ? hasAvatarLabel : noAvatarLabel;

Expand Down
4 changes: 2 additions & 2 deletions src/components/views/elements/RoomTopic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { RefObject, useCallback, useContext, useRef } from "react";
import React, { useCallback, useContext, useRef } from "react";
import { Room } from "matrix-js-sdk/src/models/room";
import classNames from "classnames";
import { EventType } from "matrix-js-sdk/src/@types/event";
Expand All @@ -38,7 +38,7 @@ interface IProps extends React.HTMLProps<HTMLDivElement> {

export default function RoomTopic({ room, ...props }: IProps): JSX.Element {
const client = useContext(MatrixClientContext);
const ref = useRef() as RefObject<HTMLDivElement>;
const ref = useRef<HTMLDivElement>(null);

const topic = useTopic(room);
const body = topicToHtml(topic?.text, topic?.html, ref);
Expand Down
3 changes: 2 additions & 1 deletion src/components/views/right_panel/UserInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1504,9 +1504,10 @@ export const UserInfoHeader: React.FC<{
const avatarUrl = (member as RoomMember).getMxcAvatarUrl
? (member as RoomMember).getMxcAvatarUrl()
: (member as User).avatarUrl;
if (!avatarUrl) return;

const httpUrl = mediaFromMxc(avatarUrl).srcHttp;
if (!httpUrl) return;

const params = {
src: httpUrl,
name: (member as RoomMember).name || (member as User).displayName,
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/rooms/AuxPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ export default class AuxPanel extends React.Component<IProps, IState> {

if (this.props.room && SettingsStore.getValue("feature_state_counters")) {
const stateEvs = this.props.room.currentState.getStateEvents("re.jki.counter");
stateEvs.sort((a, b) => lexicographicCompare(a.getStateKey(), b.getStateKey()));
stateEvs.sort((a, b) => lexicographicCompare(a.getStateKey()!, b.getStateKey()!));

for (const ev of stateEvs) {
const title = ev.getContent().title;
const value = ev.getContent().value;
const link = ev.getContent().link;
const severity = ev.getContent().severity || "normal";
const stateKey = ev.getStateKey();
const stateKey = ev.getStateKey()!;

// We want a non-empty title but can accept falsy values (e.g.
// zero)
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/rooms/BasicMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>

public render(): React.ReactNode {
let autoComplete: JSX.Element | undefined;
if (this.state.autoComplete) {
if (this.state.autoComplete && this.state.query) {
const query = this.state.query;
const queryLen = query.length;
autoComplete = (
Expand Down Expand Up @@ -800,8 +800,8 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
const { completionIndex } = this.state;
const hasAutocomplete = Boolean(this.state.autoComplete);
let activeDescendant: string | undefined;
if (hasAutocomplete && completionIndex >= 0) {
activeDescendant = generateCompletionDomId(completionIndex);
if (hasAutocomplete && completionIndex! >= 0) {
activeDescendant = generateCompletionDomId(completionIndex!);
}

return (
Expand Down
8 changes: 4 additions & 4 deletions src/components/views/rooms/EditMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ class EditMessageComposer extends React.Component<IEditMessageComposerProps, ISt
}

private get editorRoomKey(): string {
return editorRoomKey(this.props.editState.getEvent().getRoomId(), this.context.timelineRenderingType);
return editorRoomKey(this.props.editState.getEvent().getRoomId()!, this.context.timelineRenderingType);
}

private get editorStateKey(): string {
return editorStateKey(this.props.editState.getEvent().getId());
return editorStateKey(this.props.editState.getEvent().getId()!);
}

private get events(): MatrixEvent[] {
Expand Down Expand Up @@ -275,7 +275,7 @@ class EditMessageComposer extends React.Component<IEditMessageComposerProps, ISt
private saveStoredEditorState = (): void => {
const item = SendHistoryManager.createItem(this.model);
this.clearPreviousEdit();
localStorage.setItem(this.editorRoomKey, this.props.editState.getEvent().getId());
localStorage.setItem(this.editorRoomKey, this.props.editState.getEvent().getId()!);
localStorage.setItem(this.editorStateKey, JSON.stringify(item));
};

Expand Down Expand Up @@ -329,7 +329,7 @@ class EditMessageComposer extends React.Component<IEditMessageComposerProps, ISt

// If content is modified then send an updated event into the room
if (this.isContentModified(newContent)) {
const roomId = editedEvent.getRoomId();
const roomId = editedEvent.getRoomId()!;
if (!containsEmote(this.model) && isSlashCommand(this.model)) {
const [cmd, args, commandText] = getSlashCommand(this.model);
if (cmd) {
Expand Down
4 changes: 3 additions & 1 deletion src/components/views/rooms/LinkPreviewWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ export default class LinkPreviewWidget extends React.Component<IProps> {
if (ev.button != 0 || ev.metaKey) return;
ev.preventDefault();

let src = p["og:image"];
let src: string | null | undefined = p["og:image"];
if (src?.startsWith("mxc://")) {
src = mediaFromMxc(src).srcHttp;
}

if (!src) return;

const params: Omit<ComponentProps<typeof ImageView>, "onFinished"> = {
src: src,
width: p["og:image:width"],
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/rooms/MessageComposerButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import classNames from "classnames";
import { IEventRelation } from "matrix-js-sdk/src/models/event";
import { M_POLL_START } from "matrix-js-sdk/src/@types/polls";
import React, { createContext, MouseEventHandler, ReactElement, ReactNode, RefObject, useContext, useRef } from "react";
import React, { createContext, MouseEventHandler, ReactElement, ReactNode, useContext, useRef } from "react";
import { Room } from "matrix-js-sdk/src/models/room";
import { MatrixClient } from "matrix-js-sdk/src/client";
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread";
Expand Down Expand Up @@ -180,7 +180,7 @@ interface IUploadButtonProps {
const UploadButtonContextProvider: React.FC<IUploadButtonProps> = ({ roomId, relation, children }) => {
const cli = useContext(MatrixClientContext);
const roomContext = useContext(RoomContext);
const uploadInput = useRef() as RefObject<HTMLInputElement>;
const uploadInput = useRef<HTMLInputElement>(null);

const onUploadClick = (): void => {
if (cli?.isGuest()) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/rooms/ReadReceiptGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ interface ISectionHeaderProps {
}

function SectionHeader({ className, children }: PropsWithChildren<ISectionHeaderProps>): JSX.Element {
const ref = useRef<HTMLHeadingElement>();
const ref = useRef<HTMLHeadingElement>(null);
const [onFocus] = useRovingTabIndex(ref);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/rooms/ReadReceiptMarker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class ReadReceiptMarker extends React.PureComponent<IProps, IStat
private buildReadReceiptInfo(target: IReadReceiptInfo = {}): IReadReceiptInfo {
const element = this.avatar.current;
// this is the mx_ReadReceiptsGroup_container
const horizontalContainer = element.offsetParent;
const horizontalContainer = element?.offsetParent;
if (!horizontalContainer || !(horizontalContainer instanceof HTMLElement)) {
// this seems to happen sometimes for reasons I don't understand
// the docs for `offsetParent` say it may be null if `display` is
Expand Down
Loading