|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { Sheet, SheetContent, SheetTrigger } from '@/components/ui/sheet' |
| 4 | +import { Button } from '@/components/ui/button' |
| 5 | +import { LogOut } from 'lucide-react' |
| 6 | +import { signOut } from 'next-auth/react' |
| 7 | +import Image from 'next/image' |
| 8 | +import { appConfig } from 'mb-env' |
| 9 | +import type { Session } from 'next-auth' |
| 10 | +import { useState, useCallback } from 'react' |
| 11 | +import { useRouter } from 'next/navigation' |
| 12 | +import { toSlug } from 'mb-lib' |
| 13 | + |
| 14 | +interface ProfileSidebarProps { |
| 15 | + user: Session['user'] & { |
| 16 | + hasuraJwt?: string |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function getUserInitials(name: string) { |
| 21 | + const [firstName, lastName] = name.split(' ') |
| 22 | + return lastName ? `${firstName[0]}${lastName[0]}` : firstName.slice(0, 2) |
| 23 | +} |
| 24 | + |
| 25 | +export function ProfileSidebar({ user }: ProfileSidebarProps) { |
| 26 | + const [isOpen, setIsOpen] = useState(false) |
| 27 | + const router = useRouter() |
| 28 | + |
| 29 | + const handleNavigation = (path: string) => { |
| 30 | + setIsOpen(false) |
| 31 | + router.push(path) |
| 32 | + } |
| 33 | + |
| 34 | + const handleLogout = useCallback(async () => { |
| 35 | + try { |
| 36 | + setIsOpen(false) |
| 37 | + await new Promise(resolve => setTimeout(resolve, 100)) |
| 38 | + await signOut({ callbackUrl: '/' }) |
| 39 | + } catch (error) { |
| 40 | + console.error('Logout error:', error) |
| 41 | + window.location.href = '/' |
| 42 | + } |
| 43 | + }, []) |
| 44 | + |
| 45 | + const goToProfile = useCallback((e: React.MouseEvent) => { |
| 46 | + e.preventDefault() |
| 47 | + e.stopPropagation() |
| 48 | + const userSlug = toSlug(user.name || '') |
| 49 | + if (userSlug) { |
| 50 | + setIsOpen(false) |
| 51 | + router.push(`/u/${userSlug}/t`) |
| 52 | + } |
| 53 | + }, [router, user.name]) |
| 54 | + |
| 55 | + |
| 56 | + return ( |
| 57 | + <Sheet open={isOpen} onOpenChange={setIsOpen}> |
| 58 | + <SheetTrigger asChild> |
| 59 | + <Button variant="ghost" className="pl-0 rounded-full"> |
| 60 | + {user?.image ? ( |
| 61 | + <Image |
| 62 | + className="transition-opacity duration-300 rounded-full select-none size-8 bg-foreground/10 ring-1 ring-zinc-100/10 hover:opacity-80" |
| 63 | + src={user?.image ? user.image : ''} |
| 64 | + alt={user.name ?? 'Avatar'} |
| 65 | + height={32} |
| 66 | + width={32} |
| 67 | + priority |
| 68 | + /> |
| 69 | + ) : ( |
| 70 | + <div className="flex items-center justify-center text-xs font-medium uppercase rounded-full select-none size-7 shrink-0 bg-muted/50 text-muted-foreground"> |
| 71 | + {user?.name ? getUserInitials(user?.name) : null} |
| 72 | + </div> |
| 73 | + )} |
| 74 | + <span className="ml-2 hidden md:inline-block">{user?.name}</span> |
| 75 | + </Button> |
| 76 | + </SheetTrigger> |
| 77 | + <SheetContent side="right" className="w-[300px] sm:w-[400px] p-0"> |
| 78 | + <div className="flex flex-col h-full"> |
| 79 | + {/* Profile Header */} |
| 80 | + <div className="p-4 border-b"> |
| 81 | + <Button |
| 82 | + onClick={goToProfile} |
| 83 | + variant="sideBarProfile" |
| 84 | + size="sideBarProfile" |
| 85 | + > |
| 86 | + {user?.image ? ( |
| 87 | + <Image |
| 88 | + className="rounded-full size-10" |
| 89 | + src={user.image} |
| 90 | + alt={user.name ?? 'Avatar'} |
| 91 | + height={40} |
| 92 | + width={40} |
| 93 | + priority |
| 94 | + /> |
| 95 | + ) : ( |
| 96 | + <div className="flex items-center justify-center text-sm font-medium uppercase rounded-full size-10 bg-muted/50"> |
| 97 | + {user?.name ? getUserInitials(user?.name) : null} |
| 98 | + </div> |
| 99 | + )} |
| 100 | + <div className="space-y-1"> |
| 101 | + <p className="text-sm font-medium">{user?.name}</p> |
| 102 | + <p className="text-xs text-muted-foreground">{user?.email}</p> |
| 103 | + </div> |
| 104 | + </Button> |
| 105 | + </div> |
| 106 | + |
| 107 | + {/* Navigation Links - Only visible on mobile */} |
| 108 | + <nav className="flex flex-col p-4 lg:hidden"> |
| 109 | + <Button |
| 110 | + variant="ghost" |
| 111 | + className="w-full justify-start text-sm" |
| 112 | + onClick={() => handleNavigation('/c')} |
| 113 | + > |
| 114 | + Chat |
| 115 | + </Button> |
| 116 | + |
| 117 | + {appConfig.devMode && ( |
| 118 | + <Button |
| 119 | + variant="ghost" |
| 120 | + className="w-full justify-start text-sm" |
| 121 | + onClick={() => handleNavigation('/c/p')} |
| 122 | + > |
| 123 | + Pro |
| 124 | + </Button> |
| 125 | + )} |
| 126 | + |
| 127 | + <Button |
| 128 | + variant="ghost" |
| 129 | + className="w-full justify-start text-sm" |
| 130 | + onClick={() => handleNavigation('/')} |
| 131 | + > |
| 132 | + Browse |
| 133 | + </Button> |
| 134 | + |
| 135 | + {appConfig.devMode && ( |
| 136 | + <Button |
| 137 | + variant="ghost" |
| 138 | + className="w-full justify-start text-sm" |
| 139 | + onClick={() => handleNavigation('/wordware')} |
| 140 | + > |
| 141 | + Ww |
| 142 | + </Button> |
| 143 | + )} |
| 144 | + </nav> |
| 145 | + |
| 146 | + {/* Logout Button */} |
| 147 | + <div className="mt-auto p-4 border-t"> |
| 148 | + <Button |
| 149 | + variant="ghost" |
| 150 | + className="w-full justify-start text-sm" |
| 151 | + onClick={handleLogout} |
| 152 | + > |
| 153 | + <LogOut className="size-4 mr-2" /> |
| 154 | + Log Out |
| 155 | + </Button> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + </SheetContent> |
| 159 | + </Sheet> |
| 160 | + ) |
| 161 | +} |
0 commit comments