Skip to content

Commit

Permalink
remove nominatim entry from auto suggestion. we still have first resu…
Browse files Browse the repository at this point in the history
…lt from nominatim when pressing enter
  • Loading branch information
karussell committed Jul 17, 2024
1 parent e910d08 commit e6fffd9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 60 deletions.
47 changes: 20 additions & 27 deletions src/sidebar/search/AddressInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Bbox, GeocodingHit } from '@/api/graphhopper'
import Autocomplete, {
AutocompleteItem,
GeocodingItem,
MoreResultsItem,
SelectCurrentLocationItem,
} from '@/sidebar/search/AddressInputAutocomplete'

Expand All @@ -18,8 +17,8 @@ import { useMediaQuery } from 'react-responsive'
import PopUp from '@/sidebar/search/PopUp'
import PlainButton from '@/PlainButton'
import { onCurrentLocationSelected } from '@/map/MapComponent'
import {toLonLat} from "ol/proj";
import {getMap} from "@/map/map";
import { toLonLat } from 'ol/proj'
import { getMap } from '@/map/map'

export interface AddressInputProps {
point: QueryPoint
Expand Down Expand Up @@ -49,7 +48,7 @@ export default function AddressInput(props: AddressInputProps) {
const [geocoder] = useState(
new Geocoder(getApi(), (query, provider, hits) => {
const items: AutocompleteItem[] = hits.map(hit => {
const obj = provider === 'nominatim' ? nominatimHitToItem(hit) : hitToItem(hit)
const obj = hitToItem(hit)
return new GeocodingItem(
obj.mainText,
obj.secondText,
Expand All @@ -58,15 +57,11 @@ export default function AddressInput(props: AddressInputProps) {
)
})

if (provider !== 'nominatim' && getApi().supportsGeocoding()) {
items.push(new MoreResultsItem(query))
setAutocompleteItems(items)
} else {
// TODO autocompleteItems is empty here because query point changed from outside somehow
// const res = autocompleteItems.length > 1 ? autocompleteItems.slice(0, autocompleteItems.length - 2) : autocompleteItems
// res.concat(items)
setAutocompleteItems(items)
}
// TODO autocompleteItems is empty here because query point changed from outside somehow
// const res = autocompleteItems.length > 1 ? autocompleteItems.slice(0, autocompleteItems.length - 2) : autocompleteItems
// res.concat(items)

setAutocompleteItems(items)
})
)
// if item is selected we need to clear the autocompletion list
Expand Down Expand Up @@ -120,17 +115,19 @@ export default function AddressInput(props: AddressInputProps) {
} else if (autocompleteItems.length > 0) {
const index = highlightedResult >= 0 ? highlightedResult : 0
const item = autocompleteItems[index]
if(highlightedResult < 0) {
if (highlightedResult < 0) {
// by default use the first result, otherwise the highlighted one
getApi().geocode(text, 'nominatim').then(result => {
if (result && result.hits.length > 0) {
const hit: GeocodingHit = result.hits[0]
const res = nominatimHitToItem(hit)
props.onAddressSelected(res.mainText + ', ' + res.secondText, hit.point)
} else if (item instanceof GeocodingItem) {
props.onAddressSelected(item.toText(), item.point)
}
})
getApi()
.geocode(text, 'nominatim')
.then(result => {
if (result && result.hits.length > 0) {
const hit: GeocodingHit = result.hits[0]
const res = nominatimHitToItem(hit)
props.onAddressSelected(res.mainText + ', ' + res.secondText, hit.point)
} else if (item instanceof GeocodingItem) {
props.onAddressSelected(item.toText(), item.point)
}
})
} else if (item instanceof GeocodingItem) {
props.onAddressSelected(item.toText(), item.point)
}
Expand Down Expand Up @@ -231,10 +228,6 @@ export default function AddressInput(props: AddressInputProps) {
} else if (item instanceof SelectCurrentLocationItem) {
hideSuggestions()
onCurrentLocationSelected(props.onAddressSelected)
} else if (item instanceof MoreResultsItem) {
// do not hide autocomplete items
const coordinate = textToCoordinate(item.search)
if (!coordinate) geocoder.request(item.search, biasCoord, 'nominatim')
}
searchInput.current!.blur()
}}
Expand Down
5 changes: 0 additions & 5 deletions src/sidebar/search/AddressInputAutocomplete.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
width: 100%;
}

.moreResultsEntry {
padding: 0.5em 0em;
}

.geocodingEntry {
display: flex;
flex-direction: column;
Expand All @@ -62,7 +58,6 @@
font-weight: bold;
}

.moreResultsText,
.secondaryText {
text-overflow: ellipsis;
white-space: nowrap;
Expand Down
28 changes: 0 additions & 28 deletions src/sidebar/search/AddressInputAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ export class GeocodingItem implements AutocompleteItem {

export class SelectCurrentLocationItem implements AutocompleteItem {}

export class MoreResultsItem implements AutocompleteItem {
search: string

constructor(search: string) {
this.search = search
}
}

export interface AutocompleteProps {
items: AutocompleteItem[]
highlightedItem: AutocompleteItem
Expand All @@ -57,29 +49,9 @@ function mapToComponent(item: AutocompleteItem, isHighlighted: boolean, onSelect
return <GeocodingEntry item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
else if (item instanceof SelectCurrentLocationItem)
return <SelectCurrentLocation item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
else if (item instanceof MoreResultsItem)
return <MoreResultsEntry item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
else throw Error('Unsupported item type: ' + typeof item)
}

export function MoreResultsEntry({
item,
isHighlighted,
onSelect,
}: {
item: MoreResultsItem
isHighlighted: boolean
onSelect: (item: MoreResultsItem) => void
}) {
return (
<AutocompleteEntry isHighlighted={isHighlighted} onSelect={() => onSelect(item)}>
<div className={styles.moreResultsEntry}>
<span className={styles.moreResultsText}>{tr('search_with_nominatim')}</span>
</div>
</AutocompleteEntry>
)
}

export function SelectCurrentLocation({
item,
isHighlighted,
Expand Down

0 comments on commit e6fffd9

Please sign in to comment.