All files / client/src/components FirstLogin.tsx

97.05% Statements 33/34
100% Branches 12/12
100% Functions 8/8
96.66% Lines 29/30

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                      520x 520x   520x 421x   421x 421x 304x     117x     520x 953x 2717x 2717x 2717x 2717x 953x   953x   953x 611x 190x     421x 421x   421x   304x 304x   117x       953x 712x                   3x                     1x      
import { ReactElement, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { z } from "zod";
import { browserStorageEventPrefix } from "shared/constants/browserStorage";
import { formatSerial } from "shared/utils/formatSerial";
import { DismissibleBanner } from "client/components/DismissibleBanner";
import { HighlightStyle } from "client/components/componentStyles";
import { isAdminOrHelper } from "client/utils/checkUserGroup";
import { useAppSelector } from "client/utils/hooks";
 
const firstLoginValue = "firstLogin";
const FirstLoginValueSchema = z.literal(firstLoginValue);
 
const getFirstLoginState = (key: string): typeof firstLoginValue | null => {
  const serializedValue = localStorage.getItem(key);
 
  const result = FirstLoginValueSchema.safeParse(serializedValue);
  if (!result.success) {
    return null;
  }
 
  return result.data;
};
 
export const FirstLogin = (): ReactElement | null => {
  const { t } = useTranslation();
  const serial = useAppSelector((state) => state.login.serial);
  const username = useAppSelector((state) => state.login.username);
  const kompassiId = useAppSelector((state) => state.login.kompassiId);
  const userGroup = useAppSelector((state) => state.login.userGroup);
  const isLocalLogin = !kompassiId;
 
  const [isFirstLogin, setIsFirstLogin] = useState<boolean>(false);
 
  useEffect(() => {
    if (!username) {
      return;
    }
 
    const firstLoginKey = `${browserStorageEventPrefix}-firstLogin-${username}`;
    const firstLogin = getFirstLoginState(firstLoginKey);
 
    if (firstLogin === null) {
      // eslint-disable-next-line react-hooks/set-state-in-effect
      setIsFirstLogin(true);
      localStorage.setItem(firstLoginKey, firstLoginValue);
    } else {
      setIsFirstLogin(false);
    }
  }, [username]);
 
  if (!isFirstLogin || !serial || !isLocalLogin || isAdminOrHelper(userGroup)) {
    return null;
  }
 
  return (
    <DismissibleBanner
      data-testid="first-login-notice"
      icon="circle-exclamation"
      highlightStyle={HighlightStyle.INFO}
      dismissAriaLabel={t("iconAltText.closeFirstLoginNotice")}
      onDismiss={() => {
        setIsFirstLogin(false);
      }}
    >
      <Message>
        {t("firstLogin.serial")} <b>{formatSerial(serial)}</b>.{" "}
        {t("firstLogin.info")}
      </Message>
    </DismissibleBanner>
  );
};
 
const Message = styled.div`
  flex: 1;
`;