-
Notifications
You must be signed in to change notification settings - Fork 1
/
NavbarTop.tsx
204 lines (182 loc) · 5.15 KB
/
NavbarTop.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
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
import { useEffect, useState } from "react";
import Image from "next/image";
import {
Image as ImageBase,
Box,
Button,
ButtonGroup,
Container,
Flex,
HStack,
IconButton,
Menu,
MenuButton,
MenuItem,
MenuList,
Text,
useColorMode,
} from "@chakra-ui/react";
import { FiMenu, FiSettings, FiLogOut, FiLock } from "react-icons/fi";
import { signOut } from "next-auth/react";
import { getSession } from "next-auth/react";
import { UserSession } from "next-auth";
import { useRouter } from "next/router";
import avatarDarkImage from "../../assets/_dark/avatar.png";
import avatarlightImage from "../../assets/_light/avatar.png";
import { useProfile } from "@/hooks/useProfile";
import logoDark from "../../assets/_dark/logo.png";
import logoLight from "../../assets/_light/logo.png";
import useAuth from "@/hooks/useAuth";
export default function NavbarTop() {
const { colorMode } = useColorMode();
const router = useRouter();
const [user, setUser] = useState<UserSession>({} as UserSession);
const [photoProfile, setPhotoProfile] = useState("");
const { userProfile } = useProfile();
const isAuthenticated = useAuth(true);
const menuList = [
{
title: "Dashboard",
route: "dashboard",
role: "ROLE_MANAGER",
},
{
title: "Débitos",
route: "debt",
role: "ROLE_MANAGER",
},
{
title: "Usuários",
route: "admin/user",
role: "ROLE_ADMIN",
},
{
title: "Categorias",
route: "admin/category",
role: "ROLE_ADMIN",
},
{
title: "Históricos",
route: "history",
role: "ROLE_MANAGER",
},
];
function handleSignOut() {
signOut({
callbackUrl: `${window.location.origin}/signIn`,
});
}
function handleSettings() {
router.push({
pathname: "/settings",
});
}
function handleProfile() {
router.push({
pathname: "/profile",
});
}
function handleRouteItem(route: string) {
router.push({
pathname: "/" + route,
});
}
async function loadSession() {
const session = await getSession();
if (session?.user) {
setUser(session.user);
}
}
useEffect(() => {
loadSession();
}, []);
useEffect(() => {
if (userProfile?.user?.photo) setPhotoProfile(userProfile.user.photo);
}, [userProfile?.user?.photo]);
return (
<>
<Box as="section">
<Box
as="nav"
bg={colorMode == "dark" ? "black" : "secondary.400"}
boxShadow="md"
>
<Flex justify="space-between" flex="1" p={1}>
<HStack>
<Image
src={colorMode == "dark" ? logoLight : logoLight}
alt="Brand Image"
style={{ height: "auto", width: "90px" }}
/>
</HStack>
<ButtonGroup variant="link" spacing="8">
{menuList.map((item) => {
if (
(user.name !== "admin" && item.role === "ROLE_MANAGER") ||
(user.name === "admin" && item.role === user.role)
) {
return (
<Button
color="white"
key={item.title}
onClick={() => handleRouteItem(item.route)}
>
{item.title}
</Button>
);
}
})}
</ButtonGroup>
<Menu>
<MenuButton
mt={2}
as={IconButton}
aria-label="Options"
icon={
<FiMenu color={colorMode == "dark" ? "gray.50" : "white"} />
}
variant="outline"
borderColor={colorMode == "dark" ? "gray.50" : "white"}
/>
<MenuList>
<MenuItem justifyContent="center">
{userProfile?.user?.photo ? (
<ImageBase
borderRadius="full"
w={50}
h={50}
src={`data:image/jpeg;base64,${userProfile?.user?.photo}`}
alt="Photo Register"
/>
) : (
<Image
src={
colorMode == "dark" ? avatarDarkImage : avatarlightImage
}
width={50}
alt="Brand Image"
/>
)}
</MenuItem>
<MenuItem justifyContent="center">
<Text fontSize="xl" as="b">
Olá, {user.name}
</Text>
</MenuItem>
<MenuItem icon={<FiSettings />} onClick={handleSettings}>
Configurações
</MenuItem>
<MenuItem icon={<FiLock />} onClick={handleProfile}>
Alterar Senha
</MenuItem>
<MenuItem onClick={handleSignOut} icon={<FiLogOut />}>
Sair
</MenuItem>
</MenuList>
</Menu>
</Flex>
</Box>
</Box>
</>
);
}