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

85.96% Statements 49/57
68.18% Branches 15/22
100% Functions 10/10
84.61% Lines 44/52

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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 2597x 7x 7x 7x 7x 7x 7x 7x         7x 7x 7x 7x 7x 7x           27x 27x   27x                                                   24x 33x 33x 21x     33x 33x   33x   33x   33x             33x                   33x                                           18x                       7x 34x               34x 34x 34x   34x     34x                                                       34x 34x                                                                             31x 34x 34x                         6x                 28x       28x                                                            
import { Tooltip } from "@mui/material";
import makeStyles from "@mui/styles/makeStyles";
import { CheckCircleIcon, ErrorIcon } from "components/Icons";
import LabelAndText from "components/LabelAndText";
import { useLanguages } from "features/profile/hooks/useLanguages";
import { responseRateKey } from "features/queryKeys";
import { useTranslation } from "i18n";
import { COMMUNITIES, GLOBAL, PROFILE } from "i18n/namespaces";
import {
  BirthdateVerificationStatus,
  GenderVerificationStatus,
  User,
} from "proto/api_pb";
import { useQuery } from "react-query";
import { service } from "service";
import { monthFormatter, timestamp2Date } from "utils/date";
import dayjs from "utils/dayjs";
import { hourMillis, timeAgoI18n } from "utils/timeAgo";
 
interface Props {
  user: User.AsObject;
}
 
export const ReferencesLastActiveLabels = ({ user }: Props) => {
  const { t } = useTranslation(PROFILE);
  //workaround for not being able to type timeAgoI18n properly
  const { t: tGlobal } = useTranslation(GLOBAL);
  return (
    <>
      <LabelAndText
        label={t("heading.references")}
        text={`${user.numReferences || 0}`}
      />
      <LabelAndText
        label={t("heading.last_active")}
        text={
          user.lastActive
            ? timeAgoI18n({
                input: timestamp2Date(user.lastActive),
                t: tGlobal,
                fuzzy: {
                  millis: hourMillis,
                  translationKey: "relative_time.less_than_one_hour_ago",
                },
              })
            : t("last_active_false")
        }
      />
    </>
  );
};
 
export const ResponseRateLabel = ({ user }: Props) => {
  const { t } = useTranslation("profile");
  const query = useQuery(responseRateKey(user.userId), () =>
    service.requests.getResponseRate(user.userId)
  );
 
  let rateText = undefined;
  let timeText = undefined;
 
  Iif (query?.data?.insufficientData) {
    rateText = t("response_rate_text_insufficient");
  } else Iif (query?.data?.low) {
    rateText = t("response_rate_text_low");
  } else Iif (query?.data?.some) {
    rateText = t("response_rate_text_some");
    timeText = t("response_time_text_some", {
      p33: dayjs
        .duration(query.data.some.responseTimeP33!.seconds, "second")
        .humanize(),
    });
  } else Iif (query?.data?.most) {
    rateText = t("response_rate_text_most");
    timeText = t("response_time_text_most", {
      p33: dayjs
        .duration(query.data.most.responseTimeP33!.seconds, "second")
        .humanize(),
      p66: dayjs
        .duration(query.data.most.responseTimeP66!.seconds, "second")
        .humanize(),
    });
  } else Iif (query?.data?.almostAll) {
    rateText = t("response_rate_text_almost_all");
    timeText = t("response_time_text_almost_all", {
      p33: dayjs
        .duration(query.data.almostAll.responseTimeP33!.seconds, "second")
        .humanize(),
      p66: dayjs
        .duration(query.data.almostAll.responseTimeP66!.seconds, "second")
        .humanize(),
    });
  }
 
  return (
    <>
      <LabelAndText label={t("response_rate_label")} text={rateText ?? ""} />
      {timeText && (
        <LabelAndText label={t("response_time_label")} text={timeText} />
      )}
    </>
  );
};
 
const useStyles = makeStyles((theme) => ({
  iconStyles: {
    margin: theme.spacing(0.5),
    alignSelf: "center",
  },
  container: {
    display: "flex",
    flexWrap: "wrap",
    alignItems: "center",
  },
}));
 
const AgeAndGenderRenderer = ({ user }: Props) => {
  const classes = useStyles();
 
  const {
    birthdateVerificationStatus,
    genderVerificationStatus,
    age,
    gender,
    pronouns,
  } = user;
  const { iconStyles } = useStyles();
  const { t } = useTranslation("profile");
 
  const getBirthdateVerificationIcon = (
    status: BirthdateVerificationStatus
  ) => {
    switch (status) {
      case BirthdateVerificationStatus.BIRTHDATE_VERIFICATION_STATUS_VERIFIED:
        return (
          <Tooltip title={t("heading.age_verification_verified")}>
            <CheckCircleIcon
              color="primary"
              data-testid="check-circle-icon"
              className={iconStyles}
              fontSize="inherit"
            />
          </Tooltip>
        );
      case BirthdateVerificationStatus.BIRTHDATE_VERIFICATION_STATUS_MISMATCH:
        return (
          <Tooltip title={t("heading.age_verification_mismatch")}>
            <ErrorIcon
              color="error"
              data-testid="error-icon"
              className={iconStyles}
              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")}>
            <CheckCircleIcon
              color="primary"
              data-testid="check-circle-icon"
              className={iconStyles}
              fontSize="inherit"
            />
          </Tooltip>
        );
      case GenderVerificationStatus.GENDER_VERIFICATION_STATUS_MISMATCH:
        return (
          <Tooltip title={t("heading.gender_verification_mismatch")}>
            <ErrorIcon
              color="error"
              data-testid="error-icon"
              className={iconStyles}
              fontSize="inherit"
            />
          </Tooltip>
        );
      default:
        return <>&nbsp;</>;
    }
  };
  return (
    <div className={classes.container}>
      <span>{age}</span>
      {getBirthdateVerificationIcon(birthdateVerificationStatus)}
      <span>/&nbsp;</span>
      <span>{gender}</span>
      {getGenderVerificationIcon(genderVerificationStatus)}
      {pronouns && <span>({pronouns.replace(/\s+/g, "")})</span>}
    </div>
  );
};
 
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
            ? monthFormatter(locale).format(timestamp2Date(user.joined))
            : ""
        }
      />
      <LabelAndText
        label={t("profile:heading.local_time")}
        text={dayjs().tz(user.timezone).format("LT")}
      />
    </>
  );
};