/* 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 * as React from "react"; import { createRef } from "react"; import TagPanel from "./TagPanel"; import classNames from "classnames"; import dis from "../../dispatcher/dispatcher"; import { _t } from "../../languageHandler"; import RoomList2 from "../views/rooms/RoomList2"; import { Action } from "../../dispatcher/actions"; import { MatrixClientPeg } from "../../MatrixClientPeg"; import BaseAvatar from '../views/avatars/BaseAvatar'; import UserMenu from "./UserMenuButton"; import RoomSearch from "./RoomSearch"; import AccessibleButton from "../views/elements/AccessibleButton"; import RoomBreadcrumbs2 from "../views/rooms/RoomBreadcrumbs2"; import { BreadcrumbsStore } from "../../stores/BreadcrumbsStore"; import { UPDATE_EVENT } from "../../stores/AsyncStore"; import ResizeNotifier from "../../utils/ResizeNotifier"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { throttle } from 'lodash'; import { OwnProfileStore } from "../../stores/OwnProfileStore"; /******************************************************************* * CAUTION * ******************************************************************* * This is a work in progress implementation and isn't complete or * * even useful as a component. Please avoid using it until this * * warning disappears. * *******************************************************************/ interface IProps { isMinimized: boolean; resizeNotifier: ResizeNotifier; } interface IState { searchFilter: string; // TODO: Move search into room list? showBreadcrumbs: boolean; } export default class LeftPanel2 extends React.Component { private listContainerRef: React.RefObject = createRef(); // TODO: Properly support TagPanel // TODO: Properly support searching/filtering // TODO: Properly support breadcrumbs // TODO: a11y // TODO: actually make this useful in general (match design proposals) // TODO: Fadable support (is this still needed?) constructor(props: IProps) { super(props); this.state = { searchFilter: "", showBreadcrumbs: BreadcrumbsStore.instance.visible, }; BreadcrumbsStore.instance.on(UPDATE_EVENT, this.onBreadcrumbsUpdate); // We watch the middle panel because we don't actually get resized, the middle panel does. // We listen to the noisy channel to avoid choppy reaction times. this.props.resizeNotifier.on("middlePanelResizedNoisy", this.onResize); OwnProfileStore.instance.on(UPDATE_EVENT, this.onProfileUpdate); } public componentWillUnmount() { BreadcrumbsStore.instance.off(UPDATE_EVENT, this.onBreadcrumbsUpdate); this.props.resizeNotifier.off("middlePanelResizedNoisy", this.onResize); OwnProfileStore.instance.off(UPDATE_EVENT, this.onProfileUpdate); } // TSLint wants this to be a member, but we don't want that. // tslint:disable-next-line private onRoomStateUpdate = throttle((ev: MatrixEvent) => { const myUserId = MatrixClientPeg.get().getUserId(); if (ev.getType() === 'm.room.member' && ev.getSender() === myUserId && ev.getStateKey() === myUserId) { // noinspection JSIgnoredPromiseFromCall this.onProfileUpdate(); } }, 200, {trailing: true, leading: true}); private onProfileUpdate = async () => { // the store triggered an update, so force a layout update. We don't // have any state to store here for that to magically happen. this.forceUpdate(); }; private onSearch = (term: string): void => { this.setState({searchFilter: term}); }; private onExplore = () => { dis.fire(Action.ViewRoomDirectory); }; private onBreadcrumbsUpdate = () => { const newVal = BreadcrumbsStore.instance.visible; if (newVal !== this.state.showBreadcrumbs) { this.setState({showBreadcrumbs: newVal}); } }; private handleStickyHeaders(list: HTMLDivElement) { const rlRect = list.getBoundingClientRect(); const bottom = rlRect.bottom; const top = rlRect.top; const sublists = list.querySelectorAll(".mx_RoomSublist2"); const headerHeight = 32; // Note: must match the CSS! const headerRightMargin = 24; // calculated from margins and widths to align with non-sticky tiles const headerStickyWidth = rlRect.width - headerRightMargin; let gotBottom = false; for (const sublist of sublists) { const slRect = sublist.getBoundingClientRect(); const header = sublist.querySelector(".mx_RoomSublist2_stickable"); if (slRect.top + headerHeight > bottom && !gotBottom) { header.classList.add("mx_RoomSublist2_headerContainer_sticky"); header.classList.add("mx_RoomSublist2_headerContainer_stickyBottom"); header.style.width = `${headerStickyWidth}px`; header.style.top = `unset`; gotBottom = true; } else if (slRect.top < top) { header.classList.add("mx_RoomSublist2_headerContainer_sticky"); header.classList.add("mx_RoomSublist2_headerContainer_stickyTop"); header.style.width = `${headerStickyWidth}px`; header.style.top = `${rlRect.top}px`; } else { header.classList.remove("mx_RoomSublist2_headerContainer_sticky"); header.classList.remove("mx_RoomSublist2_headerContainer_stickyTop"); header.classList.remove("mx_RoomSublist2_headerContainer_stickyBottom"); header.style.width = `unset`; header.style.top = `unset`; } } } // TODO: Apply this on resize, init, etc for reliability private onScroll = (ev: React.MouseEvent) => { const list = ev.target as HTMLDivElement; this.handleStickyHeaders(list); }; private onResize = () => { if (!this.listContainerRef.current) return; // ignore: no headers to sticky this.handleStickyHeaders(this.listContainerRef.current); }; private renderHeader(): React.ReactNode { // TODO: Update when profile info changes // TODO: Presence // TODO: Breadcrumbs toggle // TODO: Menu button const avatarSize = 32; // should match border-radius of the avatar let breadcrumbs; if (this.state.showBreadcrumbs) { breadcrumbs = (
{this.props.isMinimized ? null : }
); } let name = {OwnProfileStore.instance.displayName}; let buttons = ( ); if (this.props.isMinimized) { name = null; buttons = null; } return (
{name} {buttons}
{breadcrumbs}
); } private renderSearchExplore(): React.ReactNode { // TODO: Collapsed support return (
); } public render(): React.ReactNode { const tagPanel = (
); // TODO: Improve props for RoomList2 const roomList = {/*TODO*/}} resizeNotifier={null} collapsed={false} searchFilter={this.state.searchFilter} onFocus={() => {/*TODO*/}} onBlur={() => {/*TODO*/}} isMinimized={this.props.isMinimized} />; // TODO: Conference handling / calls const containerClasses = classNames({ "mx_LeftPanel2": true, "mx_LeftPanel2_minimized": this.props.isMinimized, }); return (
{tagPanel}
); } }