144 lines
4.8 KiB
TypeScript
144 lines
4.8 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { ChevronsUpDown } from "lucide-react";
|
|
|
|
import {
|
|
Avatar,
|
|
AvatarFallback,
|
|
AvatarImage,
|
|
} from "@/app/_components/ui/avatar";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuGroup,
|
|
DropdownMenuItem,
|
|
DropdownMenuLabel,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/app/_components/ui/dropdown-menu";
|
|
import {
|
|
SidebarMenu,
|
|
SidebarMenuButton,
|
|
SidebarMenuItem,
|
|
useSidebar,
|
|
} from "@/app/_components/ui/sidebar";
|
|
import { IconLogout, IconSettings, IconSparkles } from "@tabler/icons-react";
|
|
import type { User } from "@/src/models/users/users.model";
|
|
import { signOut } from "@/app/(auth-pages)/action";
|
|
import { SettingsDialog } from "../settings/setting-dialog";
|
|
|
|
export function NavUser({ user }: { user: User | null }) {
|
|
const { isMobile } = useSidebar();
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
|
|
|
// Use profile data with fallbacks
|
|
const firstName = user?.profile?.first_name || "";
|
|
const lastName = user?.profile?.last_name || "";
|
|
const userEmail = user?.email || "";
|
|
const userAvatar = user?.profile?.avatar || "";
|
|
|
|
const getFullName = () => {
|
|
return `${firstName} ${lastName}`.trim() || "User";
|
|
};
|
|
|
|
// Generate initials for avatar fallback
|
|
const getInitials = () => {
|
|
if (firstName && lastName) {
|
|
return `${firstName[0]}${lastName[0]}`.toUpperCase();
|
|
}
|
|
if (firstName) {
|
|
return firstName[0].toUpperCase();
|
|
}
|
|
if (userEmail) {
|
|
return userEmail[0].toUpperCase();
|
|
}
|
|
return "U";
|
|
};
|
|
|
|
// Handle dialog close after successful profile update
|
|
const handleProfileUpdateSuccess = () => {
|
|
setIsDialogOpen(false);
|
|
// You might want to refresh the user data here
|
|
};
|
|
|
|
return (
|
|
<SidebarMenu>
|
|
<SidebarMenuItem>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<SidebarMenuButton
|
|
size="lg"
|
|
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground"
|
|
>
|
|
<Avatar className="h-8 w-8 rounded-lg">
|
|
<AvatarImage src={userAvatar || ""} alt={getFullName()} />
|
|
<AvatarFallback className="rounded-lg">
|
|
{getInitials()}
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
<div className="grid flex-1 text-left text-sm leading-tight">
|
|
<span className="truncate font-semibold">{getFullName()}</span>
|
|
<span className="truncate text-xs">{userEmail}</span>
|
|
</div>
|
|
<ChevronsUpDown className="ml-auto size-4" />
|
|
</SidebarMenuButton>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent
|
|
className="w-[--radix-dropdown-menu-trigger-width] min-w-56 rounded-lg"
|
|
side={isMobile ? "bottom" : "right"}
|
|
align="end"
|
|
sideOffset={4}
|
|
>
|
|
<DropdownMenuLabel className="p-0 font-normal">
|
|
<div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm">
|
|
<Avatar className="h-8 w-8 rounded-lg">
|
|
<AvatarImage src={userAvatar || ""} alt={getFullName()} />
|
|
<AvatarFallback className="rounded-lg">
|
|
{getInitials()}
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
<div className="grid flex-1 text-left text-sm leading-tight">
|
|
<span className="truncate font-semibold">
|
|
{getFullName()}
|
|
</span>
|
|
<span className="truncate text-xs">{userEmail}</span>
|
|
</div>
|
|
</div>
|
|
</DropdownMenuLabel>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuGroup>
|
|
<DropdownMenuItem className="space-x-2">
|
|
<IconSparkles className="size-4" />
|
|
<span>Upgrade to Pro</span>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuGroup>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuGroup>
|
|
<SettingsDialog
|
|
user={user}
|
|
trigger={
|
|
<DropdownMenuItem
|
|
className="space-x-2"
|
|
onSelect={(e) => {
|
|
e.preventDefault();
|
|
}}
|
|
>
|
|
<IconSettings className="size-4" />
|
|
<span>Settings</span>
|
|
</DropdownMenuItem>
|
|
}
|
|
/>
|
|
</DropdownMenuGroup>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem onSubmit={signOut} className="space-x-2">
|
|
<IconLogout className="size-4" />
|
|
<span>Log out</span>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</SidebarMenuItem>
|
|
</SidebarMenu>
|
|
);
|
|
}
|