-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHeader.tsx
More file actions
113 lines (94 loc) · 3.19 KB
/
Header.tsx
File metadata and controls
113 lines (94 loc) · 3.19 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
import { useEffect, useState } 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 { useModal } from '@/common/hook/useModal';
import { useGetUser } from '@/api/domain/signup/hook/useGetUser';
import UserModal from '@/common/component/UserModal/UserModal';
import { useAuthStore } from '@/store/useAuthStore';
const MENUS = [
{ label: '나의 할 일', path: PATH.TODO },
{ label: '나의 만다라트', path: PATH.MANDAL },
{ label: '나의 히스토리', path: PATH.HISTORY },
];
const Header = () => {
const navigate = useNavigate();
const location = useLocation();
const { data: userData, isLoading } = useGetUser();
const setUser = useAuthStore((state) => state.setUser);
const resetUser = useAuthStore((state) => state.resetUser);
const user = useAuthStore((state) => state.user);
const isLoggedIn = useAuthStore((state) => state.isLoggedIn);
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();
useEffect(() => {
if (isLoading) {
return;
}
if (userData) {
setUser(userData);
} else {
resetUser();
}
}, [userData, setUser, resetUser]);
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} aria-label="주요 메뉴">
{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>
<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} type="button">
로그인
</button>
)}
</div>
{ModalWrapper}
</header>
);
};
export default Header;