import * as React from 'react' import { useAddFilterTag, useFilterTags, useResetFilterTags, useSetSearchPhrase } from '../../hooks/useFilter' import useWindowDimensions from '../../hooks/useWindowDimension'; import axios from 'axios'; import { useRef, useState } from 'react'; import { useMap } from 'react-leaflet'; import { LatLng, LatLngBounds } from 'leaflet'; import { useDebounce } from '../../hooks/useDebounce'; import { useTags } from '../../hooks/useTags'; import { useItems } from '../../hooks/useItems'; import { useLeafletRefs } from '../../hooks/useLeafletRefs'; import { getValue } from '../../../../Utils/GetValue'; export const SearchControl = ({ clusterRef }) => { const windowDimensions = useWindowDimensions(); const [value, setValue] = useState(''); const [geoResults, setGeoResults] = useState>([]); const [tagsResults, setTagsResults] = useState>([]); const [itemsResults, setItemsResults] = useState>([]); const [hideSuggestions, setHideSuggestions] = useState(true); const map = useMap(); const tags = useTags(); const items = useItems(); const leafletRefs = useLeafletRefs(); const addFilterTag = useAddFilterTag(); const resetFilterTags = useResetFilterTags(); const filterTags = useFilterTags(); useDebounce(() => { const searchGeo = async () => { try { const { data } = await axios.get( `https://photon.komoot.io/api/?q=${value}&limit=5` ); setGeoResults(data.features); } catch (error) { console.log(error); } }; searchGeo(); setItemsResults(items.filter(item => { if (item.layer?.itemTitleField) item.name = getValue(item, item.layer.itemTitleField) return item.name?.toLowerCase().includes(value.toLowerCase()) || item.text?.toLowerCase().includes(value.toLowerCase()) })) setTagsResults(tags.filter(tag => tag.id?.toLowerCase().includes(value.toLowerCase()))) }, 500, [value]); const searchInput = useRef(null); return (<> {!(windowDimensions.height < 500) &&
setValue(e.target.value)} onFocus={() => setHideSuggestions(false)} onBlur={async () => { setTimeout(() => { setHideSuggestions(true); window.history.pushState({}, "", `/`) }, 200); }} /> {value.length > 0 && } {hideSuggestions || Array.from(geoResults).length == 0 && itemsResults.length == 0 && tagsResults.length == 0 || value.length == 0 ? "" :
{tagsResults.length > 0 &&
{tagsResults.map(tag => (
{ addFilterTag(tag) }}> #{capitalizeFirstLetter(tag.id)}
))}
} {itemsResults.length > 0 && tagsResults.length > 0 &&
} {itemsResults.slice(0, 5).map(item => (
{ const marker = Object.entries(leafletRefs).find(r => r[1].item == item)?.[1].marker; if (filterTags.length > 0) { marker !== null && window.history.pushState({}, "", `/${item.layer.name}/${item.id}`) resetFilterTags(); } else { marker !== null && clusterRef?.current?.zoomToShowLayer(marker, () => { marker?.openPopup(); }); } } }>
{item.name}
{item.text}
))} {Array.from(geoResults).length > 0 && (itemsResults.length > 0 || tagsResults.length > 0) &&
} {Array.from(geoResults).map((geo) => (
{ searchInput.current?.blur(); if (geo.properties.extent) map.fitBounds(new LatLngBounds(new LatLng(geo.properties.extent[1], geo.properties.extent[0]), new LatLng(geo.properties.extent[3], geo.properties.extent[2]))); else map.setView(new LatLng(geo.geometry.coordinates[1], geo.geometry.coordinates[0]), 15, { duration: 1 }) }}>
{geo?.properties.name ? geo?.properties.name : value}
{geo?.properties?.city && `${capitalizeFirstLetter(geo?.properties?.city)}, `} {geo?.properties?.osm_value && geo?.properties?.osm_value !== "primary" && geo?.properties?.osm_value !== "path" && geo?.properties?.osm_value !== "secondary" && geo?.properties?.osm_value !== "residential" && geo?.properties?.osm_value !== "unclassified" && `${capitalizeFirstLetter(geo?.properties?.osm_value)}, `} {geo.properties.state && `${geo.properties.state}, `} {geo.properties.country && geo.properties.country}
))}
}
} ) } function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); }