2020-06-09 02:11:58 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import { BreadcrumbsStore } from "../../../stores/BreadcrumbsStore";
|
2020-07-01 01:24:46 +03:00
|
|
|
import DecoratedRoomAvatar from "../avatars/DecoratedRoomAvatar";
|
2020-06-09 02:11:58 +03:00
|
|
|
import { _t } from "../../../languageHandler";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import defaultDispatcher from "../../../dispatcher/dispatcher";
|
|
|
|
import Analytics from "../../../Analytics";
|
|
|
|
import { UPDATE_EVENT } from "../../../stores/AsyncStore";
|
2020-06-29 05:03:04 +03:00
|
|
|
import { CSSTransition } from "react-transition-group";
|
2020-07-18 00:11:34 +03:00
|
|
|
import RoomListStore from "../../../stores/room-list/RoomListStore";
|
2020-07-01 01:24:46 +03:00
|
|
|
import { DefaultTagID } from "../../../stores/room-list/models";
|
2020-07-15 06:22:37 +03:00
|
|
|
import { RovingAccessibleTooltipButton } from "../../../accessibility/RovingTabIndex";
|
|
|
|
import Toolbar from "../../../accessibility/Toolbar";
|
2020-06-29 05:03:04 +03:00
|
|
|
|
2020-06-09 02:11:58 +03:00
|
|
|
interface IProps {
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
2020-06-10 00:06:54 +03:00
|
|
|
// Both of these control the animation for the breadcrumbs. For details on the
|
|
|
|
// actual animation, see the CSS.
|
|
|
|
//
|
|
|
|
// doAnimation is to lie to the CSSTransition component (see onBreadcrumbsUpdate
|
|
|
|
// for info). skipFirst is used to try and reduce jerky animation - also see the
|
|
|
|
// breadcrumb update function for info on that.
|
|
|
|
doAnimation: boolean;
|
|
|
|
skipFirst: boolean;
|
2020-06-09 02:11:58 +03:00
|
|
|
}
|
|
|
|
|
2020-07-18 00:32:06 +03:00
|
|
|
export default class RoomBreadcrumbs extends React.PureComponent<IProps, IState> {
|
2020-06-09 02:11:58 +03:00
|
|
|
private isMounted = true;
|
|
|
|
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
|
2020-06-10 00:06:54 +03:00
|
|
|
this.state = {
|
|
|
|
doAnimation: true, // technically we want animation on mount, but it won't be perfect
|
|
|
|
skipFirst: false, // render the thing, as boring as it is
|
|
|
|
};
|
|
|
|
|
2020-06-09 02:11:58 +03:00
|
|
|
BreadcrumbsStore.instance.on(UPDATE_EVENT, this.onBreadcrumbsUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
this.isMounted = false;
|
|
|
|
BreadcrumbsStore.instance.off(UPDATE_EVENT, this.onBreadcrumbsUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
private onBreadcrumbsUpdate = () => {
|
|
|
|
if (!this.isMounted) return;
|
2020-06-10 00:06:54 +03:00
|
|
|
|
|
|
|
// We need to trick the CSSTransition component into updating, which means we need to
|
|
|
|
// tell it to not animate, then to animate a moment later. This causes two updates
|
|
|
|
// which means two renders. The skipFirst change is so that our don't-animate state
|
|
|
|
// doesn't show the breadcrumb we're about to reveal as it causes a visual jump/jerk.
|
|
|
|
// The second update, on the next available tick, causes the "enter" animation to start
|
|
|
|
// again and this time we want to show the newest breadcrumb because it'll be hidden
|
|
|
|
// off screen for the animation.
|
|
|
|
this.setState({doAnimation: false, skipFirst: true});
|
|
|
|
setTimeout(() => this.setState({doAnimation: true, skipFirst: false}), 0);
|
2020-06-09 02:11:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
private viewRoom = (room: Room, index: number) => {
|
|
|
|
Analytics.trackEvent("Breadcrumbs", "click_node", index);
|
|
|
|
defaultDispatcher.dispatch({action: "view_room", room_id: room.roomId});
|
|
|
|
};
|
|
|
|
|
|
|
|
public render(): React.ReactElement {
|
|
|
|
const tiles = BreadcrumbsStore.instance.rooms.map((r, i) => {
|
2020-07-01 01:39:25 +03:00
|
|
|
const roomTags = RoomListStore.instance.getTagsForRoom(r);
|
2020-07-01 01:24:46 +03:00
|
|
|
const roomTag = roomTags.includes(DefaultTagID.DM) ? DefaultTagID.DM : roomTags[0];
|
2020-06-09 02:11:58 +03:00
|
|
|
return (
|
2020-07-15 06:22:37 +03:00
|
|
|
<RovingAccessibleTooltipButton
|
2020-07-18 00:32:06 +03:00
|
|
|
className="mx_RoomBreadcrumbs_crumb"
|
2020-06-09 02:11:58 +03:00
|
|
|
key={r.roomId}
|
|
|
|
onClick={() => this.viewRoom(r, i)}
|
|
|
|
aria-label={_t("Room %(name)s", {name: r.name})}
|
2020-07-01 14:23:27 +03:00
|
|
|
title={r.name}
|
2020-07-18 00:32:06 +03:00
|
|
|
tooltipClassName="mx_RoomBreadcrumbs_Tooltip"
|
2020-06-09 02:11:58 +03:00
|
|
|
>
|
2020-07-01 01:24:46 +03:00
|
|
|
<DecoratedRoomAvatar
|
|
|
|
room={r}
|
|
|
|
avatarSize={32}
|
|
|
|
tag={roomTag}
|
|
|
|
displayBadge={true}
|
|
|
|
forceCount={true}
|
|
|
|
/>
|
2020-07-15 06:22:37 +03:00
|
|
|
</RovingAccessibleTooltipButton>
|
2020-06-18 16:32:43 +03:00
|
|
|
);
|
2020-06-09 02:11:58 +03:00
|
|
|
});
|
|
|
|
|
2020-06-10 00:06:54 +03:00
|
|
|
if (tiles.length > 0) {
|
|
|
|
// NOTE: The CSSTransition timeout MUST match the timeout in our CSS!
|
|
|
|
return (
|
|
|
|
<CSSTransition
|
2020-06-10 00:35:07 +03:00
|
|
|
appear={true} in={this.state.doAnimation} timeout={640}
|
2020-07-18 00:32:06 +03:00
|
|
|
classNames='mx_RoomBreadcrumbs'
|
2020-06-10 00:06:54 +03:00
|
|
|
>
|
2020-07-18 00:32:06 +03:00
|
|
|
<Toolbar className='mx_RoomBreadcrumbs'>
|
2020-06-10 00:06:54 +03:00
|
|
|
{tiles.slice(this.state.skipFirst ? 1 : 0)}
|
2020-07-15 06:22:37 +03:00
|
|
|
</Toolbar>
|
2020-06-10 00:06:54 +03:00
|
|
|
</CSSTransition>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
2020-07-18 00:32:06 +03:00
|
|
|
<div className='mx_RoomBreadcrumbs'>
|
|
|
|
<div className="mx_RoomBreadcrumbs_placeholder">
|
2020-06-10 00:06:54 +03:00
|
|
|
{_t("No recently visited rooms")}
|
|
|
|
</div>
|
2020-06-09 02:11:58 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|