/
NavBar.tsx
200 lines (186 loc) · 5.99 KB
/
NavBar.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
'use client'
import { PropsWithChildren, Suspense, lazy, useState } from 'react'
import dynamic from 'next/dynamic'
import { usePathname } from 'next/navigation'
import Skeleton from 'react-loading-skeleton'
import Logo from './Logo'
import Container from '../ui/Container'
// import NavLinks from './NavLinks'
// import UserButtons from './UserButtons'
import { SafeUser } from '@/types/safe'
import { Disclosure } from '@headlessui/react'
import Avatar from '../ui/Avatar'
import MenuButton from './MenuButton'
import Divider from '../ui/Divider'
import 'react-loading-skeleton/dist/skeleton.css'
// const UserButtons = lazy(() => import('./UserButtons'))
// const NavLinks = lazy(() => import('./NavLinks'))
const UserButtons = dynamic(() => import('./UserButtons'), {
loading: () => <Skeleton width={125} height={40} />
})
const NavLinks = dynamic(() => import('./NavLinks'), {
loading: () => (
<Skeleton
height={32}
width={64}
count={2}
inline
wrapper={SuspenseWrapper}
/>
)
})
// import ThemeSwitcher from '../ThemeSwitcher'
interface NavBarProps {
// 這個型別是從 prisma schema 定義的,可以直接當作 type 來使用
currentUser?: SafeUser | null
}
const navLinks = [
{
name: '基本設定',
href: '/portal/basic'
},
{
name: '連結設定',
href: '/portal/links'
}
]
const userLinks = [
{
name: '個人資料',
href: '/portal/account'
},
{
name: '開始我的 Link Orchard',
href: '/portal'
},
{
name: '聯絡我們',
href: '/contact-us'
},
{
name: '登出',
href: '/logout'
}
]
const NavBar: React.FC<NavBarProps> = ({ currentUser }) => {
const [full, setFull] = useState(false)
const path = usePathname()
const isAdmin = path.includes('/portal')
const avatarImage = currentUser?.customImage || currentUser?.image
return (
<Disclosure
id="disclosure"
as="nav"
className={`fixed w-full z-10 ${full ? 'h-full' : ''}`}
>
{({ open }) => {
return (
<div className={`bg-gray-200 mx-auto ${open ? 'h-full' : ''}`}>
<div className="relative flex h-16 items-center justify-between ">
<div
className={`fixed w-full px-4
${
open
? 'bg-gray-200'
: isAdmin
? 'bg-white shadow-sm'
: 'bg-grey-50'
}
min-h-[4.5rem] z-10
`}
>
<div
className={`py-4 ${
isAdmin && !open ? 'border-b-[1px]' : ''
} `}
>
<Container>
<div className="flex items-center w-full flex-grow justify-between gap-3 md:gap:0">
<Logo />
{/* <Suspense
fallback={
<Skeleton
height={32}
width={64}
count={2}
inline
wrapper={SuspenseWrapper}
/>
}
> */}
{currentUser && isAdmin && <NavLinks />}
{/* </Suspense> */}
{/* <ThemeSwitcher /> */}
<MenuButton open={open} setFull={setFull} />
{/* <Suspense fallback={<Skeleton width={125} height={40} />}> */}
<UserButtons
currentUser={currentUser}
isAdmin={isAdmin}
/>
{/* </Suspense> */}
</div>
</Container>
</div>
</div>
</div>
<Disclosure.Panel className="sm:hidden pt-2 h-full">
{isAdmin && (
<>
<div className="space-y-1 px-2 pb-3 pt-2">
{navLinks.map((link) => (
<Disclosure.Button
key={link.name}
as="a"
href={link.href}
className={`${
path === link.href
? 'bg-gray-900 text-white'
: 'text-gray-600 hover:bg-gray-700 hover:text-white'
} block px-3 py-2 rounded-md text-base font-medium`}
>
{link.name}
</Disclosure.Button>
))}
</div>
<Divider color="bg-gray-500/50" />
</>
)}
<div className="px-5 py-3">
<div className="flex items-center">
<div className="w-[40px] h-[40px] rounded-full relative">
<Avatar src={avatarImage} />
</div>
<div className="ml-3">
<div className="text-lg font-medium text-gray-800">
@{currentUser?.username}
</div>
</div>
</div>
</div>
<div className="px-2 py-3">
{userLinks.map((link) => (
<Disclosure.Button
key={link.name}
as="a"
href={link.href}
className={`${
path === link.href
? 'bg-gray-900 text-white'
: 'text-gray-600 hover:bg-gray-700 hover:text-white'
} block px-3 py-2 rounded-md text-base font-medium`}
>
{link.name}
</Disclosure.Button>
))}
</div>
</Disclosure.Panel>
</div>
)
}}
</Disclosure>
)
}
const SuspenseWrapper = ({ children }: PropsWithChildren<unknown>) => {
return <span className=" mr-4 ">{children}</span>
}
export default NavBar