Skip to content

Commit 21edecb

Browse files
committed
Update search query
1 parent def49b2 commit 21edecb

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

packages/search/src/routes/index.lazy.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { createLazyFileRoute } from '@tanstack/react-router';
1+
import { createLazyFileRoute, useNavigate, useSearch as useSearchParams, Link } from '@tanstack/react-router';
22
import { useState, useEffect } from 'react';
33
import { useSearch } from '../hooks/useSearch';
44
import { useProfile } from '../hooks/useProfile';
5-
import { Link } from '@tanstack/react-router';
65
import { LuSearch, LuMapPin, LuMail, LuGlobe, LuTwitter, LuGithub, LuMessageSquare, LuSend } from "react-icons/lu";
76
import { useDebounce } from 'use-debounce';
87
import { getChainIconUrl } from '../utils/chainIcons';
98
import { shouldAttemptDirectLookup } from '../utils/validation';
109
import { ChainIcon } from '../components/ChainIcon';
1110

11+
interface SearchParams {
12+
q?: string
13+
}
14+
1215
export const Route = createLazyFileRoute('/')({
1316
component: Home,
1417
});
@@ -111,7 +114,9 @@ function ProfileFallback({ searchTerm }: { searchTerm: string }) {
111114
}
112115

113116
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') || '');
115120
const [debouncedSearchTerm] = useDebounce(searchInput, 300);
116121
const { data, isLoading, error } = useSearch(debouncedSearchTerm);
117122

@@ -127,8 +132,29 @@ function Home() {
127132
}
128133
}, [isLoading, data]);
129134

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+
130155
const handleSearch = (e: React.FormEvent) => {
131156
e.preventDefault();
157+
// The URL will be updated by the effect above
132158
};
133159

134160
// Render search results or appropriate message

0 commit comments

Comments
 (0)