Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
174 lines
5.9 KiB
TypeScript
174 lines
5.9 KiB
TypeScript
"use client";
|
|
|
|
import { BadgeCheck, Bird, Bomb, ChevronsUpDown, FileTextIcon, LogOut, Users2 } from "lucide-react";
|
|
import Link from "next/link";
|
|
import { useRouter } from "next/navigation";
|
|
import { useState } from "react";
|
|
|
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuGroup,
|
|
DropdownMenuItem,
|
|
DropdownMenuLabel,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu";
|
|
import {
|
|
SidebarMenu,
|
|
SidebarMenuButton,
|
|
SidebarMenuItem,
|
|
useSidebar,
|
|
} from "@/components/ui/sidebar";
|
|
import type { User } from "@/payload-types";
|
|
import { ImpersonationModal } from "@/components/frontend/impersonation/ImpersonationModal";
|
|
import { getImpersonationUsers } from "@/app/(frontend)/impersonate/actions";
|
|
|
|
export function NavUser({
|
|
user,
|
|
showCallsign = false,
|
|
canImpersonate = false,
|
|
}: {
|
|
user: {
|
|
displayName: string;
|
|
username: string;
|
|
avatar: string;
|
|
};
|
|
showCallsign?: boolean;
|
|
canImpersonate?: boolean;
|
|
}) {
|
|
const { isMobile } = useSidebar();
|
|
const router = useRouter();
|
|
const [loggingOut, setLoggingOut] = useState(false);
|
|
const [showImpersonationModal, setShowImpersonationModal] = useState(false);
|
|
const [impersonationUsers, setImpersonationUsers] = useState<any[]>([]);
|
|
const [loadingImpersonationUsers, setLoadingImpersonationUsers] = useState(false);
|
|
|
|
const primaryLine = showCallsign ? user.username : user.displayName;
|
|
const secondaryLine = showCallsign ? user.displayName : user.username;
|
|
|
|
const handleLogout = async () => {
|
|
setLoggingOut(true);
|
|
try {
|
|
await fetch("/api/users/logout", { method: "POST" });
|
|
} catch {
|
|
// ignore network errors; redirect anyway
|
|
} finally {
|
|
setLoggingOut(false);
|
|
}
|
|
router.push("/");
|
|
router.refresh();
|
|
};
|
|
|
|
const handleOpenImpersonationModal = async () => {
|
|
setLoadingImpersonationUsers(true);
|
|
try {
|
|
const users = await getImpersonationUsers();
|
|
setImpersonationUsers(users);
|
|
} catch (error) {
|
|
console.error("Failed to load impersonation users:", error);
|
|
} finally {
|
|
setLoadingImpersonationUsers(false);
|
|
}
|
|
setShowImpersonationModal(true);
|
|
};
|
|
|
|
const handleImpersonationStarted = async () => {
|
|
router.push("/");
|
|
router.refresh();
|
|
};
|
|
|
|
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={user.avatar} alt={user.displayName} />
|
|
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
|
|
</Avatar>
|
|
<div className="grid flex-1 text-left text-sm leading-tight">
|
|
<span className="truncate font-medium">{primaryLine}</span>
|
|
<span className="truncate text-xs text-muted-foreground">{secondaryLine}</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={user.avatar} alt={user.displayName} />
|
|
<AvatarFallback className="rounded-lg">
|
|
{user.username.substring(0, 2).toUpperCase()}
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
<div className="grid flex-1 text-left text-sm leading-tight">
|
|
<span className="truncate font-medium">{primaryLine}</span>
|
|
<span className="truncate text-xs">{secondaryLine}</span>
|
|
</div>
|
|
</div>
|
|
</DropdownMenuLabel>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuGroup>
|
|
<DropdownMenuItem asChild>
|
|
<Link href={`/profile/${user.username}`}>
|
|
<FileTextIcon />
|
|
Service Record
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem asChild>
|
|
<Link href="/flappy">
|
|
<Bird />
|
|
Flight Simulator
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem asChild>
|
|
<Link href="/eod">
|
|
<Bomb />
|
|
Minefield Clearance
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuGroup>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuGroup>
|
|
<DropdownMenuItem asChild>
|
|
<Link href="/account">
|
|
<BadgeCheck />
|
|
Account
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
{canImpersonate && (
|
|
<DropdownMenuItem onClick={handleOpenImpersonationModal}>
|
|
<Users2 />
|
|
Impersonate
|
|
</DropdownMenuItem>
|
|
)}
|
|
</DropdownMenuGroup>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem onSelect={() => handleLogout()}>
|
|
<LogOut />
|
|
{loggingOut ? "Logging out..." : "Log out"}
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</SidebarMenuItem>
|
|
<ImpersonationModal
|
|
open={showImpersonationModal}
|
|
onOpenChange={setShowImpersonationModal}
|
|
users={impersonationUsers}
|
|
onStart={handleImpersonationStarted}
|
|
/>
|
|
</SidebarMenu>
|
|
);
|
|
}
|