317 lines
10 KiB
TypeScript
317 lines
10 KiB
TypeScript
"use client";
|
|
|
|
import { cn } from "@/lib/utils";
|
|
import { ScrollArea } from "@/components/ui/scroll-area";
|
|
import { motion, AnimatePresence } from "framer-motion";
|
|
import {
|
|
ChevronsUpDown,
|
|
LogOut,
|
|
MessagesSquare,
|
|
UserCircle,
|
|
UserPlus,
|
|
X,
|
|
Menu,
|
|
} from "lucide-react";
|
|
import Image from "next/image";
|
|
import Link from "next/link";
|
|
import { usePathname } from "next/navigation";
|
|
import { useState, useCallback } from "react";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu";
|
|
import { Separator } from "@/components/ui/separator";
|
|
import { signOut } from "@/utils/profile/profile";
|
|
|
|
const variants = {
|
|
open: {
|
|
x: 0,
|
|
opacity: 1,
|
|
transition: { x: { stiffness: 1000, velocity: -100 } },
|
|
},
|
|
closed: {
|
|
x: -20,
|
|
opacity: 0,
|
|
transition: { x: { stiffness: 100 } },
|
|
},
|
|
};
|
|
|
|
const staggerVariants = {
|
|
open: {
|
|
transition: { staggerChildren: 0.03, delayChildren: 0.02 },
|
|
},
|
|
};
|
|
|
|
const navItems = [
|
|
{
|
|
label: "Rooms",
|
|
href: "/rooms",
|
|
icon: MessagesSquare,
|
|
},
|
|
{
|
|
label: "Friends",
|
|
href: "/friends",
|
|
icon: UserPlus,
|
|
},
|
|
{
|
|
label: "Profile",
|
|
href: "/profile",
|
|
icon: UserCircle,
|
|
},
|
|
];
|
|
|
|
function SidebarContent({
|
|
isCollapsed,
|
|
displayName,
|
|
email,
|
|
avatarUrl,
|
|
onNavigate,
|
|
}: {
|
|
isCollapsed: boolean;
|
|
displayName: string;
|
|
email: string;
|
|
avatarUrl: string;
|
|
onNavigate?: () => void;
|
|
}) {
|
|
const pathname = usePathname();
|
|
|
|
return (
|
|
<div className="flex h-full flex-col bg-white w-full">
|
|
{/* Header / brand */}
|
|
<div className="flex h-16.25 w-full shrink-0 items-center border-b border-neutral-200 px-2">
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="flex w-fit items-center gap-2 px-2"
|
|
asChild
|
|
>
|
|
<Link href="/rooms">
|
|
<div className="flex size-5 items-center justify-center">
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
viewBox="0 0 24 24"
|
|
fill="currentColor"
|
|
className="size-5 text-neutral-900"
|
|
>
|
|
<path
|
|
fillRule="evenodd"
|
|
d="M4.848 2.771A49.144 49.144 0 0 1 12 2.25c2.43 0 4.817.178 7.152.52 1.978.292 3.348 2.024 3.348 3.97v6.02c0 1.946-1.37 3.678-3.348 3.97a48.901 48.901 0 0 1-3.476.383.39.39 0 0 0-.297.17l-2.755 4.133a.75.75 0 0 1-1.248 0l-2.755-4.133a.39.39 0 0 0-.297-.17 48.9 48.9 0 0 1-3.476-.384c-1.978-.29-3.348-2.024-3.348-3.97V6.741c0-1.946 1.37-3.68 3.348-3.97ZM6.75 8.25a.75.75 0 0 1 .75-.75h9a.75.75 0 0 1 0 1.5h-9a.75.75 0 0 1-.75-.75Zm.75 2.25a.75.75 0 0 0 0 1.5H12a.75.75 0 0 0 0-1.5H7.5Z"
|
|
clipRule="evenodd"
|
|
/>
|
|
</svg>
|
|
</div>
|
|
<motion.li variants={variants} className="flex items-center gap-2">
|
|
{!isCollapsed && (
|
|
<p className="text-sm font-semibold tracking-tight text-neutral-900">
|
|
Fastchat
|
|
</p>
|
|
)}
|
|
</motion.li>
|
|
</Link>
|
|
</Button>
|
|
</div>
|
|
|
|
{/* Navigation */}
|
|
<div className="flex h-full w-full flex-col">
|
|
<div className="flex grow flex-col">
|
|
<ScrollArea className="grow p-2">
|
|
<motion.ul
|
|
variants={staggerVariants}
|
|
className={cn("flex w-full flex-col gap-1")}
|
|
>
|
|
{navItems.map((item) => {
|
|
const active = pathname === item.href;
|
|
const Icon = item.icon;
|
|
return (
|
|
<Link
|
|
key={item.href}
|
|
href={item.href}
|
|
onClick={onNavigate}
|
|
className={cn(
|
|
"flex h-8 w-full flex-row items-center rounded-md px-2 py-1.5 transition hover:bg-neutral-100 hover:text-neutral-900",
|
|
active
|
|
? "bg-neutral-100 text-neutral-900"
|
|
: "text-neutral-600",
|
|
)}
|
|
>
|
|
<Icon className="h-4 w-4 shrink-0" />
|
|
<motion.li variants={variants}>
|
|
{!isCollapsed && (
|
|
<p className="ml-2 text-sm font-medium">{item.label}</p>
|
|
)}
|
|
</motion.li>
|
|
</Link>
|
|
);
|
|
})}
|
|
</motion.ul>
|
|
</ScrollArea>
|
|
</div>
|
|
|
|
{/* User section */}
|
|
<div className="flex flex-col p-2">
|
|
<Separator className="mb-2" />
|
|
<DropdownMenu modal={false}>
|
|
<DropdownMenuTrigger className="w-full">
|
|
<div className="flex h-8 w-full flex-row items-center gap-2 rounded-md px-2 py-1.5 transition hover:bg-neutral-100 hover:text-neutral-900">
|
|
<div className="relative size-4 shrink-0">
|
|
{avatarUrl ? (
|
|
<Image
|
|
src={avatarUrl}
|
|
alt=""
|
|
width={200}
|
|
height={200}
|
|
className="size-4 rounded-full object-cover"
|
|
/>
|
|
) : (
|
|
<div className="flex size-4 items-center justify-center rounded-full bg-neutral-900 text-[8px] font-medium text-white">
|
|
{displayName.charAt(0).toUpperCase()}
|
|
</div>
|
|
)}
|
|
</div>
|
|
<motion.li
|
|
variants={variants}
|
|
className="flex w-full items-center gap-2"
|
|
>
|
|
{!isCollapsed && (
|
|
<>
|
|
<p className="truncate text-sm font-medium">
|
|
{displayName}
|
|
</p>
|
|
<ChevronsUpDown className="ml-auto h-4 w-4 text-neutral-400" />
|
|
</>
|
|
)}
|
|
</motion.li>
|
|
</div>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent sideOffset={5} align="start">
|
|
<div className="flex flex-row items-center gap-2 p-2">
|
|
<div className="relative size-6 shrink-0">
|
|
{avatarUrl ? (
|
|
<Image
|
|
src={avatarUrl}
|
|
alt=""
|
|
width={200}
|
|
height={200}
|
|
className="size-6 rounded-full object-cover"
|
|
/>
|
|
) : (
|
|
<div className="flex size-6 items-center justify-center rounded-full bg-neutral-900 text-[10px] font-medium text-white">
|
|
{displayName.charAt(0).toUpperCase()}
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div className="flex flex-col text-left">
|
|
<span className="text-sm font-medium">{displayName}</span>
|
|
<span className="line-clamp-1 text-xs text-neutral-500">
|
|
{email}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem asChild className="flex items-center gap-2">
|
|
<Link href="/profile">
|
|
<UserCircle className="h-4 w-4" /> Profile
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
className="flex items-center gap-2"
|
|
onSelect={(e) => e.preventDefault()}
|
|
>
|
|
<form action={signOut} className="contents">
|
|
<button type="submit" className="flex items-center gap-2">
|
|
<LogOut className="h-4 w-4" /> Sign out
|
|
</button>
|
|
</form>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Sidebar({
|
|
displayName,
|
|
email,
|
|
avatarUrl,
|
|
}: Readonly<{
|
|
displayName: string;
|
|
email: string;
|
|
avatarUrl: string;
|
|
}>) {
|
|
const [mobileOpen, setMobileOpen] = useState(false);
|
|
|
|
const closeMobile = useCallback(() => setMobileOpen(false), []);
|
|
|
|
return (
|
|
<>
|
|
{/* Mobile hamburger button — visible on small screens */}
|
|
<button
|
|
type="button"
|
|
onClick={() => setMobileOpen(true)}
|
|
className="fixed left-4 top-3.5 z-50 flex size-9 items-center justify-center rounded-lg bg-white shadow-sm border border-neutral-200 text-neutral-700 md:hidden"
|
|
aria-label="Open menu"
|
|
>
|
|
<Menu className="size-5" />
|
|
</button>
|
|
|
|
{/* Mobile overlay sidebar */}
|
|
<AnimatePresence>
|
|
{mobileOpen && (
|
|
<>
|
|
{/* Backdrop */}
|
|
<motion.div
|
|
className="fixed inset-0 z-40 bg-black/40 md:hidden"
|
|
initial={{ opacity: 0 }}
|
|
animate={{ opacity: 1 }}
|
|
exit={{ opacity: 0 }}
|
|
onClick={closeMobile}
|
|
/>
|
|
{/* Sidebar panel */}
|
|
<motion.div
|
|
className="fixed left-0 top-0 z-50 h-full w-64 bg-white shadow-xl md:hidden"
|
|
initial={{ x: "-100%" }}
|
|
animate={{ x: 0 }}
|
|
exit={{ x: "-100%" }}
|
|
transition={{ type: "tween", ease: "easeOut", duration: 0.2 }}
|
|
>
|
|
<div className="absolute right-2 top-2">
|
|
<button
|
|
type="button"
|
|
onClick={closeMobile}
|
|
className="flex size-8 items-center justify-center rounded-md text-neutral-400 hover:bg-neutral-100 hover:text-neutral-700"
|
|
aria-label="Close menu"
|
|
>
|
|
<X className="size-4" />
|
|
</button>
|
|
</div>
|
|
<SidebarContent
|
|
isCollapsed={false}
|
|
displayName={displayName}
|
|
email={email}
|
|
avatarUrl={avatarUrl}
|
|
onNavigate={closeMobile}
|
|
/>
|
|
</motion.div>
|
|
</>
|
|
)}
|
|
</AnimatePresence>
|
|
|
|
{/* Desktop sidebar — always open */}
|
|
<div className="hidden md:flex h-full w-64 shrink-0 border-r border-neutral-200">
|
|
<SidebarContent
|
|
isCollapsed={false}
|
|
displayName={displayName}
|
|
email={email}
|
|
avatarUrl={avatarUrl}
|
|
/>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|