-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.tsx
More file actions
256 lines (236 loc) · 7.77 KB
/
index.tsx
File metadata and controls
256 lines (236 loc) · 7.77 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { FC, memo, useState, useEffect } from 'react';
import {
Navbar,
Container,
Nav,
Form,
FormControl,
Button,
Col,
} from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import {
useSearchParams,
Link,
useNavigate,
useLocation,
useMatch,
} from 'react-router-dom';
import classnames from 'classnames';
import { userCenter, floppyNavigation } from '@/utils';
import {
loggedUserInfoStore,
siteInfoStore,
brandingStore,
loginSettingStore,
themeSettingStore,
sideNavStore,
} from '@/stores';
import { logout, useQueryNotificationStatus } from '@/services';
import NavItems from './components/NavItems';
import './index.scss';
const Header: FC = () => {
const navigate = useNavigate();
const location = useLocation();
const [urlSearch] = useSearchParams();
const q = urlSearch.get('q');
const { user, clear: clearUserStore } = loggedUserInfoStore();
const { t } = useTranslation();
const [searchStr, setSearch] = useState('');
const siteInfo = siteInfoStore((state) => state.siteInfo);
const brandingInfo = brandingStore((state) => state.branding);
const loginSetting = loginSettingStore((state) => state.login);
const { updateReview, updateVisible } = sideNavStore();
const { data: redDot } = useQueryNotificationStatus();
/**
* Automatically append `tag` information when creating a question
*/
const tagMatch = useMatch('/tags/:slugName');
let askUrl = '/questions/ask';
if (tagMatch && tagMatch.params.slugName) {
askUrl = `${askUrl}?tags=${tagMatch.params.slugName}`;
}
useEffect(() => {
updateReview({
can_revision: Boolean(redDot?.can_revision),
revision: Number(redDot?.revision),
});
}, [redDot]);
const handleInput = (val) => {
setSearch(val);
};
const handleSearch = (evt) => {
evt.preventDefault();
if (!searchStr) {
return;
}
const searchUrl = `/search?q=${encodeURIComponent(searchStr)}`;
navigate(searchUrl);
};
const handleLogout = async () => {
await logout();
clearUserStore();
window.location.replace(window.location.href);
};
useEffect(() => {
if (q && location.pathname === '/search') {
handleInput(q);
}
}, [q]);
useEffect(() => {
const collapse = document.querySelector('#navBarContent');
if (collapse && collapse.classList.contains('show')) {
const toggle = document.querySelector('#navBarToggle') as HTMLElement;
if (toggle) {
toggle?.click();
}
}
// clear search input when navigate to other page
if (location.pathname !== '/search' && searchStr) {
setSearch('');
}
}, [location.pathname]);
let navbarStyle = 'theme-colored';
const { theme, theme_config } = themeSettingStore((_) => _);
if (theme_config?.[theme]?.navbar_style) {
navbarStyle = `theme-${theme_config[theme].navbar_style}`;
}
return (
<Navbar
variant={navbarStyle === 'theme-colored' ? 'dark' : ''}
expand="lg"
className={classnames('answer_header', 'sticky-top', navbarStyle)}
id="header">
<Container className="d-flex align-items-center">
<Navbar.Toggle
aria-controls="navBarContent"
className="answer-navBar me-2"
id="navBarToggle"
onClick={() => {
updateVisible();
}}
/>
<div className="d-flex justify-content-between align-items-center nav-grow flex-nowrap">
<Navbar.Brand to="/" as={Link} className="lh-1 me-0 me-sm-5 p-0">
{brandingInfo.logo ? (
<>
<img
className="d-none d-lg-block logo me-0"
src={brandingInfo.logo}
alt={siteInfo.name}
/>
<img
className="lg-none logo me-0"
src={brandingInfo.mobile_logo || brandingInfo.logo}
alt={siteInfo.name}
/>
</>
) : (
<span>{siteInfo.name}</span>
)}
</Navbar.Brand>
{/* mobile nav */}
<div className="d-flex lg-none align-items-center flex-lg-nowrap">
{user?.username ? (
<NavItems redDot={redDot} userInfo={user} logOut={handleLogout} />
) : (
<>
<Button
variant="link"
className={classnames('me-2', {
'link-light': navbarStyle === 'theme-colored',
'link-primary': navbarStyle !== 'theme-colored',
})}
onClick={() => floppyNavigation.storageLoginRedirect()}
href={userCenter.getLoginUrl()}>
{t('btns.login')}
</Button>
{loginSetting.allow_new_registrations && (
<Button
variant={
navbarStyle === 'theme-colored' ? 'light' : 'primary'
}
href={userCenter.getSignUpUrl()}>
{t('btns.signup')}
</Button>
)}
</>
)}
</div>
</div>
<Navbar.Collapse id="navBarContent" className="me-auto">
<hr className="hr lg-none mb-3" style={{ marginTop: '12px' }} />
<Col lg={8} className="ps-0">
<Form
action="/search"
className="w-100 maxw-400"
onSubmit={handleSearch}>
<FormControl
type="search"
placeholder={t('header.search.placeholder')}
className="placeholder-search"
value={searchStr}
name="q"
onChange={(e) => handleInput(e.target.value)}
/>
</Form>
</Col>
<Nav.Item className="lg-none mt-3 pb-1">
<Link
to={askUrl}
className="text-capitalize text-nowrap btn btn-light">
{t('btns.add_question')}
</Link>
</Nav.Item>
{/* pc nav */}
<Col
lg={4}
className="d-none d-lg-flex justify-content-start justify-content-sm-end">
{user?.username ? (
<Nav className="d-flex align-items-center flex-lg-nowrap">
<Nav.Item className="me-3">
<Link
to={askUrl}
className={classnames('text-capitalize text-nowrap btn', {
'btn-light': navbarStyle !== 'theme-light',
'btn-primary': navbarStyle === 'theme-light',
})}>
{t('btns.add_question')}
</Link>
</Nav.Item>
<NavItems
redDot={redDot}
userInfo={user}
logOut={handleLogout}
/>
</Nav>
) : (
<>
<Button
variant="link"
className={classnames('me-2', {
'link-light': navbarStyle === 'theme-colored',
'link-primary': navbarStyle !== 'theme-colored',
})}
onClick={() => floppyNavigation.storageLoginRedirect()}
href={userCenter.getLoginUrl()}>
{t('btns.login')}
</Button>
{loginSetting.allow_new_registrations && (
<Button
variant={
navbarStyle === 'theme-colored' ? 'light' : 'primary'
}
href={userCenter.getSignUpUrl()}>
{t('btns.signup')}
</Button>
)}
</>
)}
</Col>
</Navbar.Collapse>
</Container>
</Navbar>
);
};
export default memo(Header);