|
| 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 from 'react'; |
| 18 | +import BaseDialog from "./BaseDialog"; |
| 19 | +import { _t, _td, TranslatedString } from "../../../languageHandler"; |
| 20 | +import { IDialogProps } from "./IDialogProps"; |
| 21 | +import { Capability, EventDirection, MatrixCapabilities, Widget, WidgetEventCapability } from "matrix-widget-api"; |
| 22 | +import { objectShallowClone } from "../../../utils/objects"; |
| 23 | +import { ElementWidgetCapabilities } from "../../../stores/widgets/ElementWidgetCapabilities"; |
| 24 | +import { EventType, MsgType } from "matrix-js-sdk/lib/@types/event"; |
| 25 | +import StyledCheckbox from "../elements/StyledCheckbox"; |
| 26 | +import DialogButtons from "../elements/DialogButtons"; |
| 27 | +import LabelledToggleSwitch from "../elements/LabelledToggleSwitch"; |
| 28 | + |
| 29 | +// TODO: These messaging things can probably get their own store of some sort |
| 30 | +const SIMPLE_CAPABILITY_MESSAGES = { |
| 31 | + [MatrixCapabilities.AlwaysOnScreen]: _td("Remain on your screen while running"), |
| 32 | + [MatrixCapabilities.StickerSending]: _td("Send stickers into your active room"), |
| 33 | + [ElementWidgetCapabilities.CanChangeViewedRoom]: _td("Change which room you're viewing"), |
| 34 | +}; |
| 35 | +const SEND_RECV_EVENT_CAPABILITY_MESSAGES = { |
| 36 | + [EventType.RoomTopic]: { |
| 37 | + // TODO: We probably want to say "this room" when we can |
| 38 | + [EventDirection.Send]: _td("Change the topic of your active room"), |
| 39 | + [EventDirection.Receive]: _td("See when the topic changes in your active room"), |
| 40 | + }, |
| 41 | + [EventType.RoomName]: { |
| 42 | + [EventDirection.Send]: _td("Change the name of your active room"), |
| 43 | + [EventDirection.Receive]: _td("See when the name changes in your active room"), |
| 44 | + }, |
| 45 | + [EventType.RoomAvatar]: { |
| 46 | + [EventDirection.Send]: _td("Change the avatar of your active room"), |
| 47 | + [EventDirection.Receive]: _td("See when the avatar changes in your active room"), |
| 48 | + }, |
| 49 | + // TODO: Add more as needed |
| 50 | +}; |
| 51 | +function textForEventCapabilitiy(cap: WidgetEventCapability): { primary: TranslatedString, byline: TranslatedString } { |
| 52 | + let primary: TranslatedString; |
| 53 | + let byline: TranslatedString; |
| 54 | + |
| 55 | + if (cap.isState) { |
| 56 | + byline = cap.keyStr |
| 57 | + ? _t("with state key %(stateKey)s", {stateKey: cap.keyStr}) |
| 58 | + : _t("with an empty state key"); |
| 59 | + } |
| 60 | + |
| 61 | + const srMessages = SEND_RECV_EVENT_CAPABILITY_MESSAGES[cap.eventType]; |
| 62 | + if (srMessages && srMessages[cap.direction]) { |
| 63 | + primary = _t(srMessages[cap.direction]); |
| 64 | + } else { |
| 65 | + if (cap.eventType === EventType.RoomMessage) { |
| 66 | + if (cap.direction === EventDirection.Receive) { |
| 67 | + if (!cap.keyStr) { |
| 68 | + primary = _t("See messages sent in your active room"); |
| 69 | + } else { |
| 70 | + if (cap.keyStr === MsgType.Text) { |
| 71 | + primary = _t("See text messages sent in your active room"); |
| 72 | + } else if (cap.keyStr === MsgType.Emote) { |
| 73 | + primary = _t("See emotes sent in your active room"); |
| 74 | + } else if (cap.keyStr === MsgType.Image) { |
| 75 | + primary = _t("See images sent in your active room"); |
| 76 | + } else if (cap.keyStr === MsgType.Video) { |
| 77 | + primary = _t("See videos sent in your active room"); |
| 78 | + } else if (cap.keyStr === MsgType.File) { |
| 79 | + primary = _t("See general files sent in your active room"); |
| 80 | + } else { |
| 81 | + primary = _t( |
| 82 | + "See <code>%(msgtype)s</code> messages sent in your active room", |
| 83 | + {msgtype: cap.keyStr}, {code: sub => <code>{sub}</code>}, |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + } else { |
| 88 | + if (!cap.keyStr) { |
| 89 | + primary = _t("Send messages as you in your active room"); |
| 90 | + } else { |
| 91 | + if (cap.keyStr === MsgType.Text) { |
| 92 | + primary = _t("Send text messages as you in your active room"); |
| 93 | + } else if (cap.keyStr === MsgType.Emote) { |
| 94 | + primary = _t("Send emotes as you in your active room"); |
| 95 | + } else if (cap.keyStr === MsgType.Image) { |
| 96 | + primary = _t("Send images as you in your active room"); |
| 97 | + } else if (cap.keyStr === MsgType.Video) { |
| 98 | + primary = _t("Send videos as you in your active room"); |
| 99 | + } else if (cap.keyStr === MsgType.File) { |
| 100 | + primary = _t("Send general files as you in your active room"); |
| 101 | + } else { |
| 102 | + primary = _t( |
| 103 | + "Send <code>%(msgtype)s</code> messages as you in your active room", |
| 104 | + {msgtype: cap.keyStr}, {code: sub => <code>{sub}</code>}, |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + if (cap.direction === EventDirection.Receive) { |
| 111 | + primary = _t( |
| 112 | + "See <code>%(eventType)s</code> events sent in your active room", |
| 113 | + {eventType: cap.eventType}, {code: sub => <code>{sub}</code>}, |
| 114 | + ); |
| 115 | + } else { |
| 116 | + primary = _t( |
| 117 | + "Send <code>%(eventType)s</code> events as you in your active room", |
| 118 | + {eventType: cap.eventType}, {code: sub => <code>{sub}</code>}, |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return {primary, byline}; |
| 125 | +} |
| 126 | + |
| 127 | +export function getRememberedCapabilitiesForWidget(widget: Widget): Capability[] { |
| 128 | + return JSON.parse(localStorage.getItem(`widget_${widget.id}_approved_caps`) || "[]"); |
| 129 | +} |
| 130 | + |
| 131 | +function setRememberedCapabilitiesForWidget(widget: Widget, caps: Capability[]) { |
| 132 | + localStorage.setItem(`widget_${widget.id}_approved_caps`, JSON.stringify(caps)); |
| 133 | +} |
| 134 | + |
| 135 | +interface IProps extends IDialogProps { |
| 136 | + requestedCapabilities: Set<Capability>; |
| 137 | + widget: Widget; |
| 138 | +} |
| 139 | + |
| 140 | +interface IBooleanStates { |
| 141 | + // @ts-ignore - TS wants a string key, but we know better |
| 142 | + [capability: Capability]: boolean; |
| 143 | +} |
| 144 | + |
| 145 | +interface IState { |
| 146 | + booleanStates: IBooleanStates; |
| 147 | + rememberSelection: boolean; |
| 148 | +} |
| 149 | + |
| 150 | +export default class WidgetCapabilitiesPromptDialog extends React.PureComponent<IProps, IState> { |
| 151 | + private eventPermissionsMap = new Map<Capability, WidgetEventCapability>(); |
| 152 | + |
| 153 | + constructor(props: IProps) { |
| 154 | + super(props); |
| 155 | + |
| 156 | + const parsedEvents = WidgetEventCapability.findEventCapabilities(this.props.requestedCapabilities); |
| 157 | + parsedEvents.forEach(e => this.eventPermissionsMap.set(e.raw, e)); |
| 158 | + |
| 159 | + const states: IBooleanStates = {}; |
| 160 | + this.props.requestedCapabilities.forEach(c => states[c] = true); |
| 161 | + |
| 162 | + this.state = { |
| 163 | + booleanStates: states, |
| 164 | + rememberSelection: true, |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + private onToggle = (capability: Capability) => { |
| 169 | + const newStates = objectShallowClone(this.state.booleanStates); |
| 170 | + newStates[capability] = !newStates[capability]; |
| 171 | + this.setState({booleanStates: newStates}); |
| 172 | + }; |
| 173 | + |
| 174 | + private onRememberSelectionChange = (newVal: boolean) => { |
| 175 | + this.setState({rememberSelection: newVal}); |
| 176 | + }; |
| 177 | + |
| 178 | + private onSubmit = async (ev) => { |
| 179 | + this.closeAndTryRemember(Object.entries(this.state.booleanStates) |
| 180 | + .filter(([_, isSelected]) => isSelected) |
| 181 | + .map(([cap]) => cap)); |
| 182 | + }; |
| 183 | + |
| 184 | + private onReject = async (ev) => { |
| 185 | + this.closeAndTryRemember([]); // nothing was approved |
| 186 | + }; |
| 187 | + |
| 188 | + private closeAndTryRemember(approved: Capability[]) { |
| 189 | + if (this.state.rememberSelection) { |
| 190 | + setRememberedCapabilitiesForWidget(this.props.widget, approved); |
| 191 | + } |
| 192 | + this.props.onFinished({approved}); |
| 193 | + } |
| 194 | + |
| 195 | + public render() { |
| 196 | + const checkboxRows = Object.entries(this.state.booleanStates).map(([cap, isChecked], i) => { |
| 197 | + const evCap = this.eventPermissionsMap.get(cap); |
| 198 | + |
| 199 | + let text: TranslatedString; |
| 200 | + let byline: TranslatedString; |
| 201 | + if (evCap) { |
| 202 | + const t = textForEventCapabilitiy(evCap); |
| 203 | + text = t.primary; |
| 204 | + byline = t.byline; |
| 205 | + } else if (SIMPLE_CAPABILITY_MESSAGES[cap]) { |
| 206 | + text = _t(SIMPLE_CAPABILITY_MESSAGES[cap]); |
| 207 | + } else { |
| 208 | + text = _t( |
| 209 | + "The <code>%(capability)s</code> capability", |
| 210 | + {capability: cap}, {code: sub => <code>{sub}</code>}, |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + return ( |
| 215 | + <div className="mx_WidgetCapabilitiesPromptDialog_cap"> |
| 216 | + <StyledCheckbox |
| 217 | + key={cap + i} |
| 218 | + checked={isChecked} |
| 219 | + onChange={() => this.onToggle(cap)} |
| 220 | + >{text}</StyledCheckbox> |
| 221 | + {byline ? <span className="mx_WidgetCapabilitiesPromptDialog_byline">{byline}</span> : null} |
| 222 | + </div> |
| 223 | + ); |
| 224 | + }); |
| 225 | + |
| 226 | + return ( |
| 227 | + <BaseDialog |
| 228 | + className="mx_WidgetCapabilitiesPromptDialog" |
| 229 | + onFinished={this.props.onFinished} |
| 230 | + title={_t("Approve widget permissions")} |
| 231 | + > |
| 232 | + <form onSubmit={this.onSubmit}> |
| 233 | + <div className="mx_Dialog_content"> |
| 234 | + {_t("This widget would like to:")} |
| 235 | + {checkboxRows} |
| 236 | + <LabelledToggleSwitch |
| 237 | + value={this.state.rememberSelection} |
| 238 | + toggleInFront={true} |
| 239 | + onChange={this.onRememberSelectionChange} |
| 240 | + label={_t("Remember my selection for this widget")} /> |
| 241 | + <DialogButtons |
| 242 | + primaryButton={_t("Approve")} |
| 243 | + cancelButton={_t("Decline All")} |
| 244 | + onPrimaryButtonClick={this.onSubmit} |
| 245 | + onCancel={this.onReject} |
| 246 | + /> |
| 247 | + </div> |
| 248 | + </form> |
| 249 | + </BaseDialog> |
| 250 | + ); |
| 251 | + } |
| 252 | +} |
0 commit comments