import { Link, useLocation, useNavigate } from '@tanstack/react-router' import { useDevToolbar } from 'dev-tools' import { FlaskConicalIcon, Loader2Icon, ScrollTextIcon, User2Icon } from 'lucide-react' import { useTheme } from 'next-themes' import { useState } from 'react' import { cn, DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuTrigger, singleThemes, } from 'ui' import { ButtonTooltip } from '../ui/ButtonTooltip' import { useFeaturePreviewModal } from './App/FeaturePreview/FeaturePreviewContext' import { DevToolbarMenuGroup } from './DevToolbarMenuGroup' import { TimezoneDropdown } from './UserDropdown/TimezoneDropdown' import { ProfileImage } from '@/components/ui/ProfileImage' import { UpgradePlanButton } from '@/components/ui/UpgradePlanButton' import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled' import { useShowUpgradeCta } from '@/hooks/misc/useShowUpgradeCta' import { IS_PLATFORM } from '@/lib/constants' import { useProfileNameAndPicture } from '@/lib/profile' import { useTrack } from '@/lib/telemetry/track' import { useAppStateSnapshot } from '@/state/app-state' export function UserDropdown({ triggerClassName, contentClassName, }: { triggerClassName?: string contentClassName?: string }) { const navigate = useNavigate() const pathname = useLocation({ select: (location) => location.pathname }) const currentRoute = useLocation({ select: (location) => location.href }) const { theme, setTheme } = useTheme() const appStateSnapshot = useAppStateSnapshot() const profileShowEmailEnabled = useIsFeatureEnabled('profile:show_email') const { username, avatarUrl, primaryEmail, isLoading } = useProfileNameAndPicture() const { toggleFeaturePreviewModal } = useFeaturePreviewModal() const track = useTrack() const { isAvailable: isDevToolbarAvailable } = useDevToolbar() const shouldShowSectionSeparator = IS_PLATFORM || isDevToolbarAvailable // The upgrade CTA is org-scoped, so only enable it on routes where an org is in scope. // Excludes /account/*, /organizations, /new, marketing routes, etc. Gating the hook here // also skips fetching organization data on routes where the CTA can't render. const isOrgScopedRoute = pathname.startsWith('/project/') || pathname.startsWith('/org/') const { showUpgradeCta } = useShowUpgradeCta({ enabled: isOrgScopedRoute }) const [isOpen, setIsOpen] = useState(false) return ( {isLoading ? (
) : ( )}
{IS_PLATFORM && ( <>
{!!username ? ( <> {username} {primaryEmail !== username && profileShowEmailEnabled && ( {primaryEmail} )} ) : ( {primaryEmail} )}
{ if (pathname !== '/account/me') { appStateSnapshot.setLastRouteBeforeVisitingAccountPage(currentRoute) } }} > Account toggleFeaturePreviewModal(true)} > Feature previews Changelog )} {shouldShowSectionSeparator && } Theme { setTheme(value) }} > {singleThemes.map((theme) => ( {theme.name} ))} {showUpgradeCta && ( <>
{ track('upgrade_cta_clicked', { placement: 'user_dropdown' }) setIsOpen(false) }} />
)} {IS_PLATFORM && ( <> { navigate({ to: '/logout' }) }} > Sign out )}
) }