'use client'
import React, { useState } from 'react'
import { SvgUserMultipleGroup, SvgTaillessArrowRight } from '../../icons'
import { cn } from '../../utils/cn'
import { Button } from '../Button'
import { useSidebarContext } from '../Navigation'
import { Select, SelectContent, SelectItem, SelectTrigger } from '../Select'
import { Separator } from '../Separator'
import { Typography } from '../Typography'
interface LogoIconProps {
index: number
initials: string
className?: string
}
const logoColor = ['purple-300', 'teal-300']
// Common organization display component
function OrganizationDisplay({
org,
color,
}: {
org: OrgOption
color: string
}) {
return (
)
}
function LogoIcon({ initials, className, index }: LogoIconProps) {
const colorValue = logoColor[index]
return (
)
}
export interface OrgOption {
/** Unique identifier for the organization */
value: string
/** Display name for the organization */
label: string
/** Optional organization logo/icon */
initials: string
}
export interface OrgSwitcherProps {
/** Array of organization options */
organizations: OrgOption[]
/** Currently selected organization value */
value?: string
/** Callback when organization selection changes */
onValueChange?: (value: string) => void
/** Default selected organization */
defaultValue?: string
/** Whether the select is disabled */
disabled?: boolean
/** Additional CSS classes */
className?: string
}
export function OrgSwitcher({
organizations,
value,
onValueChange,
defaultValue = organizations[0].value,
disabled,
className,
}: OrgSwitcherProps) {
const [internalValue, setInternalValue] = useState(defaultValue)
// Try to get sidebar context, but don't fail if it's not available
const sidebarContext = useSidebarContext()
const currentValue = value !== undefined ? value : internalValue
const handleValueChange = (newValue: string) => {
// Set active item optimistically if sidebar context is available
if (sidebarContext) {
sidebarContext.setActiveItem(newValue)
// Close any open submenus
sidebarContext.setOpenSubmenu(null)
}
if (value === undefined) {
setInternalValue(newValue)
}
onValueChange?.(newValue)
sidebarContext?.setOpenMobile(false)
}
const selectedOrg =
organizations.find((org) => org.value === currentValue) || organizations[0]
// Get the index of the selected organization for color assignment
const selectedOrgIndex = organizations.findIndex(
(org) => org.value === currentValue,
)
const selectedOrgColor = logoColor[selectedOrgIndex] || logoColor[0]
// If only one organization, render as a button that navigates immediately
if (organizations.length === 1) {
const singleOrg = organizations[0]
const singleOrgColor = logoColor[0]
const handleClick = () => {
handleValueChange(singleOrg.value)
}
// If sidebar context is available and it's an internal link, use renderLink
if (sidebarContext && singleOrg.value.startsWith('/')) {
return sidebarContext.renderLink({
href: singleOrg.value,
className: '!outline-none focus-visible:outline-none group',
children: (
),
})
}
// Fallback to button for external links or when outside SidebarProvider
return (
)
}
return (
)
}