All files / app/features/search SearchResult.tsx

100% Statements 21/21
100% Branches 1/1
100% Functions 3/3
100% Lines 20/20

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 1662x 2x 2x 2x 2x       2x       2x 2x 2x 2x   2x 2x 2x 2x   3x                                                                                                                                                                   2x             3x 3x 3x                                                                             1x                          
import { Card, CardContent, Hidden, Typography } from "@material-ui/core";
import classNames from "classnames";
import Button from "components/Button";
import { CouchIcon, LocationIcon } from "components/Icons";
import UserSummary from "components/UserSummary";
import {
  hostingStatusLabels,
  meetupStatusLabels,
} from "features/profile/constants";
import {
  AgeGenderLanguagesLabels,
  ReferencesLastActiveLabels,
} from "features/profile/view/userLabels";
import { aboutText } from "features/search/constants";
import { useTranslation } from "i18n";
import { GLOBAL, SEARCH } from "i18n/namespaces";
import { User } from "proto/api_pb";
import LinesEllipsis from "react-lines-ellipsis";
import makeStyles from "utils/makeStyles";
import { firstName } from "utils/names";
import stripMarkdown from "utils/stripMarkdown";
 
const useStyles = makeStyles((theme) => ({
  highlight: {
    borderColor: theme.palette.secondary.main,
    borderWidth: 2,
    borderStyle: "solid",
  },
  link: {
    "&:hover": {
      textDecoration: "none",
    },
    "& h2:hover": {
      textDecoration: "underline",
    },
  },
  about: {
    marginTop: theme.spacing(2),
    marginBottom: 0,
    ...theme.typography.body1,
    [theme.breakpoints.down("sm")]: {
      marginTop: theme.spacing(1),
      maxHeight: `calc(3 * ${theme.typography.body1.lineHeight} * ${theme.typography.body1.fontSize})`,
      overflow: "hidden",
    },
  },
  statusLabelWrapper: {
    display: "grid",
    gridTemplateColumns: "1fr 1fr",
    color: theme.palette.text.primary,
    "& > div": {
      display: "flex",
    },
    [theme.breakpoints.down("md")]: {
      "& > div": {
        display: "grid",
        gridTemplateColumns: "1.25rem 1fr",
        gridGap: theme.spacing(1),
        alignItems: "center",
      },
    },
    "& p": {
      wordBreak: "break-all",
    },
    [theme.breakpoints.down("sm")]: {
      "& p": {
        width: "100%",
        textOverflow: "ellipsis",
        whiteSpace: "nowrap",
        overflow: "hidden",
      },
    },
    "&:hover": {
      textDecoration: "none",
    },
  },
  statusIcon: {
    marginInlineEnd: theme.spacing(0.5),
  },
  mapButton: {
    display: "block",
    margin: "0 auto",
    maxWidth: "100%",
    marginTop: theme.spacing(2),
    [theme.breakpoints.down("sm")]: {
      marginTop: "0.5rem",
    },
    "& .MuiButton-label": {
      overflow: "hidden",
      whiteSpace: "nowrap",
      textOverflow: "ellipsis",
    },
  },
}));
 
interface SearchResultProps {
  className?: string;
  id?: string;
  user: User.AsObject;
  onSelect: (user: User.AsObject) => void;
  highlight?: boolean;
  key: number;
}
 
export default function SearchResult({
  className,
  id,
  user,
  onSelect,
  highlight = false,
}: SearchResultProps) {
  const { t } = useTranslation([GLOBAL, SEARCH]);
  const classes = useStyles();
  return (
    <Card
      id={id}
      className={classNames(className, { [classes.highlight]: highlight })}
    >
      <CardContent>
        <UserSummary user={user} titleIsLink nameOnly>
          <div className={classes.statusLabelWrapper}>
            <div>
              <CouchIcon className={classes.statusIcon} />
              <Typography display="inline" variant="body1" color="primary">
                {hostingStatusLabels(t)[user.hostingStatus]}
              </Typography>
            </div>
            <div>
              <LocationIcon className={classes.statusIcon} />
              <Typography display="inline" variant="body1">
                {meetupStatusLabels(t)[user.meetupStatus]}
              </Typography>
            </div>
          </div>
          <Typography noWrap>{user.city}</Typography>
        </UserSummary>
        <Hidden mdUp>
          <LinesEllipsis
            text={stripMarkdown(aboutText(user, t))}
            maxLine={3}
            component="p"
            className={classes.about}
          />
        </Hidden>
        <Hidden smDown>
          <Typography variant="body1" className={classes.about}>
            {stripMarkdown(aboutText(user, t))}
          </Typography>
          <AgeGenderLanguagesLabels user={user} />
          <ReferencesLastActiveLabels user={user} />
        </Hidden>
        <Button
          onClick={() => onSelect(user)}
          variant="outlined"
          className={classes.mapButton}
          size="small"
        >
          {t("search:search_result.show_user_button_label", {
            name: firstName(user.name),
          })}
        </Button>
      </CardContent>
    </Card>
  );
}