From 9848cdf4e7a362f4a9da7a7b517c91fc12ef5aff Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 30 Mar 2023 08:28:08 +0100 Subject: [PATCH] Apply `strictNullChecks` to `src/components/structures/Space*` (#10475) --- src/components/structures/SpaceHierarchy.tsx | 19 ++++++++++--------- src/components/structures/SpaceRoomView.tsx | 17 ++++++++--------- src/stores/spaces/SpaceStore.ts | 14 ++++++++++---- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/components/structures/SpaceHierarchy.tsx b/src/components/structures/SpaceHierarchy.tsx index 9bb14924f0..2baed15eb8 100644 --- a/src/components/structures/SpaceHierarchy.tsx +++ b/src/components/structures/SpaceHierarchy.tsx @@ -362,7 +362,7 @@ export const showRoom = (cli: MatrixClient, hierarchy: RoomHierarchy, roomId: st // Don't let the user view a room they won't be able to either peek or join: // fail earlier so they don't have to click back to the directory. if (cli.isGuest()) { - if (!room.world_readable && !room.guest_can_join) { + if (!room?.world_readable && !room?.guest_can_join) { defaultDispatcher.dispatch({ action: "require_registration" }); return; } @@ -374,12 +374,12 @@ export const showRoom = (cli: MatrixClient, hierarchy: RoomHierarchy, roomId: st action: Action.ViewRoom, should_peek: true, room_alias: roomAlias, - room_id: room.room_id, + room_id: roomId, via_servers: Array.from(hierarchy.viaMap.get(roomId) || []), oob_data: { - avatarUrl: room.avatar_url, + avatarUrl: room?.avatar_url, // XXX: This logic is duplicated from the JS SDK which would normally decide what the name is. - name: room.name || roomAlias || _t("Unnamed room"), + name: room?.name || roomAlias || _t("Unnamed room"), roomType, } as IOOBData, metricsTrigger: "RoomDirectory", @@ -560,7 +560,7 @@ export const useRoomHierarchy = ( const hierarchy = new RoomHierarchy(space, INITIAL_PAGE_SIZE); hierarchy.load().then(() => { if (space !== hierarchy.root) return; // discard stale results - setRooms(hierarchy.rooms); + setRooms(hierarchy.rooms ?? []); }, setError); setHierarchy(hierarchy); }, [space]); @@ -577,7 +577,7 @@ export const useRoomHierarchy = ( async (pageSize?: number): Promise => { if (!hierarchy || hierarchy.loading || !hierarchy.canLoadMore || hierarchy.noSupport || error) return; await hierarchy.load(pageSize).catch(setError); - setRooms(hierarchy.rooms); + setRooms(hierarchy.rooms ?? []); }, [error, hierarchy], ); @@ -638,7 +638,7 @@ const ManageButtons: React.FC = ({ hierarchy, selected, set const [saving, setSaving] = useState(false); const selectedRelations = Array.from(selected.keys()).flatMap((parentId) => { - return [...selected.get(parentId).values()].map((childId) => [parentId, childId]); + return [...selected.get(parentId)!.values()].map((childId) => [parentId, childId]); }); const selectionAllSuggested = selectedRelations.every(([parentId, childId]) => { @@ -814,12 +814,13 @@ const SpaceHierarchy: React.FC = ({ space, initialText = "", showRoom, a space?.getMyMembership() === "join" && space.currentState.maySendStateEvent(EventType.SpaceChild, cli.getSafeUserId()); + const root = hierarchy.roomMap.get(space.roomId); let results: JSX.Element | undefined; - if (filteredRoomSet.size) { + if (filteredRoomSet.size && root) { results = ( <> [] = [useRef(), useRef(), useRef()]; + const fieldRefs = [useRef(), useRef(), useRef()] as RefObject[]; const [emailAddresses, setEmailAddress] = useStateArray(numFields, ""); const fields = new Array(numFields).fill(0).map((x, i) => { const name = "emailAddress" + i; @@ -537,12 +537,12 @@ const SpaceSetupPrivateInvite: React.FC<{ if (busy) return; setError(""); for (const fieldRef of fieldRefs) { - const valid = await fieldRef.current.validate({ allowEmpty: true }); + const valid = await fieldRef.current?.validate({ allowEmpty: true }); if (valid === false) { // true/null are allowed - fieldRef.current.focus(); - fieldRef.current.validate({ allowEmpty: true, focused: true }); + fieldRef.current!.focus(); + fieldRef.current!.validate({ allowEmpty: true, focused: true }); return; } } @@ -636,7 +636,6 @@ export default class SpaceRoomView extends React.PureComponent { public static contextType = MatrixClientContext; public context!: React.ContextType; - private readonly creator: string; private readonly dispatcherRef: string; public constructor(props: IProps, context: React.ContextType) { @@ -644,8 +643,8 @@ export default class SpaceRoomView extends React.PureComponent { let phase = Phase.Landing; - this.creator = this.props.space.currentState.getStateEvents(EventType.RoomCreate, "")?.getSender(); - const showSetup = this.props.justCreatedOpts && context.getUserId() === this.creator; + const creator = this.props.space.currentState.getStateEvents(EventType.RoomCreate, "")?.getSender(); + const showSetup = this.props.justCreatedOpts && context.getSafeUserId() === creator; if (showSetup) { phase = diff --git a/src/stores/spaces/SpaceStore.ts b/src/stores/spaces/SpaceStore.ts index e516110a9c..a068db483a 100644 --- a/src/stores/spaces/SpaceStore.ts +++ b/src/stores/spaces/SpaceStore.ts @@ -87,7 +87,7 @@ const partitionSpacesAndRooms = (arr: Room[]): [Room[], Room[]] => { ); }; -const validOrder = (order: string): string | undefined => { +const validOrder = (order?: string): string | undefined => { if ( typeof order === "string" && order.length <= 50 && @@ -101,7 +101,11 @@ const validOrder = (order: string): string | undefined => { }; // For sorting space children using a validated `order`, `origin_server_ts`, `room_id` -export const getChildOrder = (order: string, ts: number, roomId: string): Array>> => { +export const getChildOrder = ( + order: string | undefined, + ts: number, + roomId: string, +): Array>> => { return [validOrder(order) ?? NaN, ts, roomId]; // NaN has lodash sort it at the end in asc }; @@ -378,8 +382,9 @@ export class SpaceStoreClass extends AsyncStoreWithClient { } public getParents(roomId: string, canonicalOnly = false): Room[] { - const userId = this.matrixClient?.getUserId(); - const room = this.matrixClient?.getRoom(roomId); + if (!this.matrixClient) return []; + const userId = this.matrixClient.getSafeUserId(); + const room = this.matrixClient.getRoom(roomId); const events = room?.currentState.getStateEvents(EventType.SpaceParent) ?? []; return filterBoolean( events.map((ev) => { @@ -871,6 +876,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient { }; private switchSpaceIfNeeded = (roomId = SdkContextClass.instance.roomViewStore.getRoomId()): void => { + if (!roomId) return; if (!this.isRoomInSpace(this.activeSpace, roomId) && !this.matrixClient.getRoom(roomId)?.isSpaceRoom()) { this.switchToRelatedSpace(roomId); }