show breadcrumbs only after having joined at least 10 rooms

This commit is contained in:
Bruno Windels 2019-09-12 14:07:46 +02:00
parent ab6215f9aa
commit 94bdedd25b

View file

@ -33,7 +33,7 @@ const MAX_ROOMS = 20;
export default class RoomBreadcrumbs extends React.Component { export default class RoomBreadcrumbs extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {rooms: []}; this.state = {rooms: [], enabled: false};
this.onAction = this.onAction.bind(this); this.onAction = this.onAction.bind(this);
this._dispatcherRef = null; this._dispatcherRef = null;
@ -47,10 +47,13 @@ export default class RoomBreadcrumbs extends React.Component {
this._settingWatchRef = SettingsStore.watchSetting("breadcrumb_rooms", null, this.onBreadcrumbsChanged); this._settingWatchRef = SettingsStore.watchSetting("breadcrumb_rooms", null, this.onBreadcrumbsChanged);
this.setState({enabled: this._shouldEnable()});
MatrixClientPeg.get().on("Room.myMembership", this.onMyMembership); MatrixClientPeg.get().on("Room.myMembership", this.onMyMembership);
MatrixClientPeg.get().on("Room.receipt", this.onRoomReceipt); MatrixClientPeg.get().on("Room.receipt", this.onRoomReceipt);
MatrixClientPeg.get().on("Room.timeline", this.onRoomTimeline); MatrixClientPeg.get().on("Room.timeline", this.onRoomTimeline);
MatrixClientPeg.get().on("Event.decrypted", this.onEventDecrypted); MatrixClientPeg.get().on("Event.decrypted", this.onEventDecrypted);
MatrixClientPeg.get().on("Room", this.onRoomMembershipChanged);
} }
componentWillUnmount() { componentWillUnmount() {
@ -64,6 +67,7 @@ export default class RoomBreadcrumbs extends React.Component {
client.removeListener("Room.receipt", this.onRoomReceipt); client.removeListener("Room.receipt", this.onRoomReceipt);
client.removeListener("Room.timeline", this.onRoomTimeline); client.removeListener("Room.timeline", this.onRoomTimeline);
client.removeListener("Event.decrypted", this.onEventDecrypted); client.removeListener("Event.decrypted", this.onEventDecrypted);
client.removeListener("Room", this.onRoomMembershipChanged);
} }
} }
@ -104,6 +108,7 @@ export default class RoomBreadcrumbs extends React.Component {
this.setState({rooms}); this.setState({rooms});
} }
} }
this.onRoomMembershipChanged();
}; };
onRoomReceipt = (event, room) => { onRoomReceipt = (event, room) => {
@ -143,6 +148,20 @@ export default class RoomBreadcrumbs extends React.Component {
this._loadRoomIds(value); this._loadRoomIds(value);
}; };
onRoomMembershipChanged = () => {
if (!this.state.enabled && this._shouldEnable()) {
this.setState({enabled: true});
}
}
_shouldEnable() {
const client = MatrixClientPeg.get();
const joinedRoomCount = client.getRooms().reduce((count, r) => {
return count + (r.getMyMembership() === "join" ? 1 : 0);
}, 0);
return joinedRoomCount >= 10;
}
_loadRoomIds(roomIds) { _loadRoomIds(roomIds) {
if (!roomIds || roomIds.length <= 0) return; // Skip updates with no rooms if (!roomIds || roomIds.length <= 0) return; // Skip updates with no rooms
@ -283,7 +302,7 @@ export default class RoomBreadcrumbs extends React.Component {
// check for collapsed here and not at parent so we keep rooms in our state // check for collapsed here and not at parent so we keep rooms in our state
// when collapsing and expanding // when collapsing and expanding
if (this.props.collapsed) { if (this.props.collapsed || !this.state.enabled) {
return null; return null;
} }