"use client"; import { Button } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Select, SelectContent, SelectItem, SelectTrigger, } from "@/components/ui/select"; import { Separator } from "@/components/ui/separator"; import { useGitHubAppProvider } from "@/providers/GitHubApp"; import { Building2, LogOut, User } from "lucide-react"; import { GitHubSVG } from "@/components/icons/github"; import { cn } from "@/lib/utils"; import { useState } from "react"; interface UserPopoverProps { className?: string; } export function UserPopover({ className }: UserPopoverProps) { const { installations, currentInstallation, installationsLoading: isLoading, installationsError: error, switchInstallation, } = useGitHubAppProvider(); const [isLoggingOut, setIsLoggingOut] = useState(false); const GITHUB_APP_INSTALLED_KEY = "github_app_installed"; const handleLogout = async () => { setIsLoggingOut(true); try { const response = await fetch("/api/auth/logout", { method: "POST", }); if (response.ok) { localStorage.removeItem(GITHUB_APP_INSTALLED_KEY); window.location.href = "/"; } else { console.error("Logout failed"); } } catch (error) { console.error("Error during logout:", error); } finally { setIsLoggingOut(false); } }; const handleValueChange = async (value: string) => { await switchInstallation(value); }; const getAccountIcon = (accountType: "User" | "Organization") => { return accountType === "Organization" ? ( ) : ( ); }; if (isLoading || !currentInstallation) { return ( ); } if (error) { return ( ); } return ( {currentInstallation.accountName} {getAccountIcon(currentInstallation.accountType)} {currentInstallation.accountType.toLowerCase()} {installations.length > 1 && ( <> Switch Account {currentInstallation.accountName} {installations.map((installation) => ( {installation.accountName} {getAccountIcon(installation.accountType)} ))} > )} {isLoggingOut ? "Signing out..." : "Sign out"} ); }