2020-09-08 10:48:03 +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, {useCallback, useState, useEffect, useContext} from "react";
|
|
|
|
import classNames from "classnames";
|
|
|
|
import {Room} from "matrix-js-sdk/src/models/room";
|
|
|
|
|
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
|
|
|
import { useIsEncrypted } from '../../../hooks/useIsEncrypted';
|
|
|
|
import BaseCard, { Group } from "./BaseCard";
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import RoomAvatar from "../avatars/RoomAvatar";
|
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
|
|
|
import defaultDispatcher from "../../../dispatcher/dispatcher";
|
|
|
|
import {Action} from "../../../dispatcher/actions";
|
|
|
|
import {RightPanelPhases} from "../../../stores/RightPanelStorePhases";
|
|
|
|
import {SetRightPanelPhasePayload} from "../../../dispatcher/payloads/SetRightPanelPhasePayload";
|
|
|
|
import Modal from "../../../Modal";
|
|
|
|
import ShareDialog from '../dialogs/ShareDialog';
|
|
|
|
import {useEventEmitter} from "../../../hooks/useEventEmitter";
|
|
|
|
import WidgetUtils from "../../../utils/WidgetUtils";
|
|
|
|
import {IntegrationManagers} from "../../../integrations/IntegrationManagers";
|
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
|
|
|
import TextWithTooltip from "../elements/TextWithTooltip";
|
2020-10-12 15:16:55 +03:00
|
|
|
import WidgetAvatar from "../avatars/WidgetAvatar";
|
2020-09-08 10:48:03 +03:00
|
|
|
import AccessibleTooltipButton from "../elements/AccessibleTooltipButton";
|
2020-10-12 11:51:49 +03:00
|
|
|
import WidgetStore, {IApp, MAX_PINNED} from "../../../stores/WidgetStore";
|
2020-09-11 18:04:06 +03:00
|
|
|
import { E2EStatus } from "../../../utils/ShieldUtils";
|
|
|
|
import RoomContext from "../../../contexts/RoomContext";
|
2020-09-16 13:38:50 +03:00
|
|
|
import {UIFeature} from "../../../settings/UIFeature";
|
2020-10-15 13:14:48 +03:00
|
|
|
import {ChevronFace, ContextMenuTooltipButton, useContextMenu} from "../../structures/ContextMenu";
|
2020-10-12 11:51:49 +03:00
|
|
|
import WidgetContextMenu from "../context_menus/WidgetContextMenu";
|
2020-12-09 14:02:30 +03:00
|
|
|
import {useRoomMembers} from "../../../hooks/useRoomMembers";
|
2020-09-08 10:48:03 +03:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
room: Room;
|
|
|
|
onClose(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IAppsSectionProps {
|
|
|
|
room: Room;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IButtonProps {
|
|
|
|
className: string;
|
|
|
|
onClick(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Button: React.FC<IButtonProps> = ({ children, className, onClick }) => {
|
|
|
|
return <AccessibleButton
|
|
|
|
className={classNames("mx_BaseCard_Button mx_RoomSummaryCard_Button", className)}
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
|
|
|
{ children }
|
|
|
|
</AccessibleButton>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useWidgets = (room: Room) => {
|
2020-10-15 13:14:48 +03:00
|
|
|
const [apps, setApps] = useState<IApp[]>(WidgetStore.instance.getApps(room.roomId));
|
2020-09-08 10:48:03 +03:00
|
|
|
|
|
|
|
const updateApps = useCallback(() => {
|
|
|
|
// Copy the array so that we always trigger a re-render, as some updates mutate the array of apps/settings
|
2020-10-15 13:14:48 +03:00
|
|
|
setApps([...WidgetStore.instance.getApps(room.roomId)]);
|
2020-09-08 10:48:03 +03:00
|
|
|
}, [room]);
|
|
|
|
|
|
|
|
useEffect(updateApps, [room]);
|
|
|
|
useEventEmitter(WidgetStore.instance, room.roomId, updateApps);
|
|
|
|
|
|
|
|
return apps;
|
|
|
|
};
|
|
|
|
|
2020-09-30 19:08:41 +03:00
|
|
|
interface IAppRowProps {
|
|
|
|
app: IApp;
|
2020-12-01 23:05:48 +03:00
|
|
|
room: Room;
|
2020-09-30 19:08:41 +03:00
|
|
|
}
|
|
|
|
|
2020-12-01 23:05:48 +03:00
|
|
|
const AppRow: React.FC<IAppRowProps> = ({ app, room }) => {
|
2020-09-30 19:08:41 +03:00
|
|
|
const name = WidgetUtils.getWidgetName(app);
|
|
|
|
const dataTitle = WidgetUtils.getWidgetDataTitle(app);
|
|
|
|
const subtitle = dataTitle && " - " + dataTitle;
|
|
|
|
|
|
|
|
const onOpenWidgetClick = () => {
|
|
|
|
defaultDispatcher.dispatch<SetRightPanelPhasePayload>({
|
|
|
|
action: Action.SetRightPanelPhase,
|
|
|
|
phase: RightPanelPhases.Widget,
|
|
|
|
refireParams: {
|
|
|
|
widgetId: app.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-12-01 23:05:48 +03:00
|
|
|
const isPinned = WidgetStore.instance.isPinned(room.roomId, app.id);
|
2020-09-30 19:08:41 +03:00
|
|
|
const togglePin = isPinned
|
2020-12-01 23:05:48 +03:00
|
|
|
? () => { WidgetStore.instance.unpinWidget(room.roomId, app.id); }
|
|
|
|
: () => { WidgetStore.instance.pinWidget(room.roomId, app.id); };
|
2020-09-30 19:08:41 +03:00
|
|
|
|
|
|
|
const [menuDisplayed, handle, openMenu, closeMenu] = useContextMenu<HTMLDivElement>();
|
|
|
|
let contextMenu;
|
|
|
|
if (menuDisplayed) {
|
|
|
|
const rect = handle.current.getBoundingClientRect();
|
2020-10-12 11:51:49 +03:00
|
|
|
contextMenu = <WidgetContextMenu
|
2020-09-30 19:08:41 +03:00
|
|
|
chevronFace={ChevronFace.None}
|
|
|
|
right={window.innerWidth - rect.right}
|
|
|
|
bottom={window.innerHeight - rect.top}
|
|
|
|
onFinished={closeMenu}
|
|
|
|
app={app}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:05:48 +03:00
|
|
|
const cannotPin = !isPinned && !WidgetStore.instance.canPin(room.roomId, app.id);
|
2020-10-12 11:51:49 +03:00
|
|
|
|
|
|
|
let pinTitle: string;
|
|
|
|
if (cannotPin) {
|
|
|
|
pinTitle = _t("You can only pin up to %(count)s widgets", { count: MAX_PINNED });
|
|
|
|
} else {
|
|
|
|
pinTitle = isPinned ? _t("Unpin") : _t("Pin");
|
|
|
|
}
|
|
|
|
|
2020-10-15 13:14:48 +03:00
|
|
|
const classes = classNames("mx_BaseCard_Button mx_RoomSummaryCard_Button", {
|
|
|
|
mx_RoomSummaryCard_Button_pinned: isPinned,
|
|
|
|
});
|
|
|
|
|
|
|
|
return <div className={classes} ref={handle}>
|
2020-09-30 19:08:41 +03:00
|
|
|
<AccessibleTooltipButton
|
2020-10-15 13:14:48 +03:00
|
|
|
className="mx_RoomSummaryCard_icon_app"
|
2020-09-30 19:08:41 +03:00
|
|
|
onClick={onOpenWidgetClick}
|
|
|
|
// only show a tooltip if the widget is pinned
|
2020-10-07 12:36:45 +03:00
|
|
|
title={isPinned ? _t("Unpin a widget to view it in this panel") : ""}
|
2020-09-30 19:08:41 +03:00
|
|
|
forceHide={!isPinned}
|
|
|
|
disabled={isPinned}
|
2020-10-15 13:14:48 +03:00
|
|
|
yOffset={-48}
|
2020-09-30 19:08:41 +03:00
|
|
|
>
|
2020-10-12 15:16:55 +03:00
|
|
|
<WidgetAvatar app={app} />
|
2020-09-30 19:08:41 +03:00
|
|
|
<span>{name}</span>
|
|
|
|
{ subtitle }
|
|
|
|
</AccessibleTooltipButton>
|
|
|
|
|
2020-10-15 13:14:48 +03:00
|
|
|
<ContextMenuTooltipButton
|
|
|
|
className="mx_RoomSummaryCard_app_options"
|
|
|
|
isExpanded={menuDisplayed}
|
|
|
|
onClick={openMenu}
|
|
|
|
title={_t("Options")}
|
|
|
|
yOffset={-24}
|
|
|
|
/>
|
|
|
|
|
2020-09-30 19:08:41 +03:00
|
|
|
<AccessibleTooltipButton
|
2020-10-15 13:14:48 +03:00
|
|
|
className="mx_RoomSummaryCard_app_pinToggle"
|
2020-09-30 19:08:41 +03:00
|
|
|
onClick={togglePin}
|
2020-10-12 11:51:49 +03:00
|
|
|
title={pinTitle}
|
|
|
|
disabled={cannotPin}
|
2020-10-15 13:14:48 +03:00
|
|
|
yOffset={-24}
|
2020-09-30 19:08:41 +03:00
|
|
|
/>
|
|
|
|
|
|
|
|
{ contextMenu }
|
|
|
|
</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const AppsSection: React.FC<IAppsSectionProps> = ({ room }) => {
|
2020-09-08 10:48:03 +03:00
|
|
|
const apps = useWidgets(room);
|
|
|
|
|
|
|
|
const onManageIntegrations = () => {
|
|
|
|
const managers = IntegrationManagers.sharedInstance();
|
|
|
|
if (!managers.hasManager()) {
|
|
|
|
managers.openNoManagerDialog();
|
|
|
|
} else {
|
|
|
|
if (SettingsStore.getValue("feature_many_integration_managers")) {
|
|
|
|
managers.openAll(room);
|
|
|
|
} else {
|
|
|
|
managers.getPrimaryManager().open(room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-18 18:22:35 +03:00
|
|
|
return <Group className="mx_RoomSummaryCard_appsGroup" title={_t("Widgets")}>
|
2020-12-01 23:05:48 +03:00
|
|
|
{ apps.map(app => <AppRow key={app.id} app={app} room={room} />) }
|
2020-09-08 10:48:03 +03:00
|
|
|
|
|
|
|
<AccessibleButton kind="link" onClick={onManageIntegrations}>
|
2020-09-18 18:22:35 +03:00
|
|
|
{ apps.length > 0 ? _t("Edit widgets, bridges & bots") : _t("Add widgets, bridges & bots") }
|
2020-09-08 10:48:03 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
</Group>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onRoomMembersClick = () => {
|
|
|
|
defaultDispatcher.dispatch<SetRightPanelPhasePayload>({
|
|
|
|
action: Action.SetRightPanelPhase,
|
|
|
|
phase: RightPanelPhases.RoomMemberList,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onRoomFilesClick = () => {
|
|
|
|
defaultDispatcher.dispatch<SetRightPanelPhasePayload>({
|
|
|
|
action: Action.SetRightPanelPhase,
|
|
|
|
phase: RightPanelPhases.FilePanel,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onRoomSettingsClick = () => {
|
|
|
|
defaultDispatcher.dispatch({ action: "open_room_settings" });
|
|
|
|
};
|
|
|
|
|
|
|
|
const RoomSummaryCard: React.FC<IProps> = ({ room, onClose }) => {
|
|
|
|
const cli = useContext(MatrixClientContext);
|
|
|
|
|
|
|
|
const onShareRoomClick = () => {
|
|
|
|
Modal.createTrackedDialog('share room dialog', '', ShareDialog, {
|
|
|
|
target: room,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const isRoomEncrypted = useIsEncrypted(cli, room);
|
2020-09-11 18:04:06 +03:00
|
|
|
const roomContext = useContext(RoomContext);
|
|
|
|
const e2eStatus = roomContext.e2eStatus;
|
2020-09-10 22:48:28 +03:00
|
|
|
|
2020-09-08 10:48:03 +03:00
|
|
|
const alias = room.getCanonicalAlias() || room.getAltAliases()[0] || "";
|
|
|
|
const header = <React.Fragment>
|
|
|
|
<div className="mx_RoomSummaryCard_avatar" role="presentation">
|
|
|
|
<RoomAvatar room={room} height={54} width={54} viewAvatarOnClick />
|
|
|
|
<TextWithTooltip
|
|
|
|
tooltip={isRoomEncrypted ? _t("Encrypted") : _t("Not encrypted")}
|
|
|
|
class={classNames("mx_RoomSummaryCard_e2ee", {
|
2020-09-10 22:48:28 +03:00
|
|
|
mx_RoomSummaryCard_e2ee_normal: isRoomEncrypted,
|
|
|
|
mx_RoomSummaryCard_e2ee_warning: isRoomEncrypted && e2eStatus === E2EStatus.Warning,
|
|
|
|
mx_RoomSummaryCard_e2ee_verified: isRoomEncrypted && e2eStatus === E2EStatus.Verified,
|
2020-09-08 10:48:03 +03:00
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<h2 title={room.name}>{ room.name }</h2>
|
|
|
|
<div className="mx_RoomSummaryCard_alias" title={alias}>
|
|
|
|
{ alias }
|
|
|
|
</div>
|
|
|
|
</React.Fragment>;
|
|
|
|
|
2020-12-09 14:02:30 +03:00
|
|
|
const members = useRoomMembers(room);
|
2020-09-09 14:28:12 +03:00
|
|
|
|
2020-09-08 10:48:03 +03:00
|
|
|
return <BaseCard header={header} className="mx_RoomSummaryCard" onClose={onClose}>
|
|
|
|
<Group title={_t("About")} className="mx_RoomSummaryCard_aboutGroup">
|
|
|
|
<Button className="mx_RoomSummaryCard_icon_people" onClick={onRoomMembersClick}>
|
2020-12-09 14:02:30 +03:00
|
|
|
{_t("%(count)s people", { count: members.length })}
|
2020-09-08 10:48:03 +03:00
|
|
|
</Button>
|
|
|
|
<Button className="mx_RoomSummaryCard_icon_files" onClick={onRoomFilesClick}>
|
|
|
|
{_t("Show files")}
|
|
|
|
</Button>
|
|
|
|
<Button className="mx_RoomSummaryCard_icon_share" onClick={onShareRoomClick}>
|
|
|
|
{_t("Share room")}
|
|
|
|
</Button>
|
|
|
|
<Button className="mx_RoomSummaryCard_icon_settings" onClick={onRoomSettingsClick}>
|
|
|
|
{_t("Room settings")}
|
|
|
|
</Button>
|
|
|
|
</Group>
|
|
|
|
|
2020-09-16 13:38:50 +03:00
|
|
|
{ SettingsStore.getValue(UIFeature.Widgets) && <AppsSection room={room} /> }
|
2020-09-08 10:48:03 +03:00
|
|
|
</BaseCard>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RoomSummaryCard;
|