|
| 1 | +/* |
| 2 | +Copyright 2017 Michael Telatynski <7t3chguy@gmail.com> |
| 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 sdk from 'matrix-react-sdk'; |
| 19 | +import { _t } from 'matrix-react-sdk/lib/languageHandler'; |
| 20 | +import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg'; |
| 21 | + |
| 22 | +class SendCustomEvent extends React.Component { |
| 23 | + static propTypes = { |
| 24 | + roomId: React.PropTypes.string.isRequired, |
| 25 | + onBack: React.PropTypes.func.isRequired, |
| 26 | + }; |
| 27 | + |
| 28 | + constructor(props, context) { |
| 29 | + super(props, context); |
| 30 | + this._send = this._send.bind(this); |
| 31 | + this.onBack = this.onBack.bind(this); |
| 32 | + } |
| 33 | + |
| 34 | + state = { |
| 35 | + message: null, |
| 36 | + }; |
| 37 | + |
| 38 | + onBack() { |
| 39 | + if (this.state.message) { |
| 40 | + this.setState({ message: null }); |
| 41 | + } else { |
| 42 | + this.props.onBack(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + _buttons() { |
| 47 | + return <div className="mx_Dialog_buttons"> |
| 48 | + <button onClick={this.onBack}>{ _t('Back') }</button> |
| 49 | + {!this.state.message && <button onClick={this._send}>{ _t('Send') }</button>} |
| 50 | + </div>; |
| 51 | + } |
| 52 | + |
| 53 | + send(content) { |
| 54 | + return MatrixClientPeg.get().sendEvent(this.props.roomId, this.refs.eventType.value, content); |
| 55 | + } |
| 56 | + |
| 57 | + async _send() { |
| 58 | + let message; |
| 59 | + try { |
| 60 | + const content = JSON.parse(this.refs.evContent.value); |
| 61 | + await this.send(content); |
| 62 | + message = _t('Event sent!'); |
| 63 | + } catch (e) { |
| 64 | + message = _t('Failed to send custom event.') + ' (' + e.toString() + ')'; |
| 65 | + } |
| 66 | + this.setState({ message }); |
| 67 | + } |
| 68 | + |
| 69 | + _additionalFields() { |
| 70 | + return <div></div>; |
| 71 | + } |
| 72 | + |
| 73 | + render() { |
| 74 | + if (this.state.message) { |
| 75 | + return <div> |
| 76 | + <div className="mx_Dialog_content"> |
| 77 | + {this.state.message} |
| 78 | + </div> |
| 79 | + {this._buttons()} |
| 80 | + </div>; |
| 81 | + } |
| 82 | + |
| 83 | + return <div> |
| 84 | + <div className="mx_Dialog_content"> |
| 85 | + {this._additionalFields()} |
| 86 | + <div className="mx_TextInputDialog_label"> |
| 87 | + <label htmlFor="eventType"> { _t('Event Type') } </label> |
| 88 | + </div> |
| 89 | + <div> |
| 90 | + <input id="eventType" ref="eventType" className="mx_TextInputDialog_input" size="64" /> |
| 91 | + </div> |
| 92 | + |
| 93 | + <div className="mx_TextInputDialog_label"> |
| 94 | + <label htmlFor="evContent"> { _t('Event Content') } </label> |
| 95 | + </div> |
| 96 | + <div> |
| 97 | + <textarea id="evContent" ref="evContent" className="mx_TextInputDialog_input" defaultValue={"{\n\n}"} cols="63" rows="5" /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + {this._buttons()} |
| 101 | + </div>; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +class SendCustomStateEvent extends SendCustomEvent { |
| 106 | + send(content) { |
| 107 | + return MatrixClientPeg.get().sendStateEvent(this.props.roomId, this.refs.eventType.value, content, |
| 108 | + this.refs.stateKey.value); |
| 109 | + } |
| 110 | + |
| 111 | + _additionalFields() { |
| 112 | + return <div> |
| 113 | + <div className="mx_TextInputDialog_label"> |
| 114 | + <label htmlFor="stateKey"> { _t('State Key') } </label> |
| 115 | + </div> |
| 116 | + <div> |
| 117 | + <input id="stateKey" ref="stateKey" className="mx_TextInputDialog_input" size="64" /> |
| 118 | + </div> |
| 119 | + </div>; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +class RoomStateExplorer extends React.Component { |
| 124 | + static propTypes = { |
| 125 | + roomId: React.PropTypes.string.isRequired, |
| 126 | + onBack: React.PropTypes.func.isRequired, |
| 127 | + }; |
| 128 | + |
| 129 | + constructor(props, context) { |
| 130 | + super(props, context); |
| 131 | + |
| 132 | + const room = MatrixClientPeg.get().getRoom(this.props.roomId); |
| 133 | + this.roomStateEvents = room.currentState.events; |
| 134 | + |
| 135 | + this.onBack = this.onBack.bind(this); |
| 136 | + } |
| 137 | + |
| 138 | + state = { |
| 139 | + eventType: null, |
| 140 | + event: null, |
| 141 | + }; |
| 142 | + |
| 143 | + browseEventType(eventType) { |
| 144 | + return () => { |
| 145 | + this.setState({ eventType }); |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + onViewSourceClick(event) { |
| 150 | + return () => { |
| 151 | + this.setState({ event: event.event }); |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + onBack() { |
| 156 | + if (this.state.event) { |
| 157 | + this.setState({ event: null }); |
| 158 | + } else if (this.state.eventType) { |
| 159 | + this.setState({ eventType: null }); |
| 160 | + } else { |
| 161 | + this.props.onBack(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + render() { |
| 166 | + if (this.state.event) { |
| 167 | + return <div className="mx_ViewSource"> |
| 168 | + <div className="mx_Dialog_content"> |
| 169 | + <pre>{JSON.stringify(this.state.event, null, 2)}</pre> |
| 170 | + </div> |
| 171 | + <div className="mx_Dialog_buttons"> |
| 172 | + <button onClick={this.onBack}>{ _t('Back') }</button> |
| 173 | + </div> |
| 174 | + </div>; |
| 175 | + } |
| 176 | + |
| 177 | + const rows = []; |
| 178 | + |
| 179 | + if (this.state.eventType === null) { |
| 180 | + Object.keys(this.roomStateEvents).forEach((evType) => { |
| 181 | + const stateGroup = this.roomStateEvents[evType]; |
| 182 | + const stateKeys = Object.keys(stateGroup); |
| 183 | + |
| 184 | + let onClickFn; |
| 185 | + if (stateKeys.length > 1) { |
| 186 | + onClickFn = this.browseEventType(evType); |
| 187 | + } else if (stateKeys.length === 1) { |
| 188 | + onClickFn = this.onViewSourceClick(stateGroup[stateKeys[0]]); |
| 189 | + } |
| 190 | + |
| 191 | + rows.push(<button className="mx_DevTools_RoomStateExplorer_button" key={evType} onClick={onClickFn}> |
| 192 | + { evType } |
| 193 | + </button>); |
| 194 | + }); |
| 195 | + } else { |
| 196 | + const evType = this.state.eventType; |
| 197 | + const stateGroup = this.roomStateEvents[evType]; |
| 198 | + Object.keys(stateGroup).forEach((stateKey) => { |
| 199 | + const ev = stateGroup[stateKey]; |
| 200 | + rows.push(<button className="mx_DevTools_RoomStateExplorer_button" key={stateKey} |
| 201 | + onClick={this.onViewSourceClick(ev)}> |
| 202 | + { stateKey } |
| 203 | + </button>); |
| 204 | + }); |
| 205 | + } |
| 206 | + |
| 207 | + return <div> |
| 208 | + <div className="mx_Dialog_content"> |
| 209 | + {rows} |
| 210 | + </div> |
| 211 | + <div className="mx_Dialog_buttons"> |
| 212 | + <button onClick={this.onBack}>{ _t('Back') }</button> |
| 213 | + </div> |
| 214 | + </div>; |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +export default class DevtoolsDialog extends React.Component { |
| 219 | + static propTypes = { |
| 220 | + roomId: React.PropTypes.string.isRequired, |
| 221 | + onFinished: React.PropTypes.func.isRequired, |
| 222 | + }; |
| 223 | + |
| 224 | + state = { |
| 225 | + mode: null, |
| 226 | + }; |
| 227 | + |
| 228 | + constructor(props, context) { |
| 229 | + super(props, context); |
| 230 | + this.onBack = this.onBack.bind(this); |
| 231 | + this.onCancel = this.onCancel.bind(this); |
| 232 | + } |
| 233 | + |
| 234 | + componentWillUnmount() { |
| 235 | + this._unmounted = true; |
| 236 | + } |
| 237 | + |
| 238 | + _setMode(mode) { |
| 239 | + return () => { |
| 240 | + this.setState({ mode }); |
| 241 | + }; |
| 242 | + } |
| 243 | + |
| 244 | + onBack() { |
| 245 | + this.setState({ mode: null }); |
| 246 | + } |
| 247 | + |
| 248 | + onCancel() { |
| 249 | + this.props.onFinished(false); |
| 250 | + } |
| 251 | + |
| 252 | + render() { |
| 253 | + let body; |
| 254 | + |
| 255 | + if (this.state.mode) { |
| 256 | + body = <this.state.mode {...this.props} onBack={this.onBack} />; |
| 257 | + } else { |
| 258 | + body = <div> |
| 259 | + <div className="mx_Dialog_content"> |
| 260 | + <button onClick={this._setMode(SendCustomEvent)}>{ _t('Send Custom Event') }</button> |
| 261 | + <button onClick={this._setMode(SendCustomStateEvent)}>{ _t('Send Custom State Event') }</button> |
| 262 | + <button onClick={this._setMode(RoomStateExplorer)}>{ _t('Explore Room State') }</button> |
| 263 | + </div> |
| 264 | + <div className="mx_Dialog_buttons"> |
| 265 | + <button onClick={this.onCancel}>{ _t('Cancel') }</button> |
| 266 | + </div> |
| 267 | + </div>; |
| 268 | + } |
| 269 | + |
| 270 | + const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog'); |
| 271 | + return <BaseDialog className="mx_QuestionDialog" onFinished={this.props.onFinished} title={_t('Developer Tools')}> |
| 272 | + <div>Room ID: {this.props.roomId}</div> |
| 273 | + { body } |
| 274 | + </BaseDialog>; |
| 275 | + } |
| 276 | +} |
0 commit comments