|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { |
| 4 | + BellIcon, |
| 5 | + CreditCardIcon, |
| 6 | + LogOutIcon, |
| 7 | + UserCircleIcon, |
| 8 | +} from 'lucide-react' |
| 9 | + |
| 10 | +import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' |
| 11 | +import { |
| 12 | + DropdownMenu, |
| 13 | + DropdownMenuContent, |
| 14 | + DropdownMenuGroup, |
| 15 | + DropdownMenuItem, |
| 16 | + DropdownMenuLabel, |
| 17 | + DropdownMenuSeparator, |
| 18 | + DropdownMenuTrigger, |
| 19 | +} from '@/components/ui/dropdown-menu' |
| 20 | +import { Button } from './ui/button' |
| 21 | +import { useEffect } from 'react' |
| 22 | +import { useToast } from './hooks/use-toast' |
| 23 | +import { Notification } from '@/lib/types/notification' |
| 24 | +import { streamNoti } from '@/lib/api/noti' |
| 25 | +import { logoutAction } from '@/lib/actions/logout' |
| 26 | + |
| 27 | +export function NavUser({ |
| 28 | + user, |
| 29 | +}: { |
| 30 | + user: { |
| 31 | + id: string |
| 32 | + name: string |
| 33 | + email: string |
| 34 | + avatar: string |
| 35 | + } |
| 36 | +}) { |
| 37 | + const { toast } = useToast() |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + function onMessage(data: Notification) { |
| 41 | + toast({ |
| 42 | + title: data.title, |
| 43 | + description: data.message, |
| 44 | + variant: 'default', |
| 45 | + }) |
| 46 | + } |
| 47 | + function onError(event: Event) { |
| 48 | + toast({ |
| 49 | + title: 'Error', |
| 50 | + description: JSON.stringify(event), |
| 51 | + variant: 'destructive', |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + const cleanup = streamNoti(user.id, { |
| 56 | + onMessage: onMessage, |
| 57 | + onError: onError, |
| 58 | + onConnect: console.log, |
| 59 | + }) |
| 60 | + return cleanup |
| 61 | + }, [user.id, toast]) |
| 62 | + |
| 63 | + return ( |
| 64 | + <DropdownMenu> |
| 65 | + <DropdownMenuTrigger asChild> |
| 66 | + <Button variant="ghost" className="relative h-8 w-8 rounded-full"> |
| 67 | + <Avatar className="w-8 h-8"> |
| 68 | + <AvatarImage src={user.avatar} alt={user.name} /> |
| 69 | + <AvatarFallback className="rounded-lg">AO</AvatarFallback> |
| 70 | + </Avatar> |
| 71 | + </Button> |
| 72 | + </DropdownMenuTrigger> |
| 73 | + <DropdownMenuContent |
| 74 | + className="w-[--radix-dropdown-menu-trigger-width] min-w-56 rounded-lg" |
| 75 | + // side={isMobile ? "bottom" : "right"} |
| 76 | + side="bottom" |
| 77 | + align="end" |
| 78 | + sideOffset={4} |
| 79 | + > |
| 80 | + <DropdownMenuLabel className="p-0 font-normal"> |
| 81 | + <div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm"> |
| 82 | + <Avatar className="h-8 w-8 rounded-lg"> |
| 83 | + <AvatarImage src={user.avatar} alt={user.name} /> |
| 84 | + <AvatarFallback className="rounded-lg">AO</AvatarFallback> |
| 85 | + </Avatar> |
| 86 | + <div className="grid flex-1 text-left text-sm leading-tight"> |
| 87 | + <span className="truncate font-medium">{user.name}</span> |
| 88 | + <span className="truncate text-xs text-muted-foreground"> |
| 89 | + {user.email} |
| 90 | + </span> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + </DropdownMenuLabel> |
| 94 | + <DropdownMenuSeparator /> |
| 95 | + <DropdownMenuGroup> |
| 96 | + <DropdownMenuItem> |
| 97 | + <UserCircleIcon /> |
| 98 | + Account |
| 99 | + </DropdownMenuItem> |
| 100 | + <DropdownMenuItem> |
| 101 | + <CreditCardIcon /> |
| 102 | + Billing |
| 103 | + </DropdownMenuItem> |
| 104 | + <DropdownMenuItem> |
| 105 | + <BellIcon /> |
| 106 | + Notifications |
| 107 | + </DropdownMenuItem> |
| 108 | + </DropdownMenuGroup> |
| 109 | + <DropdownMenuSeparator /> |
| 110 | + <DropdownMenuItem onClick={logoutAction}> |
| 111 | + <LogOutIcon /> |
| 112 | + Log out |
| 113 | + </DropdownMenuItem> |
| 114 | + </DropdownMenuContent> |
| 115 | + </DropdownMenu> |
| 116 | + ) |
| 117 | +} |
0 commit comments