@@ -5,7 +5,6 @@ import { Cormorant_Garamond } from "next/font/google";
55import Image from "next/image" ;
66import Link from "next/link" ;
77import { useRouter , useSearchParams } from "next/navigation" ;
8- import MapRadius from "~~/components/marketplace/MapRadiusGL" ;
98
109const cormorant = Cormorant_Garamond ( { subsets : [ "latin" ] , weight : [ "500" , "600" , "700" ] } ) ;
1110
@@ -15,8 +14,6 @@ const HomeInner = () => {
1514
1615 const [ query , setQuery ] = useState ( "" ) ;
1716 const [ locations , setLocations ] = useState < any [ ] > ( [ ] ) ;
18- const [ selected , setSelected ] = useState < any | null > ( null ) ;
19- const [ loadingSelected , setLoadingSelected ] = useState ( false ) ;
2017 const [ loadingLocations , setLoadingLocations ] = useState ( true ) ;
2118
2219 useEffect ( ( ) => {
@@ -58,33 +55,17 @@ const HomeInner = () => {
5855 } catch { }
5956 const id = parsed ?. id as string | undefined ;
6057 if ( id ) {
61- // verify the location still exists before redirecting
62- fetch ( `/api/locations/${ id } ` )
63- . then ( async res => {
64- if ( res . ok ) {
65- router . replace ( `/location/${ id } ` ) ;
66- } else {
67- try {
68- localStorage . removeItem ( "marketplace.defaultLocationData" ) ;
69- } catch { }
70- router . replace ( `/?home=1` ) ;
71- }
72- } )
73- . catch ( ( ) => {
74- try {
75- localStorage . removeItem ( "marketplace.defaultLocationData" ) ;
76- } catch { }
77- router . replace ( `/?home=1` ) ;
78- } ) ;
58+ // Navigate immediately; the location page will validate and handle 404s/cleanup.
59+ router . replace ( `/location/${ encodeURIComponent ( id ) } ` ) ;
7960 }
8061 }
8162 } catch { }
8263 } , [ router , searchParams ] ) ;
8364
8465 return (
8566 < >
86- < div className = "flex items-center flex-col grow pt-2" >
87- < div className = "px-5 w-full max-w-5xl " >
67+ < div className = "flex items-center justify-between gap-2 pt-2" >
68+ < div className = "px-5 w-full" >
8869 < div className = "flex items-center justify-between" >
8970 < div className = "flex-1" >
9071 < div className = "lg:hidden flex items-center gap-2" >
@@ -132,22 +113,27 @@ const HomeInner = () => {
132113 < button
133114 key = { l . id }
134115 className = "card bg-base-100 border border-base-300 hover:border-primary/60 transition-colors text-left cursor-pointer"
135- onClick = { async ( ) => {
136- setSelected ( null ) ;
137- setLoadingSelected ( true ) ;
116+ onClick = { ( ) => {
117+ // Set as preferred location immediately, then navigate
138118 try {
139- const res = await fetch ( `/api/locations/${ encodeURIComponent ( l . id ) } ` ) ;
140- if ( res . ok ) {
141- const json = await res . json ( ) ;
142- setSelected ( json . location || l ) ;
143- } else {
144- setSelected ( l ) ;
145- }
146- } finally {
147- setLoadingSelected ( false ) ;
148- const checkbox = document . getElementById ( "location-preview-modal" ) as HTMLInputElement | null ;
149- if ( checkbox ) checkbox . checked = true ;
150- }
119+ const data = {
120+ id : String ( l . id ) ,
121+ name : l ?. name ?? null ,
122+ lat : l ?. lat ?? null ,
123+ lng : l ?. lng ?? null ,
124+ radiusMiles : l ?. radiusMiles ?? null ,
125+ savedAt : Date . now ( ) ,
126+ } ;
127+ localStorage . setItem ( "marketplace.defaultLocationData" , JSON . stringify ( data ) ) ;
128+ // Mirror to cookie for server-side redirects (middleware)
129+ try {
130+ document . cookie =
131+ "last_location_id=" +
132+ encodeURIComponent ( String ( l . id ) ) +
133+ "; Max-Age=15552000; Path=/; SameSite=Lax" ;
134+ } catch { }
135+ } catch { }
136+ router . push ( `/location/${ encodeURIComponent ( l . id ) } ` ) ;
151137 } }
152138 >
153139 < div className = "card-body p-3" >
@@ -161,81 +147,19 @@ const HomeInner = () => {
161147 </ div >
162148 </ div >
163149 </ div >
164-
165- { /* Location Preview Modal */ }
166- < div >
167- < input type = "checkbox" id = "location-preview-modal" className = "modal-toggle" />
168- < label htmlFor = "location-preview-modal" className = "modal cursor-pointer" >
169- < label className = "modal-box relative max-w-3xl max-h-[90vh] overflow-y-auto" >
170- < input className = "h-0 w-0 absolute top-0 left-0" />
171- < label htmlFor = "location-preview-modal" className = "btn btn-ghost btn-sm btn-circle absolute right-3 top-3" >
172- ✕
173- </ label >
174- < div className = "space-y-3" >
175- < div className = "text-lg font-semibold" > { selected ?. name || selected ?. id || "Location" } </ div >
176- < div className = "rounded-xl overflow-hidden border bg-base-100" >
177- { loadingSelected ? (
178- < div className = "p-4 text-sm opacity-70" > Loading…</ div >
179- ) : selected ?. lat != null && selected ?. lng != null && selected ?. radiusMiles != null ? (
180- < MapRadius
181- lat = { Number ( selected . lat ) }
182- lng = { Number ( selected . lng ) }
183- radiusMiles = { Number ( selected . radiusMiles ) }
184- onMove = { ( ) => { } }
185- />
186- ) : (
187- < div className = "p-4 text-sm opacity-70" > No map preview available for this location.</ div >
188- ) }
189- </ div >
190- { /* Additional location details */ }
191- { selected && (
192- < div className = "space-y-2" >
193- { Array . isArray ( selected . akas ) && selected . akas . length > 0 && (
194- < div className = "flex flex-wrap gap-2" >
195- { selected . akas . map ( ( aka : string , idx : number ) => (
196- < span key = { idx } className = "badge badge-outline" >
197- { aka }
198- </ span >
199- ) ) }
200- </ div >
201- ) }
202- </ div >
203- ) }
204- < button
205- className = "btn btn-primary w-full"
206- onClick = { ( ) => {
207- try {
208- if ( selected ?. id ) {
209- const data = {
210- id : String ( selected . id ) ,
211- name : selected ?. name ?? null ,
212- lat : selected ?. lat ?? null ,
213- lng : selected ?. lng ?? null ,
214- radiusMiles : selected ?. radiusMiles ?? null ,
215- savedAt : Date . now ( ) ,
216- } ;
217- localStorage . setItem ( "marketplace.defaultLocationData" , JSON . stringify ( data ) ) ;
218- }
219- } catch { }
220- const checkbox = document . getElementById ( "location-preview-modal" ) as HTMLInputElement | null ;
221- if ( checkbox ) checkbox . checked = false ;
222- router . push ( `/location/${ String ( selected ?. id || "" ) } ` ) ;
223- } }
224- disabled = { ! selected }
225- >
226- Select this location
227- </ button >
228- </ div >
229- </ label >
230- </ label >
231- </ div >
232150 </ >
233151 ) ;
234152} ;
235153
236154export default function Home ( ) {
237155 return (
238156 < Suspense fallback = { null } >
157+ { /* Early redirect before hydration using localStorage (no data fetch) */ }
158+ < script
159+ dangerouslySetInnerHTML = { {
160+ __html : `(function(){try{var params=new URLSearchParams(window.location.search);if(params.get('home')==='1')return;var raw=localStorage.getItem('marketplace.defaultLocationData');if(!raw)return;var parsed;try{parsed=JSON.parse(raw)}catch(e){parsed=null}var id=parsed&&parsed.id;if(id){window.location.replace('/location/'+encodeURIComponent(id))}}catch(e){}})();` ,
161+ } }
162+ />
239163 < HomeInner />
240164 </ Suspense >
241165 ) ;
0 commit comments