From 84a679b0e48ed76eee2fa44d3caac83591aa3c8c Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Mon, 30 Oct 2023 11:18:11 +0100
Subject: feat(hooks): add useBoolean and useToggle hooks

---
 src/components/organisms/toolbar/toolbar.tsx | 53 +++++++++++++++-------------
 1 file changed, 28 insertions(+), 25 deletions(-)

(limited to 'src/components/organisms/toolbar/toolbar.tsx')

diff --git a/src/components/organisms/toolbar/toolbar.tsx b/src/components/organisms/toolbar/toolbar.tsx
index c400285..c0be464 100644
--- a/src/components/organisms/toolbar/toolbar.tsx
+++ b/src/components/organisms/toolbar/toolbar.tsx
@@ -1,6 +1,10 @@
 /* eslint-disable max-statements */
-import { type FC, useState, useCallback } from 'react';
-import { useOnClickOutside, useRouteChange } from '../../../utils/hooks';
+import type { FC } from 'react';
+import {
+  useBoolean,
+  useOnClickOutside,
+  useRouteChange,
+} from '../../../utils/hooks';
 import { MainNavItem, type MainNavItemProps } from './main-nav';
 import { Search, type SearchProps } from './search';
 import { Settings } from './settings';
@@ -27,54 +31,53 @@ export const Toolbar: FC<ToolbarProps> = ({
   nav,
   searchPage,
 }) => {
-  const [isNavOpened, setIsNavOpened] = useState<boolean>(false);
-  const [isSearchOpened, setIsSearchOpened] = useState<boolean>(false);
-  const [isSettingsOpened, setIsSettingsOpened] = useState<boolean>(false);
+  const {
+    deactivate: deactivateMainNav,
+    state: isMainNavOpen,
+    toggle: toggleMainNav,
+  } = useBoolean(false);
+  const {
+    deactivate: deactivateSearch,
+    state: isSearchOpen,
+    toggle: toggleSearch,
+  } = useBoolean(false);
+  const {
+    deactivate: deactivateSettings,
+    state: isSettingsOpen,
+    toggle: toggleSettings,
+  } = useBoolean(false);
 
   const mainNavRef = useOnClickOutside<HTMLDivElement>(
-    () => isNavOpened && setIsNavOpened(false)
+    () => isMainNavOpen && deactivateMainNav()
   );
   const searchRef = useOnClickOutside<HTMLDivElement>(
-    () => isSearchOpened && setIsSearchOpened(false)
+    () => isSearchOpen && deactivateSearch()
   );
   const settingsRef = useOnClickOutside<HTMLDivElement>(
-    () => isSettingsOpened && setIsSettingsOpened(false)
+    () => isSettingsOpen && deactivateSettings()
   );
 
-  const toggleMainNav = useCallback(
-    () => setIsNavOpened((prevState) => !prevState),
-    []
-  );
-  const toggleSearch = useCallback(
-    () => setIsSearchOpened((prevState) => !prevState),
-    []
-  );
-  const toggleSettings = useCallback(
-    () => setIsSettingsOpened((prevState) => !prevState),
-    []
-  );
-
-  useRouteChange(() => setIsSearchOpened(false));
+  useRouteChange(deactivateSearch);
 
   return (
     <div className={`${styles.wrapper} ${className}`}>
       <MainNavItem
         className={styles.modal}
-        isActive={isNavOpened}
+        isActive={isMainNavOpen}
         items={nav}
         ref={mainNavRef}
         setIsActive={toggleMainNav}
       />
       <Search
         className={`${styles.modal} ${styles['modal--search']}`}
-        isActive={isSearchOpened}
+        isActive={isSearchOpen}
         ref={searchRef}
         searchPage={searchPage}
         setIsActive={toggleSearch}
       />
       <Settings
         className={`${styles.modal} ${styles['modal--settings']}`}
-        isActive={isSettingsOpened}
+        isActive={isSettingsOpen}
         ref={settingsRef}
         setIsActive={toggleSettings}
       />
-- 
cgit v1.2.3

