mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-22 15:25:46 +00:00
7c724a642f
* chore(eslint): add eslint toolchain and configs * fix(eslint): resolve gradual-config errors (rules-of-hooks, no-empty, prefer-const, …) * chore(eslint): clear unused vars in config, contexts, app and test * chore(eslint): clear unused vars in api and music-network * chore(eslint): clear unused vars in utils * chore(eslint): clear unused vars in store * chore(eslint): clear unused vars in cover and hooks * chore(eslint): clear unused vars in components * chore(eslint): clear unused vars in pages * chore(eslint): remove explicit any in src * chore(eslint): align react-hooks exhaustive-deps * chore(eslint): zero gradual config on src * chore(eslint): strict hook rules in store and utils * chore(eslint): strict hook rules in hooks and cover * chore(eslint): strict hook rules in components * chore(eslint): strict hook rules in pages and contexts * chore(eslint): document scripts ignore in eslint config * chore(eslint): add lint script and zero strict findings * chore(eslint): address review round 1 (gradual 0/0, per-site disable reasons) * chore(eslint): tighten two set-state disable comments (review round 2 LOW) * chore(nix): sync npmDepsHash with package-lock.json * docs(changelog): add Under the Hood entry for ESLint setup (PR #1165) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import type React from 'react';
|
|
import { search } from '../api/subsonicSearch';
|
|
import type { SubsonicSong } from '../api/subsonicTypes';
|
|
|
|
export interface PlaylistSongSearchResult {
|
|
searchResults: SubsonicSong[];
|
|
setSearchResults: React.Dispatch<React.SetStateAction<SubsonicSong[]>>;
|
|
searching: boolean;
|
|
}
|
|
|
|
export function usePlaylistSongSearch(
|
|
songs: SubsonicSong[],
|
|
searchOpen: boolean,
|
|
searchQuery: string,
|
|
): PlaylistSongSearchResult {
|
|
const [searchResults, setSearchResults] = useState<SubsonicSong[]>([]);
|
|
const [searching, setSearching] = useState(false);
|
|
const searchDebounce = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
useEffect(() => {
|
|
// React Compiler set-state-in-effect rule: state set from a timer/animation callback.
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
if (!searchOpen || !searchQuery.trim()) { setSearchResults([]); return; }
|
|
if (searchDebounce.current) clearTimeout(searchDebounce.current);
|
|
searchDebounce.current = setTimeout(async () => {
|
|
setSearching(true);
|
|
try {
|
|
const res = await search(searchQuery, { songCount: 20, artistCount: 0, albumCount: 0 });
|
|
const existingIds = new Set(songs.map(s => s.id));
|
|
setSearchResults(res.songs.filter(s => !existingIds.has(s.id)));
|
|
} catch { /* ignore: best-effort */ }
|
|
setSearching(false);
|
|
}, 350);
|
|
return () => { if (searchDebounce.current) clearTimeout(searchDebounce.current); };
|
|
}, [searchQuery, searchOpen, songs]);
|
|
|
|
return { searchResults, setSearchResults, searching };
|
|
}
|