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

Commit

Permalink
Merge pull request #6798 from matrix-org/t3chguy/fix/randoms
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Sep 14, 2021
2 parents 4a4c68b + f59baf1 commit 6900fb7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/components/structures/SpaceHierarchy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { Key } from "../../Keyboard";
import { IState, RovingTabIndexProvider, useRovingTabIndex } from "../../accessibility/RovingTabIndex";
import { getDisplayAliasForRoom } from "./RoomDirectory";
import MatrixClientContext from "../../contexts/MatrixClientContext";
import { useEventEmitterState } from "../../hooks/useEventEmitter";

interface IProps {
space: Room;
Expand Down Expand Up @@ -87,7 +88,8 @@ const Tile: React.FC<ITileProps> = ({
}) => {
const cli = useContext(MatrixClientContext);
const joinedRoom = cli.getRoom(room.room_id)?.getMyMembership() === "join" ? cli.getRoom(room.room_id) : null;
const name = joinedRoom?.name || room.name || room.canonical_alias || room.aliases?.[0]
const joinedRoomName = useEventEmitterState(joinedRoom, "Room.name", room => room?.name);
const name = joinedRoomName || room.name || room.canonical_alias || room.aliases?.[0]
|| (room.room_type === RoomType.Space ? _t("Unnamed Space") : _t("Unnamed Room"));

const [showChildren, toggleShowChildren] = useStateToggle(true);
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/rooms/RoomTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class RoomTile extends React.PureComponent<IProps, IState> {
this.roomProps = EchoChamber.forRoom(this.props.room);
}

private onRoomNameUpdate = (room) => {
private onRoomNameUpdate = (room: Room) => {
this.forceUpdate();
};

Expand Down Expand Up @@ -164,7 +164,7 @@ export default class RoomTile extends React.PureComponent<IProps, IState> {
);
this.notificationState.on(NOTIFICATION_STATE_UPDATE, this.onNotificationUpdate);
this.roomProps.on(PROPERTY_UPDATED, this.onRoomPropertyUpdate);
this.roomProps.on("Room.name", this.onRoomNameUpdate);
this.props.room?.on("Room.name", this.onRoomNameUpdate);
CommunityPrototypeStore.instance.on(
CommunityPrototypeStore.getUpdateEventName(this.props.room.roomId),
this.onCommunityUpdate,
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/spaces/SpaceCreateMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const SpaceCreateForm: React.FC<ISpaceCreateFormProps> = ({
const newName = ev.target.value;
if (!alias || alias === `#${nameToLocalpart(name)}:${domain}`) {
setAlias(`#${nameToLocalpart(newName)}:${domain}`);
aliasFieldRef.current.validate({ allowEmpty: true });
aliasFieldRef.current?.validate({ allowEmpty: true });
}
setName(newName);
}}
Expand Down
12 changes: 10 additions & 2 deletions src/hooks/useEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import type { EventEmitter } from "events";
type Handler = (...args: any[]) => void;

// Hook to wrap event emitter on and removeListener in hook lifecycle
export const useEventEmitter = (emitter: EventEmitter, eventName: string | symbol, handler: Handler) => {
export const useEventEmitter = (
emitter: EventEmitter | undefined,
eventName: string | symbol,
handler: Handler,
) => {
// Create a ref that stores handler
const savedHandler = useRef(handler);

Expand Down Expand Up @@ -51,7 +55,11 @@ export const useEventEmitter = (emitter: EventEmitter, eventName: string | symbo

type Mapper<T> = (...args: any[]) => T;

export const useEventEmitterState = <T>(emitter: EventEmitter, eventName: string | symbol, fn: Mapper<T>): T => {
export const useEventEmitterState = <T>(
emitter: EventEmitter | undefined,
eventName: string | symbol,
fn: Mapper<T>,
): T => {
const [value, setValue] = useState<T>(fn());
const handler = useCallback((...args: any[]) => {
setValue(fn(...args));
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useRoomState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const defaultMapper: Mapper<RoomState> = (roomState: RoomState) => roomState;

// Hook to simplify watching Matrix Room state
export const useRoomState = <T extends any = RoomState>(
room: Room,
room?: Room,
mapper: Mapper<T> = defaultMapper as Mapper<T>,
): T => {
const [value, setValue] = useState<T>(room ? mapper(room.currentState) : undefined);
Expand Down

0 comments on commit 6900fb7

Please sign in to comment.