{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "device-select",
  "title": "Device Select",
  "description": "Media device picker for microphones, speakers, or cameras — as a select or a dropdown menu around any trigger.",
  "dependencies": [
    "@pipecat-ai/client-js@^1.14.0",
    "@pipecat-ai/client-react@^1.9.0",
    "lucide-react@^1.41.0"
  ],
  "registryDependencies": [
    "select",
    "dropdown-menu"
  ],
  "files": [
    {
      "path": "src/components/device-select.tsx",
      "content": "\"use client\";\n\nimport type { DeviceErrorReason } from \"@pipecat-ai/client-js\";\nimport {\n  type OptionalMediaDeviceInfo,\n  useMediaState,\n  usePipecatClientMediaDevices,\n} from \"@pipecat-ai/client-react\";\nimport { Loader2Icon } from \"lucide-react\";\nimport { createContext, useContext } from \"react\";\n\nimport {\n  DropdownMenu,\n  DropdownMenuCheckboxItem,\n  DropdownMenuContent,\n  DropdownMenuGroup,\n  DropdownMenuLabel,\n  DropdownMenuSeparator,\n  DropdownMenuTrigger,\n} from \"@/components/ui/dropdown-menu\";\nimport {\n  Select,\n  SelectContent,\n  SelectItem,\n  SelectTrigger,\n  SelectValue,\n} from \"@/components/ui/select\";\nimport { cn } from \"@/lib/utils\";\n\n/** Which class of media device to list and control. */\nexport type DeviceKind = \"audioinput\" | \"audiooutput\" | \"videoinput\";\n\nconst KIND_DEFAULTS: Record<\n  DeviceKind,\n  { placeholder: string; menuLabel: string; fallbackName: string }\n> = {\n  audioinput: {\n    placeholder: \"Select a microphone\",\n    menuLabel: \"Microphones\",\n    fallbackName: \"Microphone\",\n  },\n  audiooutput: {\n    placeholder: \"Select a speaker\",\n    menuLabel: \"Speakers\",\n    fallbackName: \"Speaker\",\n  },\n  videoinput: {\n    placeholder: \"Select a camera\",\n    menuLabel: \"Cameras\",\n    fallbackName: \"Camera\",\n  },\n};\n\n/**\n * Devices, selection, and updater for one device kind, sourced from the\n * Pipecat client. Must be used inside a PipecatClientProvider.\n */\nexport function usePipecatDevices(kind: DeviceKind): {\n  devices: MediaDeviceInfo[];\n  selectedDevice: OptionalMediaDeviceInfo;\n  updateDevice: (deviceId: string) => void;\n} {\n  const {\n    availableCams,\n    availableMics,\n    availableSpeakers,\n    selectedCam,\n    selectedMic,\n    selectedSpeaker,\n    updateCam,\n    updateMic,\n    updateSpeaker,\n  } = usePipecatClientMediaDevices();\n\n  switch (kind) {\n    case \"audiooutput\":\n      return {\n        devices: availableSpeakers,\n        selectedDevice: selectedSpeaker,\n        updateDevice: updateSpeaker,\n      };\n    case \"videoinput\":\n      return {\n        devices: availableCams,\n        selectedDevice: selectedCam,\n        updateDevice: updateCam,\n      };\n    case \"audioinput\":\n    default:\n      return {\n        devices: availableMics,\n        selectedDevice: selectedMic,\n        updateDevice: updateMic,\n      };\n  }\n}\n\n/** Speaker enumeration depends on microphone permission, so audiooutput follows mic MediaState. */\nexport function usePipecatDeviceState(kind: DeviceKind): {\n  isLoading: boolean;\n  unavailableText?: string;\n} {\n  const { mic, cam } = useMediaState();\n  const state = kind === \"videoinput\" ? cam : mic;\n  return {\n    isLoading: state.state === \"initializing\",\n    unavailableText:\n      state.state === \"error\"\n        ? deviceErrorText(state.reason, KIND_DEFAULTS[kind].fallbackName)\n        : undefined,\n  };\n}\n\n/** Short, user-facing message for a device error. */\nfunction deviceErrorText(\n  reason: DeviceErrorReason,\n  fallbackName: string,\n): string {\n  switch (reason) {\n    case \"blocked\":\n      return `${fallbackName} access blocked`;\n    case \"already-in-use\":\n      return `${fallbackName} in use`;\n    case \"not-found\":\n      return `No ${fallbackName.toLowerCase()} found`;\n    case \"not-supported\":\n      return `${fallbackName} not supported`;\n    case \"unknown\":\n    default:\n      return `${fallbackName} unavailable`;\n  }\n}\n\n/** Human-readable device label with a stable fallback. */\nexport function formatDeviceLabel(\n  device: MediaDeviceInfo,\n  fallbackName: string,\n) {\n  if (device.label) return device.label;\n  // Pre-permission, browsers may return devices with empty labels and ids.\n  return device.deviceId\n    ? `${fallbackName} ${device.deviceId.slice(0, 5)}`\n    : fallbackName;\n}\n\n/** Narrows OptionalMediaDeviceInfo (which may be an empty object) to a real device. */\nexport function asMediaDevice(\n  device?: OptionalMediaDeviceInfo,\n): MediaDeviceInfo | undefined {\n  return device && \"deviceId\" in device\n    ? (device as MediaDeviceInfo)\n    : undefined;\n}\n\nexport interface DeviceSelectViewProps extends Omit<\n  React.ComponentProps<typeof SelectTrigger>,\n  \"children\"\n> {\n  /** Devices to list. */\n  devices?: MediaDeviceInfo[];\n  /** Currently selected device, matched by deviceId. */\n  selectedDevice?: OptionalMediaDeviceInfo;\n  /** Called with the deviceId when the user picks a device. */\n  onDeviceChange?: (deviceId: string) => void;\n  /** Device kind — controls default placeholder and fallback labels. */\n  kind?: DeviceKind;\n  /** Placeholder shown when nothing is selected. */\n  placeholder?: string;\n  /** Optional leading content inside the trigger (icon or short label). */\n  guide?: React.ReactNode;\n  /** Disables the trigger and shows a spinner (e.g. while devices initialize). */\n  isLoading?: boolean;\n  /** Content shown next to the spinner while loading. Spinner only by default. */\n  loadingText?: React.ReactNode;\n  /**\n   * When set, the trigger renders disabled with this message (device access\n   * blocked, in use, …). Takes precedence over the interactive state;\n   * isLoading takes precedence over both.\n   */\n  unavailableText?: React.ReactNode;\n}\n\n/** Loading takes precedence over unavailability. The trigger is named from kind unless overridden. */\nexport function DeviceSelectView({\n  devices,\n  selectedDevice,\n  onDeviceChange,\n  kind = \"audioinput\",\n  placeholder,\n  guide,\n  isLoading = false,\n  loadingText,\n  unavailableText,\n  disabled,\n  className,\n  ...triggerProps\n}: DeviceSelectViewProps) {\n  const defaults = KIND_DEFAULTS[kind];\n  const selected = asMediaDevice(selectedDevice);\n  const selectedValue = selected?.deviceId ?? null;\n  const blocked = isLoading || !!unavailableText;\n\n  return (\n    <Select\n      value={selectedValue}\n      onValueChange={(value) => {\n        if (typeof value === \"string\") onDeviceChange?.(value);\n      }}\n    >\n      <SelectTrigger\n        data-slot=\"device-select\"\n        aria-label={defaults.placeholder}\n        aria-busy={isLoading || undefined}\n        data-state={\n          isLoading ? \"loading\" : unavailableText ? \"unavailable\" : undefined\n        }\n        className={cn(\"min-w-0\", className)}\n        {...triggerProps}\n        disabled={blocked || disabled}\n      >\n        {isLoading ? (\n          <>\n            <Loader2Icon className=\"animate-spin\" />\n            {loadingText && <span className=\"truncate\">{loadingText}</span>}\n          </>\n        ) : unavailableText ? (\n          <span className=\"truncate\">{unavailableText}</span>\n        ) : (\n          <>\n            {guide ? (\n              <span className=\"text-muted-foreground flex items-center gap-1.5\">\n                {guide}\n              </span>\n            ) : null}\n            <SelectValue>\n              <span className=\"truncate\">\n                {selected\n                  ? formatDeviceLabel(selected, defaults.fallbackName)\n                  : (placeholder ?? defaults.placeholder)}\n              </span>\n            </SelectValue>\n          </>\n        )}\n      </SelectTrigger>\n      {/* Pop below the trigger (popover-style) rather than Base UI's\n          default of overlaying the selected item on the trigger, so both\n          picker UIs position consistently. */}\n      <SelectContent alignItemWithTrigger={false}>\n        {devices?.length ? (\n          devices.map((device, index) => (\n            <SelectItem key={device.deviceId || index} value={device.deviceId}>\n              {formatDeviceLabel(device, defaults.fallbackName)}\n            </SelectItem>\n          ))\n        ) : (\n          <SelectItem value=\"__none\" disabled>\n            No devices found\n          </SelectItem>\n        )}\n      </SelectContent>\n    </Select>\n  );\n}\n\nexport interface DeviceSelectProps extends Omit<\n  DeviceSelectViewProps,\n  | \"devices\"\n  | \"selectedDevice\"\n  | \"onDeviceChange\"\n  | \"isLoading\"\n  | \"unavailableText\"\n> {\n  /** Replaces the automatically derived device-error message. */\n  unavailableText?: React.ReactNode;\n}\n\n/**\n * Device picker (select) wired to the Pipecat client: device list,\n * selection, and loading/error states for the given kind.\n * Must be rendered inside a PipecatClientProvider.\n */\nexport function DeviceSelect({\n  kind = \"audioinput\",\n  unavailableText,\n  ...props\n}: DeviceSelectProps) {\n  const { devices, selectedDevice, updateDevice } = usePipecatDevices(kind);\n  const derived = usePipecatDeviceState(kind);\n\n  return (\n    <DeviceSelectView\n      kind={kind}\n      devices={devices}\n      selectedDevice={selectedDevice}\n      onDeviceChange={updateDevice}\n      isLoading={derived.isLoading}\n      unavailableText={unavailableText ?? derived.unavailableText}\n      {...props}\n    />\n  );\n}\n\ninterface DeviceDropdownContextValue {\n  devices?: MediaDeviceInfo[];\n  selectedDevice?: OptionalMediaDeviceInfo;\n  onDeviceChange?: (deviceId: string) => void;\n  kind: DeviceKind;\n  isLoading: boolean;\n  loadingText?: React.ReactNode;\n  unavailableText?: React.ReactNode;\n}\n\nconst DeviceDropdownContext = createContext<DeviceDropdownContextValue | null>(\n  null,\n);\n\nfunction useDeviceDropdownContext() {\n  const context = useContext(DeviceDropdownContext);\n  if (!context) {\n    throw new Error(\n      \"DeviceDropdown components must be used within DeviceDropdownView or DeviceDropdown\",\n    );\n  }\n  return context;\n}\n\nexport interface DeviceDropdownViewProps {\n  /** Anatomy: a DeviceDropdownTrigger and a DeviceDropdownContent. */\n  children: React.ReactNode;\n  /** Devices to list. */\n  devices?: MediaDeviceInfo[];\n  /** Currently selected device, matched by deviceId. */\n  selectedDevice?: OptionalMediaDeviceInfo;\n  /** Called with the deviceId when the user picks a device. */\n  onDeviceChange?: (deviceId: string) => void;\n  /** Device kind — controls default menu label and fallback names. */\n  kind?: DeviceKind;\n  /** Shows a loading row in the menu instead of devices. */\n  isLoading?: boolean;\n  /** Content for the loading row. \"Loading devices…\" by default. */\n  loadingText?: React.ReactNode;\n  /**\n   * When set, the menu shows this message instead of devices (device\n   * access blocked, in use, …). isLoading takes precedence.\n   */\n  unavailableText?: React.ReactNode;\n}\n\n/** Compose with DeviceDropdownTrigger and DeviceDropdownContent. Name icon-only triggers with aria-label. */\nexport function DeviceDropdownView({\n  children,\n  devices,\n  selectedDevice,\n  onDeviceChange,\n  kind = \"audioinput\",\n  isLoading = false,\n  loadingText,\n  unavailableText,\n}: DeviceDropdownViewProps) {\n  return (\n    <DeviceDropdownContext.Provider\n      value={{\n        devices,\n        selectedDevice,\n        onDeviceChange,\n        kind,\n        isLoading,\n        loadingText,\n        unavailableText,\n      }}\n    >\n      <DropdownMenu>{children}</DropdownMenu>\n    </DeviceDropdownContext.Provider>\n  );\n}\n\n/** Menu trigger — compose your own element via the render prop. */\nexport const DeviceDropdownTrigger = DropdownMenuTrigger;\n\nexport interface DeviceDropdownContentProps extends React.ComponentProps<\n  typeof DropdownMenuContent\n> {\n  /** Menu heading. Pass null to hide. */\n  menuLabel?: React.ReactNode | null;\n}\n\n/**\n * Menu content listing the root's devices, with loading, unavailable,\n * and empty states. Must be used within a DeviceDropdownView.\n */\nexport function DeviceDropdownContent({\n  menuLabel,\n  align = \"end\",\n  ...props\n}: DeviceDropdownContentProps) {\n  const {\n    devices,\n    selectedDevice,\n    onDeviceChange,\n    kind,\n    isLoading,\n    loadingText,\n    unavailableText,\n  } = useDeviceDropdownContext();\n  const defaults = KIND_DEFAULTS[kind];\n  const resolvedLabel =\n    menuLabel === undefined ? defaults.menuLabel : menuLabel;\n\n  return (\n    <DropdownMenuContent align={align} {...props}>\n      {/* Base UI requires GroupLabel to live inside a Group. */}\n      <DropdownMenuGroup>\n        {resolvedLabel !== null && (\n          <>\n            <DropdownMenuLabel>{resolvedLabel}</DropdownMenuLabel>\n            <DropdownMenuSeparator />\n          </>\n        )}\n        {isLoading ? (\n          <DropdownMenuLabel\n            aria-busy\n            className=\"text-muted-foreground flex items-center gap-1.5 font-normal\"\n          >\n            <Loader2Icon className=\"size-3.5 animate-spin\" />\n            {loadingText ?? \"Loading devices…\"}\n          </DropdownMenuLabel>\n        ) : unavailableText ? (\n          <DropdownMenuLabel className=\"text-muted-foreground font-normal\">\n            {unavailableText}\n          </DropdownMenuLabel>\n        ) : devices?.length ? (\n          devices.map((device, index) => (\n            <DropdownMenuCheckboxItem\n              key={device.deviceId || index}\n              checked={selectedDevice?.deviceId === device.deviceId}\n              onCheckedChange={(checked) => {\n                if (checked) onDeviceChange?.(device.deviceId);\n              }}\n            >\n              {formatDeviceLabel(device, defaults.fallbackName)}\n            </DropdownMenuCheckboxItem>\n          ))\n        ) : (\n          <DropdownMenuLabel className=\"text-muted-foreground font-normal\">\n            No devices found\n          </DropdownMenuLabel>\n        )}\n      </DropdownMenuGroup>\n    </DropdownMenuContent>\n  );\n}\n\nexport interface DeviceDropdownProps extends Omit<\n  DeviceDropdownViewProps,\n  | \"devices\"\n  | \"selectedDevice\"\n  | \"onDeviceChange\"\n  | \"isLoading\"\n  | \"unavailableText\"\n> {\n  /** Replaces the automatically derived device-error message. */\n  unavailableText?: React.ReactNode;\n}\n\n/**\n * Device picker (dropdown menu) wired to the Pipecat client: device\n * list, selection, and loading/error states for the given kind.\n * Must be rendered inside a PipecatClientProvider.\n */\nexport function DeviceDropdown({\n  kind = \"audioinput\",\n  unavailableText,\n  children,\n  ...props\n}: DeviceDropdownProps) {\n  const { devices, selectedDevice, updateDevice } = usePipecatDevices(kind);\n  const derived = usePipecatDeviceState(kind);\n\n  return (\n    <DeviceDropdownView\n      kind={kind}\n      devices={devices}\n      selectedDevice={selectedDevice}\n      onDeviceChange={updateDevice}\n      isLoading={derived.isLoading}\n      unavailableText={unavailableText ?? derived.unavailableText}\n      {...props}\n    >\n      {children}\n    </DeviceDropdownView>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/pipecat/device-select.tsx"
    }
  ],
  "docs": "DeviceSelect / DeviceDropdown pick their device list via kind: audioinput | audiooutput | videoinput. Render inside a PipecatClientProvider.",
  "categories": [
    "voice"
  ],
  "type": "registry:component"
}