-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHeader.tsx
More file actions
189 lines (174 loc) · 6.44 KB
/
Header.tsx
File metadata and controls
189 lines (174 loc) · 6.44 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
import { useState, useEffect, useRef } from "react";
import { useNavigate, useLocation } from "react-router";
import * as S from "./Header.styled";
import LogoImage from "../../assets/home/farming_log.png";
import ProfileImage from "../../assets/home/default_profile.png";
import mainIcon from "@/assets/logos/logo.basic.png"; // 메인 아이콘
import crownIcon from "@/assets/Icons/tabler_crown.png"; // 랭킹 아이콘
import pencilIcon from "@/assets/Icons/edit-3.png"; // 파밍 아이콘
import thumbsUpIcon from "@/assets/Icons/goodgood.png"; // 응원 아이콘
import gameIcon from "@/assets/Icons/Seed.png"; // 게임 아이콘 (씨앗 아이콘 사용)
import useMediaQueries from "@/hooks/useMediaQueries";
import Popup from "@/components/Popup/popup";
import { useUserInfoQuery } from "@repo/auth/services/query/useUserInfoQuery";
import { convertTrackToString } from "@/utils/convertTrackToString";
const navItems = [
{ label: "홈", path: "/home" },
{ label: "응원", path: "/cheer" },
{ label: "파밍로그", path: "/farminglog/view" },
{ label: "게임", path: "/game" },
{ label: "랭킹", path: "/rankingDetail" },
];
export default function Header() {
const [isProfilePopupOpen, setProfilePopupOpen] = useState(false);
const [isMenuOpen, setMenuOpen] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
const navigate = useNavigate();
const location = useLocation();
const { isMobile, isTablet } = useMediaQueries();
const { data: user } = useUserInfoQuery();
const name = user?.name;
const profileImageUrl = user?.profileImageUrl;
const totalSeed = user?.totalSeed;
const handleNavigation = (path: string) => {
navigate(path);
setMenuOpen(false); // 이동 후 메뉴 닫기
};
// ✅ 메뉴 외 클릭 시 메뉴 닫기
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
setMenuOpen(false);
}
};
if (isMenuOpen) {
document.addEventListener("mousedown", handleClickOutside);
}
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [isMenuOpen]);
const ProfileAndSeed = (
<S.ProfileAndSeedContainer $isMobile={isMobile} $isTablet={isTablet}>
<S.ProfileContainer
$isMobile={isMobile}
onClick={(e) => {
e.stopPropagation();
setProfilePopupOpen(true);
}}
>
<S.ProfileImage
src={profileImageUrl || ProfileImage}
alt={name || "사용자"}
$isMobile={isMobile}
/>
<S.ProfileName $isMobile={isMobile}>{name || ""}</S.ProfileName>
</S.ProfileContainer>
<S.RecordCount $isMobile={isMobile} $isTablet={isTablet}>
<span className="seed-text">내 씨앗</span>
<span className="seed-count">{totalSeed ?? 0}</span>
</S.RecordCount>
</S.ProfileAndSeedContainer>
);
return (
<>
<S.HeaderContainer $isMobile={isMobile}>
<S.Logo
onClick={() => navigate("/home")}
$isMobile={isMobile}
$isTablet={isTablet}
>
<img src={LogoImage} alt="파밍로그" />
</S.Logo>
{isMobile ? (
<S.MobileHeader>{ProfileAndSeed}</S.MobileHeader>
) : (
<>
<S.NavWrapper>
<S.Nav>
{navItems.map(({ label, path }) => (
<S.NavItem
key={path}
$isTablet={isTablet}
$isMobile={isMobile}
onClick={() => handleNavigation(path)}
isActive={location.pathname === path}
>
{label}
</S.NavItem>
))}
</S.Nav>
</S.NavWrapper>
{ProfileAndSeed}
</>
)}
{/* ✅ 모바일 네비게이션 버튼 */}
{isMobile && (
<S.MobileWrapper ref={menuRef}>
{isMenuOpen && (
<>
<S.MobileNavButton
style={{
bottom: "70px",
right: "7px",
backgroundImage: `url(${crownIcon})`,
opacity: isMenuOpen ? 1 : 0,
transform: isMenuOpen ? "scale(1)" : "scale(0.5)",
} as React.CSSProperties}
onClick={() => handleNavigation("/rankingDetail")}
/>
<S.MobileNavButton
style={{
top: "7px",
left: "65px",
backgroundImage: `url(${thumbsUpIcon})`,
opacity: isMenuOpen ? 1 : 0,
transform: isMenuOpen ? "scale(1)" : "scale(0.5)",
} as React.CSSProperties}
onClick={() => handleNavigation("/cheer")}
/>
<S.MobileNavButton
style={{
bottom: "50px",
left: "50px",
backgroundImage: `url(${pencilIcon})`,
opacity: isMenuOpen ? 1 : 0,
transform: isMenuOpen ? "scale(1)" : "scale(0.5)",
} as React.CSSProperties}
onClick={() => handleNavigation("/farminglog/view")}
/>
<S.MobileNavButton
style={{
top: "50px",
right: "50px",
backgroundImage: `url(${gameIcon})`,
opacity: isMenuOpen ? 1 : 0,
transform: isMenuOpen ? "scale(1)" : "scale(0.5)",
} as React.CSSProperties}
onClick={() => handleNavigation("/game")}
/>
</>
)}
<S.MobileMainButton onClick={() => setMenuOpen((prev) => !prev)}>
<S.MobileMainButtonIcon src={mainIcon} alt="Menu" />
</S.MobileMainButton>
</S.MobileWrapper>
)}
</S.HeaderContainer>
{/* 프로필 팝업 */}
<Popup
isOpen={isProfilePopupOpen}
onClose={() => setProfilePopupOpen(false)}
variant="MYPAGE"
userName={user?.name}
generationAndPart={
user?.generation && user?.track
? `${user.generation}기 ${convertTrackToString(user.track)}`
: "기수 정보 없음"
}
profileImg={user?.profileImageUrl}
hasLogout={true}
/>
</>
);
}