mirror of https://github.com/vector-im/riot-web
Merge pull request #6798 from matrix-org/t3chguy/fix/randoms
commit
6900fb79c2
|
@ -57,6 +57,7 @@ import { Key } from "../../Keyboard";
|
||||||
import { IState, RovingTabIndexProvider, useRovingTabIndex } from "../../accessibility/RovingTabIndex";
|
import { IState, RovingTabIndexProvider, useRovingTabIndex } from "../../accessibility/RovingTabIndex";
|
||||||
import { getDisplayAliasForRoom } from "./RoomDirectory";
|
import { getDisplayAliasForRoom } from "./RoomDirectory";
|
||||||
import MatrixClientContext from "../../contexts/MatrixClientContext";
|
import MatrixClientContext from "../../contexts/MatrixClientContext";
|
||||||
|
import { useEventEmitterState } from "../../hooks/useEventEmitter";
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
space: Room;
|
space: Room;
|
||||||
|
@ -87,7 +88,8 @@ const Tile: React.FC<ITileProps> = ({
|
||||||
}) => {
|
}) => {
|
||||||
const cli = useContext(MatrixClientContext);
|
const cli = useContext(MatrixClientContext);
|
||||||
const joinedRoom = cli.getRoom(room.room_id)?.getMyMembership() === "join" ? cli.getRoom(room.room_id) : null;
|
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"));
|
|| (room.room_type === RoomType.Space ? _t("Unnamed Space") : _t("Unnamed Room"));
|
||||||
|
|
||||||
const [showChildren, toggleShowChildren] = useStateToggle(true);
|
const [showChildren, toggleShowChildren] = useStateToggle(true);
|
||||||
|
|
|
@ -101,7 +101,7 @@ export default class RoomTile extends React.PureComponent<IProps, IState> {
|
||||||
this.roomProps = EchoChamber.forRoom(this.props.room);
|
this.roomProps = EchoChamber.forRoom(this.props.room);
|
||||||
}
|
}
|
||||||
|
|
||||||
private onRoomNameUpdate = (room) => {
|
private onRoomNameUpdate = (room: Room) => {
|
||||||
this.forceUpdate();
|
this.forceUpdate();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ export default class RoomTile extends React.PureComponent<IProps, IState> {
|
||||||
);
|
);
|
||||||
this.notificationState.on(NOTIFICATION_STATE_UPDATE, this.onNotificationUpdate);
|
this.notificationState.on(NOTIFICATION_STATE_UPDATE, this.onNotificationUpdate);
|
||||||
this.roomProps.on(PROPERTY_UPDATED, this.onRoomPropertyUpdate);
|
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.instance.on(
|
||||||
CommunityPrototypeStore.getUpdateEventName(this.props.room.roomId),
|
CommunityPrototypeStore.getUpdateEventName(this.props.room.roomId),
|
||||||
this.onCommunityUpdate,
|
this.onCommunityUpdate,
|
||||||
|
|
|
@ -177,7 +177,7 @@ export const SpaceCreateForm: React.FC<ISpaceCreateFormProps> = ({
|
||||||
const newName = ev.target.value;
|
const newName = ev.target.value;
|
||||||
if (!alias || alias === `#${nameToLocalpart(name)}:${domain}`) {
|
if (!alias || alias === `#${nameToLocalpart(name)}:${domain}`) {
|
||||||
setAlias(`#${nameToLocalpart(newName)}:${domain}`);
|
setAlias(`#${nameToLocalpart(newName)}:${domain}`);
|
||||||
aliasFieldRef.current.validate({ allowEmpty: true });
|
aliasFieldRef.current?.validate({ allowEmpty: true });
|
||||||
}
|
}
|
||||||
setName(newName);
|
setName(newName);
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -20,7 +20,11 @@ import type { EventEmitter } from "events";
|
||||||
type Handler = (...args: any[]) => void;
|
type Handler = (...args: any[]) => void;
|
||||||
|
|
||||||
// Hook to wrap event emitter on and removeListener in hook lifecycle
|
// 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
|
// Create a ref that stores handler
|
||||||
const savedHandler = useRef(handler);
|
const savedHandler = useRef(handler);
|
||||||
|
|
||||||
|
@ -51,7 +55,11 @@ export const useEventEmitter = (emitter: EventEmitter, eventName: string | symbo
|
||||||
|
|
||||||
type Mapper<T> = (...args: any[]) => T;
|
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 [value, setValue] = useState<T>(fn());
|
||||||
const handler = useCallback((...args: any[]) => {
|
const handler = useCallback((...args: any[]) => {
|
||||||
setValue(fn(...args));
|
setValue(fn(...args));
|
||||||
|
|
|
@ -25,7 +25,7 @@ const defaultMapper: Mapper<RoomState> = (roomState: RoomState) => roomState;
|
||||||
|
|
||||||
// Hook to simplify watching Matrix Room state
|
// Hook to simplify watching Matrix Room state
|
||||||
export const useRoomState = <T extends any = RoomState>(
|
export const useRoomState = <T extends any = RoomState>(
|
||||||
room: Room,
|
room?: Room,
|
||||||
mapper: Mapper<T> = defaultMapper as Mapper<T>,
|
mapper: Mapper<T> = defaultMapper as Mapper<T>,
|
||||||
): T => {
|
): T => {
|
||||||
const [value, setValue] = useState<T>(room ? mapper(room.currentState) : undefined);
|
const [value, setValue] = useState<T>(room ? mapper(room.currentState) : undefined);
|
||||||
|
|
Loading…
Reference in New Issue