All files / app/features/search SearchResultsList.tsx

96.29% Statements 26/27
85% Branches 17/20
100% Functions 7/7
96.15% Lines 25/26

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 1972x 2x 2x 2x 2x 2x 2x 2x 2x             2x   6x                                                                                                                                                                       2x                           7x 7x 7x   7x   7x 3x 3x     7x 3x     7x       7x                                                                         3x           1x                                      
import { Hidden, makeStyles, Paper } from "@material-ui/core";
import Alert from "components/Alert";
import CircularProgress from "components/CircularProgress";
import HorizontalScroller from "components/HorizontalScroller";
import TextBody from "components/TextBody";
import SearchResult from "features/search/SearchResult";
import { useUser } from "features/userQueries/useUsers";
import { useTranslation } from "i18n";
import { SEARCH } from "i18n/namespaces";
import { User } from "proto/api_pb";
import { UserSearchRes } from "proto/search_pb";
import { Dispatch, SetStateAction } from "react";
import { InfiniteData } from "react-query";
import { GeocodeResult } from "utils/hooks";
 
import SearchBox from "./SearchBox";
 
const useStyles = makeStyles((theme) => ({
  mapResults: {
    height: "17rem",
    overflowY: "auto",
    backgroundColor: theme.palette.background.default,
    [theme.breakpoints.up("md")]: {
      height: "auto",
      width: "30rem",
      padding: theme.spacing(3),
    },
  },
  baseMargin: {
    margin: theme.spacing(2),
  },
  scroller: {
    marginTop: theme.spacing(3),
    [theme.breakpoints.down("sm")]: {
      marginTop: 0,
    },
  },
  singleResult: {
    maxWidth: "100%",
    [theme.breakpoints.up("md")]: {
      margin: theme.spacing(2),
    },
  },
  searchResult: {
    borderRadius: theme.shape.borderRadius * 2,
    backgroundColor: theme.palette.background.paper,
    boxShadow: "0 0 4px rgba(0,0,0,0.25)",
    marginBottom: theme.spacing(3),
    "&:last-child": {
      marginBottom: 0,
    },
    "& .MuiCardContent-root": {
      padding: theme.spacing(3),
    },
    [theme.breakpoints.down("sm")]: {
      padding: 0,
      overflow: "hidden",
      flexShrink: 0,
      width: "85vw",
      maxWidth: "33rem",
      height: "100%",
      margin: theme.spacing(0, 2, 0, 0),
      scrollSnapAlign: "start",
      "&:last-child": {
        marginRight: 0,
      },
      "& .MuiCardActionArea-root": {
        height: "100%",
      },
      "& .MuiCardContent-root": {
        height: "100%",
        padding: theme.spacing(2),
        overflow: "hidden",
        display: "flex",
        flexDirection: "column",
      },
    },
  },
}));
 
interface mapWrapperProps {
  isLoading: boolean;
  results: InfiniteData<UserSearchRes.AsObject> | undefined;
  error?: string | undefined;
  hasNext?: boolean | undefined;
  selectedResult:
    | Pick<User.AsObject, "username" | "userId" | "lng" | "lat">
    | undefined;
  setSelectedResult: Dispatch<
    SetStateAction<
      Pick<User.AsObject, "username" | "userId" | "lng" | "lat"> | undefined
    >
  >;
  searchType: "location" | "keyword";
  setSearchType: Dispatch<SetStateAction<"location" | "keyword">>;
  locationResult: GeocodeResult;
  setLocationResult: Dispatch<SetStateAction<GeocodeResult>>;
  setQueryName: Dispatch<SetStateAction<string>>;
  queryName: string;
}
 
export default function SearchResultsList({
  isLoading,
  results,
  error,
  hasNext,
  selectedResult,
  setSelectedResult,
  searchType,
  setSearchType,
  locationResult,
  setLocationResult,
  setQueryName,
  queryName,
}: mapWrapperProps) {
  const selectedUserData = useUser(selectedResult?.userId);
  const { t } = useTranslation(SEARCH);
  const classes = useStyles();
  const hasAtLeastOnePageResults =
    results && results?.pages[0]?.resultsList?.length !== 0;
 
  let resultsList = results?.pages
    .flatMap((page) => page.resultsList)
    .filter((result) => result.user);
 
  const wasResultFound =
    resultsList?.find(
      (value) => value.user?.userId === selectedResult?.userId
    ) !== undefined;
 
  Iif (!wasResultFound && selectedUserData.data) {
    resultsList = [{ user: selectedUserData.data, rank: 0, snippet: "" }];
  }
 
  return (
    <Paper className={classes.mapResults}>
      {error && <Alert severity="error">{error}</Alert>}
 
      <Hidden smDown>
        <SearchBox
          searchType={searchType}
          setSearchType={setSearchType}
          locationResult={locationResult}
          setLocationResult={setLocationResult}
          setQueryName={setQueryName}
          queryName={queryName}
        />
      </Hidden>
 
      <>
        {isLoading ||
          (selectedUserData.isLoading && (
            <CircularProgress className={classes.baseMargin} />
          ))}
 
        {!isLoading && !hasAtLeastOnePageResults && (
          <TextBody className={classes.baseMargin}>
            {t("search_result.no_user_result_message")}
          </TextBody>
        )}
 
        {!isLoading && hasAtLeastOnePageResults && (
          <HorizontalScroller
            breakpoint="sm"
            className={classes.scroller}
            isFetching={isLoading}
            // fetchNext={fetchNextPage} // TODO: disabled for now (until pagination)
            hasMore={hasNext}
          >
            {resultsList &&
              resultsList.map((result) => (
                <SearchResult
                  id={`search-result-${result.user!.userId}`}
                  className={classes.searchResult}
                  key={result.user!.userId}
                  user={result.user!}
                  onSelect={() => {
                    setSelectedResult({
                      username: result.user!.username,
                      userId: result.user!.userId,
                      lng: result.user!.lng,
                      lat: result.user!.lat,
                    });
                  }}
                  highlight={
                    selectedResult &&
                    selectedResult.userId === result.user!.userId
                  }
                />
              ))}
          </HorizontalScroller>
        )}
      </>
    </Paper>
  );
}