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

Commit e1acf11

Browse files
committed
Create Room Name & Topic HOCs to simplify code elsewhere
1 parent 354925c commit e1acf11

File tree

3 files changed

+101
-42
lines changed

3 files changed

+101
-42
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2021 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import {useEffect, useState} from "react";
18+
import {Room} from "matrix-js-sdk/src/models/room";
19+
20+
import {useEventEmitter} from "../../../hooks/useEventEmitter";
21+
22+
interface IProps {
23+
room: Room;
24+
children?(name: string): JSX.Element;
25+
}
26+
27+
const RoomName = ({ room, children }: IProps): JSX.Element => {
28+
const [name, setName] = useState(room?.name);
29+
useEventEmitter(room, "Room.name", () => {
30+
setName(room?.name);
31+
});
32+
useEffect(() => {
33+
setName(room?.name);
34+
}, [room]);
35+
36+
if (children) return children(name);
37+
return name || "";
38+
};
39+
40+
export default RoomName;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2021 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React, {useEffect, useState} from "react";
18+
import {EventType} from "matrix-js-sdk/src/@types/event";
19+
import {Room} from "matrix-js-sdk/src/models/room";
20+
21+
import {useEventEmitter} from "../../../hooks/useEventEmitter";
22+
import {linkifyElement} from "../../../HtmlUtils";
23+
24+
interface IProps {
25+
room?: Room;
26+
children?(topic: string, ref: (element: HTMLElement) => void): JSX.Element;
27+
}
28+
29+
export const getTopic = room => room?.currentState?.getStateEvents(EventType.RoomTopic, "")?.getContent()?.topic;
30+
31+
const RoomTopic = ({ room, children }: IProps): JSX.Element => {
32+
const [topic, setTopic] = useState(getTopic(room));
33+
useEventEmitter(room.currentState, "RoomState.events", () => {
34+
setTopic(getTopic(room));
35+
});
36+
useEffect(() => {
37+
setTopic(getTopic(room));
38+
}, [room]);
39+
40+
const ref = e => e && linkifyElement(e);
41+
if (children) return children(topic, ref);
42+
return <span ref={ref}>{ topic }</span>;
43+
};
44+
45+
export default RoomTopic;

src/components/views/rooms/RoomHeader.js

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18-
import React, {createRef} from 'react';
18+
import React from 'react';
1919
import PropTypes from 'prop-types';
2020
import classNames from 'classnames';
2121
import { _t } from '../../../languageHandler';
2222
import {MatrixClientPeg} from '../../../MatrixClientPeg';
2323
import RateLimitedFunc from '../../../ratelimitedfunc';
2424

25-
import { linkifyElement } from '../../../HtmlUtils';
2625
import {CancelButton} from './SimpleRoomHeader';
2726
import SettingsStore from "../../../settings/SettingsStore";
2827
import RoomHeaderButtons from '../right_panel/RoomHeaderButtons';
2928
import E2EIcon from './E2EIcon';
3029
import DecoratedRoomAvatar from "../avatars/DecoratedRoomAvatar";
3130
import {DefaultTagID} from "../../../stores/room-list/models";
3231
import AccessibleTooltipButton from "../elements/AccessibleTooltipButton";
32+
import RoomTopic from "../elements/RoomTopic";
33+
import RoomName from "../elements/RoomName";
3334

3435
export default class RoomHeader extends React.Component {
3536
static propTypes = {
@@ -52,35 +53,13 @@ export default class RoomHeader extends React.Component {
5253
onCancelClick: null,
5354
};
5455

55-
constructor(props) {
56-
super(props);
57-
58-
this._topic = createRef();
59-
}
60-
6156
componentDidMount() {
6257
const cli = MatrixClientPeg.get();
6358
cli.on("RoomState.events", this._onRoomStateEvents);
6459
cli.on("Room.accountData", this._onRoomAccountData);
65-
66-
// When a room name occurs, RoomState.events is fired *before*
67-
// room.name is updated. So we have to listen to Room.name as well as
68-
// RoomState.events.
69-
if (this.props.room) {
70-
this.props.room.on("Room.name", this._onRoomNameChange);
71-
}
72-
}
73-
74-
componentDidUpdate() {
75-
if (this._topic.current) {
76-
linkifyElement(this._topic.current);
77-
}
7860
}
7961

8062
componentWillUnmount() {
81-
if (this.props.room) {
82-
this.props.room.removeListener("Room.name", this._onRoomNameChange);
83-
}
8463
const cli = MatrixClientPeg.get();
8564
if (cli) {
8665
cli.removeListener("RoomState.events", this._onRoomStateEvents);
@@ -109,10 +88,6 @@ export default class RoomHeader extends React.Component {
10988
this.forceUpdate();
11089
}, 500);
11190

112-
_onRoomNameChange = (room) => {
113-
this.forceUpdate();
114-
};
115-
11691
_hasUnreadPins() {
11792
const currentPinEvent = this.props.room.currentState.getStateEvents("m.room.pinned_events", '');
11893
if (!currentPinEvent) return false;
@@ -170,29 +145,28 @@ export default class RoomHeader extends React.Component {
170145
}
171146
}
172147

173-
let roomName = _t("Join Room");
148+
let oobName = _t("Join Room");
174149
if (this.props.oobData && this.props.oobData.name) {
175-
roomName = this.props.oobData.name;
176-
} else if (this.props.room) {
177-
roomName = this.props.room.name;
150+
oobName = this.props.oobData.name;
178151
}
179152

180153
const textClasses = classNames('mx_RoomHeader_nametext', { mx_RoomHeader_settingsHint: settingsHint });
181154
const name =
182155
<div className="mx_RoomHeader_name" onClick={this.props.onSettingsClick}>
183-
<div dir="auto" className={textClasses} title={roomName}>{ roomName }</div>
156+
<RoomName room={this.props.room}>
157+
{(name) => {
158+
const roomName = name || oobName;
159+
return <div dir="auto" className={textClasses} title={roomName}>{ roomName }</div>;
160+
}}
161+
</RoomName>
184162
{ searchStatus }
185163
</div>;
186164

187-
let topic;
188-
if (this.props.room) {
189-
const ev = this.props.room.currentState.getStateEvents('m.room.topic', '');
190-
if (ev) {
191-
topic = ev.getContent().topic;
192-
}
193-
}
194-
const topicElement =
195-
<div className="mx_RoomHeader_topic" ref={this._topic} title={topic} dir="auto">{ topic }</div>;
165+
const topicElement = <RoomTopic room={this.props.room}>
166+
{(topic, ref) => <div className="mx_RoomHeader_topic" ref={ref} title={topic} dir="auto">
167+
{ topic }
168+
</div>}
169+
</RoomTopic>;
196170

197171
let roomAvatar;
198172
if (this.props.room) {

0 commit comments

Comments
 (0)