Skip to content

Commit 7e8dc79

Browse files
fix(web): search community themes as you type
1 parent d47bb0a commit 7e8dc79

1 file changed

Lines changed: 39 additions & 44 deletions

File tree

‎apps/web/src/components/settings/ThemeSearchSection.tsx‎

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ import {
22
ExternalLinkIcon,
33
GithubIcon,
44
GitlabIcon,
5+
Loader2Icon,
56
PackagePlusIcon,
67
PaletteIcon,
78
RefreshCwIcon,
89
SearchIcon,
910
} from "lucide-react";
10-
import type { FormEvent } from "react";
1111
import { useCallback, useEffect, useRef, useState } from "react";
1212
import {
1313
importOpenVsxThemeExtension,
1414
searchOpenVsxThemes,
1515
type OpenVsxThemeExtension,
1616
type OpenVsxThemeSort,
1717
} from "../../openVsxThemes";
18+
import { useDebouncedValue } from "../../state/queries";
1819
import {
1920
getCustomThemes,
2021
getStoredCustomThemeCollection,
@@ -46,6 +47,7 @@ const SORT_OPTIONS: ReadonlyArray<{ value: OpenVsxThemeSort; label: string }> =
4647
{ value: "timestamp", label: "Newest" },
4748
{ value: "relevance", label: "Most relevant" },
4849
];
50+
const SEARCH_DEBOUNCE_MS = 350;
4951

5052
function SourceLinkIcon({ url }: { url: string }) {
5153
try {
@@ -145,25 +147,25 @@ export function ThemeSearchSection({
145147
[sortBy],
146148
);
147149

148-
const handleSearch = useCallback(
149-
(event: FormEvent<HTMLFormElement>) => {
150-
event.preventDefault();
151-
void runSearch(query);
152-
},
153-
[query, runSearch],
154-
);
150+
const debouncedQuery = useDebouncedValue(query.trim(), SEARCH_DEBOUNCE_MS);
155151

156-
const handleSortChange = useCallback(
157-
(value: OpenVsxThemeSort | null) => {
158-
const nextSort = SORT_OPTIONS.find((option) => option.value === value)?.value;
159-
if (!nextSort) return;
160-
setSortBy(nextSort);
161-
if ((results !== null || isSearching) && query.trim()) {
162-
void runSearch(query, nextSort);
163-
}
164-
},
165-
[isSearching, query, results, runSearch],
166-
);
152+
useEffect(() => {
153+
if (!debouncedQuery) {
154+
requestRef.current?.abort();
155+
requestRef.current = null;
156+
setResults(null);
157+
setError(null);
158+
setIsSearching(false);
159+
return;
160+
}
161+
void runSearch(debouncedQuery);
162+
}, [debouncedQuery, runSearch]);
163+
164+
const handleSortChange = useCallback((value: OpenVsxThemeSort | null) => {
165+
const nextSort = SORT_OPTIONS.find((option) => option.value === value)?.value;
166+
if (!nextSort) return;
167+
setSortBy(nextSort);
168+
}, []);
167169

168170
const handleInstall = useCallback(
169171
async (extension: OpenVsxThemeExtension, allowUpdate: boolean) => {
@@ -216,31 +218,24 @@ export function ThemeSearchSection({
216218
Find open-source themes from Open VSX.
217219
</p>
218220
</div>
219-
<form className="flex gap-2" onSubmit={handleSearch}>
220-
<InputGroup>
221-
<InputGroupAddon>
222-
<SearchIcon />
223-
</InputGroupAddon>
224-
<InputGroupInput
225-
aria-label="Search Open VSX themes"
226-
autoFocus
227-
onChange={(event) => setQuery(event.currentTarget.value)}
228-
placeholder="Search themes..."
229-
size="lg"
230-
type="search"
231-
value={query}
232-
/>
233-
</InputGroup>
234-
<Button
235-
disabled={!query.trim() || isSearching || installingId !== null}
221+
<InputGroup>
222+
<InputGroupAddon>
223+
{isSearching ? (
224+
<Loader2Icon aria-hidden className="animate-spin" />
225+
) : (
226+
<SearchIcon aria-hidden />
227+
)}
228+
</InputGroupAddon>
229+
<InputGroupInput
230+
aria-label="Search Open VSX themes"
231+
autoFocus
232+
onChange={(event) => setQuery(event.currentTarget.value)}
233+
placeholder="Search themes..."
236234
size="lg"
237-
type="submit"
238-
variant="outline"
239-
>
240-
{isSearching ? <Spinner /> : <SearchIcon />}
241-
Search
242-
</Button>
243-
</form>
235+
type="search"
236+
value={query}
237+
/>
238+
</InputGroup>
244239

245240
{!isSearching ? (
246241
<div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
@@ -251,7 +246,7 @@ export function ThemeSearchSection({
251246
key={suggestion}
252247
size="xs"
253248
variant="ghost"
254-
onClick={() => void runSearch(suggestion)}
249+
onClick={() => setQuery(suggestion)}
255250
>
256251
{suggestion}
257252
</Button>

0 commit comments

Comments
 (0)