"use client"; import { useRouter } from "next/navigation"; import { memo, useState } from "react"; import { PricingPlanType } from "@turbostarter/billing"; import { useTranslation } from "@turbostarter/i18n"; import { Avatar, AvatarFallback, AvatarImage, } from "@turbostarter/ui-web/avatar"; import { Command, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, } from "@turbostarter/ui-web/command"; import { Icons } from "@turbostarter/ui-web/icons"; import { Popover, PopoverTrigger, PopoverContent, PopoverPortal, } from "@turbostarter/ui-web/popover"; import { SidebarMenu, SidebarMenuButton, SidebarMenuItem, useSidebar, } from "@turbostarter/ui-web/sidebar"; import { pathsConfig } from "~/config/paths"; import { authClient } from "~/lib/auth/client"; import { useCustomer } from "~/modules/billing/hooks/use-customer"; import { TurboLink } from "~/modules/common/turbo-link"; import { CreateOrganizationModal } from "~/modules/organization/create-organization"; import { useActiveOrganization } from "./hooks/use-active-organization"; import type { User } from "@turbostarter/auth"; interface AccountSwitcherProps { readonly user: User; } export const AccountSwitcher = memo(({ user }) => { const { t } = useTranslation(["common", "auth", "organization"]); const { isMobile } = useSidebar(); const router = useRouter(); const [open, setOpen] = useState(false); const [createOrganizationOpen, setCreateOrganizationOpen] = useState(false); const { data: customer } = useCustomer(); const { data: organizations } = authClient.useListOrganizations(); const { activeOrganization } = useActiveOrganization(); return ( {activeOrganization ? ( <> {activeOrganization.name.charAt(0)} ) : ( <> )}
{activeOrganization ? activeOrganization.name : t("account.personal")} {(customer?.plan ?? PricingPlanType.FREE).toLowerCase()}
{ setOpen(false); router.replace(pathsConfig.dashboard.user.index); }} asChild > {t("account.personal")} {!activeOrganization && ( )} {organizations && organizations.length > 0 && ( <> {organizations.map((organization) => ( { router.replace( pathsConfig.dashboard.organization( organization.slug, ).index, ); }} > {organization.name.charAt(0)} {organization.name} {activeOrganization?.slug === organization.slug && ( )} ))} )} setCreateOrganizationOpen(true)} >
{t("create.cta")}
); }); AccountSwitcher.displayName = "AccountSwitcher";