All files / app/features/auth/logins LoginCard.tsx

0% Statements 0/25
0% Branches 0/3
0% Functions 0/5
0% Lines 0/24

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                                                                                                                                                                                                                                                                                         
import {
  Card,
  CardActions,
  CardContent,
  styled,
  Typography,
} from "@mui/material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import {
  CalendarIcon,
  ClockIcon,
  InfoIcon,
  LocationIcon,
} from "components/Icons";
import IconText from "components/IconText";
import { activeLoginsKey } from "features/queryKeys";
import { Trans } from "i18n";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { useTranslation } from "next-i18next";
import { ActiveSession } from "proto/account_pb";
import { service } from "service";
import { dateFormatter, dateTimeFormatter, timestamp2Date } from "utils/date";
import { timeAgoI18n } from "utils/timeAgo";
 
const StyledCard = styled(Card)(({ theme }) => ({
  marginTop: theme.spacing(1),
  marginBottom: theme.spacing(1),
}));
 
export default function LoginsPage({
  session,
}: {
  session: ActiveSession.AsObject;
}) {
  const {
    t,
    i18n: { language: locale },
  } = useTranslation([GLOBAL, AUTH]);
 
  const lastSeenDisplay = timeAgoI18n({
    input: timestamp2Date(session.lastSeen!),
    t: t,
  });
  const createdDisplay = dateTimeFormatter(locale).format(
    timestamp2Date(session.created!),
  );
  const expiryDisplay = dateFormatter(locale).format(
    timestamp2Date(session.expiry!),
  );
  const queryClient = useQueryClient();
 
  const {
    error,
    isPending,
    mutate: logOutThisSession,
  } = useMutation({
    mutationFn: async () => {
      await service.account.logOutSession(session.created!);
    },
 
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: [activeLoginsKey],
      });
    },
  });
 
  return (
    <StyledCard>
      <CardContent>
        <Typography variant="h2">
          <Trans t={t} i18nKey="auth:active_logins.login_header">
            Login on {{ login_datetime: createdDisplay }}
          </Trans>
        </Typography>
        {error && <Alert severity="error">{error.message}</Alert>}
        <IconText
          icon={LocationIcon}
          text={
            <Trans
              t={t}
              i18nKey="auth:active_logins.location"
              values={{ approximate_location: session.approximateLocation }}
            >
              {`Near `}
              <strong>{session.approximateLocation}</strong>
            </Trans>
          }
        />
        <IconText
          icon={ClockIcon}
          text={
            <Trans
              t={t}
              i18nKey="auth:active_logins.last_activity"
              values={{ last_activity_ago: lastSeenDisplay }}
            >
              Last activity <strong>{lastSeenDisplay}</strong>
            </Trans>
          }
        />
        <IconText
          icon={CalendarIcon}
          text={
            <Trans
              t={t}
              i18nKey="auth:active_logins.expiry"
              values={{ expiry_datetime: expiryDisplay }}
            >
              {`Expires on `}
              <strong>{expiryDisplay}</strong>
            </Trans>
          }
        />
        <IconText
          icon={InfoIcon}
          text={
            <>
              {session.operatingSystem} / {session.browser} / {session.device}
            </>
          }
        />
        {session.isCurrentSession && (
          <Typography variant="body1">
            <strong>{t("auth:active_logins.current_session")}</strong>
          </Typography>
        )}
      </CardContent>
      {!session.isCurrentSession && (
        <CardActions>
          <Button onClick={() => logOutThisSession()} loading={isPending}>
            {t("auth:active_logins.log_out_of_session")}
          </Button>
        </CardActions>
      )}
    </StyledCard>
  );
}