2024-12-07 14:06:25 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import {
|
|
|
|
Bell,
|
2024-12-08 16:37:35 +00:00
|
|
|
LogIn,
|
2024-12-07 14:06:25 +00:00
|
|
|
LogOut,
|
2024-12-09 05:22:10 +00:00
|
|
|
Settings,
|
2024-12-08 16:37:35 +00:00
|
|
|
ChevronsUpDown,
|
2024-12-09 05:22:10 +00:00
|
|
|
CircleUserRound,
|
2024-12-07 14:06:25 +00:00
|
|
|
} from "lucide-react";
|
2024-12-08 16:37:35 +00:00
|
|
|
import { useMemo } from "react";
|
2024-12-07 14:06:25 +00:00
|
|
|
import {
|
2024-12-08 16:37:35 +00:00
|
|
|
useSidebar,
|
2024-12-07 14:06:25 +00:00
|
|
|
SidebarMenu,
|
|
|
|
SidebarMenuButton,
|
|
|
|
SidebarMenuItem,
|
|
|
|
} from "@/components/ui/sidebar";
|
|
|
|
import {
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownMenuItem,
|
2024-12-08 16:37:35 +00:00
|
|
|
DropdownMenuGroup,
|
2024-12-07 14:06:25 +00:00
|
|
|
DropdownMenuLabel,
|
2024-12-08 16:37:35 +00:00
|
|
|
DropdownMenuContent,
|
2024-12-07 14:06:25 +00:00
|
|
|
DropdownMenuTrigger,
|
2024-12-08 16:37:35 +00:00
|
|
|
DropdownMenuSeparator,
|
2024-12-07 14:06:25 +00:00
|
|
|
} from "@/components/ui/dropdown-menu";
|
2024-12-12 12:51:10 +00:00
|
|
|
import { useTranslations } from "next-intl";
|
2024-12-09 05:22:10 +00:00
|
|
|
import useConfirmationStore from "@/store/confirmationStore";
|
|
|
|
import { signIn, signOut, useSession } from "next-auth/react";
|
2024-12-07 14:06:25 +00:00
|
|
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
|
|
|
|
2024-12-08 16:37:35 +00:00
|
|
|
const UserAvatar = ({ src, alt }: { src: string; alt: string }) => (
|
|
|
|
<Avatar className="h-8 w-8 rounded-lg">
|
|
|
|
<AvatarImage src={src} alt={alt} />
|
|
|
|
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
|
|
|
|
</Avatar>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const NavUser = ({
|
2024-12-07 14:06:25 +00:00
|
|
|
user,
|
|
|
|
}: {
|
|
|
|
user: {
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
avatar: string;
|
|
|
|
};
|
2024-12-08 16:37:35 +00:00
|
|
|
}) => {
|
2024-12-07 14:06:25 +00:00
|
|
|
const { isMobile } = useSidebar();
|
2024-12-08 16:37:35 +00:00
|
|
|
const { data: session } = useSession();
|
2024-12-12 12:51:10 +00:00
|
|
|
const t = useTranslations("Components.NavUser");
|
2024-12-09 05:22:10 +00:00
|
|
|
const { openConfirmation } = useConfirmationStore();
|
2024-12-08 16:37:35 +00:00
|
|
|
|
|
|
|
const currentUser = useMemo(() => {
|
|
|
|
if (session?.user) {
|
|
|
|
return {
|
|
|
|
name: session.user.name ?? "Unknown",
|
|
|
|
email: session.user.email ?? "Unknown",
|
|
|
|
avatar: session.user.image ?? "/gitea.svg",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}, [session, user]);
|
2024-12-07 14:06:25 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SidebarMenu>
|
|
|
|
<SidebarMenuItem>
|
|
|
|
<DropdownMenu>
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<SidebarMenuButton
|
|
|
|
size="lg"
|
|
|
|
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground"
|
|
|
|
>
|
2024-12-08 16:37:35 +00:00
|
|
|
<UserAvatar src={currentUser.avatar} alt={currentUser.name} />
|
2024-12-07 14:06:25 +00:00
|
|
|
<div className="grid flex-1 text-left text-sm leading-tight">
|
2024-12-08 16:37:35 +00:00
|
|
|
<span className="truncate font-semibold">
|
|
|
|
{currentUser.name}
|
|
|
|
</span>
|
|
|
|
<span className="truncate text-xs">{currentUser.email}</span>
|
2024-12-07 14:06:25 +00:00
|
|
|
</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}
|
|
|
|
>
|
2024-12-08 16:37:35 +00:00
|
|
|
{session?.user ? (
|
|
|
|
<>
|
|
|
|
<DropdownMenuLabel className="p-0 font-normal">
|
|
|
|
<div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm">
|
|
|
|
<UserAvatar
|
|
|
|
src={currentUser.avatar}
|
|
|
|
alt={currentUser.name}
|
|
|
|
/>
|
|
|
|
<div className="grid flex-1 text-left text-sm leading-tight">
|
|
|
|
<span className="truncate font-semibold">
|
|
|
|
{currentUser.name}
|
|
|
|
</span>
|
|
|
|
<span className="truncate text-xs">
|
|
|
|
{currentUser.email}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</DropdownMenuLabel>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
<DropdownMenuGroup>
|
|
|
|
<DropdownMenuItem>
|
2024-12-09 05:22:10 +00:00
|
|
|
<CircleUserRound />
|
2024-12-12 12:51:10 +00:00
|
|
|
{t("profile")}
|
2024-12-08 16:37:35 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuGroup>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
<DropdownMenuGroup>
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<Bell />
|
2024-12-12 12:51:10 +00:00
|
|
|
{t("notifications")}
|
2024-12-08 16:37:35 +00:00
|
|
|
</DropdownMenuItem>
|
2024-12-09 05:22:10 +00:00
|
|
|
<DropdownMenuItem>
|
|
|
|
<Settings />
|
2024-12-12 12:51:10 +00:00
|
|
|
{t("settings")}
|
2024-12-09 05:22:10 +00:00
|
|
|
</DropdownMenuItem>
|
2024-12-08 16:37:35 +00:00
|
|
|
</DropdownMenuGroup>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
<DropdownMenuGroup>
|
2024-12-09 05:22:10 +00:00
|
|
|
<DropdownMenuItem
|
|
|
|
onClick={() => {
|
|
|
|
openConfirmation({
|
|
|
|
title: "Leaving Already?",
|
|
|
|
description: "Are you sure you want to say goodbye?",
|
|
|
|
cancelLabel: "Stay",
|
|
|
|
actionLabel: "Leave",
|
|
|
|
onCancel: () => {},
|
|
|
|
onAction: () => {
|
|
|
|
signOut();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
2024-12-08 16:37:35 +00:00
|
|
|
<LogOut />
|
2024-12-12 12:51:10 +00:00
|
|
|
{t("logout")}
|
2024-12-08 16:37:35 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuGroup>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<DropdownMenuGroup>
|
2024-12-09 05:22:10 +00:00
|
|
|
<DropdownMenuItem onClick={() => signIn()}>
|
2024-12-08 16:37:35 +00:00
|
|
|
<LogIn />
|
2024-12-12 12:51:10 +00:00
|
|
|
{t("login")}
|
2024-12-08 16:37:35 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuGroup>
|
|
|
|
)}
|
2024-12-07 14:06:25 +00:00
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
</SidebarMenuItem>
|
|
|
|
</SidebarMenu>
|
|
|
|
);
|
2024-12-08 16:37:35 +00:00
|
|
|
};
|