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

Commit

Permalink
Space panel should watch spaces for space name changes (#7432)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Dec 21, 2021
1 parent 70dc035 commit 38634f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/components/views/rooms/RoomListHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ const RoomListHeader = ({ spacePanelDisabled, onVisibilityChange }: IProps) => {
}
});

const spaceName = useEventEmitterState(activeSpace, "Room.name", () => activeSpace?.name);

useEffect(() => {
if (onVisibilityChange) {
onVisibilityChange();
Expand Down Expand Up @@ -320,7 +322,7 @@ const RoomListHeader = ({ spacePanelDisabled, onVisibilityChange }: IProps) => {

let title: string;
if (activeSpace) {
title = activeSpace.name;
title = spaceName;
} else if (communityId) {
title = CommunityPrototypeStore.instance.getSelectedCommunityName();
} else {
Expand All @@ -344,7 +346,7 @@ const RoomListHeader = ({ spacePanelDisabled, onVisibilityChange }: IProps) => {
isExpanded={mainMenuDisplayed}
className="mx_RoomListHeader_contextMenuButton"
title={activeSpace
? _t("%(spaceName)s menu", { spaceName: activeSpace.name })
? _t("%(spaceName)s menu", { spaceName })
: _t("Home options")}
>
{ title }
Expand Down
14 changes: 12 additions & 2 deletions src/components/views/spaces/SpaceTreeLevel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const SpaceButton: React.FC<IButtonProps> = ({
};

interface IItemProps extends InputHTMLAttributes<HTMLLIElement> {
space?: Room;
space: Room;
activeSpaces: SpaceKey[];
isNested?: boolean;
isPanelCollapsed?: boolean;
Expand All @@ -157,6 +157,7 @@ interface IItemProps extends InputHTMLAttributes<HTMLLIElement> {
}

interface IItemState {
name: string;
collapsed: boolean;
childSpaces: Room[];
}
Expand All @@ -176,15 +177,18 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
);

this.state = {
name: this.props.space.name,
collapsed,
childSpaces: this.childSpaces,
};

SpaceStore.instance.on(this.props.space.roomId, this.onSpaceUpdate);
this.props.space.on("Room.name", this.onRoomNameChange);
}

componentWillUnmount() {
SpaceStore.instance.off(this.props.space.roomId, this.onSpaceUpdate);
this.props.space.off("Room.name", this.onRoomNameChange);
}

private onSpaceUpdate = () => {
Expand All @@ -193,6 +197,12 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
});
};

private onRoomNameChange = () => {
this.setState({
name: this.props.space.name,
});
};

private get childSpaces() {
return SpaceStore.instance.getChildSpaces(this.props.space.roomId)
.filter(s => !this.props.parents?.has(s.roomId));
Expand Down Expand Up @@ -318,7 +328,7 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
space={space}
className={isInvite ? "mx_SpaceButton_invite" : undefined}
selected={activeSpaces.includes(space.roomId)}
label={space.name}
label={this.state.name}
contextMenuTooltip={_t("Space options")}
notificationState={notificationState}
isNarrow={isPanelCollapsed}
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export const useEventEmitterState = <T>(
const handler = useCallback((...args: any[]) => {
setValue(fn(...args));
}, [fn]);
// re-run when the emitter changes
useEffect(handler, [emitter]); // eslint-disable-line react-hooks/exhaustive-deps
useEventEmitter(emitter, eventName, handler);
return value;
};

0 comments on commit 38634f8

Please sign in to comment.