"use client" import { ComponentPropsWithoutRef, useMemo, useState } from "react" import { Check, ChevronsUpDown, PlusCircle } from "lucide-react"; import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, } from "@/components/ui/command" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import NewTeamForm from "@/components/basejump/new-team-form"; import { useAccounts } from "@/hooks/use-accounts"; type PopoverTriggerProps = ComponentPropsWithoutRef; type SelectedAccount = NonNullable["data"]>[0]; interface AccountSelectorProps extends PopoverTriggerProps { accountId: string; placeholder?: string; onAccountSelected?: (account: SelectedAccount) => void; } export default function AccountSelector({ className, accountId, onAccountSelected, placeholder = "Select an account..." }: AccountSelectorProps) { const [open, setOpen] = useState(false) const [showNewTeamDialog, setShowNewTeamDialog] = useState(false) const { data: accounts } = useAccounts(); const { teamAccounts, personalAccount, selectedAccount } = useMemo(() => { const personalAccount = accounts?.find((account) => account.personal_account); const teamAccounts = accounts?.filter((account) => !account.personal_account); const selectedAccount = accounts?.find((account) => account.account_id === accountId); return { personalAccount, teamAccounts, selectedAccount, } }, [accounts, accountId]); return ( No account found. { if (onAccountSelected) { onAccountSelected(personalAccount!) } setOpen(false) }} className="text-sm rounded-md bg-card-bg dark:bg-background-secondary hover:!bg-[#f1eee7] dark:hover:!bg-[#141413] aria-selected:!bg-[#f1eee7] dark:aria-selected:!bg-[#141413] text-foreground/90" > {personalAccount?.name} {Boolean(teamAccounts?.length) && ( {teamAccounts?.map((team) => ( { if (onAccountSelected) { onAccountSelected(team) } setOpen(false) }} className="text-sm rounded-md bg-card-bg dark:bg-background-secondary hover:!bg-[#f1eee7] dark:hover:!bg-[#141413] aria-selected:!bg-[#f1eee7] dark:aria-selected:!bg-[#141413] text-foreground/90" > {team.name} ))} )} { setOpen(false) setShowNewTeamDialog(true) }} className="text-sm rounded-md bg-card-bg dark:bg-background-secondary hover:!bg-[#f1eee7] dark:hover:!bg-[#141413] text-foreground/90" > Create Team Create a new team Create a team to collaborate with others. ) }