SurfSense/surfsense_web/components/UserDropdown.tsx

112 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-07-27 10:05:37 -07:00
"use client";
import { BadgeCheck, LogOut } from "lucide-react";
2025-07-27 10:41:15 -07:00
import { useRouter } from "next/navigation";
import { useState } from "react";
2025-07-27 10:05:37 -07:00
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
2025-07-27 10:41:15 -07:00
import { Button } from "@/components/ui/button";
import {
2025-07-27 10:05:37 -07:00
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
2026-02-09 16:49:11 -08:00
import { Spinner } from "@/components/ui/spinner";
import { logout } from "@/lib/auth-utils";
import { cleanupElectric } from "@/lib/electric/client";
import { resetUser, trackLogout } from "@/lib/posthog/events";
export function UserDropdown({
2025-07-27 10:05:37 -07:00
user,
}: {
2025-07-27 10:05:37 -07:00
user: {
name: string;
email: string;
avatar: string;
};
}) {
2025-07-27 10:05:37 -07:00
const router = useRouter();
const [isLoggingOut, setIsLoggingOut] = useState(false);
const handleLogout = async () => {
if (isLoggingOut) return;
setIsLoggingOut(true);
2025-07-27 10:05:37 -07:00
try {
// Track logout event and reset PostHog identity
trackLogout();
resetUser();
// Best-effort cleanup of Electric SQL / PGlite
// Even if this fails, login-time cleanup will handle it
try {
await cleanupElectric();
} catch (err) {
console.warn("[Logout] Electric cleanup failed (will be handled on next login):", err);
}
// Revoke refresh token on server and clear all tokens from localStorage
await logout();
2025-07-27 10:05:37 -07:00
if (typeof window !== "undefined") {
window.location.href = "/";
2025-07-27 10:05:37 -07:00
}
} catch (error) {
console.error("Error during logout:", error);
// Even if there's an error, try to clear tokens and redirect
await logout();
2025-07-27 10:05:37 -07:00
if (typeof window !== "undefined") {
window.location.href = "/";
2025-07-27 10:05:37 -07:00
}
}
};
2025-07-27 10:05:37 -07:00
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="relative h-10 w-10 rounded-full">
<Avatar className="h-8 w-8">
<AvatarImage src={user.avatar} alt={user.name} />
<AvatarFallback>{user.name.charAt(0)?.toUpperCase() || "?"}</AvatarFallback>
</Avatar>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-44 md:w-56" align="end" forceMount>
<DropdownMenuLabel className="font-normal p-2 md:p-3">
2025-07-27 10:05:37 -07:00
<div className="flex flex-col space-y-1">
<p className="text-xs md:text-sm font-medium leading-none">{user.name}</p>
<p className="text-[10px] md:text-xs leading-none text-muted-foreground">
{user.email}
</p>
2025-07-27 10:05:37 -07:00
</div>
</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem
onClick={() => router.push(`/dashboard/api-key`)}
className="text-xs md:text-sm"
>
<BadgeCheck className="mr-2 h-3.5 w-3.5 md:h-4 md:w-4" />
2025-07-27 10:05:37 -07:00
API Key
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={handleLogout}
className="text-xs md:text-sm"
disabled={isLoggingOut}
>
2026-02-08 12:43:31 +05:30
{isLoggingOut ? (
<Spinner size="sm" className="mr-2" />
) : (
<LogOut className="mr-2 h-3.5 w-3.5 md:h-4 md:w-4" />
)}
{isLoggingOut ? "Logging out..." : "Log out"}
2025-07-27 10:05:37 -07:00
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}