-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathHeader.tsx
80 lines (71 loc) · 1.95 KB
/
Header.tsx
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
import LogoImg from "@/public/assets/common/img_logo.png";
import { useEffect, useState } from "react";
import { useRouter } from "next/router";
import Image from "next/image";
import { HeaderContainer, ProfileContainer } from "./headerStyled";
import GradientButton from "@/components/button/GradientButton";
import { useQuery } from "@tanstack/react-query";
import QUERY_KEYS from "@/constants/queryKey";
import { getUsers } from "@/lib/api/user";
interface User {
email: string | null;
name: string | null;
id: number | null;
image_source: string;
}
const Header = () => {
const [isFixed, setIsFixed] = useState(true);
const router = useRouter();
const { pathname } = router;
const [isAuth, setIsAuth] = useState(false);
const { data: userData, isSuccess } = useQuery<User>({
queryKey: [QUERY_KEYS.users],
queryFn: () => getUsers(),
enabled: !!isAuth,
});
const handleLoginBtn = () => {
router.push("/signin");
};
useEffect(() => {
if (pathname.includes("/folder")) {
setIsFixed(false);
return;
}
setIsFixed(true);
}, [pathname]);
useEffect(() => {
if (!localStorage.getItem("accessToken")) {
return;
}
setIsAuth(true);
}, []);
return (
<HeaderContainer $isFixed={isFixed}>
<nav className="contentContainer">
<Image
priority
src={LogoImg}
id="logoImg"
alt="logoImg"
height="24"
width="133"
onClick={() => router.push("/")}
/>
{isSuccess ? (
<ProfileContainer>
<Image
src={userData.image_source}
alt="profileImg"
width="28"
height="28"
/>
<div className="profileEmail">{userData.email}</div>
</ProfileContainer>
) : (
<GradientButton onClick={handleLoginBtn}>로그인</GradientButton>
)}
</nav>
</HeaderContainer>
);
};
export default Header;