All files / src/store/features/editor/hooks useNavigatorSearch.ts

84.61% Statements 11/13
75% Branches 3/4
50% Functions 2/4
83.33% Lines 10/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331x 1x   1x   1x 5x   5x 5x     5x                 5x       5x              
import { useCallback } from "react";
import editorActions from "store/features/editor/editorActions";
import type { NavigatorSearchKey } from "store/features/editor/editorState";
import { useAppDispatch, useAppSelector } from "store/hooks";
 
export const useNavigatorSearch = (key: NavigatorSearchKey) => {
  const dispatch = useAppDispatch();
 
  const storedSearchTerm = useAppSelector(
    (state) => state.editor.navigatorSearch[key],
  );
 
  const setSearchTerm = useCallback(
    (value: string) => {
      dispatch(
        editorActions.setNavigatorSearchTerm({ key, searchTerm: value }),
      );
    },
    [dispatch, key],
  );
 
  const toggleSearchEnabled = useCallback(() => {
    dispatch(editorActions.toggleNavigatorSearch(key));
  }, [dispatch, key]);
 
  return {
    searchEnabled: storedSearchTerm !== undefined,
    searchTerm: storedSearchTerm ?? "",
    setSearchTerm,
    toggleSearchEnabled,
  } as const;
};