Skip to content

Commit 989289a

Browse files
committed
Refactor usePackageSuggestions hook: Remove UI rendering logic for better separation of concerns
1 parent 93fd0ae commit 989289a

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

src/components/PackageSearchInput.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import React, { useCallback } from "react";
1+
import React, { useMemo } from "react";
22
import { AutoComplete, Button, Space, Input, Spin } from "antd";
33
import { SearchOutlined, LoadingOutlined } from "@ant-design/icons";
4-
import { NpmPackageSuggestion } from "../services/npmAutocompleteService";
54
import { usePackageSuggestions } from "../hooks/usePackageSuggestions";
65
import PackageSuggestionLabel from "./PackageSuggestionLabel";
76

@@ -15,6 +14,11 @@ interface PackageSearchInputProps {
1514
style?: React.CSSProperties;
1615
}
1716

17+
interface AutoCompleteOption {
18+
value: string;
19+
label: React.ReactNode;
20+
}
21+
1822
const PackageSearchInput: React.FC<PackageSearchInputProps> = ({
1923
searchTerm,
2024
onSearchTermChange,
@@ -23,17 +27,20 @@ const PackageSearchInput: React.FC<PackageSearchInputProps> = ({
2327
size = "middle",
2428
style,
2529
}) => {
26-
// Render function for suggestion items
27-
const renderSuggestion = useCallback((pkg: NpmPackageSuggestion) => {
28-
return <PackageSuggestionLabel package={pkg} />;
29-
}, []);
30-
3130
// Use our custom hook for package suggestions
3231
const {
33-
suggestions,
32+
suggestions: packageSuggestions,
3433
isLoading: fetchingSuggestions,
3534
handleInputChange,
36-
} = usePackageSuggestions(searchTerm, renderSuggestion);
35+
} = usePackageSuggestions(searchTerm);
36+
37+
// Transform raw package data into AutoComplete options
38+
const autoCompleteOptions = useMemo<AutoCompleteOption[]>(() => {
39+
return packageSuggestions.map((pkg) => ({
40+
value: pkg.name,
41+
label: <PackageSuggestionLabel package={pkg} />,
42+
}));
43+
}, [packageSuggestions]);
3744

3845
const handleLocalInputChange = (value: string) => {
3946
onSearchTermChange(value);
@@ -63,7 +70,7 @@ const PackageSearchInput: React.FC<PackageSearchInputProps> = ({
6370
<AutoComplete
6471
style={{ width: "100%" }}
6572
value={searchTerm}
66-
options={suggestions}
73+
options={autoCompleteOptions}
6774
onSelect={handleSelect}
6875
onChange={handleLocalInputChange}
6976
notFoundContent={notFoundContent}

src/hooks/usePackageSuggestions.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@ import {
55
NpmPackageSuggestion,
66
} from "../services/npmAutocompleteService";
77

8-
export interface AutoCompleteOption {
9-
value: string;
10-
label: React.ReactNode;
11-
}
12-
138
/**
149
* Custom hook for managing npm package suggestions
1510
* @param searchTerm The current search term
16-
* @param renderSuggestion Function to render a suggestion label component
17-
* @returns Suggestion options, loading state, and handle input change function
11+
* @returns Raw package suggestion data, loading state, and input change handler
1812
*/
19-
export function usePackageSuggestions(
20-
searchTerm: string,
21-
renderSuggestion: (pkg: NpmPackageSuggestion) => React.ReactNode
22-
) {
23-
const [suggestions, setSuggestions] = useState<AutoCompleteOption[]>([]);
13+
export function usePackageSuggestions(searchTerm: string) {
14+
const [suggestions, setSuggestions] = useState<NpmPackageSuggestion[]>([]);
2415
const [isLoading, setIsLoading] = useState(false);
2516

2617
// Debounced function to fetch package suggestions
@@ -35,18 +26,14 @@ export function usePackageSuggestions(
3526
setIsLoading(true);
3627
try {
3728
const packages = await fetchPackageSuggestions(value);
38-
const options = packages.map((pkg: NpmPackageSuggestion) => ({
39-
value: pkg.name,
40-
label: renderSuggestion(pkg),
41-
}));
42-
setSuggestions(options);
29+
setSuggestions(packages);
4330
} catch (error) {
4431
console.error("Error fetching suggestions:", error);
4532
} finally {
4633
setIsLoading(false);
4734
}
4835
}, 300),
49-
[renderSuggestion]
36+
[]
5037
);
5138

5239
// Clear suggestions when searchTerm is empty

0 commit comments

Comments
 (0)