@@ -85,11 +85,49 @@ function HomeContent() {
8585 }
8686 } , [ ] ) ;
8787
88- const handleParamsLoaded = useCallback ( ( cityFromUrl : string , latFromUrl : string , lonFromUrl : string ) => {
89- setCity ( cityFromUrl ) ;
90- fetchWeatherData ( parseFloat ( latFromUrl ) , parseFloat ( lonFromUrl ) )
91- . finally ( ( ) => setInitialLoad ( false ) ) ;
92- } , [ fetchWeatherData ] ) ;
88+ // Memoize the getLocationByIP function
89+ const getLocationByIP = useCallback ( async ( ) => {
90+ try {
91+ // Get location from IP using ipapi.co
92+ const response = await axios . get ( 'https://ipapi.co/json/' ) ;
93+ const { city, latitude, longitude, region : admin1 , country_name : country } = response . data ;
94+
95+ if ( city && latitude && longitude ) {
96+ setCity ( city ) ;
97+ const urlParams = new URLSearchParams ( ) ;
98+ urlParams . set ( 'city' , city ) ;
99+ urlParams . set ( 'lat' , latitude . toString ( ) ) ;
100+ urlParams . set ( 'lon' , longitude . toString ( ) ) ;
101+ if ( admin1 ) urlParams . set ( 'admin1' , admin1 ) ;
102+ if ( country ) urlParams . set ( 'country' , country ) ;
103+ router . push ( `?${ urlParams . toString ( ) } ` ) ;
104+ await fetchWeatherData ( latitude , longitude ) ;
105+ } else {
106+ throw new Error ( 'Location not found' ) ;
107+ }
108+ } catch ( error ) {
109+ console . error ( 'Failed to get location:' , error ) ;
110+ setError ( 'Failed to get location data' ) ;
111+ } finally {
112+ setInitialLoad ( false ) ;
113+ }
114+ } , [ router , fetchWeatherData ] ) ;
115+
116+ // Get user's location on component mount
117+ useEffect ( ( ) => {
118+ const cityFromUrl = searchParams ?. get ( 'city' ) ;
119+ const latFromUrl = searchParams ?. get ( 'lat' ) ;
120+ const lonFromUrl = searchParams ?. get ( 'lon' ) ;
121+
122+ if ( cityFromUrl && latFromUrl && lonFromUrl ) {
123+ setCity ( cityFromUrl ) ;
124+ fetchWeatherData ( parseFloat ( latFromUrl ) , parseFloat ( lonFromUrl ) )
125+ . finally ( ( ) => setInitialLoad ( false ) ) ;
126+ } else {
127+ // Only get location by IP if no parameters in URL
128+ getLocationByIP ( ) ;
129+ }
130+ } , [ searchParams , fetchWeatherData , getLocationByIP ] ) ;
93131
94132 // Memoize the selectCity function
95133 const selectCity = useCallback ( async ( result : CityResult ) => {
@@ -143,40 +181,6 @@ function HomeContent() {
143181 }
144182 } , [ inputValue , selectCity ] ) ;
145183
146- // Memoize the getLocationByIP function
147- const getLocationByIP = useCallback ( async ( ) => {
148- try {
149- // Get location from IP using ipapi.co
150- const response = await axios . get ( 'https://ipapi.co/json/' ) ;
151- const { city, latitude, longitude, region : admin1 , country_name : country } = response . data ;
152-
153- if ( city && latitude && longitude ) {
154- setCity ( city ) ;
155- const urlParams = new URLSearchParams ( ) ;
156- urlParams . set ( 'city' , city ) ;
157- urlParams . set ( 'lat' , latitude . toString ( ) ) ;
158- urlParams . set ( 'lon' , longitude . toString ( ) ) ;
159- if ( admin1 ) urlParams . set ( 'admin1' , admin1 ) ;
160- if ( country ) urlParams . set ( 'country' , country ) ;
161- router . push ( `?${ urlParams . toString ( ) } ` ) ;
162- await fetchWeatherData ( latitude , longitude ) ;
163- } else {
164- throw new Error ( 'Location not found' ) ;
165- }
166- } catch ( error ) {
167- console . error ( 'Failed to get location:' , error ) ;
168- setError ( 'Failed to get location data' ) ;
169- } finally {
170- setInitialLoad ( false ) ;
171- }
172- } , [ router , fetchWeatherData ] ) ;
173-
174- // Get user's location on component mount
175- useEffect ( ( ) => {
176- // Only get location by IP if no parameters in URL
177- getLocationByIP ( ) ;
178- } , [ getLocationByIP ] ) ;
179-
180184 // Handle keyboard navigation
181185 const handleKeyDown = useCallback ( ( e : React . KeyboardEvent ) => {
182186 if ( ! showSuggestions ) return ;
0 commit comments