Add safety around missing topics and space settings (#8957)

This commit is contained in:
Travis Ralston 2022-06-30 21:34:24 +02:00 committed by GitHub
parent 3329b35bd0
commit 924865b315
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 2 deletions

View file

@ -48,7 +48,7 @@ const SpaceSettingsGeneralTab = ({ matrixClient: cli, space, onFinished }: IProp
const canSetName = space.currentState.maySendStateEvent(EventType.RoomName, userId); const canSetName = space.currentState.maySendStateEvent(EventType.RoomName, userId);
const nameChanged = name !== space.name; const nameChanged = name !== space.name;
const currentTopic = getTopic(space).text; const currentTopic = getTopic(space)?.text;
const [topic, setTopic] = useState<string>(currentTopic); const [topic, setTopic] = useState<string>(currentTopic);
const canSetTopic = space.currentState.maySendStateEvent(EventType.RoomTopic, userId); const canSetTopic = space.currentState.maySendStateEvent(EventType.RoomTopic, userId);
const topicChanged = topic !== currentTopic; const topicChanged = topic !== currentTopic;

View file

@ -21,10 +21,11 @@ import { Room } from "matrix-js-sdk/src/models/room";
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state"; import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
import { parseTopicContent, TopicState } from "matrix-js-sdk/src/content-helpers"; import { parseTopicContent, TopicState } from "matrix-js-sdk/src/content-helpers";
import { MRoomTopicEventContent } from "matrix-js-sdk/src/@types/topic"; import { MRoomTopicEventContent } from "matrix-js-sdk/src/@types/topic";
import { Optional } from "matrix-events-sdk";
import { useTypedEventEmitter } from "../useEventEmitter"; import { useTypedEventEmitter } from "../useEventEmitter";
export const getTopic = (room: Room) => { export const getTopic = (room: Room): Optional<TopicState> => {
const content: MRoomTopicEventContent = room?.currentState?.getStateEvents(EventType.RoomTopic, "")?.getContent(); const content: MRoomTopicEventContent = room?.currentState?.getStateEvents(EventType.RoomTopic, "")?.getContent();
return !!content ? parseTopicContent(content) : null; return !!content ? parseTopicContent(content) : null;
}; };