-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBlogAppbar.tsx
More file actions
293 lines (269 loc) · 11 KB
/
BlogAppbar.tsx
File metadata and controls
293 lines (269 loc) · 11 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { useParams, useRouter } from "next/navigation";
import { signIn } from "next-auth/react";
import { useSession } from "next-auth/react";
import Image from "next/image";
import { useRecoilState } from "recoil";
import { Download, Menu, RefreshCcwDot, User, X } from "lucide-react";
import { AnimatePresence, motion } from "framer-motion";
import { DownloadIcon } from "@radix-ui/react-icons";
import { Problem, Track } from "@prisma/client";
import { isLegacyViewMode } from "@repo/store";
import { Button, Card, CardContent, Separator } from "@repo/ui";
import { ModeToggle } from "./ModeToggle";
import UserAccountDropDown from "./UserAccountDropDown";
import CustomPagination from "./CustomPagination";
export const BlogAppbar = ({
track,
problemIndex,
}: {
problem: Problem & { notionRecordMap: any };
track: Track & { problems: Problem[] };
problemIndex: number;
}) => {
const router = useRouter();
const session = useSession();
const user = session.data?.user;
const [isLegacyMode, setIsLegacyMode] = useRecoilState(isLegacyViewMode);
const { trackIds }: { trackIds?: string[] } = useParams();
const currentTrack = trackIds ? trackIds.join("/") : "";
const [isOpen, setIsOpen] = useState<boolean>(() => {
const storedPreference = localStorage.getItem("modeToggleIsOpen");
return storedPreference === "true" || false;
});
const [visible, setVisible] = useState(true);
const [lastScrollY, setLastScrollY] = useState(0);
const [scrollingDown, setScrollingDown] = useState(false);
const [prevScrollPos, setPrevScrollPos] = useState(0);
const toggleViewMode = () => {
const newViewMode = !isLegacyMode ? "legacy" : "new";
setIsLegacyMode(!isLegacyMode);
localStorage.setItem("viewMode", newViewMode);
};
const debounce = (func: any, delay: any) => {
let timeoutId: any;
return (...args: any) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func(...args);
}, delay);
};
};
const debouncedHandleScroll = debounce(() => {
const currentScrollPos = window.scrollY;
setVisible(prevScrollPos > currentScrollPos || currentScrollPos < 50);
setScrollingDown(prevScrollPos < currentScrollPos);
setPrevScrollPos(currentScrollPos);
}, 90);
const handleScroll = () => {
if (typeof window !== "undefined") {
const currentScrollY = window.scrollY;
if (currentScrollY > lastScrollY && currentScrollY > 100) {
// Scrolling down
setVisible(false);
setIsOpen(false);
} else {
// Scrolling up
setVisible(true);
}
setLastScrollY(currentScrollY);
}
};
const renderTopics = () => {
return (
<AnimatePresence>
<motion.div
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ x: -20 }}
transition={{ duration: 0.3 }}
className={`${visible ? "translate-y-0" : "-translate-y-full"} px-6`}
>
<Card className="border-primary/10 no-scrollbar max-h-[50vh] w-fit overflow-auto bg-black/10 pt-4 backdrop-blur-lg">
{track?.problems?.map((problem: { id: string; title: string }, index: number) => {
const isDisabled = currentTrack === `${track.id}/${problem.id}`;
return (
<CardContent key={index} className={isDisabled ? "opacity-50" : ""}>
<Link
key={problem.id}
prefetch
className="w-full max-w-screen-md"
href={`/tracks/${track.id}/${problem.id}`}
onClick={(e) => isDisabled && e.preventDefault()}
>
{index + 1} - {problem.title}
</Link>
</CardContent>
);
})}
</Card>
</motion.div>
</AnimatePresence>
);
};
const renderUIModeToggleButton = () => (
<Button onClick={toggleViewMode} className="flex gap-2">
<span className="hidden lg:block">{`Switch to ${isLegacyMode ? "New" : "Old"} UI`}</span>
<RefreshCcwDot size={16} />
</Button>
);
const renderBlogAppbar = () =>
isLegacyMode ? (
<div
className={`sticky top-0 z-50 flex w-full flex-col items-center justify-between gap-4 border-b bg-zinc-50 p-4 shadow-md transition-transform duration-300 md:gap-0 dark:bg-zinc-950 ${
!visible && scrollingDown ? "-translate-y-full transform" : " "
}`}
style={{ transform: !visible && !scrollingDown ? "translateY(0)" : "" }}
>
<div className="mr-2 flex w-full flex-col items-center sm:gap-2 md:flex-row md:items-center md:justify-between">
<div className="mb-2 text-3xl font-semibold text-zinc-950 md:mb-0 dark:text-zinc-100">
<Link href={"/"}>DailyCode</Link>
</div>
<p className="ml-2 hidden flex-1 items-center justify-center font-medium md:flex">
{track.title} ({problemIndex + 1} / {track.problems.length})
</p>
<div className="flex items-center space-x-2">
<CustomPagination allProblems={track.problems} isAtHeader track={track} problemIndex={problemIndex} />
<ModeToggle />
<Link href={`/pdf/${track.id}/${track.problems[problemIndex]!.id}`} target="_blank">
<Button variant="outline" className="ml-2 hidden bg-black text-white md:flex">
{/* Don't think this is required and its cluttering the AppBar at the top. Uncomment this if its required */}
{/* Download */}
<DownloadIcon />
</Button>
<Button variant="outline" className="block bg-black text-white md:hidden">
<div>
<DownloadIcon />
</div>
</Button>
</Link>
{renderUIModeToggleButton()}
<UserAccountDropDown />
</div>
</div>
<p className="ml-2 flex w-full flex-1 items-center justify-center border-t bg-opacity-60 pt-2 text-center font-medium md:hidden">
{track.title} ({problemIndex + 1} / {track.problems.length})
</p>
</div>
) : (
<>
<motion.div
className={`z-[50] flex w-full flex-col justify-between gap-2 pt-6 px-6 pb-0 md:flex-row ${visible ? "translate-y-0" : "-translate-y-full"}`}
initial={{ y: 0 }}
animate={{ y: visible ? 0 : -100 }}
transition={{ duration: 0.3 }}
>
<div className="flex items-center gap-2">
{/* menu */}
<div
onClick={() => setIsOpen(!isOpen)}
className="text-primary border-primary/10 flex cursor-pointer items-center gap-4 rounded-lg border bg-black/10 p-3 backdrop-blur-lg"
>
{isOpen ? <X className="size-6" /> : <Menu className="size-6" />}
</div>
{/* track title */}
<motion.div
initial={{ y: -20, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={{ duration: 0.5, ease: "easeInOut", type: "spring", damping: 10 }}
className="flex justify-between gap-2"
>
<div
className={`border-primary/10 flex items-center gap-4 rounded-lg border bg-black/10 p-2 backdrop-blur-lg transition-all duration-500 ease-in-out`}
>
<Link href={"/"} className="hidden cursor-pointer items-center gap-4 md:flex">
<Image
src={"https://appx-wsb-gcp.akamai.net.in/subject/2023-01-17-0.17044360120951185.jpg"}
alt="Logo"
width={200}
height={200}
className="size-8 rounded-full"
/>
</Link>
<Separator className="bg-primary/25 hidden h-6 w-0.5 md:flex" />
<h4 className="flex items-center gap-2 font-medium tracking-tighter md:max-w-[50vw] md:text-lg">
{track.title}
<span className="text-primary/80 text-sm">
{problemIndex + 1} of {track.problems.length}
</span>
</h4>
</div>
</motion.div>
</div>
<motion.div
initial={{ y: -20, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={{ duration: 0.5, ease: "easeInOut", type: "spring", damping: 10 }}
className="flex gap-2"
>
<div
className={`border-primary/10 flex items-center gap-2 rounded-lg border bg-black/10 p-2 backdrop-blur-lg transition-all duration-500 ease-in-out ${
isOpen ? `translate-y-0 opacity-100` : `-translate-y-32 opacity-0`
}`}
>
{!user ? (
<Button
size={"default"}
onClick={async () => {
await signIn();
}}
className="flex gap-2"
>
<User className="size-4" />
<span className="hidden lg:block">Login</span>
</Button>
) : (
<UserAccountDropDown />
)}
<Link href={`/pdf/${track.id}/${track.problems[problemIndex]!.id}`} target="_blank">
<Button className="flex gap-2" size={"default"}>
<span className="hidden lg:block">Download</span>
<Download className="size-4" />
</Button>
</Link>
<ModeToggle />
{renderUIModeToggleButton()}
</div>
</motion.div>
</motion.div>
{isOpen && renderTopics()}
</>
);
useEffect(() => {
window.addEventListener("scroll", isLegacyMode ? debouncedHandleScroll : handleScroll);
return () => {
window.removeEventListener("scroll", isLegacyMode ? debouncedHandleScroll : handleScroll);
};
}, [prevScrollPos, debouncedHandleScroll, lastScrollY]);
useEffect(() => {
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, [lastScrollY]);
useEffect(() => {
const savedView = localStorage.getItem("viewMode");
console.log({ savedView, check: savedView === "legacy" });
setIsLegacyMode(savedView === "legacy");
}, []);
useEffect(() => {
const handleKeyPress = (event: KeyboardEvent) => {
if (event.key === "ArrowRight") {
router.push(
problemIndex + 1 === track.problems.length
? ``
: `/tracks/${track.id}/${track.problems[problemIndex + 1]?.id}`
);
} else if (event.key === "ArrowLeft") {
router.push(problemIndex !== 0 ? `/tracks/${track.id}/${track.problems[problemIndex - 1]?.id}` : ``);
}
};
window.addEventListener("keydown", handleKeyPress);
return () => {
window.removeEventListener("keydown", handleKeyPress);
};
}, [problemIndex, router, track]);
return renderBlogAppbar();
};