Improved error handling, improved sidebar for mobile
This commit is contained in:
1 parent
167baf9e82
commit
3135131ae4
3 files changed
+286
-134
No files matched your search
+280
-129
@@ -1,165 +1,316 @@
|
||||
"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";
|
||||
import Image from "next/image";
|
||||
|
||||
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: (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth={1.5}
|
||||
className="size-5"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
d="M7.5 8.25h9m-9 3H12m-9.75 1.51c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 0 1 1.037-.443 48.282 48.282 0 0 0 5.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0 0 12 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018Z"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
icon: MessagesSquare,
|
||||
},
|
||||
{
|
||||
label: "Friends",
|
||||
href: "/friends",
|
||||
icon: (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth={1.5}
|
||||
className="size-5"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
d="M15 19.128a9.38 9.38 0 0 0 2.625.372 9.337 9.337 0 0 0 4.121-.952 4.125 4.125 0 0 0-7.533-2.493M15 19.128v-.003c0-1.113-.285-2.16-.786-3.07M15 19.128v.106A12.318 12.318 0 0 1 8.624 21c-2.331 0-4.512-.645-6.374-1.766l-.001-.109a6.375 6.375 0 0 1 11.964-3.07M12 6.375a3.375 3.375 0 1 1-6.75 0 3.375 3.375 0 0 1 6.75 0Zm8.25 2.25a2.625 2.625 0 1 1-5.25 0 2.625 2.625 0 0 1 5.25 0Z"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
icon: UserPlus,
|
||||
},
|
||||
{
|
||||
label: "Profile",
|
||||
href: "/profile",
|
||||
icon: (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth={1.5}
|
||||
className="size-5"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
d="M15.75 6a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0ZM4.501 20.118a7.5 7.5 0 0 1 14.998 0A17.933 17.933 0 0 1 12 21.75c-2.676 0-5.216-.584-7.499-1.632Z"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
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 pathname = usePathname();
|
||||
}>) {
|
||||
const [mobileOpen, setMobileOpen] = useState(false);
|
||||
|
||||
const closeMobile = useCallback(() => setMobileOpen(false), []);
|
||||
|
||||
return (
|
||||
<aside className="flex w-64 flex-col border-r border-neutral-200 bg-white">
|
||||
{/* Brand */}
|
||||
<div className="flex h-16 items-center gap-2 border-b border-neutral-200 px-6">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
fill="currentColor"
|
||||
className="size-6 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>
|
||||
<span className="text-lg font-semibold tracking-tight">Fastchat</span>
|
||||
</div>
|
||||
<>
|
||||
{/* 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>
|
||||
|
||||
{/* Navigation */}
|
||||
<nav aria-label="Main navigation" className="flex-1 space-y-1 px-3 py-4">
|
||||
{navItems.map((item) => {
|
||||
const active = pathname === item.href;
|
||||
return (
|
||||
<Link
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
className={`flex items-center gap-3 rounded-lg px-3 py-2.5 text-sm font-medium transition-colors ${
|
||||
active
|
||||
? "bg-neutral-100 text-neutral-900"
|
||||
: "text-neutral-600 hover:bg-neutral-50 hover:text-neutral-900"
|
||||
}`}
|
||||
>
|
||||
{item.icon}
|
||||
{item.label}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
|
||||
{/* User section */}
|
||||
<div className="border-t border-neutral-200 p-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex size-9 items-center justify-center rounded-full bg-neutral-900 text-sm font-medium text-white">
|
||||
<Image
|
||||
src={avatarUrl}
|
||||
alt=""
|
||||
width={200}
|
||||
height={200}
|
||||
className="size-9 rounded-full object-cover"
|
||||
{/* 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}
|
||||
/>
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="truncate text-sm font-medium text-neutral-900">
|
||||
{displayName}
|
||||
</p>
|
||||
<p className="truncate text-xs text-neutral-500">{email}</p>
|
||||
</div>
|
||||
</div>
|
||||
<form action={signOut}>
|
||||
<button
|
||||
type="submit"
|
||||
className="mt-3 flex w-full items-center gap-2 rounded-lg px-3 py-2 text-sm text-neutral-600 transition-colors hover:bg-neutral-50 hover:text-neutral-900"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth={1.5}
|
||||
className="size-4"
|
||||
{/* 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 }}
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
d="M15.75 9V5.25A2.25 2.25 0 0 0 13.5 3h-6a2.25 2.25 0 0 0-2.25 2.25v13.5A2.25 2.25 0 0 0 7.5 21h6a2.25 2.25 0 0 0 2.25-2.25V15m3 0 3-3m0 0-3-3m3 3H9"
|
||||
<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}
|
||||
/>
|
||||
</svg>
|
||||
Sign out
|
||||
</button>
|
||||
</form>
|
||||
</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>
|
||||
</aside>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user