All files / src/components ProfileDropdown.tsx

43.75% Statements 7/16
30% Branches 3/10
42.85% Functions 3/7
46.15% Lines 6/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                      42x                 42x 42x   42x 21x                   42x                                                          
import { useState, useRef, useEffect } from 'react'
import { LogOut, User } from 'lucide-react'
 
interface ProfileDropdownProps {
  profileName: string
  avatarColor: string
  mode: string
  onLogout: () => void
}
 
function getInitial(name: string) {
  return name.charAt(0).toUpperCase()
}
 
export default function ProfileDropdown({
  profileName,
  avatarColor,
  mode,
  onLogout,
}: ProfileDropdownProps) {
  const [dropdownOpen, setDropdownOpen] = useState(false)
  const dropdownRef = useRef<HTMLDivElement>(null)
 
  useEffect(() => {
    Eif (!dropdownOpen) return
    const handleClick = (e: MouseEvent) => {
      if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
        setDropdownOpen(false)
      }
    }
    document.addEventListener('mousedown', handleClick)
    return () => document.removeEventListener('mousedown', handleClick)
  }, [dropdownOpen])
 
  return (
    <div className="relative" ref={dropdownRef}>
      <button
        onClick={() => setDropdownOpen(!dropdownOpen)}
        className="flex h-8 w-8 items-center justify-center rounded-full text-sm font-medium text-white"
        style={{ backgroundColor: avatarColor }}
        aria-label={profileName}
      >
        {mode === 'passkey' ? (
          <User className="h-4 w-4" />
        ) : (
          getInitial(profileName)
        )}
      </button>
 
      {dropdownOpen && (
        <div className="absolute right-0 top-full z-50 mt-1 w-44 rounded-lg border border-border bg-card py-1 shadow-lg">
          <button
            onClick={() => { setDropdownOpen(false); onLogout() }}
            className="flex w-full items-center gap-2 px-3 py-2 text-sm text-card-foreground hover:bg-muted"
          >
            <LogOut className="h-4 w-4" />
            Log out
          </button>
        </div>
      )}
    </div>
  )
}
 
← Back to Dashboard