All files / app/features/search SearchControls.tsx

0% Statements 0/30
0% Branches 0/12
0% Functions 0/9
0% Lines 0/28

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                                                                                                                                                                                                                                                                   
import { styled, useMediaQuery } from "@mui/material";
import { useState } from "react";
import { LngLatLike, MapRef } from "react-map-gl/maplibre";
import { theme } from "theme";
 
import FilterDialog from "./FilterDialog";
import FloatingSearchControls from "./FloatingSearchControls";
import MapViewToggle from "./MapViewToggle";
import SearchTypeRadioGroup from "./SearchTypeRadioGroup";
import { useSearchFilters } from "./state/useSearchFilters";
import { MapSearchTypes, MapViewOptions, MapViews } from "./utils/constants";
 
interface SearchControlsProps {
  drawerWidth: number;
  mapView: MapViewOptions;
  mapRef: React.RefObject<MapRef>;
  onSetMapView: (view: MapViewOptions) => void;
  onZoomIn: (newZoom: number, center?: LngLatLike) => void;
}
 
const MapControlsWrapper = styled("div", {
  shouldForwardProp: (prop) =>
    prop !== "isDualView" && prop !== "drawerWidth" && prop !== "isMobile",
})<{ drawerWidth: number; isDualView: boolean; isMobile: boolean }>(
  ({ theme, drawerWidth, isDualView, isMobile }) => ({
    display: "flex",
    alignItems: "center",
    width: "100%",
    marginTop: theme.spacing(2),
    flexDirection: "column",
    gap: theme.spacing(2),
 
    ...(!isMobile && {
      position: "absolute",
      top: theme.spacing(8),
      zIndex: 2,
      right: 0, // Ensure it stays within bounds
    }),
 
    ...(!isMobile &&
      isDualView && {
        ...(drawerWidth > window.innerWidth / 2
          ? { left: 0, width: `${drawerWidth}px` }
          : { right: 0, width: `calc(100% - ${drawerWidth}px)` }),
      }),
  }),
);
 
const CenterAligner = styled("div")(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  width: "100%",
  gap: theme.spacing(1),
  justifyContent: "center",
}));
 
const SearchControls = ({
  drawerWidth,
  mapView,
  mapRef,
  onSetMapView,
  onZoomIn,
}: SearchControlsProps) => {
  const isMobile = useMediaQuery(theme.breakpoints.down("md"));
 
  const [isFiltersOpen, setIsFiltersOpen] = useState(false);
  const [searchType, setSearchType] = useState<MapSearchTypes>("location");
 
  const { filters, resetFilters, updateFilter } = useSearchFilters();
 
  const handleMapViewChange = (view: MapViewOptions) => {
    onSetMapView(view);
  };
 
  const handleOpenFiltersDialog = () => {
    setIsFiltersOpen(true);
  };
 
  const handleCloseFiltersDialog = () => {
    setIsFiltersOpen(false);
  };
 
  const handleSetSearchType = (type: MapSearchTypes) => {
    setSearchType(type);
  };
 
  return (
    <>
      <MapControlsWrapper
        drawerWidth={drawerWidth}
        isDualView={mapView === MapViews.MAP_AND_LIST}
        isMobile={isMobile}
        onClick={(e) => e.stopPropagation()}
      >
        <CenterAligner>
          {!isMobile && (
            <MapViewToggle
              mapView={mapView}
              onMapViewChange={handleMapViewChange}
            />
          )}
          <FloatingSearchControls
            mapRef={mapRef}
            onClearFilters={resetFilters}
            onOpenFilters={handleOpenFiltersDialog}
            onSetSearchType={handleSetSearchType}
            searchType={searchType}
            onZoomIn={onZoomIn}
          />
        </CenterAligner>
      </MapControlsWrapper>
      {isMobile && (
        <SearchTypeRadioGroup
          onChange={handleSetSearchType}
          searchType={searchType}
        />
      )}
      <FilterDialog
        filters={filters}
        isOpen={isFiltersOpen}
        onCloseDialog={handleCloseFiltersDialog}
        updateFilter={updateFilter}
        resetFilters={resetFilters}
      />
    </>
  );
};
 
export default SearchControls;