2016-08-01 15:42:29 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2019-08-19 20:09:37 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2016-08-01 15:42:29 +03:00
|
|
|
|
|
|
|
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 from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2016-08-01 15:42:29 +03:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import sdk from '../../../index';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
2017-06-08 14:33:29 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-11-28 18:23:09 +03:00
|
|
|
import Modal from '../../../Modal';
|
2016-08-01 15:42:29 +03:00
|
|
|
|
|
|
|
export default class DevicesPanel extends React.Component {
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
devices: undefined,
|
|
|
|
deviceLoadError: undefined,
|
2017-11-28 18:23:09 +03:00
|
|
|
|
|
|
|
selectedDevices: [],
|
|
|
|
deleting: false,
|
2016-08-01 15:42:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
this._unmounted = false;
|
|
|
|
|
|
|
|
this._renderDevice = this._renderDevice.bind(this);
|
2017-11-28 18:23:09 +03:00
|
|
|
this._onDeviceSelectionToggled = this._onDeviceSelectionToggled.bind(this);
|
|
|
|
this._onDeleteClick = this._onDeleteClick.bind(this);
|
2016-08-01 15:42:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this._loadDevices();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._unmounted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadDevices() {
|
2019-11-18 13:03:05 +03:00
|
|
|
MatrixClientPeg.get().getDevices().then(
|
2016-08-01 15:42:29 +03:00
|
|
|
(resp) => {
|
|
|
|
if (this._unmounted) { return; }
|
|
|
|
this.setState({devices: resp.devices || []});
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
if (this._unmounted) { return; }
|
2017-10-11 19:56:17 +03:00
|
|
|
let errtxt;
|
2016-08-11 18:16:53 +03:00
|
|
|
if (error.httpStatus == 404) {
|
2016-08-01 15:42:29 +03:00
|
|
|
// 404 probably means the HS doesn't yet support the API.
|
2019-02-01 03:52:39 +03:00
|
|
|
errtxt = _t("Your homeserver does not support device management.");
|
2016-08-01 15:42:29 +03:00
|
|
|
} else {
|
|
|
|
console.error("Error loading devices:", error);
|
2017-06-08 14:33:29 +03:00
|
|
|
errtxt = _t("Unable to load device list");
|
2016-08-01 15:42:29 +03:00
|
|
|
}
|
|
|
|
this.setState({deviceLoadError: errtxt});
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-08-01 15:42:29 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* compare two devices, sorting from most-recently-seen to least-recently-seen
|
|
|
|
* (and then, for stability, by device id)
|
|
|
|
*/
|
|
|
|
_deviceCompare(a, b) {
|
|
|
|
// return < 0 if a comes before b, > 0 if a comes after b.
|
|
|
|
const lastSeenDelta =
|
|
|
|
(b.last_seen_ts || 0) - (a.last_seen_ts || 0);
|
|
|
|
|
|
|
|
if (lastSeenDelta !== 0) { return lastSeenDelta; }
|
|
|
|
|
|
|
|
const idA = a.device_id;
|
|
|
|
const idB = b.device_id;
|
|
|
|
return (idA < idB) ? -1 : (idA > idB) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2017-11-28 18:23:09 +03:00
|
|
|
_onDeviceSelectionToggled(device) {
|
2016-08-01 15:42:29 +03:00
|
|
|
if (this._unmounted) { return; }
|
|
|
|
|
2017-11-28 18:23:09 +03:00
|
|
|
const deviceId = device.device_id;
|
2016-08-01 15:42:29 +03:00
|
|
|
this.setState((state, props) => {
|
2017-11-28 18:23:09 +03:00
|
|
|
// Make a copy of the selected devices, then add or remove the device
|
|
|
|
const selectedDevices = state.selectedDevices.slice();
|
|
|
|
|
|
|
|
const i = selectedDevices.indexOf(deviceId);
|
|
|
|
if (i === -1) {
|
|
|
|
selectedDevices.push(deviceId);
|
|
|
|
} else {
|
|
|
|
selectedDevices.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {selectedDevices};
|
2016-08-01 15:42:29 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-28 18:23:09 +03:00
|
|
|
_onDeleteClick() {
|
|
|
|
this.setState({
|
|
|
|
deleting: true,
|
|
|
|
});
|
|
|
|
|
2017-11-28 18:54:00 +03:00
|
|
|
this._makeDeleteRequest(null).catch((error) => {
|
2017-11-28 18:23:09 +03:00
|
|
|
if (this._unmounted) { return; }
|
|
|
|
if (error.httpStatus !== 401 || !error.data || !error.data.flows) {
|
|
|
|
// doesn't look like an interactive-auth failure
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pop up an interactive auth dialog
|
|
|
|
const InteractiveAuthDialog = sdk.getComponent("dialogs.InteractiveAuthDialog");
|
|
|
|
|
|
|
|
Modal.createTrackedDialog('Delete Device Dialog', '', InteractiveAuthDialog, {
|
|
|
|
title: _t("Authentication"),
|
|
|
|
matrixClient: MatrixClientPeg.get(),
|
|
|
|
authData: error.data,
|
|
|
|
makeRequest: this._makeDeleteRequest.bind(this),
|
|
|
|
});
|
|
|
|
}).catch((e) => {
|
|
|
|
console.error("Error deleting devices", e);
|
|
|
|
if (this._unmounted) { return; }
|
|
|
|
}).finally(() => {
|
|
|
|
this.setState({
|
|
|
|
deleting: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_makeDeleteRequest(auth) {
|
|
|
|
return MatrixClientPeg.get().deleteMultipleDevices(this.state.selectedDevices, auth).then(
|
|
|
|
() => {
|
|
|
|
// Remove the deleted devices from `devices`, reset selection to []
|
|
|
|
this.setState({
|
|
|
|
devices: this.state.devices.filter(
|
2017-11-28 18:54:00 +03:00
|
|
|
(d) => !this.state.selectedDevices.includes(d.device_id),
|
2017-11-28 18:23:09 +03:00
|
|
|
),
|
|
|
|
selectedDevices: [],
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-01 15:42:29 +03:00
|
|
|
_renderDevice(device) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const DevicesPanelEntry = sdk.getComponent('settings.DevicesPanelEntry');
|
2017-11-28 18:23:09 +03:00
|
|
|
return <DevicesPanelEntry
|
|
|
|
key={device.device_id}
|
|
|
|
device={device}
|
|
|
|
selected={this.state.selectedDevices.includes(device.device_id)}
|
|
|
|
onDeviceToggled={this._onDeviceSelectionToggled}
|
|
|
|
/>;
|
2016-08-01 15:42:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const Spinner = sdk.getComponent("elements.Spinner");
|
2018-10-02 14:55:24 +03:00
|
|
|
const AccessibleButton = sdk.getComponent("elements.AccessibleButton");
|
2016-08-01 15:42:29 +03:00
|
|
|
|
|
|
|
if (this.state.deviceLoadError !== undefined) {
|
|
|
|
const classes = classNames(this.props.className, "error");
|
|
|
|
return (
|
|
|
|
<div className={classes}>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ this.state.deviceLoadError }
|
2016-08-01 15:42:29 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const devices = this.state.devices;
|
|
|
|
if (devices === undefined) {
|
|
|
|
// still loading
|
|
|
|
const classes = this.props.className;
|
2017-10-11 19:56:17 +03:00
|
|
|
return <Spinner className={classes} />;
|
2016-08-01 15:42:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
devices.sort(this._deviceCompare);
|
|
|
|
|
2017-11-28 18:23:09 +03:00
|
|
|
const deleteButton = this.state.deleting ?
|
|
|
|
<Spinner w={22} h={22} /> :
|
2019-08-19 20:09:37 +03:00
|
|
|
<AccessibleButton onClick={this._onDeleteClick} kind="danger_sm">
|
2017-11-28 18:23:09 +03:00
|
|
|
{ _t("Delete %(count)s devices", {count: this.state.selectedDevices.length}) }
|
2018-10-02 14:55:24 +03:00
|
|
|
</AccessibleButton>;
|
2017-11-28 18:23:09 +03:00
|
|
|
|
2016-08-01 15:42:29 +03:00
|
|
|
const classes = classNames(this.props.className, "mx_DevicesPanel");
|
|
|
|
return (
|
|
|
|
<div className={classes}>
|
|
|
|
<div className="mx_DevicesPanel_header">
|
2019-08-19 20:09:37 +03:00
|
|
|
<div className="mx_DevicesPanel_deviceId">{ _t("ID") }</div>
|
|
|
|
<div className="mx_DevicesPanel_deviceName">{ _t("Public Name") }</div>
|
2017-10-11 19:56:17 +03:00
|
|
|
<div className="mx_DevicesPanel_deviceLastSeen">{ _t("Last seen") }</div>
|
2017-11-28 18:23:09 +03:00
|
|
|
<div className="mx_DevicesPanel_deviceButtons">
|
2019-08-19 20:09:37 +03:00
|
|
|
{ this.state.selectedDevices.length > 0 ? deleteButton : null }
|
2017-11-28 18:23:09 +03:00
|
|
|
</div>
|
2016-08-01 15:42:29 +03:00
|
|
|
</div>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ devices.map(this._renderDevice) }
|
2016-08-01 15:42:29 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 03:55:51 +03:00
|
|
|
|
|
|
|
DevicesPanel.propTypes = {
|
2017-12-26 04:03:18 +03:00
|
|
|
className: PropTypes.string,
|
2016-09-15 03:55:51 +03:00
|
|
|
};
|