Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 5x 5x 4x 4x 38x 38x 38x 38x 38x 5x 60x 5x 5x 60x 60x 60x 60x 60x 1x 1x 1x | import { Box, Stack } from "@mui/material";
import Header from "./Header";
import { TDevice, TDevices } from "./security.types";
import { UAParser } from "ua-parser-js";
import { styled, useAppTheme } from "@theme/v2/Provider";
import {
DeviceMobileCameraIcon,
DevicesIcon,
LaptopIcon,
TrashIcon,
} from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import { formatDistanceToNow, fromUnixTime } from "date-fns";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import NiceModal from "@ebay/nice-modal-react";
import { GIVE_CONFIRMATION_POP_UP } from "modals/modal_names";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { useDeleteTrustedDevice } from "@components/ProfileMenu/modals/hooks/useDeleteTrustedDevice";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import GiveButton from "@shared/Button/GiveButton";
import { useMemo } from "react";
import { useFingerprint } from "@hooks/common/useFingerprint";
export default function TrustedDevices({
devicesData,
}: {
devicesData?: TDevices;
}) {
const { palette } = useAppTheme();
const { isMobileView } = useCustomThemeV2();
const { fingerprint } = useFingerprint();
const areDevices = devicesData && devicesData.length > 0;
const isOnlyCurrentDevice =
devicesData &&
devicesData.length === 1 &&
devicesData[0].fingerprintToken === fingerprint;
const { handleDelete: handleDeleteDevice } = useDeleteTrustedDevice();
const handleDelete = (id: number) => {
const device = devicesData?.find((d) => d?.id === id);
const parser = new UAParser(device?.userAgent);
const locationInfo = device?.timezone
? `${device?.timezone} - ${device.ipAddress}`
: device?.ipAddress;
NiceModal.show(GIVE_CONFIRMATION_POP_UP, {
modalType: "delete",
title: "Remove Trusted Device",
description:
"Are you sure you want to delete this device? This action is irreversible.",
customSubmitBtnText: "Yes, Remove",
actions: {
handleSuccess: {
onClick: () => handleDeleteDevice(id),
},
},
inputNodes: [
{
element: (
<RemovedTrustedDeviceContainer>
<DevicesIcon size={24} fill={palette?.icon?.["icon-primary"]} />
<Stack spacing="6px">
<GiveText variant="bodyS" fontWeight="500">
{parser.getBrowser().name}
</GiveText>
<GiveText variant="bodyS">{locationInfo}</GiveText>
</Stack>
</RemovedTrustedDeviceContainer>
),
},
],
});
};
const handleDeleteAllDevices = () => {
NiceModal.show(GIVE_CONFIRMATION_POP_UP, {
modalType: "delete",
title: "Remove All Trusted Devices",
description:
"Are you sure you want to remove all trusted devices? This action cannot be undone. You will remain logged in on this device, and any new device will require your approval to access your account.",
customSubmitBtnText: "Yes, Remove",
actions: {
handleSuccess: {
onClick: () => handleDeleteDevice(undefined, fingerprint),
},
},
});
};
const sortedDevices = useMemo(() => {
Iif (!devicesData) {
return [];
}
return devicesData.sort((a, b) => {
const aMatches = a.fingerprintToken === fingerprint;
const bMatches = b.fingerprintToken === fingerprint;
Iif (aMatches && !bMatches) {
return -1;
}
Iif (!aMatches && bMatches) {
return 1;
}
return 0;
});
}, [devicesData, fingerprint]);
return (
<Stack sx={{ pt: "24px" }} gap={areDevices ? "4px" : "8px"}>
<Header
title={
<Title
areDevices={areDevices && !isOnlyCurrentDevice}
onClick={handleDeleteAllDevices}
/>
}
isEmpty={!areDevices}
/>
<Box>
{sortedDevices?.map((device, index) => (
<DeviceItem
device={device}
key={index}
handleDelete={handleDelete}
isCurrent={device.fingerprintToken === fingerprint}
/>
))}
</Box>
{isMobileView && areDevices && !isOnlyCurrentDevice && (
<GiveButton
variant="outline"
label="Remove All Devices"
color="destructive"
onClick={handleDeleteAllDevices}
fullWidth
sx={{ borderRadius: "8px", mt: "12px" }}
/>
)}
</Stack>
);
}
function Title({
onClick,
areDevices,
}: {
onClick?: () => void;
areDevices?: boolean;
}) {
const { isMobileView } = useCustomThemeV2();
return (
<Stack direction="row" alignItems="center" justifyContent="space-between">
<GiveText variant="h5">Trusted Devices and Browsers</GiveText>
{!isMobileView && areDevices && (
<GiveButton
variant="outline"
label="Remove All Devices"
color="destructive"
onClick={onClick}
sx={{ borderRadius: "8px" }}
/>
)}
</Stack>
);
}
function DeviceItem({
device,
handleDelete,
isCurrent,
}: {
device: TDevice;
handleDelete: (id: number) => void;
isCurrent?: boolean;
}) {
const { palette } = useAppTheme();
const parser = new UAParser(device.userAgent);
const locationInfo = device.timezone
? `${device.timezone} - ${device.ipAddress}`
: device.ipAddress;
const currentInfo = isCurrent ? "This device - " : "";
return (
<Stack
direction="row"
justifyContent="space-between"
alignItems="center"
gap="12px"
sx={{
borderBottom: `1px solid ${palette.border?.primary}`,
}}
>
<Stack
direction="row"
gap="12px"
alignItems="center"
p="16px 0px"
width="45%"
>
<Box
sx={{
height: "40px",
width: "40px",
borderRadius: "30px",
alignItems: "center",
justifyContent: "center",
display: "flex",
backgroundColor: palette.primitive?.transparent["darken-5"],
}}
>
{parser.getDevice().type === "mobile" ? (
<DeviceMobileCameraIcon
color={palette.icon?.["icon-primary"]}
size={20}
/>
) : (
<LaptopIcon color={palette.icon?.["icon-primary"]} size={20} />
)}
</Box>
<Stack gap="2px" width="80%">
<GiveText variant="bodyS">{parser.getBrowser().name}</GiveText>
<GiveTooltip
title={device.ipAddress}
color="default"
sx={{
alignItems: "flex-start",
}}
>
<GiveTruncateText variant="bodyXS" color="secondary" lineClamp={2}>
{currentInfo}
{locationInfo}
</GiveTruncateText>
</GiveTooltip>
</Stack>
</Stack>
<Stack gap="2px" flex={1}>
<GiveText variant="bodyS">
{parser.getOS().name} {parser.getOS().version}
</GiveText>
<StyledTruncateText variant="bodyXS" color="secondary" lineClamp={1}>
Last access:{" "}
{device.updatedAt &&
formatDistanceToNow(fromUnixTime(device.updatedAt), {
addSuffix: true,
})}
</StyledTruncateText>
</Stack>
{!isCurrent && (
<GiveIconButton
Icon={TrashIcon}
color={palette.primitive?.error[50]}
size="medium"
variant="ghost"
onClick={() => handleDelete(device.id)}
data-testid="remove-button"
/>
)}
</Stack>
);
}
const StyledTruncateText = styled(GiveTruncateText)({
wordBreak: "break-all",
});
const RemovedTrustedDeviceContainer = styled(Box)(({ theme }) => ({
display: "flex",
gap: "12px",
borderRadius: "8px",
padding: "12px",
background: theme?.palette.primitive?.transparent["darken-5"],
}));
|