Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Feat: Updated the Nav merged the both profile and nav into one nav fo… (#1309)

Praveen Shinde96eb5dfb24ef
diff --git a/apps/web/src/components/Navigation/index.tsx b/apps/web/src/components/Navigation/index.tsx
index 6e8aae65..35f25222 100644
--- a/apps/web/src/components/Navigation/index.tsx
+++ b/apps/web/src/components/Navigation/index.tsx
@@ -1,4 +1,4 @@
-import { auth } from '@repo/auth/server';
+import { auth, type Session } from '@repo/auth/server';
import { Badge } from '@repo/ui/components/badge';
import {
DropdownMenu,
@@ -32,10 +32,13 @@ export function getAdminUrl() {
}
export async function Navigation() {
+ const session = await auth();
+
+ const isAdminOrMod = isAdminOrModerator(session);
const t = await getScopedI18n('navigation');
const featureFlags = await getAllFlags();
- const NavLinks = () => (
+ const TopSectionLinks = (
<>
{featureFlags?.enableExplore ? <NavLink title={t('explore')} href="/explore" /> : null}
{featureFlags?.enableTracks ? <NavLink title={t('tracks')} href="/tracks" /> : null}
@@ -50,6 +53,29 @@ export async function Navigation() {
</>
);
+ const NavLinks = (
+ <>
+ <div className="ml-4 hidden items-center gap-4 md:flex">{TopSectionLinks}</div>
+ <div className="flex flex-col gap-5 pl-4 md:hidden">
+ {TopSectionLinks}
+ {session?.user ? (
+ <>
+ <hr />
+ <NavLink title="Profile" href={`/@${session.user.name}`} />
+ <NavLink title="Settings" href="/settings" />
+ {isAdminOrMod ? <NavLink title="Admin" href={getAdminUrl()} /> : null}
+ {isAdminOrMod ? (
+ <NavLink title="Challenge Playground" href="/challenge-playground" />
+ ) : null}
+ <SignOutLink className="px-0" />
+ </>
+ ) : (
+ <LoginLink className="px-0 hover:bg-transparent hover:dark:bg-transparent" />
+ )}
+ </div>
+ </>
+ );
+
return (
<header className="z-50 w-full">
<NavWrapper>
@@ -82,9 +108,7 @@ export async function Navigation() {
hero <span className="text-muted-foreground bg-muted px-1 text-xs">BETA</span>
</div>
</Link>
- <div className="hidden items-center md:flex">
- <NavLinks />
- </div>
+ <div className="hidden items-center md:ml-4 md:flex md:gap-4">{NavLinks}</div>
</div>
<div className="flex">
@@ -92,10 +116,10 @@ export async function Navigation() {
<Suspense>
<Search />
</Suspense>
- {featureFlags?.enableLogin ? <LoginButton /> : null}
- <MobileNav>
- <NavLinks />
- </MobileNav>
+ {featureFlags?.enableLogin ? (
+ <LoginButton isAdminOrMod={isAdminOrMod} session={session} />
+ ) : null}
+ <MobileNav>{NavLinks}</MobileNav>
</div>
</div>
</div>
@@ -104,17 +128,19 @@ export async function Navigation() {
);
}
-async function LoginButton() {
- const session = await auth();
-
- const isAdminOrMod = isAdminOrModerator(session);
-
- return session && session.user ? (
+async function LoginButton({
+ isAdminOrMod,
+ session,
+}: {
+ isAdminOrMod: boolean;
+ session: Session | null;
+}) {
+ return session?.user ? (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
aria-label="profile button"
- className="focus:bg-accent rounded-lg p-2 duration-300 focus:outline-none focus-visible:ring-2"
+ className="focus:bg-accent hidden rounded-lg p-2 duration-300 focus:outline-none focus-visible:ring-2 md:block"
>
<User className="h-5 w-5" />
</button>
@@ -153,10 +179,12 @@ async function LoginButton() {
) : null}
<DropdownMenuSeparator />
- <SignOutLink />
+ <SignOutLink className="w-full rounded-b-lg rounded-t-sm" />
</DropdownMenuContent>
</DropdownMenu>
) : (
- <LoginLink />
+ <span className="hidden md:flex">
+ <LoginLink />
+ </span>
);
}
diff --git a/apps/web/src/components/Navigation/login-link.tsx b/apps/web/src/components/Navigation/login-link.tsx
index acebb2c9..af7aa4de 100644
--- a/apps/web/src/components/Navigation/login-link.tsx
+++ b/apps/web/src/components/Navigation/login-link.tsx
@@ -1,18 +1,22 @@
'use client';
import { LogIn } from '@repo/ui/icons';
+import clsx from 'clsx';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
const BLACKLISTED_LOGIN_REDIRECT_PATHS = ['/', '/login'];
-export function LoginLink() {
+export function LoginLink({ className }: { className?: string }) {
const pathname = usePathname();
const isBlacklistedPath = BLACKLISTED_LOGIN_REDIRECT_PATHS.some((blacklistedPath) => {
return blacklistedPath === pathname;
});
return (
<Link
- className="focus:bg-accent w-20 rounded-lg bg-transparent p-2 text-black duration-300 hover:bg-gray-200 focus:outline-none dark:text-white hover:dark:bg-gray-800"
+ className={clsx(
+ 'focus:bg-accent w-20 rounded-lg bg-transparent p-2 text-black duration-300 hover:bg-gray-200 focus:outline-none dark:text-white hover:dark:bg-gray-800',
+ className,
+ )}
href={{
pathname: '/login',
query: {
diff --git a/apps/web/src/components/Navigation/mobile-nav.tsx b/apps/web/src/components/Navigation/mobile-nav.tsx
index a101ad49..7e8a4dc2 100644
--- a/apps/web/src/components/Navigation/mobile-nav.tsx
+++ b/apps/web/src/components/Navigation/mobile-nav.tsx
@@ -1,13 +1,15 @@
'use client';
import { cn } from '@repo/ui/cn';
-import React, { useEffect } from 'react';
import { motion } from 'framer-motion';
+import { usePathname } from 'next/navigation';
+import React, { useEffect } from 'react';
interface Props {
children: React.ReactNode;
}
export function MobileNav({ children }: Props) {
+ const pathname = usePathname();
const [open, setOpen] = React.useState(false);
const [mounted, setMounted] = React.useState(false);
@@ -15,6 +17,10 @@ export function MobileNav({ children }: Props) {
setMounted(true);
}, []);
+ useEffect(() => {
+ setOpen(false);
+ }, [pathname]);
+
//blocking scroll on open nav-menu
useEffect(() => {
if (mounted && open) {
@@ -46,8 +52,6 @@ export function MobileNav({ children }: Props) {
<motion.div
key={open ? 'open' : 'close'}
initial={{ opacity: 0 }}
- // Clicking on link closes the nav
- onClickCapture={() => setOpen(false)}
animate={{ opacity: 1 }}
className={`bg-background/80 left-0 top-[55px] z-10 flex h-full w-full flex-1 snap-y flex-col gap-5 justify-self-center border-b p-3 backdrop-blur-md md:mt-0 md:hidden md:pb-0 ${
open ? 'absolute block w-full' : 'hidden'
diff --git a/apps/web/src/components/Navigation/nav-link.tsx b/apps/web/src/components/Navigation/nav-link.tsx
index 6bc752eb..22cd40d3 100644
--- a/apps/web/src/components/Navigation/nav-link.tsx
+++ b/apps/web/src/components/Navigation/nav-link.tsx
@@ -6,7 +6,7 @@ import { usePathname } from 'next/navigation';
export function NavLink({ href, title }: { href: string; title: string }) {
const pathname = usePathname();
return (
- <Link href={href} className="ml-4">
+ <Link href={href}>
<div
className={clsx('hover:text-foreground text-foreground/80 transition-colors', {
'!text-foreground': pathname === href,
diff --git a/apps/web/src/components/Navigation/signout-link.tsx b/apps/web/src/components/Navigation/signout-link.tsx
index 657259b5..3321e4eb 100644
--- a/apps/web/src/components/Navigation/signout-link.tsx
+++ b/apps/web/src/components/Navigation/signout-link.tsx
@@ -1,9 +1,10 @@
'use client';
import { signOut } from '@repo/auth/react';
import { Button } from '@repo/ui/components/button';
+import clsx from 'clsx';
import { useRouter } from 'next/navigation';
-export function SignOutLink() {
+export function SignOutLink({ className }: { className?: string }) {
const router = useRouter();
const handleSignOut = async () => {
@@ -12,7 +13,10 @@ export function SignOutLink() {
};
return (
<Button
- className="h-8 w-full justify-start rounded-b-lg rounded-t-sm bg-opacity-50 px-2 text-red-500 hover:bg-red-500/20 hover:text-red-500"
+ className={clsx(
+ 'h-8 justify-start bg-opacity-50 px-2 text-red-500 hover:bg-red-500/20 hover:text-red-500',
+ className,
+ )}
onClick={handleSignOut}
variant="ghost"
>