Skip to content

Commit 589dfdc

Browse files
committed
remove previous search hook, fix display of apis amount found
1 parent a75b801 commit 589dfdc

File tree

3 files changed

+23
-39
lines changed

3 files changed

+23
-39
lines changed

components/ApiGrid.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import React, { useEffect, useRef, useCallback } from "react";
2-
import { useInfiniteHits, useInstantSearch } from "react-instantsearch";
2+
import {
3+
useInfiniteHits,
4+
useInstantSearch,
5+
useSearchBox,
6+
} from "react-instantsearch";
37
import Card from "./ApiCard";
48
import { CardSkeleton } from "@/components/ui/CardSkeleton";
59
import { cleanDescription } from "@/utils/textProcessing";
610

711
interface ApiGridProps {
8-
searchTerm: string;
912
gridColumns: number;
1013
pageSize: number;
1114
}
15+
1216
const transformItems = (items: any[]) => {
13-
console.log("Transforming items:", items);
1417
return items.map((item) => ({
1518
...item,
1619
name: item.name || item.objectID,
@@ -30,9 +33,10 @@ const transformItems = (items: any[]) => {
3033
}));
3134
};
3235

33-
export function ApiGrid({ searchTerm, gridColumns, pageSize }: ApiGridProps) {
36+
export function ApiGrid({ gridColumns, pageSize }: ApiGridProps) {
37+
const { query } = useSearchBox(); // <-- Get current search term
3438
const { status, error } = useInstantSearch({ catchError: true });
35-
const { hits, isLastPage, showMore, results } = useInfiniteHits({
39+
const { hits, isLastPage, showMore } = useInfiniteHits({
3640
transformItems,
3741
showPrevious: false,
3842
});
@@ -95,13 +99,12 @@ export function ApiGrid({ searchTerm, gridColumns, pageSize }: ApiGridProps) {
9599
<>
96100
<div className="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-5 gap-4 mt-4">
97101
{hits.length > 0 ? (
98-
hits.map((hit, index) => {
99-
console.log("Rendering hit:", hit);
100-
return <Card key={`${hit.objectID}-${index}`} model={hit} />;
101-
})
102+
hits.map((hit, index) => (
103+
<Card key={`${hit.objectID}-${index}`} model={hit} />
104+
))
102105
) : (
103106
<div className="col-span-full text-center py-6 bg-gray-50 rounded-lg border border-gray-100">
104-
No APIs found matching &quot;{searchTerm}&quot;
107+
No APIs found matching &quot;{query}&quot;
105108
</div>
106109
)}
107110
</div>

components/SearchClientComponent.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ function SearchClientComponentInner({
3131
: initialSearchTerm;
3232

3333
const { gridColumns, pageSize } = useGridLayout();
34-
const { searchTerm, setSearchTerm, resetSearch, totalCount } = useApiSearch(
35-
initialCombinedSearchTerm,
36-
pageSize
37-
);
3834

3935
useEffect(() => {
4036
Object.entries(repoStarCounts).forEach(([name, stars]) => {
@@ -47,10 +43,6 @@ function SearchClientComponentInner({
4743
});
4844
}, [repoStarCounts]);
4945

50-
const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
51-
setSearchTerm(e.target.value);
52-
};
53-
5446
return (
5547
<div className="container mx-auto px-4 relative">
5648
<div className="relative z-10">
@@ -59,17 +51,14 @@ function SearchClientComponentInner({
5951
searchClient={searchClient}
6052
initialUiState={{
6153
test_apis_guru: {
54+
query: initialCombinedSearchTerm,
6255
sortBy: "test_apis_guru_by_name_asc",
6356
},
6457
}}
6558
>
66-
<SearchSection searchTerm={searchTerm} apiCount={totalCount} />
59+
<SearchSection />
6760

68-
<ApiGrid
69-
gridColumns={gridColumns}
70-
pageSize={pageSize}
71-
searchTerm={searchTerm}
72-
/>
61+
<ApiGrid gridColumns={gridColumns} pageSize={pageSize} />
7362
</InstantSearch>
7463
</div>
7564
</div>

components/SearchSection.tsx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import React, { useEffect } from "react";
2-
import { SearchBox, useInstantSearch } from "react-instantsearch";
2+
import { SearchBox, useInstantSearch, useSearchBox } from "react-instantsearch";
33

4-
interface SearchSectionProps {
5-
searchTerm: string;
6-
apiCount: number;
7-
8-
}
9-
10-
export function SearchSection({
11-
searchTerm,
12-
apiCount,
13-
14-
}: SearchSectionProps) {
4+
export function SearchSection() {
155
const { results } = useInstantSearch({});
6+
const { query } = useSearchBox();
7+
168
useEffect(() => {
179
console.log(results);
1810
}, [results]);
@@ -42,13 +34,13 @@ export function SearchSection({
4234
"w-full pl-12 pr-4 py-2 text-lg border-2 border-gray-200 rounded-xl transition-all duration-200 shadow-sm hover:shadow-md",
4335
submit: "hidden",
4436
reset: "hidden",
45-
loadingIndicator: "hidden", // Hide the loading indicator
37+
loadingIndicator: "hidden",
4638
}}
4739
/>
4840
</div>
49-
{searchTerm && (
41+
{query && (
5042
<div className="mt-3 text-lg text-gray-600 text-center">
51-
{apiCount.toLocaleString()} APIs found
43+
{results.nbHits.toLocaleString()} APIs found
5244
</div>
5345
)}
5446
</div>

0 commit comments

Comments
 (0)