Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Device manager - data fetching (PSG-637) #9151

Merged
merged 8 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/views/settings/DevicesPanelEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,25 @@ export default class DevicesPanelEntry extends React.Component<IProps, IState> {
</AccessibleButton>
</React.Fragment>;

const deviceWithVerification = {
...this.props.device,
isVerified: this.props.verified,
};

if (this.props.isOwnDevice) {
return <div className={"mx_DevicesPanel_device" + myDeviceClass}>
<div className="mx_DevicesPanel_deviceTrust">
<span className={"mx_DevicesPanel_icon mx_E2EIcon " + iconClass} />
</div>
<DeviceTile device={this.props.device}>
<DeviceTile device={deviceWithVerification}>
{ buttons }
</DeviceTile>
</div>;
}

return (
<div className={"mx_DevicesPanel_device" + myDeviceClass}>
<SelectableDeviceTile device={this.props.device} onClick={this.onDeviceToggled} isSelected={this.props.selected}>
<SelectableDeviceTile device={deviceWithVerification} onClick={this.onDeviceToggled} isSelected={this.props.selected}>
{ buttons }
</SelectableDeviceTile>
</div>
Expand Down
10 changes: 6 additions & 4 deletions src/components/views/settings/devices/DeviceTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ limitations under the License.
*/

import React, { Fragment } from "react";
import { IMyDevice } from "matrix-js-sdk/src/matrix";

import { _t } from "../../../../languageHandler";
import { formatDate, formatRelativeTime } from "../../../../DateUtils";
import TooltipTarget from "../../elements/TooltipTarget";
import { Alignment } from "../../elements/Tooltip";
import Heading from "../../typography/Heading";
import { DeviceWithVerification } from "./useOwnDevices";

export interface DeviceTileProps {
device: IMyDevice;
device: DeviceWithVerification;
children?: React.ReactNode;
onClick?: () => void;
}

const DeviceTileName: React.FC<{ device: IMyDevice }> = ({ device }) => {
const DeviceTileName: React.FC<{ device: DeviceWithVerification }> = ({ device }) => {
if (device.display_name) {
return <TooltipTarget
alignment={Alignment.Top}
Expand Down Expand Up @@ -62,12 +62,14 @@ const DeviceMetadata: React.FC<{ value: string, id: string }> = ({ value, id })

const DeviceTile: React.FC<DeviceTileProps> = ({ device, children, onClick }) => {
const lastActivity = device.last_seen_ts && `${_t('Last activity')} ${formatLastActivity(device.last_seen_ts)}`;
const verificationStatus = device.isVerified ? _t('Verified') : _t('Unverified');
const metadata = [
{ id: 'isVerified', value: verificationStatus },
{ id: 'lastActivity', value: lastActivity },
{ id: 'lastSeenIp', value: device.last_seen_ip },
];

return <div className="mx_DeviceTile">
return <div className="mx_DeviceTile" data-testid={`device-tile-${device.device_id}`}>
<div className="mx_DeviceTile_info" onClick={onClick}>
<DeviceTileName device={device} />
<div className="mx_DeviceTile_metadata">
Expand Down
105 changes: 105 additions & 0 deletions src/components/views/settings/devices/useOwnDevices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2022 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 { useContext, useEffect, useState } from "react";
import { IMyDevice, MatrixClient } from "matrix-js-sdk/src/matrix";
import { CrossSigningInfo } from "matrix-js-sdk/src/crypto/CrossSigning";
import { logger } from "matrix-js-sdk/src/logger";

import MatrixClientContext from "../../../../contexts/MatrixClientContext";

export type DeviceWithVerification = IMyDevice & { isVerified: boolean | null };

const isDeviceVerified = (
matrixClient: MatrixClient,
crossSigningInfo: CrossSigningInfo,
device: IMyDevice,
): boolean | null => {
try {
const deviceInfo = matrixClient.getStoredDevice(matrixClient.getUserId(), device.device_id);
return crossSigningInfo.checkDeviceTrust(
crossSigningInfo,
deviceInfo,
false,
true,
).isCrossSigningVerified();
} catch (error) {
logger.error("Error getting device cross-signing info", error);
return null;
}
};

const fetchDevicesWithVerification = async (matrixClient: MatrixClient): Promise<DevicesState['devices']> => {
const { devices } = await matrixClient.getDevices();
const crossSigningInfo = matrixClient.getStoredCrossSigningForUser(matrixClient.getUserId());

const devicesDict = devices.reduce((acc, device: IMyDevice) => ({
...acc,
[device.device_id]: {
...device,
isVerified: isDeviceVerified(matrixClient, crossSigningInfo, device),
},
}), {});

return devicesDict;
};
export enum OwnDevicesError {
Unsupported = 'Unsupported',
Default = 'Default',
}
type DevicesState = {
devices: Record<DeviceWithVerification['device_id'], DeviceWithVerification>;
currentDeviceId: string;
isLoading: boolean;
error?: OwnDevicesError;
};
export const useOwnDevices = (): DevicesState => {
const matrixClient = useContext(MatrixClientContext);

const currentDeviceId = matrixClient.getDeviceId();

const [devices, setDevices] = useState<DevicesState['devices']>({});
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<OwnDevicesError>();

useEffect(() => {
const getDevicesAsync = async () => {
setIsLoading(true);
try {
const devices = await fetchDevicesWithVerification(matrixClient);
setDevices(devices);
setIsLoading(false);
} catch (error) {
if (error.httpStatus == 404) {
// 404 probably means the HS doesn't yet support the API.
setError(OwnDevicesError.Unsupported);
} else {
logger.error("Error loading sessions:", error);
setError(OwnDevicesError.Default);
}
setIsLoading(false);
}
};
getDevicesAsync();
}, [matrixClient]);

return {
devices,
currentDeviceId,
isLoading,
error,
};
};
8 changes: 4 additions & 4 deletions src/components/views/settings/shared/SettingsSubsection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React from "react";
import React, { HTMLAttributes } from "react";

import Heading from "../../typography/Heading";

export interface SettingsSubsectionProps {
export interface SettingsSubsectionProps extends HTMLAttributes<HTMLDivElement> {
heading: string;
description?: string | React.ReactNode;
children?: React.ReactNode;
}

const SettingsSubsection: React.FC<SettingsSubsectionProps> = ({ heading, description, children }) => (
<div className="mx_SettingsSubsection">
const SettingsSubsection: React.FC<SettingsSubsectionProps> = ({ heading, description, children, ...rest }) => (
<div {...rest} className="mx_SettingsSubsection">
<Heading className="mx_SettingsSubsection_heading" size='h3'>{ heading }</Heading>
{ !!description && <div className="mx_SettingsSubsection_description">{ description }</div> }
<div className="mx_SettingsSubsection_content">
Expand Down
16 changes: 13 additions & 3 deletions src/components/views/settings/tabs/user/SessionManagerTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ limitations under the License.
import React from 'react';

import { _t } from "../../../../../languageHandler";
import Spinner from '../../../elements/Spinner';
import { useOwnDevices } from '../../devices/useOwnDevices';
import DeviceTile from '../../devices/DeviceTile';
import SettingsSubsection from '../../shared/SettingsSubsection';
import SettingsTab from '../SettingsTab';

const SessionManagerTab: React.FC = () => {
const { devices, currentDeviceId, isLoading } = useOwnDevices();

const currentDevice = devices[currentDeviceId];
return <SettingsTab heading={_t('Sessions')}>
<SettingsSubsection
heading={_t('Current session')}
// TODO session content coming here
// in next PR
/>
data-testid='current-session-section'
>
{ isLoading && <Spinner /> }
{ !!currentDevice && <DeviceTile
device={currentDevice}
/> }
</SettingsSubsection>
</SettingsTab>;
};

Expand Down
2 changes: 2 additions & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,8 @@
"Verification code": "Verification code",
"Discovery options will appear once you have added a phone number above.": "Discovery options will appear once you have added a phone number above.",
"Last activity": "Last activity",
"Verified": "Verified",
"Unverified": "Unverified",
"Unable to remove contact information": "Unable to remove contact information",
"Remove %(email)s?": "Remove %(email)s?",
"Invalid Email Address": "Invalid Email Address",
Expand Down
6 changes: 6 additions & 0 deletions test/components/views/settings/devices/DeviceTile-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('<DeviceTile />', () => {
const defaultProps = {
device: {
device_id: '123',
isVerified: false,
},
};
const getComponent = (props = {}) => (
Expand All @@ -43,6 +44,11 @@ describe('<DeviceTile />', () => {
expect(container).toMatchSnapshot();
});

it('renders a verified device with no metadata', () => {
const { container } = render(getComponent());
expect(container).toMatchSnapshot();
});

it('renders display name with a tooltip', () => {
const device: IMyDevice = {
device_id: '123',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('<SelectableDeviceTile />', () => {
display_name: 'My Device',
device_id: 'my-device',
last_seen_ip: '123.456.789',
isVerified: false,
};
const defaultProps = {
onClick: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`<DeviceTile /> renders a device with no metadata 1`] = `
<div>
<div
class="mx_DeviceTile"
data-testid="device-tile-123"
>
<div
class="mx_DeviceTile_info"
Expand All @@ -16,6 +17,45 @@ exports[`<DeviceTile /> renders a device with no metadata 1`] = `
<div
class="mx_DeviceTile_metadata"
>
<span
data-testid="device-metadata-isVerified"
>
Unverified
</span>
·
·
</div>
</div>
<div
class="mx_DeviceTile_actions"
/>
</div>
</div>
`;

exports[`<DeviceTile /> renders a verified device with no metadata 1`] = `
<div>
<div
class="mx_DeviceTile"
data-testid="device-tile-123"
>
<div
class="mx_DeviceTile_info"
>
<h4
class="mx_Heading_h4"
>
123
</h4>
<div
class="mx_DeviceTile_metadata"
>
<span
data-testid="device-metadata-isVerified"
>
Unverified
</span>
·
·
</div>
</div>
Expand All @@ -30,6 +70,7 @@ exports[`<DeviceTile /> renders display name with a tooltip 1`] = `
<div>
<div
class="mx_DeviceTile"
data-testid="device-tile-123"
>
<div
class="mx_DeviceTile_info"
Expand All @@ -46,6 +87,12 @@ exports[`<DeviceTile /> renders display name with a tooltip 1`] = `
<div
class="mx_DeviceTile_metadata"
>
<span
data-testid="device-metadata-isVerified"
>
Unverified
</span>
·
·
</div>
</div>
Expand All @@ -60,6 +107,7 @@ exports[`<DeviceTile /> separates metadata with a dot 1`] = `
<div>
<div
class="mx_DeviceTile"
data-testid="device-tile-123"
>
<div
class="mx_DeviceTile_info"
Expand All @@ -72,6 +120,12 @@ exports[`<DeviceTile /> separates metadata with a dot 1`] = `
<div
class="mx_DeviceTile_metadata"
>
<span
data-testid="device-metadata-isVerified"
>
Unverified
</span>
·
<span
data-testid="device-metadata-lastActivity"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ exports[`<SelectableDeviceTile /> renders unselected device tile with checkbox 1
</span>
<div
class="mx_DeviceTile"
data-testid="device-tile-my-device"
>
<div
class="mx_DeviceTile_info"
Expand All @@ -50,6 +51,12 @@ exports[`<SelectableDeviceTile /> renders unselected device tile with checkbox 1
<div
class="mx_DeviceTile_metadata"
>
<span
data-testid="device-metadata-isVerified"
>
Unverified
</span>
·
·
<span
data-testid="device-metadata-lastSeenIp"
Expand Down
Loading