@@ -4,49 +4,23 @@ import { CgTab } from 'react-icons/cg'
44import { BsFillBookmarksFill } from 'react-icons/bs'
55import { ReactComponent as HackertabLogo } from 'src/assets/logo.svg'
66import UserTags from './UserTags'
7- import { SUPPORTED_SEARCH_ENGINES } from '../Constants'
87import { SettingsModal } from 'src/features/settings'
98import { BsMoon } from 'react-icons/bs'
109import { IoMdSunny } from 'react-icons/io'
11- import { trackSearchEngineUse } from 'src/lib/analytics'
12- import { Changelog } from 'src/features/changelog'
13- import { GoSearch } from 'react-icons/go'
10+ import { Changelog } from 'src/features/changelog'
11+ import { SearchBar } from 'src/components/Elements/SearchBar'
12+ import { useUserPreferences } from 'src/stores/preferences'
13+ import { useBookmarks } from 'src/stores/bookmarks'
1414
15- function SearchBar ( { state } ) {
16- const keywordsInputRef = React . useRef ( null )
17- const userSearchEngine = SUPPORTED_SEARCH_ENGINES . find (
18- ( engine ) => engine . label === state . searchEngine
19- )
20-
21- const handleSubmit = ( e ) => {
22- e . preventDefault ( )
23- const keywords = e . target . children [ 1 ] . value
24- trackSearchEngineUse ( userSearchEngine . label )
25- window . open ( `${ userSearchEngine . url } ${ keywords } ` , '_self' )
26- }
27-
28- useEffect ( ( ) => {
29- keywordsInputRef . current . focus ( )
30- } , [ ] )
31-
32- return (
33- < form className = "searchBar" onSubmit = { handleSubmit } >
34- < GoSearch className = "searchBarIcon" size = { 20 } />
35- < input
36- ref = { keywordsInputRef }
37- type = "text"
38- className = "searchBarInput"
39- placeholder = { `Search on ${ userSearchEngine . label } ` }
40- />
41- </ form >
42- )
43- }
44- function Header ( { state, dispatcher, showSideBar, setShowSideBar, showSettings, setShowSettings } ) {
15+ function Header ( { showSideBar, setShowSideBar, showSettings, setShowSettings } ) {
4516 const [ themeIcon , setThemeIcon ] = useState ( < BsMoon /> )
4617 const isFirstRun = useRef ( true )
18+ const { theme, setTheme } = useUserPreferences ( )
19+ const { userBookmarks } = useBookmarks ( )
4720
4821 useEffect ( ( ) => {
49- document . documentElement . classList . add ( state . theme )
22+ document . documentElement . classList . add ( theme )
23+ // eslint-disable-next-line react-hooks/exhaustive-deps
5024 } , [ ] )
5125
5226 useEffect ( ( ) => {
@@ -58,27 +32,31 @@ function Header({ state, dispatcher, showSideBar, setShowSideBar, showSettings,
5832 }
5933 }
6034
61- if ( state . theme === 'light' ) {
62- document . documentElement . classList . replace ( 'dark' , state . theme )
35+ if ( theme === 'light' ) {
36+ document . documentElement . classList . replace ( 'dark' , theme )
6337 setThemeIcon ( < BsMoon /> )
64- } else if ( state . theme === 'dark' ) {
65- document . documentElement . classList . replace ( 'light' , state . theme )
38+ } else if ( theme === 'dark' ) {
39+ document . documentElement . classList . replace ( 'light' , theme )
6640 setThemeIcon ( < IoMdSunny /> )
6741 }
68- } , [ state ?. theme ] )
42+ } , [ theme ] )
6943
7044 const onThemeChange = ( ) => {
71- dispatcher ( { type : 'toggleTheme' } )
45+ if ( theme === 'dark' ) {
46+ setTheme ( 'light' )
47+ } else {
48+ setTheme ( 'dark' )
49+ }
7250 }
7351
7452 const onSettingsClick = ( ) => {
7553 setShowSettings ( true )
7654 }
7755
7856 const BookmarksBadgeCount = ( ) => {
79- return state . userBookmarks . length > 0 ? (
80- state . userBookmarks . length < 10 ? (
81- < span className = "badgeCount" > { state . userBookmarks . length } </ span >
57+ return userBookmarks . length > 0 ? (
58+ userBookmarks . length < 10 ? (
59+ < span className = "badgeCount" > { userBookmarks . length } </ span >
8260 ) : (
8361 < span className = "badgeCount" > +9</ span >
8462 )
@@ -97,7 +75,7 @@ function Header({ state, dispatcher, showSideBar, setShowSideBar, showSettings,
9775 < HackertabLogo className = "logoText" />
9876 < Changelog />
9977 </ span >
100- < SearchBar state = { state } />
78+ < SearchBar />
10179 < div className = "extras" >
10280 < button className = "extraBtn" onClick = { onSettingsClick } >
10381 < BsFillGearFill />
@@ -111,7 +89,7 @@ function Header({ state, dispatcher, showSideBar, setShowSideBar, showSettings,
11189 </ button >
11290 </ div >
11391 < div className = "break" > </ div >
114- < UserTags userSelectedTags = { state . userSelectedTags } onAddClicked = { onSettingsClick } />
92+ < UserTags onAddClicked = { onSettingsClick } />
11593 </ header >
11694 </ >
11795 )
0 commit comments