/* Copyright 2021 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 classNames from "classnames"; import {Room} from "matrix-js-sdk/src/models/room"; import RoomAvatar from "../avatars/RoomAvatar"; import SpaceStore from "../../../stores/SpaceStore"; import NotificationBadge from "../rooms/NotificationBadge"; import {RovingAccessibleButton} from "../../../accessibility/roving/RovingAccessibleButton"; import {RovingAccessibleTooltipButton} from "../../../accessibility/roving/RovingAccessibleTooltipButton"; import IconizedContextMenu, { IconizedContextMenuOption, IconizedContextMenuOptionList, } from "../context_menus/IconizedContextMenu"; import {_t} from "../../../languageHandler"; import {ContextMenuTooltipButton} from "../../../accessibility/context_menu/ContextMenuTooltipButton"; import {toRightOf} from "../../structures/ContextMenu"; import { shouldShowSpaceSettings, showAddExistingRooms, showCreateNewRoom, showSpaceInvite, showSpaceSettings, } from "../../../utils/space"; import MatrixClientContext from "../../../contexts/MatrixClientContext"; import AccessibleButton, {ButtonEvent} from "../elements/AccessibleButton"; import defaultDispatcher from "../../../dispatcher/dispatcher"; import {Action} from "../../../dispatcher/actions"; import RoomViewStore from "../../../stores/RoomViewStore"; import {SetRightPanelPhasePayload} from "../../../dispatcher/payloads/SetRightPanelPhasePayload"; import {RightPanelPhases} from "../../../stores/RightPanelStorePhases"; import {EventType} from "matrix-js-sdk/src/@types/event"; import {StaticNotificationState} from "../../../stores/notifications/StaticNotificationState"; import {NotificationColor} from "../../../stores/notifications/NotificationColor"; interface IItemProps { space?: Room; activeSpaces: Room[]; isNested?: boolean; isPanelCollapsed?: boolean; onExpand?: Function; parents?: Set; } interface IItemState { collapsed: boolean; contextMenuPosition: Pick; } export class SpaceItem extends React.PureComponent { static contextType = MatrixClientContext; constructor(props) { super(props); this.state = { collapsed: !props.isNested, // default to collapsed for root items contextMenuPosition: null, }; } private toggleCollapse(evt) { if (this.props.onExpand && this.state.collapsed) { this.props.onExpand(); } this.setState({collapsed: !this.state.collapsed}); // don't bubble up so encapsulating button for space // doesn't get triggered evt.stopPropagation(); } private onContextMenu = (ev: React.MouseEvent) => { if (this.props.space.getMyMembership() !== "join") return; ev.preventDefault(); ev.stopPropagation(); this.setState({ contextMenuPosition: { right: ev.clientX, top: ev.clientY, height: 0, }, }); } private onClick = (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); SpaceStore.instance.setActiveSpace(this.props.space); }; private onMenuOpenClick = (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); const target = ev.target as HTMLButtonElement; this.setState({contextMenuPosition: target.getBoundingClientRect()}); }; private onMenuClose = () => { this.setState({contextMenuPosition: null}); }; private onInviteClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); showSpaceInvite(this.props.space); this.setState({contextMenuPosition: null}); // also close the menu }; private onSettingsClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); showSpaceSettings(this.context, this.props.space); this.setState({contextMenuPosition: null}); // also close the menu }; private onLeaveClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); defaultDispatcher.dispatch({ action: "leave_room", room_id: this.props.space.roomId, }); this.setState({contextMenuPosition: null}); // also close the menu }; private onNewRoomClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); showCreateNewRoom(this.context, this.props.space); this.setState({contextMenuPosition: null}); // also close the menu }; private onAddExistingRoomClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); showAddExistingRooms(this.context, this.props.space); this.setState({contextMenuPosition: null}); // also close the menu }; private onMembersClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); if (!RoomViewStore.getRoomId()) { defaultDispatcher.dispatch({ action: "view_room", room_id: this.props.space.roomId, }, true); } defaultDispatcher.dispatch({ action: Action.SetRightPanelPhase, phase: RightPanelPhases.SpaceMemberList, refireParams: { space: this.props.space }, }); this.setState({contextMenuPosition: null}); // also close the menu }; private onExploreRoomsClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); defaultDispatcher.dispatch({ action: "view_room", room_id: this.props.space.roomId, }); this.setState({contextMenuPosition: null}); // also close the menu }; private renderContextMenu(): React.ReactElement { if (this.props.space.getMyMembership() !== "join") return null; let contextMenu = null; if (this.state.contextMenuPosition) { const userId = this.context.getUserId(); let inviteOption; if (this.props.space.canInvite(userId)) { inviteOption = ( ); } let settingsOption; let leaveSection; if (shouldShowSpaceSettings(this.context, this.props.space)) { settingsOption = ( ); } else { leaveSection = ; } const canAddRooms = this.props.space.currentState.maySendStateEvent(EventType.SpaceChild, userId); let newRoomSection; if (this.props.space.currentState.maySendStateEvent(EventType.SpaceChild, userId)) { newRoomSection = ; } contextMenu =
{ this.props.space.name }
{ inviteOption } { settingsOption } { newRoomSection } { leaveSection }
; } return ( { contextMenu } ); } render() { const {space, activeSpaces, isNested} = this.props; const forceCollapsed = this.props.isPanelCollapsed; const isNarrow = this.props.isPanelCollapsed; const collapsed = this.state.collapsed || forceCollapsed; const childSpaces = SpaceStore.instance.getChildSpaces(space.roomId) .filter(s => !this.props.parents?.has(s.roomId)); const isActive = activeSpaces.includes(space); const itemClasses = classNames({ "mx_SpaceItem": true, "collapsed": collapsed, "hasSubSpaces": childSpaces && childSpaces.length, }); const classes = classNames("mx_SpaceButton", { mx_SpaceButton_active: isActive, mx_SpaceButton_hasMenuOpen: !!this.state.contextMenuPosition, mx_SpaceButton_narrow: isNarrow, }); const notificationState = space.getMyMembership() === "invite" ? StaticNotificationState.forSymbol("!", NotificationColor.Red) : SpaceStore.instance.getNotificationState(space.roomId); let childItems; if (childSpaces && !collapsed) { childItems = ; } let notifBadge; if (notificationState) { notifBadge =
; } const avatarSize = isNested ? 24 : 32; const toggleCollapseButton = childSpaces && childSpaces.length ? this.toggleCollapse(evt)} /> : null; let button; if (isNarrow) { button = ( { toggleCollapseButton }
{ notifBadge } { this.renderContextMenu() }
); } else { button = ( { toggleCollapseButton }
{ space.name } { notifBadge } { this.renderContextMenu() }
); } return (
  • { button } { childItems }
  • ); } } interface ITreeLevelProps { spaces: Room[]; activeSpaces: Room[]; isNested?: boolean; parents: Set; } const SpaceTreeLevel: React.FC = ({ spaces, activeSpaces, isNested, parents, }) => { return
      {spaces.map(s => { return (); })}
    ; } export default SpaceTreeLevel;