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

69.56% Statements 48/69
48.57% Branches 17/35
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 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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 3046x 6x 6x 6x 6x 6x         6x 6x 6x             6x 6x           28x       28x                                           6x                                                     28x       28x   28x   28x 28x   28x   28x   28x               28x                       28x                                               39x           6x     104x           6x 6x   6x             39x 39x   39x     39x                                                   39x 39x                                                                         30x 39x 39x                         8x                 30x       30x                                                                                    
import { styled, Tooltip } from "@mui/material";
import { CheckCircleIcon, ErrorIcon } from "components/Icons";
import LabelAndText from "components/LabelAndText";
import { useLanguages } from "features/profile/hooks/useLanguages";
import { useTranslation } from "i18n";
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 {
  localizeDateTime,
  localizeRelativeTime,
  localizeYearMonth,
  timestampToPlainDateTime,
  UTC_TIMEZONE,
} from "utils/date";
import dayjs, { i18nToDayjsLocale } from "utils/dayjs";
 
interface Props {
  user: User.AsObject;
}
 
export const ReferencesLastActiveLabels = ({ user }: Props) => {
  const {
    t,
    i18n: { language: locale },
  } = useTranslation(PROFILE);
  return (
    <>
      <LabelAndText
        label={t("heading.references")}
        text={`${user.numReferences || 0}`}
      />
      <LabelAndText
        label={t("heading.last_active")}
        text={
          user.lastActive
            ? localizeRelativeTime(user.lastActive, locale, {
                smallestUnit: "hours",
                t,
              })
            : 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 },
  } = useTranslation(PROFILE);
  // Localize the humanized durations at the call site (no global dayjs locale).
  const dayjsLocale = i18nToDayjsLocale(language);
 
  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: dayjs
        .duration(user.some.responseTimeP33!.seconds, "second")
        .locale(dayjsLocale)
        .humanize(),
    });
  } else Iif (user.most) {
    rateText = t("response_rate_text_most");
    timeText = t("response_time_text_most", {
      p33: dayjs
        .duration(user.most.responseTimeP33!.seconds, "second")
        .locale(dayjsLocale)
        .humanize(),
      p66: dayjs
        .duration(user.most.responseTimeP66!.seconds, "second")
        .locale(dayjsLocale)
        .humanize(),
    });
  } else Iif (user.almostAll) {
    rateText = t("response_rate_text_almost_all");
    timeText = t("response_time_text_almost_all", {
      p33: dayjs
        .duration(user.almostAll.responseTimeP33!.seconds, "second")
        .locale(dayjsLocale)
        .humanize(),
      p66: dayjs
        .duration(user.almostAll.responseTimeP66!.seconds, "second")
        .locale(dayjsLocale)
        .humanize(),
    });
  }
 
  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).toPlainDate(),
                {
                  locale,
                  capitalize: true,
                },
              )
            : ""
        }
      />
      <LabelAndText
        label={t("profile:heading.local_time")}
        text={localizeDateTime(
          Temporal.Now.plainDateTimeISO(user.timezone || UTC_TIMEZONE),
          {
            locale,
            includeDate: false,
          },
        )}
      />
    </>
  );
};