|
| 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, {useContext, useEffect, useMemo} from "react"; |
| 18 | +import {Resizable} from "re-resizable"; |
| 19 | +import classNames from "classnames"; |
| 20 | + |
| 21 | +import AccessibleButton from "../views/elements/AccessibleButton"; |
| 22 | +import {useRovingTabIndex} from "../../accessibility/RovingTabIndex"; |
| 23 | +import {Key} from "../../Keyboard"; |
| 24 | +import {useLocalStorageState} from "../../hooks/useLocalStorageState"; |
| 25 | +import MatrixClientContext from "../../contexts/MatrixClientContext"; |
| 26 | +import WidgetUtils, {IWidgetEvent} from "../../utils/WidgetUtils"; |
| 27 | +import {useAccountData} from "../../hooks/useAccountData"; |
| 28 | +import AppTile from "../views/elements/AppTile"; |
| 29 | +import {useSettingValue} from "../../hooks/useSettings"; |
| 30 | + |
| 31 | +interface IProps { |
| 32 | + onResize(): void; |
| 33 | +} |
| 34 | + |
| 35 | +const MIN_HEIGHT = 100; |
| 36 | +const MAX_HEIGHT = 500; // or 50% of the window height |
| 37 | +const INITIAL_HEIGHT = 280; |
| 38 | + |
| 39 | +const LeftPanelWidget: React.FC<IProps> = ({ onResize }) => { |
| 40 | + const cli = useContext(MatrixClientContext); |
| 41 | + |
| 42 | + const mWidgetsEvent = useAccountData<Record<string, IWidgetEvent>>(cli, "m.widgets"); |
| 43 | + const leftPanelWidgetId = useSettingValue("Widgets.leftPanel"); |
| 44 | + const app = useMemo(() => { |
| 45 | + if (!mWidgetsEvent || !leftPanelWidgetId) return null; |
| 46 | + const widgetConfig = Object.values(mWidgetsEvent).find(w => w.id === leftPanelWidgetId); |
| 47 | + if (!widgetConfig) return null; |
| 48 | + |
| 49 | + return WidgetUtils.makeAppConfig( |
| 50 | + widgetConfig.state_key, |
| 51 | + widgetConfig.content, |
| 52 | + widgetConfig.sender, |
| 53 | + null, |
| 54 | + widgetConfig.id); |
| 55 | + }, [mWidgetsEvent, leftPanelWidgetId]); |
| 56 | + |
| 57 | + const [height, setHeight] = useLocalStorageState("left-panel-widget-height", INITIAL_HEIGHT); |
| 58 | + const [expanded, setExpanded] = useLocalStorageState("left-panel-widget-expanded", true); |
| 59 | + useEffect(onResize, [expanded]); |
| 60 | + |
| 61 | + const [onFocus, isActive, ref] = useRovingTabIndex(); |
| 62 | + const tabIndex = isActive ? 0 : -1; |
| 63 | + |
| 64 | + if (!app) return null; |
| 65 | + |
| 66 | + let content; |
| 67 | + if (expanded) { |
| 68 | + content = <Resizable |
| 69 | + size={{height} as any} |
| 70 | + minHeight={MIN_HEIGHT} |
| 71 | + maxHeight={Math.min(window.innerHeight / 2, MAX_HEIGHT)} |
| 72 | + onResize={onResize} |
| 73 | + onResizeStop={(e, dir, ref, d) => { |
| 74 | + setHeight(height + d.height); |
| 75 | + }} |
| 76 | + handleWrapperClass="mx_LeftPanelWidget_resizerHandles" |
| 77 | + handleClasses={{top: "mx_LeftPanelWidget_resizerHandle"}} |
| 78 | + className="mx_LeftPanelWidget_resizeBox" |
| 79 | + enable={{ top: true }} |
| 80 | + > |
| 81 | + <AppTile |
| 82 | + app={app} |
| 83 | + fullWidth |
| 84 | + show |
| 85 | + showMenubar={false} |
| 86 | + userWidget |
| 87 | + userId={cli.getUserId()} |
| 88 | + creatorUserId={app.creatorUserId} |
| 89 | + widgetPageTitle={WidgetUtils.getWidgetDataTitle(app)} |
| 90 | + waitForIframeLoad={app.waitForIframeLoad} |
| 91 | + /> |
| 92 | + </Resizable>; |
| 93 | + } |
| 94 | + |
| 95 | + return <div className="mx_LeftPanelWidget"> |
| 96 | + <div |
| 97 | + onFocus={onFocus} |
| 98 | + className="mx_LeftPanelWidget_headerContainer" |
| 99 | + onKeyDown={(ev: React.KeyboardEvent) => { |
| 100 | + switch (ev.key) { |
| 101 | + case Key.ARROW_LEFT: |
| 102 | + ev.stopPropagation(); |
| 103 | + setExpanded(false); |
| 104 | + break; |
| 105 | + case Key.ARROW_RIGHT: { |
| 106 | + ev.stopPropagation(); |
| 107 | + setExpanded(true); |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + }} |
| 112 | + > |
| 113 | + <div className="mx_LeftPanelWidget_stickable"> |
| 114 | + <AccessibleButton |
| 115 | + onFocus={onFocus} |
| 116 | + inputRef={ref} |
| 117 | + tabIndex={tabIndex} |
| 118 | + className="mx_LeftPanelWidget_headerText" |
| 119 | + role="treeitem" |
| 120 | + aria-expanded={expanded} |
| 121 | + aria-level={1} |
| 122 | + onClick={() => { |
| 123 | + setExpanded(e => !e); |
| 124 | + }} |
| 125 | + > |
| 126 | + <span className={classNames({ |
| 127 | + "mx_LeftPanelWidget_collapseBtn": true, |
| 128 | + "mx_LeftPanelWidget_collapseBtn_collapsed": !expanded, |
| 129 | + })} /> |
| 130 | + <span>{ WidgetUtils.getWidgetName(app) }</span> |
| 131 | + </AccessibleButton> |
| 132 | + |
| 133 | + {/* Code for the maximise button for once we have full screen widgets */} |
| 134 | + {/*<AccessibleTooltipButton |
| 135 | + tabIndex={tabIndex} |
| 136 | + onClick={() => { |
| 137 | + }} |
| 138 | + className="mx_LeftPanelWidget_maximizeButton" |
| 139 | + tooltipClassName="mx_LeftPanelWidget_maximizeButtonTooltip" |
| 140 | + title={_t("Maximize")} |
| 141 | + />*/} |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + |
| 145 | + { content } |
| 146 | + </div>; |
| 147 | +}; |
| 148 | + |
| 149 | +export default LeftPanelWidget; |
0 commit comments