diff options
| author | Maze Winther <mazewinther@gmail.com> | 2026-03-27 00:55:15 +0100 |
|---|---|---|
| committer | Maze Winther <mazewinther@gmail.com> | 2026-03-29 15:56:48 +0200 |
| commit | 4d127a038bf768eded68aae3cfa5c1b12241bdac (patch) | |
| tree | 16bbb801caf6770546cff4fdb37b6408ec9ead98 /apps/web/src/hooks/use-sound-search.ts | |
| parent | 82817cb50cb0487350573d6129130aec705f924b (diff) | |
| download | opencut-4d127a038bf768eded68aae3cfa5c1b12241bdac.tar.gz | |
chore: normalize line endings
Diffstat (limited to 'apps/web/src/hooks/use-sound-search.ts')
| -rw-r--r-- | apps/web/src/hooks/use-sound-search.ts | 308 |
1 files changed, 154 insertions, 154 deletions
diff --git a/apps/web/src/hooks/use-sound-search.ts b/apps/web/src/hooks/use-sound-search.ts index f3c0aab4..263a01e2 100644 --- a/apps/web/src/hooks/use-sound-search.ts +++ b/apps/web/src/hooks/use-sound-search.ts @@ -1,154 +1,154 @@ -import { useEffect } from "react"; -import { useSoundsStore } from "@/stores/sounds-store"; - -export function useSoundSearch({ - query, - commercialOnly, -}: { - query: string; - commercialOnly: boolean; -}) { - const { - searchResults, - isSearching, - searchError, - lastSearchQuery, - currentPage, - hasNextPage, - isLoadingMore, - totalCount, - setSearchResults, - setSearching, - setSearchError, - setLastSearchQuery, - setCurrentPage, - setHasNextPage, - setTotalCount, - setLoadingMore, - appendSearchResults, - appendTopSounds, - resetPagination, - } = useSoundsStore(); - - const loadMore = async () => { - if (isLoadingMore || !hasNextPage) return; - - try { - setLoadingMore({ loading: true }); - const nextPage = currentPage + 1; - - const searchParams = new URLSearchParams({ - page: nextPage.toString(), - type: "effects", - }); - - if (query.trim()) { - searchParams.set("q", query); - } - - searchParams.set("commercial_only", commercialOnly.toString()); - const response = await fetch( - `/api/sounds/search?${searchParams.toString()}`, - ); - - if (response.ok) { - const data = await response.json(); - - if (query.trim()) { - appendSearchResults(data.results); - } else { - appendTopSounds(data.results); - } - - setCurrentPage({ page: nextPage }); - setHasNextPage({ hasNext: !!data.next }); - setTotalCount(data.count); - } else { - setSearchError({ error: `Load more failed: ${response.status}` }); - } - } catch (err) { - setSearchError({ - error: err instanceof Error ? err.message : "Load more failed", - }); - } finally { - setLoadingMore({ loading: false }); - } - }; - - useEffect(() => { - if (!query.trim()) { - setSearchResults({ results: [] }); - setSearchError({ error: null }); - setLastSearchQuery({ query: "" }); - return; - } - - if (query === lastSearchQuery && searchResults.length > 0) { - return; - } - - let ignore = false; - - const timeoutId = setTimeout(async () => { - try { - setSearching({ searching: true }); - setSearchError({ error: null }); - resetPagination(); - - const response = await fetch( - `/api/sounds/search?q=${encodeURIComponent(query)}&type=effects&page=1`, - ); - - if (!ignore) { - if (response.ok) { - const data = await response.json(); - setSearchResults({ results: data.results }); - setLastSearchQuery({ query: query }); - setHasNextPage({ hasNext: !!data.next }); - setTotalCount({ count: data.count }); - setCurrentPage({ page: 1 }); - } else { - setSearchError({ error: `Search failed: ${response.status}` }); - } - } - } catch (err) { - if (!ignore) { - setSearchError({ - error: err instanceof Error ? err.message : "Search failed", - }); - } - } finally { - if (!ignore) { - setSearching({ searching: false }); - } - } - }, 300); - - return () => { - clearTimeout(timeoutId); - ignore = true; - }; - }, [ - query, - lastSearchQuery, - searchResults.length, - setSearchResults, - setSearching, - setSearchError, - setLastSearchQuery, - setCurrentPage, - setHasNextPage, - setTotalCount, - resetPagination, - ]); - - return { - results: searchResults, - isLoading: isSearching, - error: searchError, - loadMore, - hasNextPage, - isLoadingMore, - totalCount, - }; -} +import { useEffect } from "react";
+import { useSoundsStore } from "@/stores/sounds-store";
+
+export function useSoundSearch({
+ query,
+ commercialOnly,
+}: {
+ query: string;
+ commercialOnly: boolean;
+}) {
+ const {
+ searchResults,
+ isSearching,
+ searchError,
+ lastSearchQuery,
+ currentPage,
+ hasNextPage,
+ isLoadingMore,
+ totalCount,
+ setSearchResults,
+ setSearching,
+ setSearchError,
+ setLastSearchQuery,
+ setCurrentPage,
+ setHasNextPage,
+ setTotalCount,
+ setLoadingMore,
+ appendSearchResults,
+ appendTopSounds,
+ resetPagination,
+ } = useSoundsStore();
+
+ const loadMore = async () => {
+ if (isLoadingMore || !hasNextPage) return;
+
+ try {
+ setLoadingMore({ loading: true });
+ const nextPage = currentPage + 1;
+
+ const searchParams = new URLSearchParams({
+ page: nextPage.toString(),
+ type: "effects",
+ });
+
+ if (query.trim()) {
+ searchParams.set("q", query);
+ }
+
+ searchParams.set("commercial_only", commercialOnly.toString());
+ const response = await fetch(
+ `/api/sounds/search?${searchParams.toString()}`,
+ );
+
+ if (response.ok) {
+ const data = await response.json();
+
+ if (query.trim()) {
+ appendSearchResults(data.results);
+ } else {
+ appendTopSounds(data.results);
+ }
+
+ setCurrentPage({ page: nextPage });
+ setHasNextPage({ hasNext: !!data.next });
+ setTotalCount(data.count);
+ } else {
+ setSearchError({ error: `Load more failed: ${response.status}` });
+ }
+ } catch (err) {
+ setSearchError({
+ error: err instanceof Error ? err.message : "Load more failed",
+ });
+ } finally {
+ setLoadingMore({ loading: false });
+ }
+ };
+
+ useEffect(() => {
+ if (!query.trim()) {
+ setSearchResults({ results: [] });
+ setSearchError({ error: null });
+ setLastSearchQuery({ query: "" });
+ return;
+ }
+
+ if (query === lastSearchQuery && searchResults.length > 0) {
+ return;
+ }
+
+ let ignore = false;
+
+ const timeoutId = setTimeout(async () => {
+ try {
+ setSearching({ searching: true });
+ setSearchError({ error: null });
+ resetPagination();
+
+ const response = await fetch(
+ `/api/sounds/search?q=${encodeURIComponent(query)}&type=effects&page=1`,
+ );
+
+ if (!ignore) {
+ if (response.ok) {
+ const data = await response.json();
+ setSearchResults({ results: data.results });
+ setLastSearchQuery({ query: query });
+ setHasNextPage({ hasNext: !!data.next });
+ setTotalCount({ count: data.count });
+ setCurrentPage({ page: 1 });
+ } else {
+ setSearchError({ error: `Search failed: ${response.status}` });
+ }
+ }
+ } catch (err) {
+ if (!ignore) {
+ setSearchError({
+ error: err instanceof Error ? err.message : "Search failed",
+ });
+ }
+ } finally {
+ if (!ignore) {
+ setSearching({ searching: false });
+ }
+ }
+ }, 300);
+
+ return () => {
+ clearTimeout(timeoutId);
+ ignore = true;
+ };
+ }, [
+ query,
+ lastSearchQuery,
+ searchResults.length,
+ setSearchResults,
+ setSearching,
+ setSearchError,
+ setLastSearchQuery,
+ setCurrentPage,
+ setHasNextPage,
+ setTotalCount,
+ resetPagination,
+ ]);
+
+ return {
+ results: searchResults,
+ isLoading: isSearching,
+ error: searchError,
+ loadMore,
+ hasNextPage,
+ isLoadingMore,
+ totalCount,
+ };
+}
|
