Skip to content

Commit 798558e

Browse files
authored
Merge pull request element-hq#5117 from vector-im/luke/refactor-right-panel-header-buttons
Refactor right panel header buttons
2 parents 75fef3f + bfdb92f commit 798558e

1 file changed

Lines changed: 87 additions & 72 deletions

File tree

‎src/components/structures/RightPanel.js‎

Lines changed: 87 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,62 @@ limitations under the License.
1717
*/
1818

1919
import React from 'react';
20+
import PropTypes from 'prop-types';
2021
import { _t } from 'matrix-react-sdk/lib/languageHandler';
2122
import sdk from 'matrix-react-sdk';
22-
import Matrix from "matrix-js-sdk";
2323
import dis from 'matrix-react-sdk/lib/dispatcher';
2424
import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg';
2525
import Analytics from 'matrix-react-sdk/lib/Analytics';
2626
import rate_limited_func from 'matrix-react-sdk/lib/ratelimitedfunc';
27-
import Modal from 'matrix-react-sdk/lib/Modal';
2827
import AccessibleButton from 'matrix-react-sdk/lib/components/views/elements/AccessibleButton';
2928
import { showGroupInviteDialog } from 'matrix-react-sdk/lib/GroupInvite';
3029

30+
class HeaderButton extends React.Component {
31+
constructor() {
32+
super();
33+
this.onClick = this.onClick.bind(this);
34+
}
35+
36+
onClick(ev) {
37+
Analytics.trackEvent(...this.props.analytics);
38+
dis.dispatch({
39+
action: 'view_right_panel_phase',
40+
phase: this.props.clickPhase,
41+
});
42+
}
43+
44+
render() {
45+
const TintableSvg = sdk.getComponent("elements.TintableSvg");
46+
const AccessibleButton = sdk.getComponent("elements.AccessibleButton");
47+
48+
return <AccessibleButton
49+
className="mx_RightPanel_headerButton"
50+
key={this.props.key}
51+
onClick={ this.onClick }
52+
>
53+
<div className="mx_RightPanel_headerButton_badge">
54+
{ this.props.badge ? this.props.badge : <span>&nbsp;</span>}
55+
</div>
56+
<TintableSvg src={this.props.iconSrc} width="25" height="25"/>
57+
{ this.props.isHighlighted ? <div className="mx_RightPanel_headerButton_highlight"></div> : <div/> }
58+
</AccessibleButton>;
59+
}
60+
}
61+
62+
HeaderButton.propTypes = {
63+
// Whether this button is highlighted
64+
isHighlighted: PropTypes.bool.isRequired,
65+
// The phase to swap to when the button is clicked
66+
clickPhase: PropTypes.string.isRequired,
67+
// The source file of the icon to display
68+
iconSrc: PropTypes.string.isRequired,
69+
70+
// The badge to display above the icon
71+
badge: PropTypes.node,
72+
// The parameters to track the click event
73+
analytics: PropTypes.arrayOf(PropTypes.string).isRequired,
74+
};
75+
3176
module.exports = React.createClass({
3277
displayName: 'RightPanel',
3378

@@ -73,21 +118,6 @@ module.exports = React.createClass({
73118
}
74119
},
75120

76-
onMemberListButtonClick: function() {
77-
Analytics.trackEvent('Right Panel', 'Member List Button', 'click');
78-
this.setState({ phase: this.Phase.RoomMemberList });
79-
},
80-
81-
onFileListButtonClick: function() {
82-
Analytics.trackEvent('Right Panel', 'File List Button', 'click');
83-
this.setState({ phase: this.Phase.FilePanel });
84-
},
85-
86-
onNotificationListButtonClick: function() {
87-
Analytics.trackEvent('Right Panel', 'Notification List Button', 'click');
88-
this.setState({ phase: this.Phase.NotificationPanel });
89-
},
90-
91121
onCollapseClick: function() {
92122
dis.dispatch({
93123
action: 'hide_right_panel',
@@ -140,7 +170,7 @@ module.exports = React.createClass({
140170
} else {
141171
if (this.props.roomId) {
142172
this.setState({
143-
phase: this.Phase.RoomMemberList
173+
phase: this.Phase.RoomMemberList,
144174
});
145175
} else if (this.props.groupId) {
146176
this.setState({
@@ -164,7 +194,11 @@ module.exports = React.createClass({
164194
});
165195
} else if (payload.action === "view_room") {
166196
this.setState({
167-
phase: this.Phase.RoomMemberList
197+
phase: this.Phase.RoomMemberList,
198+
});
199+
} else if (payload.action === "view_right_panel_phase") {
200+
this.setState({
201+
phase: payload.phase,
168202
});
169203
}
170204
},
@@ -176,36 +210,22 @@ module.exports = React.createClass({
176210
const FilePanel = sdk.getComponent('structures.FilePanel');
177211
const TintableSvg = sdk.getComponent("elements.TintableSvg");
178212
let inviteGroup;
179-
let panel;
180-
181-
let filesHighlight;
182-
let membersHighlight;
183-
let notificationsHighlight;
184-
if (!this.props.collapsed) {
185-
if (this.state.phase == this.Phase.RoomMemberList || this.state.phase === this.Phase.RoomMemberInfo) {
186-
membersHighlight = <div className="mx_RightPanel_headerButton_highlight"></div>;
187-
}
188-
else if (this.state.phase == this.Phase.FilePanel) {
189-
filesHighlight = <div className="mx_RightPanel_headerButton_highlight"></div>;
190-
}
191-
else if (this.state.phase == this.Phase.NotificationPanel) {
192-
notificationsHighlight = <div className="mx_RightPanel_headerButton_highlight"></div>;
193-
}
194-
}
195213

196214
let membersBadge;
197-
if ((this.state.phase == this.Phase.RoomMemberList || this.state.phase === this.Phase.RoomMemberInfo) && this.props.roomId) {
215+
if ((this.state.phase == this.Phase.RoomMemberList || this.state.phase === this.Phase.RoomMemberInfo)
216+
&& this.props.roomId
217+
) {
198218
const cli = MatrixClientPeg.get();
199219
const room = cli.getRoom(this.props.roomId);
200-
let user_is_in_room;
220+
let userIsInRoom;
201221
if (room) {
202222
membersBadge = room.getJoinedMembers().length;
203-
user_is_in_room = room.hasMembershipState(
204-
MatrixClientPeg.get().credentials.userId, 'join'
223+
userIsInRoom = room.hasMembershipState(
224+
MatrixClientPeg.get().credentials.userId, 'join',
205225
);
206226
}
207227

208-
if (user_is_in_room) {
228+
if (userIsInRoom) {
209229
inviteGroup =
210230
<AccessibleButton className="mx_RightPanel_invite" onClick={ this.onInviteButtonClick } >
211231
<div className="mx_RightPanel_icon" >
@@ -214,37 +234,28 @@ module.exports = React.createClass({
214234
<div className="mx_RightPanel_message">{ _t('Invite to this room') }</div>
215235
</AccessibleButton>;
216236
}
217-
218237
}
219238

220239
let headerButtons = [];
221240
if (this.props.roomId) {
222-
headerButtons.push(
223-
<AccessibleButton className="mx_RightPanel_headerButton" key="_membersButton"
224-
title={ _t('Members') } onClick={ this.onMemberListButtonClick }>
225-
<div className="mx_RightPanel_headerButton_badge">{ membersBadge ? membersBadge : <span>&nbsp;</span>}</div>
226-
<TintableSvg src="img/icons-people.svg" width="25" height="25"/>
227-
{ membersHighlight }
228-
</AccessibleButton>
229-
);
230-
headerButtons.push(
231-
<AccessibleButton
232-
className="mx_RightPanel_headerButton mx_RightPanel_filebutton" key="_filesButton"
233-
title={ _t('Files') } onClick={ this.onFileListButtonClick }>
234-
<div className="mx_RightPanel_headerButton_badge">&nbsp;</div>
235-
<TintableSvg src="img/icons-files.svg" width="25" height="25"/>
236-
{ filesHighlight }
237-
</AccessibleButton>
238-
);
239-
headerButtons.push(
240-
<AccessibleButton
241-
className="mx_RightPanel_headerButton mx_RightPanel_notificationbutton" key="_notifsButton"
242-
title={ _t('Notifications') } onClick={ this.onNotificationListButtonClick }>
243-
<div className="mx_RightPanel_headerButton_badge">&nbsp;</div>
244-
<TintableSvg src="img/icons-notifications.svg" width="25" height="25"/>
245-
{ notificationsHighlight }
246-
</AccessibleButton>
247-
);
241+
headerButtons = [
242+
<HeaderButton key="_membersButton" title={_t('Members')} iconSrc="img/icons-people.svg"
243+
isHighlighted={[this.Phase.RoomMemberList, this.Phase.RoomMemberInfo].includes(this.state.phase)}
244+
clickPhase={this.Phase.RoomMemberList}
245+
badge={membersBadge}
246+
analytics={['Right Panel', 'Member List Button', 'click']}
247+
/>,
248+
<HeaderButton key="_filesButton" title={_t('Files')} iconSrc="img/icons-files.svg"
249+
isHighlighted={this.state.phase === this.Phase.FilePanel}
250+
clickPhase={this.Phase.FilePanel}
251+
analytics={['Right Panel', 'File List Button', 'click']}
252+
/>,
253+
<HeaderButton key="_notifsButton" title={_t('Notifications')} iconSrc="img/icons-notifications.svg"
254+
isHighlighted={this.state.phase === this.Phase.NotificationPanel}
255+
clickPhase={this.Phase.NotificationPanel}
256+
analytics={['Right Panel', 'Notification List Button', 'click']}
257+
/>,
258+
];
248259
}
249260

250261
if (this.props.roomId || this.props.groupId) {
@@ -256,13 +267,14 @@ module.exports = React.createClass({
256267
title={ _t("Hide panel") } onClick={ this.onCollapseClick }
257268
>
258269
<TintableSvg src="img/minimise.svg" width="10" height="16"/>
259-
</div>
270+
</div>,
260271
);
261272
}
262273

274+
let panel = <div />;
263275
if (!this.props.collapsed) {
264276
if (this.props.roomId && this.state.phase == this.Phase.RoomMemberList) {
265-
panel = <MemberList roomId={this.props.roomId} key={this.props.roomId} />
277+
panel = <MemberList roomId={this.props.roomId} key={this.props.roomId} />;
266278
} else if (this.props.groupId && this.state.phase == this.Phase.GroupMemberList) {
267279
panel = <GroupMemberList groupId={this.props.groupId} key={this.props.groupId} />;
268280
inviteGroup = (
@@ -275,10 +287,13 @@ module.exports = React.createClass({
275287
);
276288
} else if (this.state.phase == this.Phase.RoomMemberInfo) {
277289
const MemberInfo = sdk.getComponent('rooms.MemberInfo');
278-
panel = <MemberInfo member={this.state.member} key={this.props.roomId || this.state.member.userId} />
290+
panel = <MemberInfo member={this.state.member} key={this.props.roomId || this.state.member.userId} />;
279291
} else if (this.state.phase == this.Phase.GroupMemberInfo) {
280292
const GroupMemberInfo = sdk.getComponent('groups.GroupMemberInfo');
281-
panel = <GroupMemberInfo groupMember={this.state.member} groupId={this.props.groupId} key={this.state.member.user_id} />;
293+
panel = <GroupMemberInfo
294+
groupMember={this.state.member}
295+
groupId={this.props.groupId}
296+
key={this.state.member.user_id} />;
282297
} else if (this.state.phase == this.Phase.NotificationPanel) {
283298
panel = <NotificationPanel />;
284299
} else if (this.state.phase == this.Phase.FilePanel) {
@@ -308,5 +323,5 @@ module.exports = React.createClass({
308323
</div>
309324
</aside>
310325
);
311-
}
326+
},
312327
});

0 commit comments

Comments
 (0)