Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

4.1 KB
Raw Blame
"use client";

import { UserAvatar } from "@/ui/users/user-avatar";
import { Icon, Popover, User } from "@dub/ui";
import { Gear } from "@dub/ui/icons";
import { cn } from "@dub/utils";
import { LogOut } from "lucide-react";
import { signOut, useSession } from "next-auth/react";
import Link from "next/link";
import { useParams } from "next/navigation";
import {
  ComponentPropsWithoutRef,
  ElementType,
  useMemo,
  useState,
} from "react";

export function UserDropdown() {
  const { data: session } = useSession();
  const [openPopover, setOpenPopover] = useState(false);
  const { slug: paramsSlug } = useParams() as { slug?: string | string[] };

  const workspaceSlug =
    (Array.isArray(paramsSlug) ? paramsSlug[0] : paramsSlug) ||
    session?.user?.["defaultWorkspace"];

  const menuOptions = useMemo(() => {
    const options: Array<{
      label: string;
      icon: any;
      href?: string;
      type?: string;
      onClick?: () => void;
    }> = [
      {
        label: "Account settings",
        icon: User,
        href: "/account/settings",
        onClick: () => setOpenPopover(false),
      },
    ];

    if (workspaceSlug) {
      options.push({
        label: "Workspace settings",
        icon: Gear,
        href: `/${workspaceSlug}/settings`,
        onClick: () => setOpenPopover(false),
      });
    }

    // Add logout option
    options.push({
      type: "button",
      label: "Log out",
      icon: LogOut,
      onClick: () => {
        signOut({
          callbackUrl: "/login",
        });
      },
    });

    return options;
  }, [workspaceSlug, setOpenPopover]);

  return (
    <Popover
      content={
        <div className="flex w-full flex-col space-y-px rounded-md bg-white p-2 sm:min-w-56">
          {session?.user ? (
            <div className="px-2 pb-4 sm:pb-2">
              <p className="truncate text-base font-medium text-neutral-900 sm:text-sm">
                {session.user.name || session.user.email?.split("@")[0]}
              </p>
              <p className="truncate text-base text-neutral-500 sm:text-sm">
                {session.user.email}
              </p>
            </div>
          ) : (
            <div className="grid gap-2 px-2 py-3">
              <div className="h-3 w-12 animate-pulse rounded-full bg-neutral-200" />
              <div className="h-3 w-20 animate-pulse rounded-full bg-neutral-200" />
            </div>
          )}
          {menuOptions.map((menuOption, idx) => (
            <UserOption
              key={idx}
              as={menuOption.href ? Link : "button"}
              {...menuOption}
            />
          ))}
        </div>
      }
      align="start"
      openPopover={openPopover}
      setOpenPopover={setOpenPopover}
    >
      <button
        onClick={() => setOpenPopover(!openPopover)}
        className={cn(
          "group relative flex size-11 items-center justify-center rounded-lg transition-all",
          "hover:bg-bg-inverted/5 active:bg-bg-inverted/10 data-[state=open]:bg-bg-inverted/10 transition-colors duration-150",
          "outline-none focus-visible:ring-2 focus-visible:ring-black/50",
        )}
      >
        {session?.user ? (
          <UserAvatar
            user={session.user}
            className="size-7 border-none duration-75 sm:size-7"
          />
        ) : (
          <div className="size-7 animate-pulse rounded-full bg-neutral-100" />
        )}
      </button>
    </Popover>
  );
}

type UserOptionProps<T extends ElementType> = {
  as?: T;
  label: string;
  icon: Icon;
};

function UserOption<T extends ElementType = "button">({
  as,
  label,
  icon: Icon,
  children,
  ...rest
}: UserOptionProps<T> &
  Omit<ComponentPropsWithoutRef<T>, keyof UserOptionProps<T>>) {
  const Component = as ?? "button";

  return (
    <Component
      className="flex items-center gap-x-4 rounded-md px-2.5 py-1.5 text-base transition-all duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80 sm:text-sm"
      {...rest}
    >
      <Icon className="size-5 text-neutral-500 sm:size-4" />
      <span className="block truncate text-neutral-600">{label}</span>
      {children}
    </Component>
  );
}