From ed30750f6382ecd3bf1bc4458d53706b859722a0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 30 Sep 2020 15:38:35 +0100 Subject: [PATCH] Extract RoomWidgetContextMenu Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/structures/ContextMenu.tsx | 5 +- .../context_menus/RoomWidgetContextMenu.tsx | 79 +++++++++++++++++++ .../views/right_panel/WidgetCard.tsx | 50 +----------- 3 files changed, 86 insertions(+), 48 deletions(-) create mode 100644 src/components/views/context_menus/RoomWidgetContextMenu.tsx diff --git a/src/components/structures/ContextMenu.tsx b/src/components/structures/ContextMenu.tsx index 884f77aba5..fa0d6682dd 100644 --- a/src/components/structures/ContextMenu.tsx +++ b/src/components/structures/ContextMenu.tsx @@ -416,8 +416,9 @@ export const aboveLeftOf = (elementRect: DOMRect, chevronFace = ChevronFace.None return menuOptions; }; -export const useContextMenu = (): [boolean, RefObject, () => void, () => void, (val: boolean) => void] => { - const button = useRef(null); +type ContextMenuTuple = [boolean, RefObject, () => void, () => void, (val: boolean) => void]; +export const useContextMenu = (): ContextMenuTuple => { + const button = useRef(null); const [isOpen, setIsOpen] = useState(false); const open = () => { setIsOpen(true); diff --git a/src/components/views/context_menus/RoomWidgetContextMenu.tsx b/src/components/views/context_menus/RoomWidgetContextMenu.tsx new file mode 100644 index 0000000000..5d5e88197e --- /dev/null +++ b/src/components/views/context_menus/RoomWidgetContextMenu.tsx @@ -0,0 +1,79 @@ +/* +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, {useContext} from "react"; + +import IconizedContextMenu, {IconizedContextMenuOption, IconizedContextMenuOptionList} from "./IconizedContextMenu"; +import {ChevronFace} from "../../structures/ContextMenu"; +import {_t} from "../../../languageHandler"; +import {IApp} from "../../../stores/WidgetStore"; +import defaultDispatcher from "../../../dispatcher/dispatcher"; +import {AppTileActionPayload} from "../../../dispatcher/payloads/AppTileActionPayload"; +import {Action} from "../../../dispatcher/actions"; +import {Capability} from "../../../widgets/WidgetApi"; +import WidgetUtils from "../../../utils/WidgetUtils"; +import ActiveWidgetStore from "../../../stores/ActiveWidgetStore"; +import RoomContext from "../../../contexts/RoomContext"; + +interface IProps extends React.ComponentProps { + app: IApp; +} + +const RoomWidgetContextMenu: React.FC = ({ onFinished, app, ...props}) => { + const {roomId} = useContext(RoomContext); + + let snapshotButton; + if (ActiveWidgetStore.widgetHasCapability(app.id, Capability.Screenshot)) { + const onSnapshotClick = () => { + WidgetUtils.snapshotWidget(app); + onFinished(); + }; + + snapshotButton = ; + } + + let deleteButton; + if (WidgetUtils.canUserModifyWidgets(roomId)) { + const onDeleteClick = () => { + defaultDispatcher.dispatch({ + action: Action.AppTileDelete, + widgetId: app.id, + }); + onFinished(); + }; + + deleteButton = ; + } + + const onRevokeClick = () => { + defaultDispatcher.dispatch({ + action: Action.AppTileRevoke, + widgetId: app.id, + }); + onFinished(); + }; + + return + + { snapshotButton } + { deleteButton } + + + ; +}; + +export default RoomWidgetContextMenu; + diff --git a/src/components/views/right_panel/WidgetCard.tsx b/src/components/views/right_panel/WidgetCard.tsx index 1677494708..230e71c000 100644 --- a/src/components/views/right_panel/WidgetCard.tsx +++ b/src/components/views/right_panel/WidgetCard.tsx @@ -29,16 +29,10 @@ import defaultDispatcher from "../../../dispatcher/dispatcher"; import {SetRightPanelPhasePayload} from "../../../dispatcher/payloads/SetRightPanelPhasePayload"; import {Action} from "../../../dispatcher/actions"; import WidgetStore from "../../../stores/WidgetStore"; -import ActiveWidgetStore from "../../../stores/ActiveWidgetStore"; import {ChevronFace, ContextMenuButton, useContextMenu} from "../../structures/ContextMenu"; -import IconizedContextMenu, { - IconizedContextMenuOption, - IconizedContextMenuOptionList, -} from "../context_menus/IconizedContextMenu"; -import {AppTileActionPayload} from "../../../dispatcher/payloads/AppTileActionPayload"; -import {Capability} from "../../../widgets/WidgetApi"; import AccessibleTooltipButton from "../elements/AccessibleTooltipButton"; import classNames from "classnames"; +import RoomWidgetContextMenu from "../context_menus/RoomWidgetContextMenu"; interface IProps { room: Room; @@ -76,51 +70,15 @@ const WidgetCard: React.FC = ({ room, widgetId, onClose }) => { let contextMenu; if (menuDisplayed) { - let snapshotButton; - if (ActiveWidgetStore.widgetHasCapability(app.id, Capability.Screenshot)) { - const onSnapshotClick = () => { - WidgetUtils.snapshotWidget(app); - closeMenu(); - }; - - snapshotButton = ; - } - - let deleteButton; - if (canModify) { - const onDeleteClick = () => { - defaultDispatcher.dispatch({ - action: Action.AppTileDelete, - widgetId: app.id, - }); - closeMenu(); - }; - - deleteButton = ; - } - - const onRevokeClick = () => { - defaultDispatcher.dispatch({ - action: Action.AppTileRevoke, - widgetId: app.id, - }); - closeMenu(); - }; - const rect = handle.current.getBoundingClientRect(); contextMenu = ( - - - { snapshotButton } - { deleteButton } - - - + app={app} + /> ); }