Apply strictNullChecks to src/components/views/room_settings/* (#10366

* Apply `strictNullChecks` to `src/components/views/room_settings/*`

* Restore tsconfig.json
This commit is contained in:
Michael Telatynski 2023-03-15 08:39:02 +00:00 committed by GitHub
parent e3930fb8b0
commit ad65b4e444
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 30 deletions

View file

@ -16,6 +16,7 @@ limitations under the License.
import React, { createRef } from "react";
import classNames from "classnames";
import { EventType } from "matrix-js-sdk/src/matrix";
import { _t } from "../../../languageHandler";
import { MatrixClientPeg } from "../../../MatrixClientPeg";
@ -33,9 +34,9 @@ interface IProps {
interface IState {
originalDisplayName: string;
displayName: string;
originalAvatarUrl: string;
avatarUrl: string;
avatarFile: File;
originalAvatarUrl: string | null;
avatarUrl: string | null;
avatarFile: File | null;
originalTopic: string;
topic: string;
profileFieldsTouched: Record<string, boolean>;
@ -55,16 +56,17 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
const room = client.getRoom(props.roomId);
if (!room) throw new Error(`Expected a room for ID: ${props.roomId}`);
const avatarEvent = room.currentState.getStateEvents("m.room.avatar", "");
let avatarUrl = avatarEvent && avatarEvent.getContent() ? avatarEvent.getContent()["url"] : null;
const avatarEvent = room.currentState.getStateEvents(EventType.RoomAvatar, "");
let avatarUrl = avatarEvent?.getContent()["url"] ?? null;
if (avatarUrl) avatarUrl = mediaFromMxc(avatarUrl).getSquareThumbnailHttp(96);
const topicEvent = room.currentState.getStateEvents("m.room.topic", "");
const topicEvent = room.currentState.getStateEvents(EventType.RoomTopic, "");
const topic = topicEvent && topicEvent.getContent() ? topicEvent.getContent()["topic"] : "";
const nameEvent = room.currentState.getStateEvents("m.room.name", "");
const nameEvent = room.currentState.getStateEvents(EventType.RoomName, "");
const name = nameEvent && nameEvent.getContent() ? nameEvent.getContent()["name"] : "";
const userId = client.getSafeUserId();
this.state = {
originalDisplayName: name,
displayName: name,
@ -74,19 +76,19 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
originalTopic: topic,
topic: topic,
profileFieldsTouched: {},
canSetName: room.currentState.maySendStateEvent("m.room.name", client.getUserId()),
canSetTopic: room.currentState.maySendStateEvent("m.room.topic", client.getUserId()),
canSetAvatar: room.currentState.maySendStateEvent("m.room.avatar", client.getUserId()),
canSetName: room.currentState.maySendStateEvent(EventType.RoomName, userId),
canSetTopic: room.currentState.maySendStateEvent(EventType.RoomTopic, userId),
canSetAvatar: room.currentState.maySendStateEvent(EventType.RoomAvatar, userId),
};
}
private uploadAvatar = (): void => {
this.avatarUpload.current.click();
this.avatarUpload.current?.click();
};
private removeAvatar = (): void => {
// clear file upload field so same file can be selected
this.avatarUpload.current.value = "";
if (this.avatarUpload.current) this.avatarUpload.current.value = "";
this.setState({
avatarUrl: null,
avatarFile: null,
@ -135,12 +137,12 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
if (this.state.avatarFile) {
const { content_uri: uri } = await client.uploadContent(this.state.avatarFile);
await client.sendStateEvent(this.props.roomId, "m.room.avatar", { url: uri }, "");
await client.sendStateEvent(this.props.roomId, EventType.RoomAvatar, { url: uri }, "");
newState.avatarUrl = mediaFromMxc(uri).getSquareThumbnailHttp(96);
newState.originalAvatarUrl = newState.avatarUrl;
newState.avatarFile = null;
} else if (this.state.originalAvatarUrl !== this.state.avatarUrl) {
await client.sendStateEvent(this.props.roomId, "m.room.avatar", {}, "");
await client.sendStateEvent(this.props.roomId, EventType.RoomAvatar, {}, "");
}
if (this.state.originalTopic !== this.state.topic) {
@ -207,7 +209,7 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
const reader = new FileReader();
reader.onload = (ev) => {
this.setState({
avatarUrl: String(ev.target.result),
avatarUrl: String(ev.target?.result),
avatarFile: file,
profileFieldsTouched: {
...this.state.profileFieldsTouched,
@ -269,7 +271,7 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
/>
</div>
<AvatarSetting
avatarUrl={this.state.avatarUrl}
avatarUrl={this.state.avatarUrl ?? undefined}
avatarName={this.state.displayName || this.props.roomId}
avatarAltText={_t("Room avatar")}
uploadAvatar={this.state.canSetAvatar ? this.uploadAvatar : undefined}