-
Notifications
You must be signed in to change notification settings - Fork 788
/
Copy pathnav.tsx
250 lines (240 loc) · 7.21 KB
/
nav.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"use client";
import Link from "next/link";
import {
ArrowLeft,
BarChart3,
Edit3,
Globe,
Layout,
LayoutDashboard,
Megaphone,
Menu,
Newspaper,
Settings,
FileCode,
Github,
} from "lucide-react";
import {
useParams,
usePathname,
useSelectedLayoutSegments,
} from "next/navigation";
import { ReactNode, useEffect, useMemo, useState } from "react";
import { getSiteFromPostId } from "@/lib/actions";
import Image from "next/image";
const externalLinks = [
{
name: "Read announcement",
href: "https://vercel.com/blog/platforms-starter-kit",
icon: <Megaphone width={18} />,
},
{
name: "Star on GitHub",
href: "https://github.com/vercel/platforms",
icon: <Github width={18} />,
},
{
name: "Read the guide",
href: "https://vercel.com/guides/nextjs-multi-tenant-application",
icon: <FileCode width={18} />,
},
{
name: "View demo site",
href: "https://demo.vercel.pub",
icon: <Layout width={18} />,
},
{
name: "Deploy your own",
href: "https://vercel.com/templates/next.js/platforms-starter-kit",
icon: (
<svg
width={18}
viewBox="0 0 76 76"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className="py-1 text-black dark:text-white"
>
<path d="M37.5274 0L75.0548 65H0L37.5274 0Z" fill="currentColor" />
</svg>
),
},
];
export default function Nav({ children }: { children: ReactNode }) {
const segments = useSelectedLayoutSegments();
const { id } = useParams() as { id?: string };
const [siteId, setSiteId] = useState<string | null>();
useEffect(() => {
if (segments[0] === "post" && id) {
getSiteFromPostId(id).then((id) => {
setSiteId(id);
});
}
}, [segments, id]);
const tabs = useMemo(() => {
if (segments[0] === "site" && id) {
return [
{
name: "Back to All Sites",
href: "/sites",
icon: <ArrowLeft width={18} />,
},
{
name: "Posts",
href: `/site/${id}`,
isActive: segments.length === 2,
icon: <Newspaper width={18} />,
},
{
name: "Analytics",
href: `/site/${id}/analytics`,
isActive: segments.includes("analytics"),
icon: <BarChart3 width={18} />,
},
{
name: "Settings",
href: `/site/${id}/settings`,
isActive: segments.includes("settings"),
icon: <Settings width={18} />,
},
];
} else if (segments[0] === "post" && id) {
return [
{
name: "Back to All Posts",
href: siteId ? `/site/${siteId}` : "/sites",
icon: <ArrowLeft width={18} />,
},
{
name: "Editor",
href: `/post/${id}`,
isActive: segments.length === 2,
icon: <Edit3 width={18} />,
},
{
name: "Settings",
href: `/post/${id}/settings`,
isActive: segments.includes("settings"),
icon: <Settings width={18} />,
},
];
}
return [
{
name: "Overview",
href: "/",
isActive: segments.length === 0,
icon: <LayoutDashboard width={18} />,
},
{
name: "Sites",
href: "/sites",
isActive: segments[0] === "sites",
icon: <Globe width={18} />,
},
{
name: "Settings",
href: "/settings",
isActive: segments[0] === "settings",
icon: <Settings width={18} />,
},
];
}, [segments, id, siteId]);
const [showSidebar, setShowSidebar] = useState(false);
const pathname = usePathname();
useEffect(() => {
// hide sidebar on path change
setShowSidebar(false);
}, [pathname]);
return (
<>
<button
className={`fixed z-20 ${
// left align for Editor, right align for other pages
segments[0] === "post" && segments.length === 2 && !showSidebar
? "left-5 top-5"
: "right-5 top-7"
} sm:hidden`}
onClick={() => setShowSidebar(!showSidebar)}
>
<Menu width={20} />
</button>
<div
className={`transform ${
showSidebar ? "w-full translate-x-0" : "-translate-x-full"
} fixed z-10 flex h-full flex-col justify-between border-r border-stone-200 bg-stone-100 p-4 transition-all sm:w-60 sm:translate-x-0 dark:border-stone-700 dark:bg-stone-900`}
>
<div className="grid gap-2">
<div className="flex items-center space-x-2 rounded-lg px-2 py-1.5">
<a
href="https://vercel.com/templates/next.js/platforms-starter-kit"
target="_blank"
rel="noopener noreferrer"
className="rounded-lg p-1.5 hover:bg-stone-200 dark:hover:bg-stone-700"
>
<svg
width="26"
viewBox="0 0 76 65"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className="text-black dark:text-white"
>
<path
d="M37.5274 0L75.0548 65H0L37.5274 0Z"
fill="currentColor"
/>
</svg>
</a>
<div className="h-6 rotate-[30deg] border-l border-stone-400 dark:border-stone-500" />
<Link
href="/"
className="rounded-lg p-2 hover:bg-stone-200 dark:hover:bg-stone-700"
>
<Image
src="/logo.png"
width={24}
height={24}
alt="Logo"
className="dark:scale-110 dark:rounded-full dark:border dark:border-stone-400"
/>
</Link>
</div>
<div className="grid gap-1">
{tabs.map(({ name, href, isActive, icon }) => (
<Link
key={name}
href={href}
className={`flex items-center space-x-3 ${
isActive ? "bg-stone-200 text-black dark:bg-stone-700" : ""
} rounded-lg px-2 py-1.5 transition-all duration-150 ease-in-out hover:bg-stone-200 active:bg-stone-300 dark:text-white dark:hover:bg-stone-700 dark:active:bg-stone-800`}
>
{icon}
<span className="text-sm font-medium">{name}</span>
</Link>
))}
</div>
</div>
<div>
<div className="grid gap-1">
{externalLinks.map(({ name, href, icon }) => (
<a
key={name}
href={href}
target="_blank"
rel="noopener noreferrer"
className="flex items-center justify-between rounded-lg px-2 py-1.5 transition-all duration-150 ease-in-out hover:bg-stone-200 active:bg-stone-300 dark:text-white dark:hover:bg-stone-700 dark:active:bg-stone-800"
>
<div className="flex items-center space-x-3">
{icon}
<span className="text-sm font-medium">{name}</span>
</div>
<p>↗</p>
</a>
))}
</div>
<div className="my-2 border-t border-stone-200 dark:border-stone-700" />
{children}
</div>
</div>
</>
);
}