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                                                                                                                                                                                                         
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 { localizeDateOnly, localizeDateTime, localizeRelativeTime } from "i18n/datetimes";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { useTranslation } from "next-i18next";
import { ActiveSession } from "proto/account_pb";
import { service } from "service";
import { timestampToPlainDateTime } from "utils/date";
 
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 = localizeRelativeTime(session.lastSeen!, locale);
  const createdDisplay = localizeDateTime(timestampToPlainDateTime(session.created!), locale, {
    includeSeconds: true,
  });
  const expiryDisplay = localizeDateOnly(timestampToPlainDateTime(session.expiry!), locale);
  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" values={{ login_datetime: createdDisplay }} />
        </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 }}
            />
          }
        />
        <IconText
          icon={ClockIcon}
          text={
            <Trans t={t} i18nKey="auth:active_logins.last_activity" values={{ last_activity_ago: lastSeenDisplay }} />
          }
        />
        <IconText
          icon={CalendarIcon}
          text={<Trans t={t} i18nKey="auth:active_logins.expiry" values={{ expiry_datetime: expiryDisplay }} />}
        />
        <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>
  );
}