All files / src/components/ProfileMenu/modals/Tabs/SecurityTab TrustedDevices.tsx

30% Statements 6/20
8.33% Branches 1/12
12.5% Functions 1/8
30% Lines 6/20

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                                                          7x                                                     7x 432x   432x       432x 432x                                                                                                                                                                                                                                          
import { TrashBin } from "@assets/icons/RebrandedIcons";
import Popup from "@common/Popup";
import { Text } from "@common/Text";
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { Box, Divider, Skeleton, Stack } from "@mui/material";
import { palette } from "@palette";
import { LaptopIcon, DeviceMobileCameraIcon } from "@phosphor-icons/react";
import { formatDistanceToNow, fromUnixTime } from "date-fns";
import UAParser from "ua-parser-js";
import { useDeleteTrustedDevice } from "../../hooks/useDeleteTrustedDevice";
import { useListTrustedDevices } from "../../hooks/useListTrustedDevices";
 
type TDevices = Array<{
  id: number;
  userID: number;
  userAgent: string;
  ipAddress: string;
  createdAt: number;
  updatedAt: number;
}>;
 
function isMobile(os?: string) {
  if (os) {
    return ["iOS", "Android"].includes(os);
  }
 
  return false;
}
 
const DeleteTrustedDeviceModal = NiceModal.create(({ id }: { id: number }) => {
  const { handleDelete } = useDeleteTrustedDevice();
  const modal = useModal();
 
  return (
    <Popup
      open={true}
      title={
        (
          <Text fontSize="24px" sx={{ fontWeight: "normal" }}>
            Delete trusted device
          </Text>
        ) as any
      }
      onCancel={() => modal.remove()}
      onSubmit={() => {
        handleDelete(id);
        modal.remove();
      }}
      actionLabel="Delete"
    >
      Do you confirm that you want to delete this trusted device?
      <br /> This action is undoable.
    </Popup>
  );
});
 
const TrustedDevices = () => {
  const { data, isLoading } = useListTrustedDevices();
  // const { handleDelete } = useDeleteTrustedDevice();
  const handleDelete = (id: number) => {
    NiceModal.show(DeleteTrustedDeviceModal, { id: id as any });
  };
 
  Eif (isLoading) {
    return (
      <Stack gap={1} py={2}>
        <Stack direction="row" gap={3} alignItems="center">
          <Skeleton
            variant="circular"
            width={60}
            sx={{ minWidth: 60 }}
            height={60}
          />
          <Skeleton variant="rounded" width="100%" height={50} />
        </Stack>
      </Stack>
    );
  }
 
  return (
    <Stack gap={2}>
      <Text
        mt={2}
        sx={{ fontWeight: 350, fontSize: "18px", lineHeight: "21.6px" }}
      >
        Trusted Devices and Browsers
      </Text>
      {!!data.total === false ? (
        <Text sx={{ fontSize: "14px", color: "#8F8F8F" }}>
          No devices or browsers saved
        </Text>
      ) : (
        <Box>
          {(data.data as TDevices)?.map((device, index) => {
            const parser = new UAParser(device.userAgent);
            return (
              <>
                <Stack
                  direction="row"
                  key={index}
                  sx={{ mb: 2 }}
                  gap={4}
                  justifyContent="space-between"
                  alignItems="center"
                >
                  <Stack direction="row" gap={1} alignItems="center">
                    <Stack direction="row" gap={1} alignItems="center">
                      <Box
                        sx={{
                          height: "48px",
                          width: "48px",
                          backgroundColor: palette.neutral[10],
                          display: "flex",
                          borderRadius: "50%",
                          justifyContent: "center",
                          alignItems: "center",
                        }}
                      >
                        {isMobile(parser.getOS().name) ? (
                          <DeviceMobileCameraIcon
                            color={palette.neutral[70]}
                            size={32}
                          />
                        ) : (
                          <LaptopIcon color={palette.neutral[70]} size={32} />
                        )}
                      </Box>
                      <Box width="200px">
                        <Text sx={{ fontSize: "14px", fontWeight: 350 }}>
                          {parser.getBrowser().name}
                        </Text>
                        <Text
                          sx={{
                            fontSize: "12px",
                            fontWeight: 350,
                            color: "#8F8F8F",
                          }}
                        >
                          {device.ipAddress}
                        </Text>
                      </Box>
                    </Stack>
                    <Box
                      sx={{
                        color: "#8F8F8F",
                        fontWeight: 350,
                      }}
                    >
                      <Text sx={{ fontSize: "12px", fontWeight: 350 }}>
                        {parser.getOS().name +
                          " " +
                          (parser.getOS().version ?? "")}
                      </Text>
                      <Text sx={{ fontSize: "12px", fontWeight: 350 }}>
                        Last access:{" "}
                        {formatDistanceToNow(fromUnixTime(device.updatedAt), {
                          addSuffix: true,
                        })}
                      </Text>
                    </Box>
                  </Stack>
                  <span
                    style={{ cursor: "pointer" }}
                    onClick={() => handleDelete(device.id)}
                  >
                    <TrashBin fill="#D80D0D" />
                  </span>
                </Stack>
                {index < data.total - 1 && (
                  <Divider sx={{ marginBottom: "16px" }} />
                )}
              </>
            );
          })}
        </Box>
      )}
    </Stack>
  );
};
 
export default TrustedDevices;