-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHeader.tsx
More file actions
105 lines (88 loc) · 2.97 KB
/
Header.tsx
File metadata and controls
105 lines (88 loc) · 2.97 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
import { useState, useEffect } from 'react';
import { useNavigate, useLocation, Link } from 'react-router-dom';
import * as styles from './Header.css';
import { PATH } from '@/route/path';
import IcLogo from '@/assets/svg/IcLogo';
import LoginModal from '@/common/component/LoginModal/LoginModal';
import UserModal from '@/common/component/UserModal/UserModal';
import { useModal } from '@/common/hook/useModal';
import { useGetUser } from '@/api/domain/signup/hook/useGetUser';
const MENUS = [
{ label: '나의 할 일', path: PATH.TODO },
{ label: '나의 만다라트', path: PATH.MANDAL },
{ label: '나의 히스토리', path: PATH.HISTORY },
];
const Header = () => {
const navigate = useNavigate();
const location = useLocation();
const accessToken = localStorage.getItem('accessToken');
const findActiveMenu = MENUS.find((menu) => location.pathname.startsWith(menu.path));
const initialMenu = findActiveMenu ? findActiveMenu.label : '';
const [activeMenu, setActiveMenu] = useState<string>(initialMenu);
const [openProfile, setOpenProfile] = useState<boolean>(false);
const { openModal, closeModal, ModalWrapper } = useModal();
const [isLoggedIn, setIsLoggedIn] = useState<boolean>(!!accessToken);
const { data: user, isLoading } = useGetUser();
useEffect(() => {
setIsLoggedIn(!!localStorage.getItem('accessToken'));
}, []);
const handleLogin = () => {
openModal(<LoginModal onClose={closeModal} />);
};
const handleMenuClick = (menuLabel: string, path: string) => {
setActiveMenu(menuLabel);
navigate(path);
};
const handleProfile = () => {
setOpenProfile((prev) => !prev);
};
const renderNavMenu = () => (
<>
<nav className={styles.navWrapper}>
{MENUS.map((menu) => {
const isActive = activeMenu === menu.label;
const buttonClass = `${styles.navItem} ${isActive ? styles.navItemActive : ''}`;
return (
<button
key={menu.label}
className={buttonClass}
onClick={() => handleMenuClick(menu.label, menu.path)}
aria-current={isActive ? 'page' : undefined}
>
{menu.label}
</button>
);
})}
</nav>
{!isLoading && user && (
<>
<img
src={user.profileImageUrl}
alt="유저 프로필"
className={styles.profilePlaceholder}
onClick={handleProfile}
/>
{openProfile && <UserModal onClose={handleProfile} />}
</>
)}
</>
);
return (
<header className={styles.header}>
<div className={styles.headerInner}>
<Link to={PATH.ROOT}>
<IcLogo className={styles.logoImage} />
</Link>
{isLoggedIn ? (
renderNavMenu()
) : (
<button className={styles.loginButton} onClick={handleLogin}>
로그인
</button>
)}
</div>
{ModalWrapper}
</header>
);
};
export default Header;