All files / src/components/UserMenu index.tsx

98.05% Statements 101/103
80% Branches 16/20
100% Functions 3/3
98.05% Lines 101/103

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149  1x 1x     1x                 1x 1x     1x     1x     1x 1x     1x     1x 1x         1x 19x 19x 19x 19x   19x 19x   19x 3x 3x 3x 1x 1x 1x 1x 3x   19x 2x 2x   19x 2x 2x 2x 2x 2x   2x   2x   19x       19x   19x 19x 19x 19x 19x 5x   10x 10x 10x 10x 10x     19x 19x 19x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   19x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   19x 3x 3x 3x 3x 3x   19x   19x   1x  
// Libs
import { useState } from 'react';
import { toast } from 'sonner';
 
// Components
import {
  Skeleton,
  ImageFallback,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  FullScreenLoader,
  Button,
} from '@/components/common';
import { ForceLogoutDialog } from '../ForceLogoutDialog';
import { WithCopilotKitHeadless } from '../SearchBarWithAI/WithCopilotKitHeadless';
 
// Providers
import { useAuthContext, useFirebase } from '@/providers';
 
// Hooks
import { useTheme } from '@/hooks';
 
// Icons
import LogOutIcon from '@shared/icons/LogOutIcon';
import { MoonIcon, SunIcon } from '../icons/reacts';
 
// Constants
import { AUTH_ERROR_MESSAGES, ERROR_MESSAGES, ROUTES } from '@/constants';
 
// Utils
import { Dropdown } from '../common/Dropdown';
import { THEMES } from '@/types';
interface UserMenuProps {
  currentPath: string;
}
 
const UserMenu = ({ currentPath }: UserMenuProps) => {
  const { user, signOut } = useAuthContext();
  const { isLoadingProfile, isChangedRole } = useFirebase();
  const { theme, toggleTheme, isDarkMode } = useTheme();
  const [open, setOpen] = useState(false);
 
  const { email = '', avatar = '' } = user || {};
  const userAvatar = `/api/image?url=${encodeURIComponent(avatar)}`;
 
  const handleSignOut = async () => {
    try {
      await signOut();
    } catch (error: unknown) {
      toast.error(
        error instanceof Error ? error.message : ERROR_MESSAGES.UNKNOWN_ERROR,
      );
    }
  };
 
  if (isLoadingProfile) {
    return <FullScreenLoader />;
  }
 
  if (!user) {
    return (
      <a
        href={ROUTES.SIGN_IN}
        className="text-ocean capitalize pt-0.5 hover:opacity-50 text-xs"
      >
        sign in
      </a>
    );
  }
 
  if (!userAvatar) {
    return <Skeleton className="size-10 rounded-full" />;
  }
 
  const ariaLabel = `Switch to ${isDarkMode ? THEMES.LIGHT : THEMES.DARK} mode`;
 
  return (
    <>
      <WithCopilotKitHeadless email={email} />
      <div className="flex items-center gap-3">
        {theme === null ? (
          <div className="w-[40px] h-[40px]" />
        ) : (
          <Button
            icon={isDarkMode ? <SunIcon /> : <MoonIcon />}
            onClick={toggleTheme}
            aria-label={ariaLabel}
          />
        )}
 
        <Dropdown
          selectedItem={
            <button
              type="button"
              className="relative h-[40px] w-[40px] rounded-full overflow-hidden focus:outline-none touch-manipulation"
              onClick={() => setOpen((prev) => !prev)}
            >
              <ImageFallback
                src={userAvatar}
                alt="User profile"
                sizes="(max-width: 641px) 16px, 20px"
                className="rounded-full object-contain h-[40px] w-[40px] block"
              />
            </button>
          }
          items={
            <>
              <DropdownMenuLabel className="px-5">
                <div className="flex space-x-2 items-center">
                  <p className="break-word text-4xs font-normal dark:text-white">
                    {email}
                  </p>
                </div>
              </DropdownMenuLabel>
              <DropdownMenuSeparator />
              <DropdownMenuItem
                className="cursor-pointer px-5 dark:hover:bg-gray-700"
                onClick={handleSignOut}
              >
                <p className="text-4xs dark:text-white">Logout</p>
                <LogOutIcon className="mr-2 size-4 dark:text-white" />
              </DropdownMenuItem>
            </>
          }
          isOpen={open}
          rootProps={{
            onOpenChange: setOpen,
          }}
          contentProps={{
            align: 'end',
            className: 'min-w-56',
          }}
        />
      </div>
 
      {isChangedRole && (
        <ForceLogoutDialog
          open={isChangedRole}
          message={AUTH_ERROR_MESSAGES.ROLE_MISMATCH}
          onClose={handleSignOut}
        />
      )}
    </>
  );
};
 
export default UserMenu;