Skip to content

Commit 3f134be

Browse files
committed
Introduce suggested search
1 parent 92a05d4 commit 3f134be

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,49 @@ import { useSearch } from '../hooks/useSearch';
44
import { useProfile } from '../hooks/useProfile';
55
import { LuSearch } from "react-icons/lu";
66
import { useDebounce } from 'use-debounce';
7-
import { shouldAttemptDirectLookup } from '../utils/validation';
7+
import { shouldAttemptDirectLookup, isValidENSNameFormat } from '../utils/validation';
88
import { SearchResult } from '../components/SearchResult';
99

1010
export const Route = createLazyFileRoute('/')({
1111
component: Home,
1212
});
1313

14+
// SuggestedSearch component that shows .eth auto-complete when user forgets to add .eth
15+
function SuggestedSearch({ searchTerm }: { searchTerm: string }) {
16+
const suggestedEns = `${searchTerm}.eth`;
17+
const { data: profile, isLoading } = useProfile(suggestedEns);
18+
19+
// Only show if we found a profile
20+
if (isLoading || !profile) {
21+
return null;
22+
}
23+
24+
return (
25+
<div className="mb-4 p-3 bg-blue-50 rounded-md shadow-sm border border-blue-200">
26+
<div className="flex items-center">
27+
{profile.avatar && (
28+
<img
29+
src={profile.avatar}
30+
alt={suggestedEns}
31+
className="w-8 h-8 rounded-full mr-3"
32+
/>
33+
)}
34+
<div>
35+
<p className="text-gray-700">
36+
Did you mean <a href={`/${suggestedEns}`} className="font-semibold text-blue-600 hover:text-blue-800">{suggestedEns}</a>?
37+
</p>
38+
<a
39+
href={`/${suggestedEns}`}
40+
className="text-sm text-blue-600 hover:text-blue-800 underline"
41+
>
42+
Go to {suggestedEns} profile
43+
</a>
44+
</div>
45+
</div>
46+
</div>
47+
);
48+
}
49+
1450
// ProfileFallback component that attempts direct profile lookup
1551
function ProfileFallback({ searchTerm }: { searchTerm: string }) {
1652
const { data: profile, isLoading, error } = useProfile(searchTerm);
@@ -91,6 +127,17 @@ function Home() {
91127
// The URL will be updated by the effect above
92128
};
93129

130+
// Check if we should show the .eth auto-complete
131+
const shouldShowEthAutoComplete = () => {
132+
// Only show auto-complete if there's a search term, no dots in it (not already ENS), and it's not an Ethereum address
133+
return (
134+
debouncedSearchTerm &&
135+
debouncedSearchTerm.length > 1 &&
136+
!debouncedSearchTerm.includes('.') &&
137+
!debouncedSearchTerm.startsWith('0x')
138+
);
139+
};
140+
94141
// Render search results or appropriate message
95142
const renderResults = () => {
96143
// Case 1: We have data to show
@@ -195,6 +242,11 @@ function Home() {
195242
</form>
196243
</div>
197244

245+
{/* .eth Auto-Complete */}
246+
{shouldShowEthAutoComplete() && (
247+
<SuggestedSearch searchTerm={debouncedSearchTerm} />
248+
)}
249+
198250
{/* Results section */}
199251
<div className="rounded-lg overflow-hidden relative">
200252
{renderResults()}

0 commit comments

Comments
 (0)