|
| 1 | +/* |
| 2 | +Copyright 2020 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 from "react"; |
| 18 | +import { BreadcrumbsStore } from "../../../stores/BreadcrumbsStore"; |
| 19 | +import AccessibleButton from "../elements/AccessibleButton"; |
| 20 | +import RoomAvatar from "../avatars/RoomAvatar"; |
| 21 | +import { _t } from "../../../languageHandler"; |
| 22 | +import { Room } from "matrix-js-sdk/src/models/room"; |
| 23 | +import defaultDispatcher from "../../../dispatcher/dispatcher"; |
| 24 | +import Analytics from "../../../Analytics"; |
| 25 | +import { UPDATE_EVENT } from "../../../stores/AsyncStore"; |
| 26 | +import { CSSTransition, TransitionGroup } from "react-transition-group"; |
| 27 | + |
| 28 | +/******************************************************************* |
| 29 | + * CAUTION * |
| 30 | + ******************************************************************* |
| 31 | + * This is a work in progress implementation and isn't complete or * |
| 32 | + * even useful as a component. Please avoid using it until this * |
| 33 | + * warning disappears. * |
| 34 | + *******************************************************************/ |
| 35 | + |
| 36 | +interface IProps { |
| 37 | +} |
| 38 | + |
| 39 | +interface IState { |
| 40 | + // Both of these control the animation for the breadcrumbs. For details on the |
| 41 | + // actual animation, see the CSS. |
| 42 | + // |
| 43 | + // doAnimation is to lie to the CSSTransition component (see onBreadcrumbsUpdate |
| 44 | + // for info). skipFirst is used to try and reduce jerky animation - also see the |
| 45 | + // breadcrumb update function for info on that. |
| 46 | + doAnimation: boolean; |
| 47 | + skipFirst: boolean; |
| 48 | +} |
| 49 | + |
| 50 | +export default class RoomBreadcrumbs2 extends React.PureComponent<IProps, IState> { |
| 51 | + private isMounted = true; |
| 52 | + |
| 53 | + constructor(props: IProps) { |
| 54 | + super(props); |
| 55 | + |
| 56 | + this.state = { |
| 57 | + doAnimation: true, // technically we want animation on mount, but it won't be perfect |
| 58 | + skipFirst: false, // render the thing, as boring as it is |
| 59 | + }; |
| 60 | + |
| 61 | + BreadcrumbsStore.instance.on(UPDATE_EVENT, this.onBreadcrumbsUpdate); |
| 62 | + } |
| 63 | + |
| 64 | + public componentWillUnmount() { |
| 65 | + this.isMounted = false; |
| 66 | + BreadcrumbsStore.instance.off(UPDATE_EVENT, this.onBreadcrumbsUpdate); |
| 67 | + } |
| 68 | + |
| 69 | + private onBreadcrumbsUpdate = () => { |
| 70 | + if (!this.isMounted) return; |
| 71 | + |
| 72 | + // We need to trick the CSSTransition component into updating, which means we need to |
| 73 | + // tell it to not animate, then to animate a moment later. This causes two updates |
| 74 | + // which means two renders. The skipFirst change is so that our don't-animate state |
| 75 | + // doesn't show the breadcrumb we're about to reveal as it causes a visual jump/jerk. |
| 76 | + // The second update, on the next available tick, causes the "enter" animation to start |
| 77 | + // again and this time we want to show the newest breadcrumb because it'll be hidden |
| 78 | + // off screen for the animation. |
| 79 | + this.setState({doAnimation: false, skipFirst: true}); |
| 80 | + setTimeout(() => this.setState({doAnimation: true, skipFirst: false}), 0); |
| 81 | + }; |
| 82 | + |
| 83 | + private viewRoom = (room: Room, index: number) => { |
| 84 | + Analytics.trackEvent("Breadcrumbs", "click_node", index); |
| 85 | + defaultDispatcher.dispatch({action: "view_room", room_id: room.roomId}); |
| 86 | + }; |
| 87 | + |
| 88 | + public render(): React.ReactElement { |
| 89 | + // TODO: Decorate crumbs with icons |
| 90 | + const tiles = BreadcrumbsStore.instance.rooms.map((r, i) => { |
| 91 | + return ( |
| 92 | + <AccessibleButton |
| 93 | + className="mx_RoomBreadcrumbs2_crumb" |
| 94 | + key={r.roomId} |
| 95 | + onClick={() => this.viewRoom(r, i)} |
| 96 | + aria-label={_t("Room %(name)s", {name: r.name})} |
| 97 | + > |
| 98 | + <RoomAvatar room={r} width={32} height={32}/> |
| 99 | + </AccessibleButton> |
| 100 | + ) |
| 101 | + }); |
| 102 | + |
| 103 | + if (tiles.length > 0) { |
| 104 | + // NOTE: The CSSTransition timeout MUST match the timeout in our CSS! |
| 105 | + return ( |
| 106 | + <CSSTransition |
| 107 | + appear={true} in={this.state.doAnimation} timeout={640} |
| 108 | + classNames='mx_RoomBreadcrumbs2' |
| 109 | + > |
| 110 | + <div className='mx_RoomBreadcrumbs2'> |
| 111 | + {tiles.slice(this.state.skipFirst ? 1 : 0)} |
| 112 | + </div> |
| 113 | + </CSSTransition> |
| 114 | + ); |
| 115 | + } else { |
| 116 | + return ( |
| 117 | + <div className='mx_RoomBreadcrumbs2'> |
| 118 | + <div className="mx_RoomBreadcrumbs2_placeholder"> |
| 119 | + {_t("No recently visited rooms")} |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments