Skip to content

Commit

Permalink
fix: search route without path and stale debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Sep 22, 2022
1 parent dfce230 commit bebd490
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/components/Router/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { createBrowserRouter, createRoutesFromElements, createHashRouter, RouterProvider, Route } from 'react-router-dom';
import { createBrowserRouter, createHashRouter, createRoutesFromElements, Route, RouterProvider } from 'react-router-dom';

import ErrorPage from '../ErrorPage/ErrorPage';
import Root from '../Root/Root';
Expand Down Expand Up @@ -29,7 +29,7 @@ export default function Router({ error }: Props) {
<Route path="/p/:id" element={<Playlist />} />
<Route path="/m/:id/:slug" element={<Movie />} />
<Route path="/s/:id/:slug" element={<Series />} />
<Route path="/q/:query" element={<Search />} />
<Route path="/q/*" element={<Search />} />
<Route path="/u/:page/*" element={<User />} />
<Route path="/o/about" element={<About />} />
<Route
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MutableRefObject, useEffect, useRef } from 'react';

import { debounce } from '#src/utils/common';

const useDebounce = <T extends (...args: any[]) => unknown>(callback: T, time: number) => {
const fnRef = useRef() as MutableRefObject<T | undefined>;
const debounceRef = useRef(
debounce((...rest) => {
if (fnRef.current) fnRef.current(...rest);
}, time),
);

useEffect(() => {
fnRef.current = callback;
}, [callback]);

return debounceRef.current;
};

export default useDebounce;
15 changes: 7 additions & 8 deletions src/hooks/useSearchQueryUpdater.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { useCallback, useRef } from 'react';
import { useCallback } from 'react';
import { useNavigate } from 'react-router';

import { debounce } from '../utils/common';
import { useUIStore } from '../stores/UIStore';

import useDebounce from '#src/hooks/useDebounce';

const useSearchQueryUpdater = () => {
const navigate = useNavigate();

const updateSearchPath = useRef(
debounce((query: string) => {
navigate(`/q/${encodeURIComponent(query)}`);
}, 250),
);
const updateSearchPath = useDebounce((query: string) => {
navigate(`/q/${encodeURIComponent(query)}`);
}, 350);
const updateSearchQuery = useCallback((query: string) => {
useUIStore.setState({
searchQuery: query,
});
updateSearchPath.current(query);
updateSearchPath(query);
}, []);
const resetSearchQuery = useCallback(() => {
const returnPage = useUIStore.getState().preSearchPage;
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Search = () => {
const { updateSearchQuery } = useSearchQueryUpdater();
const navigate = useNavigate();
const params = useParams();
const query = params.query;
const query = params['*'];
const { isFetching, error, data: { playlist } = { playlist: [] } } = usePlaylist(features?.searchPlaylist || '', { search: query || '' }, true, !!query);

const updateBlurImage = useBlurImageUpdater(playlist);
Expand Down

0 comments on commit bebd490

Please sign in to comment.