-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathHeader.tsx
More file actions
149 lines (137 loc) 路 4.85 KB
/
Copy pathHeader.tsx
File metadata and controls
149 lines (137 loc) 路 4.85 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { useEffect, useState } from 'react'
import { BsFillBookmarksFill, BsFillGearFill, BsMoonFill } from 'react-icons/bs'
import { CgTab } from 'react-icons/cg'
import { IoMdSunny } from 'react-icons/io'
import { MdDoDisturbOff } from 'react-icons/md'
import { RxArrowLeft } from 'react-icons/rx'
import { Link, useLocation, useNavigate } from 'react-router-dom'
import { ReactComponent as HackertabLogo } from 'src/assets/logo.svg'
import { SearchBar } from 'src/components/Elements/SearchBar'
import { UserTags } from 'src/components/Elements/UserTags'
import { Changelog } from 'src/features/changelog'
import { identifyUserTheme, trackDNDDisable, trackThemeSelect } from 'src/lib/analytics'
import { useBookmarks } from 'src/stores/bookmarks'
import { useUserPreferences } from 'src/stores/preferences'
export const Header = () => {
const [themeIcon, setThemeIcon] = useState(<BsMoonFill />)
const { theme, setTheme, themePreferences, setThemePreferences, setDNDDuration, isDNDModeActive } = useUserPreferences()
const { userBookmarks } = useBookmarks()
const navigate = useNavigate()
const location = useLocation()
// Check and update theme based on time
useEffect(() => {
const checkAutoTheme = () => {
if (themePreferences.mode === 'auto') {
const now = new Date()
const currentHour = now.getHours()
const { autoStartHour, autoEndHour } = themePreferences
// If start hour is less than end hour, dark mode is during the same day
// If start hour is greater than end hour, dark mode spans across midnight
const isDarkModeTime = autoStartHour <= autoEndHour
? currentHour >= autoStartHour || currentHour < autoEndHour
: currentHour >= autoStartHour && currentHour < autoEndHour
const newTheme = isDarkModeTime ? 'dark' : 'light'
if (theme !== newTheme) {
setTheme(newTheme)
trackThemeSelect(newTheme)
identifyUserTheme(newTheme)
}
}
}
checkAutoTheme()
const interval = setInterval(checkAutoTheme, 60000) // Check every minute
return () => clearInterval(interval)
}, [themePreferences, theme, setTheme])
useEffect(() => {
document.documentElement.classList.add(theme)
}, [])
useEffect(() => {
if (theme === 'light') {
document.documentElement.classList.replace('dark', theme)
setThemeIcon(<BsMoonFill />)
} else if (theme === 'dark') {
document.documentElement.classList.replace('light', theme)
setThemeIcon(<IoMdSunny />)
}
}, [theme])
const onThemeChange = () => {
if (themePreferences.mode === 'auto') {
// Switch to manual mode with toggled theme
setThemePreferences({ mode: 'manual' })
const newTheme = theme === 'dark' ? 'light' : 'dark'
setTheme(newTheme)
trackThemeSelect(newTheme)
identifyUserTheme(newTheme)
} else {
// Already in manual mode, just toggle theme
const newTheme = theme === 'dark' ? 'light' : 'dark'
setTheme(newTheme)
trackThemeSelect(newTheme)
identifyUserTheme(newTheme)
}
}
const onSettingsClick = () => {
navigate('/settings/general')
}
const BookmarksBadgeCount = () => {
return userBookmarks.length > 0 ? (
userBookmarks.length < 10 ? (
<span className="badgeCount">{userBookmarks.length}</span>
) : (
<span className="badgeCount">+9</span>
)
) : null
}
const onUnpauseClicked = () => {
trackDNDDisable()
setDNDDuration('never')
}
return (
<>
<header className="AppHeader">
<span className="AppName">
<i className="logo">
<CgTab />
</i>{' '}
<Link to="/">
<HackertabLogo aria-label="hackertab.dev" className="logoText" />
</Link>
<Changelog />
</span>
<SearchBar />
<div className="extras">
{isDNDModeActive() && (
<button className="extraBtn extraTextBtn" onClick={() => onUnpauseClicked()}>
<MdDoDisturbOff />
Unpause
</button>
)}
<button aria-label="Open settings" className="extraBtn" onClick={onSettingsClick}>
<BsFillGearFill />
</button>
<button
aria-label="Toggle theme"
className="extraBtn darkModeBtn"
onClick={onThemeChange}>
{themeIcon}
</button>
<Link to="/settings/bookmarks" className="extraBtn" aria-label="Open bookmarks">
<>
<BsFillBookmarksFill />
<BookmarksBadgeCount />
</>
</Link>
</div>
{location.pathname === '/' ? (
<UserTags />
) : (
<div className="backToHome">
<Link to="/">
<RxArrowLeft size={20} /> Back
</Link>
</div>
)}
</header>
</>
)
}