Skip to content

Commit 3f93761

Browse files
committed
Refactor: Conditionally pass search query to history hook based on search activity.
1 parent 3ff8f95 commit 3f93761

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

client/src/Pages/Music/History.jsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ const HistoryPage = () => {
190190
const [currentPage, setCurrentPage] = useState(1)
191191
const [pageSize, setPageSize] = useState(28)
192192
const [showFilters, setShowFilters] = useState(false)
193+
const isSearchActive = debouncedSearch && debouncedSearch.length >= 3
193194

194195
useEffect(() => {
195196
const q = searchQuery.trim()
@@ -198,26 +199,21 @@ const HistoryPage = () => {
198199
if (q.length >= 3) {
199200
setDebouncedSearch(q)
200201
} else {
201-
setDebouncedSearch("") // disable search
202+
setDebouncedSearch("")
202203
}
203204
setCurrentPage(1)
204205
}, 500)
205206

206207
return () => clearTimeout(timer)
207208
}, [searchQuery])
208209

209-
const { data, isLoading, isError, error, refetch } = useHistoryQuery(
210-
{
211-
page: currentPage,
212-
limit: pageSize,
213-
sortBy,
214-
sortOrder,
215-
searchQuery: debouncedSearch,
216-
},
217-
{
218-
enabled: debouncedSearch.length >= 3,
219-
},
220-
)
210+
const { data, isLoading, isError, error, refetch } = useHistoryQuery({
211+
page: currentPage,
212+
limit: pageSize,
213+
sortBy,
214+
sortOrder,
215+
searchQuery: isSearchActive ? debouncedSearch : null,
216+
})
221217

222218
const songs = data?.songs ?? []
223219
const totalCount = data?.count ?? 0

0 commit comments

Comments
 (0)