1
- import { createLazyFileRoute } from '@tanstack/react-router' ;
1
+ import { createLazyFileRoute , useNavigate , useSearch as useSearchParams , Link } from '@tanstack/react-router' ;
2
2
import { useState , useEffect } from 'react' ;
3
3
import { useSearch } from '../hooks/useSearch' ;
4
4
import { useProfile } from '../hooks/useProfile' ;
5
- import { Link } from '@tanstack/react-router' ;
6
5
import { LuSearch , LuMapPin , LuMail , LuGlobe , LuTwitter , LuGithub , LuMessageSquare , LuSend } from "react-icons/lu" ;
7
6
import { useDebounce } from 'use-debounce' ;
8
7
import { getChainIconUrl } from '../utils/chainIcons' ;
9
8
import { shouldAttemptDirectLookup } from '../utils/validation' ;
10
9
import { ChainIcon } from '../components/ChainIcon' ;
11
10
11
+ interface SearchParams {
12
+ q ?: string
13
+ }
14
+
12
15
export const Route = createLazyFileRoute ( '/' ) ( {
13
16
component : Home ,
14
17
} ) ;
@@ -111,7 +114,9 @@ function ProfileFallback({ searchTerm }: { searchTerm: string }) {
111
114
}
112
115
113
116
function Home ( ) {
114
- const [ searchInput , setSearchInput ] = useState ( '' ) ;
117
+ const navigate = useNavigate ( ) ;
118
+ const params = new URLSearchParams ( typeof window !== 'undefined' ? window . location . search : '' ) ;
119
+ const [ searchInput , setSearchInput ] = useState ( params . get ( 'q' ) || '' ) ;
115
120
const [ debouncedSearchTerm ] = useDebounce ( searchInput , 300 ) ;
116
121
const { data, isLoading, error } = useSearch ( debouncedSearchTerm ) ;
117
122
@@ -127,8 +132,29 @@ function Home() {
127
132
}
128
133
} , [ isLoading , data ] ) ;
129
134
135
+ // Update URL when search term changes
136
+ useEffect ( ( ) => {
137
+ if ( typeof window === 'undefined' ) return ;
138
+
139
+ const currentParams = new URLSearchParams ( window . location . search ) ;
140
+ if ( debouncedSearchTerm ) {
141
+ currentParams . set ( 'q' , debouncedSearchTerm ) ;
142
+ } else {
143
+ currentParams . delete ( 'q' ) ;
144
+ }
145
+
146
+ const newSearch = currentParams . toString ( ) ;
147
+ const newPath = window . location . pathname + ( newSearch ? `?${ newSearch } ` : '' ) ;
148
+
149
+ navigate ( {
150
+ to : newPath ,
151
+ replace : true ,
152
+ } ) ;
153
+ } , [ debouncedSearchTerm , navigate ] ) ;
154
+
130
155
const handleSearch = ( e : React . FormEvent ) => {
131
156
e . preventDefault ( ) ;
157
+ // The URL will be updated by the effect above
132
158
} ;
133
159
134
160
// Render search results or appropriate message
0 commit comments