1- import { JSX , ReactNode , useCallback , useEffect , useRef , useState } from 'react'
1+ import { JSX , ReactNode , useCallback , useContext , useEffect , useRef , useState } from 'react'
22import { QueryPoint , QueryPointType } from '@/stores/QueryStore'
33import { Bbox , GeocodingHit , ReverseGeocodingHit } from '@/api/graphhopper'
4- import Autocomplete , { AutocompleteItem , GeocodingItem , POIQueryItem , RecentLocationItem } from '@/sidebar/search/AddressInputAutocomplete'
4+ import Autocomplete , {
5+ AutocompleteItem ,
6+ GeocodingItem ,
7+ POIQueryItem ,
8+ RecentLocationItem ,
9+ } from '@/sidebar/search/AddressInputAutocomplete'
510import { getRecentLocations , saveRecentLocation } from '@/sidebar/search/RecentLocations'
11+ import { SettingsContext } from '@/contexts/SettingsContext'
612import ArrowBack from './arrow_back.svg'
713import Cross from '@/sidebar/times-solid-thin.svg'
814import CurrentLocationIcon from './current-location.svg'
@@ -34,6 +40,7 @@ export interface AddressInputProps {
3440}
3541
3642export default function AddressInput ( props : AddressInputProps ) {
43+ const saveRecent = useContext ( SettingsContext ) . saveRecentLocations
3744 const [ origText , setOrigText ] = useState ( props . point . queryText )
3845 // controlled component pattern with initial value set from props
3946 const [ text , setText ] = useState ( props . point . queryText )
@@ -118,7 +125,8 @@ export default function AddressInput(props: AddressInputProps) {
118125 setText ( origText )
119126 } else if ( nextIndex >= 0 ) {
120127 const item = autocompleteItems [ nextIndex ]
121- if ( item instanceof GeocodingItem || item instanceof RecentLocationItem ) setText ( item . mainText )
128+ if ( item instanceof GeocodingItem || item instanceof RecentLocationItem )
129+ setText ( item . mainText )
122130 else setText ( origText )
123131 }
124132 }
@@ -150,15 +158,15 @@ export default function AddressInput(props: AddressInputProps) {
150158 const hit : GeocodingHit = result . hits [ 0 ]
151159 const res = nominatimHitToItem ( hit )
152160 props . onAddressSelected ( res . mainText + ', ' + res . secondText , hit . point )
153- saveRecentLocation ( res . mainText , res . secondText , hit . point )
161+ if ( saveRecent ) saveRecentLocation ( res . mainText , res . secondText , hit . point )
154162 } else if ( item instanceof GeocodingItem ) {
155163 props . onAddressSelected ( item . toText ( ) , item . point )
156- saveRecentLocation ( item . mainText , item . secondText , item . point )
164+ if ( saveRecent ) saveRecentLocation ( item . mainText , item . secondText , item . point )
157165 }
158166 } )
159167 } else if ( item instanceof GeocodingItem ) {
160168 props . onAddressSelected ( item . toText ( ) , item . point )
161- saveRecentLocation ( item . mainText , item . secondText , item . point )
169+ if ( saveRecent ) saveRecentLocation ( item . mainText , item . secondText , item . point )
162170 }
163171 }
164172 if ( event . key === 'Enter' ) focusNextOrBlur ( )
@@ -303,7 +311,7 @@ export default function AddressInput(props: AddressInputProps) {
303311 if ( item instanceof GeocodingItem ) {
304312 setText ( item . toText ( ) )
305313 props . onAddressSelected ( item . toText ( ) , item . point )
306- saveRecentLocation ( item . mainText , item . secondText , item . point )
314+ if ( saveRecent ) saveRecentLocation ( item . mainText , item . secondText , item . point )
307315 } else if ( item instanceof RecentLocationItem ) {
308316 setText ( item . toText ( ) )
309317 props . onAddressSelected ( item . toText ( ) , item . point )
@@ -323,14 +331,16 @@ export default function AddressInput(props: AddressInputProps) {
323331
324332function buildRecentItems ( filter ?: string , limit ?: number , excludeCoord ?: Coordinate ) : RecentLocationItem [ ] {
325333 let recents = getRecentLocations ( 0 )
326- if ( excludeCoord )
327- recents = recents . filter ( e => calcDist ( { lat : e . lat , lng : e . lng } , excludeCoord ) > 0 )
334+ if ( excludeCoord ) recents = recents . filter ( e => calcDist ( { lat : e . lat , lng : e . lng } , excludeCoord ) > 0 )
328335 if ( filter ) {
329336 const lower = filter . toLowerCase ( )
330337 recents = recents . filter (
331338 e =>
332339 e . mainText . toLowerCase ( ) . startsWith ( lower ) ||
333- e . secondText . toLowerCase ( ) . split ( / [ \s , ] + / ) . some ( word => word . startsWith ( lower ) ) ,
340+ e . secondText
341+ . toLowerCase ( )
342+ . split ( / [ \s , ] + / )
343+ . some ( word => word . startsWith ( lower ) ) ,
334344 )
335345 }
336346 if ( limit ) recents = recents . slice ( 0 , limit )
0 commit comments