2022-08-08 08:59:22 +02:00
|
|
|
/*
|
|
|
|
|
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 React, { Fragment } from "react";
|
2022-10-14 13:43:20 +02:00
|
|
|
import classNames from "classnames";
|
2022-08-08 08:59:22 +02:00
|
|
|
|
2022-08-11 23:39:38 +02:00
|
|
|
import { Icon as InactiveIcon } from "../../../../../res/img/element-icons/settings/inactive.svg";
|
2022-08-08 08:59:22 +02:00
|
|
|
import { _t } from "../../../../languageHandler";
|
|
|
|
|
import { formatDate, formatRelativeTime } from "../../../../DateUtils";
|
|
|
|
|
import Heading from "../../typography/Heading";
|
2022-08-16 16:05:10 +02:00
|
|
|
import { INACTIVE_DEVICE_AGE_DAYS, isDeviceInactive } from "./filter";
|
2022-10-05 13:41:01 +02:00
|
|
|
import { ExtendedDevice } from "./types";
|
|
|
|
|
import { DeviceTypeIcon } from "./DeviceTypeIcon";
|
2022-10-14 13:43:20 +02:00
|
|
|
import { preventDefaultWrapper } from "../../../../utils/NativeEventUtils";
|
2022-08-09 15:07:25 +02:00
|
|
|
export interface DeviceTileProps {
|
2022-10-05 13:41:01 +02:00
|
|
|
device: ExtendedDevice;
|
2022-09-30 09:07:50 +02:00
|
|
|
isSelected?: boolean;
|
2022-08-08 08:59:22 +02:00
|
|
|
children?: React.ReactNode;
|
2022-08-09 15:07:25 +02:00
|
|
|
onClick?: () => void;
|
2022-08-08 08:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:41:01 +02:00
|
|
|
const DeviceTileName: React.FC<{ device: ExtendedDevice }> = ({ device }) => {
|
2022-09-23 09:21:04 -04:00
|
|
|
return <Heading size="h4">{device.display_name || device.device_id}</Heading>;
|
2022-08-08 08:59:22 +02:00
|
|
|
};
|
|
|
|
|
|
2022-08-11 23:39:38 +02:00
|
|
|
const MS_DAY = 24 * 60 * 60 * 1000;
|
|
|
|
|
const MS_6_DAYS = 6 * MS_DAY;
|
2022-08-08 08:59:22 +02:00
|
|
|
const formatLastActivity = (timestamp: number, now = new Date().getTime()): string => {
|
|
|
|
|
// less than a week ago
|
|
|
|
|
if (timestamp + MS_6_DAYS >= now) {
|
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
// Tue 20:15
|
|
|
|
|
return formatDate(date);
|
|
|
|
|
}
|
|
|
|
|
return formatRelativeTime(new Date(timestamp));
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-05 13:41:01 +02:00
|
|
|
const getInactiveMetadata = (device: ExtendedDevice): { id: string; value: React.ReactNode } | undefined => {
|
2022-08-11 23:39:38 +02:00
|
|
|
const isInactive = isDeviceInactive(device);
|
|
|
|
|
|
|
|
|
|
if (!isInactive) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
id: "inactive",
|
|
|
|
|
value: (
|
|
|
|
|
<>
|
|
|
|
|
<InactiveIcon className="mx_DeviceTile_inactiveIcon" />
|
2022-08-16 16:05:10 +02:00
|
|
|
{_t("Inactive for %(inactiveAgeDays)s+ days", { inactiveAgeDays: INACTIVE_DEVICE_AGE_DAYS }) +
|
2022-08-11 23:39:38 +02:00
|
|
|
` (${formatLastActivity(device.last_seen_ts)})`}
|
|
|
|
|
</>
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const DeviceMetadata: React.FC<{ value: string | React.ReactNode; id: string }> = ({ value, id }) =>
|
2022-08-08 08:59:22 +02:00
|
|
|
value ? <span data-testid={`device-metadata-${id}`}>{value}</span> : null;
|
|
|
|
|
|
2022-09-30 09:07:50 +02:00
|
|
|
const DeviceTile: React.FC<DeviceTileProps> = ({ device, children, isSelected, onClick }) => {
|
2022-08-11 23:39:38 +02:00
|
|
|
const inactive = getInactiveMetadata(device);
|
2022-08-08 08:59:22 +02:00
|
|
|
const lastActivity = device.last_seen_ts && `${_t("Last activity")} ${formatLastActivity(device.last_seen_ts)}`;
|
2022-08-10 18:14:59 +02:00
|
|
|
const verificationStatus = device.isVerified ? _t("Verified") : _t("Unverified");
|
2022-08-11 23:39:38 +02:00
|
|
|
// if device is inactive, don't display last activity or verificationStatus
|
|
|
|
|
const metadata = inactive
|
|
|
|
|
? [inactive, { id: "lastSeenIp", value: device.last_seen_ip }]
|
|
|
|
|
: [
|
|
|
|
|
{ id: "isVerified", value: verificationStatus },
|
|
|
|
|
{ id: "lastActivity", value: lastActivity },
|
|
|
|
|
{ id: "lastSeenIp", value: device.last_seen_ip },
|
2022-09-23 09:21:04 -04:00
|
|
|
{ id: "deviceId", value: device.device_id },
|
2022-08-11 23:39:38 +02:00
|
|
|
];
|
2022-08-08 08:59:22 +02:00
|
|
|
|
2022-10-14 13:43:20 +02:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={classNames("mx_DeviceTile", { mx_DeviceTile_interactive: !!onClick })}
|
|
|
|
|
data-testid={`device-tile-${device.device_id}`}
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
>
|
2022-10-05 13:41:01 +02:00
|
|
|
<DeviceTypeIcon isVerified={device.isVerified} isSelected={isSelected} deviceType={device.deviceType} />
|
2022-10-14 13:43:20 +02:00
|
|
|
<div className="mx_DeviceTile_info">
|
2022-08-08 08:59:22 +02:00
|
|
|
<DeviceTileName device={device} />
|
|
|
|
|
<div className="mx_DeviceTile_metadata">
|
|
|
|
|
{metadata.map(({ id, value }, index) =>
|
2022-09-12 13:06:12 +02:00
|
|
|
!!value ? (
|
|
|
|
|
<Fragment key={id}>
|
|
|
|
|
{!!index && " · "}
|
|
|
|
|
<DeviceMetadata id={id} value={value} />
|
|
|
|
|
</Fragment>
|
|
|
|
|
) : null,
|
2022-08-08 08:59:22 +02:00
|
|
|
)}
|
2022-12-12 12:24:14 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2022-08-08 08:59:22 +02:00
|
|
|
<div className="mx_DeviceTile_actions" onClick={preventDefaultWrapper(() => {})}>
|
2022-12-12 12:24:14 +01:00
|
|
|
{children}
|
2022-08-08 08:59:22 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DeviceTile;
|