All files / app/features/profile/view userLabels.tsx

69.56% Statements 48/69
48.48% Branches 16/33
91.66% Functions 11/12
67.74% Lines 42/62

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 2227x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x           31x 31x                                   7x                                               31x       31x 31x 31x   31x   31x   31x         31x           31x                                                   45x           7x 120x           7x 7x   7x 45x 45x   45x 45x                                   45x 45x                                                         34x 45x 45x                 8x                 34x       34x                                      
import { styled, Tooltip } from "@mui/material";
import { CheckCircleIcon, ErrorIcon } from "components/Icons";
import LabelAndText from "components/LabelAndText";
import RelativeTime from "components/RelativeTime";
import { useLanguages } from "features/profile/hooks/useLanguages";
import { useTranslation } from "i18n";
import { localizeDuration, localizeYearMonth } from "i18n/datetimes";
import { COMMUNITIES, GLOBAL, PROFILE } from "i18n/namespaces";
import { BirthdateVerificationStatus, GenderVerificationStatus, User } from "proto/api_pb";
import { Temporal } from "temporal-polyfill";
import { theme } from "theme";
import { timestampToPlainDateTime } from "utils/date";
 
interface Props {
  user: User.AsObject;
}
 
export const ReferencesLastActiveLabels = ({ user }: Props) => {
  const { t } = useTranslation(PROFILE);
  return (
    <>
      <LabelAndText label={t("heading.references")} text={`${user.numReferences || 0}`} />
      <LabelAndText
        label={t("heading.last_active")}
        text={
          user.lastActive ? (
            <RelativeTime instant={user.lastActive} smallestUnit="hours" capitalize={true} />
          ) : (
            t("last_active_false")
          )
        }
      />
    </>
  );
};
 
export const ResponseRateText = ({
  user,
}: {
  user: Pick<User.AsObject, "insufficientData" | "low" | "some" | "most" | "almostAll">;
}) => {
  const { t } = useTranslation([PROFILE]);
 
  let rateText = undefined;
 
  if (user.insufficientData) {
    rateText = t("response_rate_text_insufficient");
  } else if (user.low) {
    rateText = t("response_rate_text_low");
  } else if (user.some) {
    rateText = t("response_rate_text_some");
  } else if (user.most) {
    rateText = t("response_rate_text_most");
  } else if (user.almostAll) {
    rateText = t("response_rate_text_almost_all");
  }
 
  return <>{rateText}</>;
};
 
export const ResponseRateLabel = ({ user }: Props) => {
  const {
    t,
    i18n: { language: locale },
  } = useTranslation(PROFILE);
  let rateText = undefined;
  let timeText = undefined;
 
  Iif (user.insufficientData) {
    rateText = t("response_rate_text_insufficient");
  } else Iif (user.low) {
    rateText = t("response_rate_text_low");
  } else Iif (user.some) {
    rateText = t("response_rate_text_some");
    timeText = t("response_time_text_some", {
      p33: localizeDuration(Temporal.Duration.from({ seconds: user.some.responseTimeP33!.seconds }), locale),
    });
  } else Iif (user.most) {
    rateText = t("response_rate_text_most");
    timeText = t("response_time_text_most", {
      p33: localizeDuration(Temporal.Duration.from({ seconds: user.most.responseTimeP33!.seconds }), locale),
      p66: localizeDuration(Temporal.Duration.from({ seconds: user.most.responseTimeP66!.seconds }), locale),
    });
  } else Iif (user.almostAll) {
    rateText = t("response_rate_text_almost_all");
    timeText = t("response_time_text_almost_all", {
      p33: localizeDuration(
        Temporal.Duration.from({
          seconds: user.almostAll.responseTimeP33!.seconds,
        }),
        locale,
      ),
      p66: localizeDuration(
        Temporal.Duration.from({
          seconds: user.almostAll.responseTimeP66!.seconds,
        }),
        locale,
      ),
    });
  }
 
  return (
    <>
      <LabelAndText label={t("response_rate_label")} text={rateText ?? ""} />
      {timeText && <LabelAndText label={t("response_time_label")} text={timeText} />}
    </>
  );
};
 
const StyledContainer = styled("div")(() => ({
  display: "flex",
  flexWrap: "wrap",
  alignItems: "center",
}));
 
const styledIcon = <C extends React.ComponentType<React.ComponentProps<C>>>(component: C) => {
  return styled(component)(() => ({
    margin: theme.spacing(0.5),
    alignSelf: "center",
  }));
};
 
const StyledCheckCircleIcon = styledIcon(CheckCircleIcon);
const StyledErrorIcon = styledIcon(ErrorIcon);
 
const AgeAndGenderRenderer = ({ user }: Props) => {
  const { birthdateVerificationStatus, genderVerificationStatus, age, gender, pronouns } = user;
  const { t } = useTranslation(PROFILE);
 
  const getBirthdateVerificationIcon = (status: BirthdateVerificationStatus) => {
    switch (status) {
      case BirthdateVerificationStatus.BIRTHDATE_VERIFICATION_STATUS_VERIFIED:
        return (
          <Tooltip title={t("heading.age_verification_verified")}>
            <StyledCheckCircleIcon color="primary" data-testid="check-circle-icon" fontSize="inherit" />
          </Tooltip>
        );
      case BirthdateVerificationStatus.BIRTHDATE_VERIFICATION_STATUS_MISMATCH:
        return (
          <Tooltip title={t("heading.age_verification_mismatch")}>
            <StyledErrorIcon color="error" data-testid="error-icon" fontSize="inherit" />
          </Tooltip>
        );
      default:
        return <>&nbsp;</>;
    }
  };
 
  const getGenderVerificationIcon = (status: GenderVerificationStatus) => {
    switch (status) {
      case GenderVerificationStatus.GENDER_VERIFICATION_STATUS_VERIFIED:
        return (
          <Tooltip title={t("heading.gender_verification_verified")}>
            <StyledCheckCircleIcon color="primary" data-testid="check-circle-icon" fontSize="inherit" />
          </Tooltip>
        );
      case GenderVerificationStatus.GENDER_VERIFICATION_STATUS_MISMATCH:
        return (
          <Tooltip title={t("heading.gender_verification_mismatch")}>
            <StyledErrorIcon color="error" data-testid="error-icon" fontSize="inherit" />
          </Tooltip>
        );
      default:
        return <>&nbsp;</>;
    }
  };
  return (
    <StyledContainer>
      <span>{age}</span>
      {getBirthdateVerificationIcon(birthdateVerificationStatus)}
      <span>/&nbsp;</span>
      <span>{gender}</span>
      {getGenderVerificationIcon(genderVerificationStatus)}
      {pronouns && <span>({pronouns.replace(/\s+/g, "")})</span>}
    </StyledContainer>
  );
};
 
export const AgeGenderLanguagesLabels = ({ user }: Props) => {
  const { t } = useTranslation(PROFILE);
  const { languages } = useLanguages();
 
  return (
    <>
      <LabelAndText label={t("heading.age_gender")} text={<AgeAndGenderRenderer user={user} />} />
      {languages && (
        <LabelAndText
          label={t("heading.languages_fluent")}
          text={
            user.languageAbilitiesList.map((ability) => languages[ability.code]).join(", ") ||
            t("languages_fluent_false")
          }
        />
      )}
    </>
  );
};
 
export const RemainingAboutLabels = ({ user }: Props) => {
  const {
    t,
    i18n: { language: locale },
  } = useTranslation([GLOBAL, COMMUNITIES, PROFILE]);
  return (
    <>
      <LabelAndText label={t("profile:heading.hometown")} text={user.hometown} />
      <LabelAndText label={t("profile:heading.occupation")} text={user.occupation} />
      <LabelAndText label={t("profile:heading.education")} text={user.education} />
      <LabelAndText
        label={t("profile:heading.joined")}
        text={
          user.joined
            ? localizeYearMonth(timestampToPlainDateTime(user.joined), locale, {
                capitalize: true,
              })
            : ""
        }
      />
    </>
  );
};