{"version":3,"file":"ChatOptions.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/AIMessageComposer/ChatOptions.tsx"],"sourcesContent":["import React, { createContext, type ReactNode, useContext, useState } from 'react';\nimport clsx from 'clsx';\n\nimport { useTranslationContext } from '../../../context/TranslationContext';\nimport { useComponentContextIcons } from '../../../context/useComponentContextIcons';\nimport { Dropdown, type DropdownTriggerProps } from '../../../components/Form/Dropdown';\nimport {\n  AIThemeScope,\n  ThemeScopeProvider,\n  useAIThemeScopeClassName,\n} from '../themeScope';\n\n/**\n * A way to ask, which the person picks for their next message, such as \"Think harder\". The\n * counterpart of `ChatOption` in StreamChatAI.\n */\nexport type AIChatOption = {\n  id: string;\n  /** The option's name in the menu, such as \"Think harder\". */\n  title: string;\n  /** Data of your own, such as what the option sends with the message. */\n  customData?: Record<string, unknown>;\n  /** A line under the title in the menu, such as \"Takes longer, for harder questions\". */\n  description?: string;\n  /** An icon shown in the menu and on the chip. */\n  icon?: ReactNode;\n  /** The option's name on the chip once picked, such as \"Think\". Defaults to `title`. */\n  shortTitle?: string;\n};\n\nexport type ChatOptionsListProps = {\n  onSelect: (option: AIChatOption) => void;\n  options: AIChatOption[];\n  selectedId?: string | null;\n};\n\n/**\n * The items of the chat options menu, one per option. `ChatOptions` shows them in the SDK's\n * `Dropdown`, which moves focus between them and closes on Escape. The counterpart of\n * `ChatOptionsList`.\n */\nexport const ChatOptionsList = ({\n  onSelect,\n  options,\n  selectedId,\n}: ChatOptionsListProps) => (\n  // The items have no root of their own, so the scope wraps them when rendered on their own.\n  <AIThemeScope>\n    {options.map((option) => (\n      <button\n        aria-checked={option.id === selectedId}\n        className='str-chat__ai-chat-options__item'\n        key={option.id}\n        onClick={() => onSelect(option)}\n        role='menuitemradio'\n        type='button'\n      >\n        {option.icon && (\n          <span className='str-chat__ai-chat-options__item-icon'>{option.icon}</span>\n        )}\n        <span className='str-chat__ai-chat-options__item-text'>\n          <span className='str-chat__ai-chat-options__item-title'>{option.title}</span>\n          {option.description && (\n            <span className='str-chat__ai-chat-options__item-description'>\n              {option.description}\n            </span>\n          )}\n        </span>\n      </button>\n    ))}\n  </AIThemeScope>\n);\n\nexport type SelectedChatOptionChipProps = {\n  onRemove: () => void;\n  option: AIChatOption;\n  className?: string;\n  disabled?: boolean;\n};\n\n/** The picked option, with a button that removes it. The counterpart of `SelectedChatOptionChip`. */\nexport const SelectedChatOptionChip = ({\n  className,\n  disabled,\n  onRemove,\n  option,\n}: SelectedChatOptionChipProps) => {\n  const themeScopeClassName = useAIThemeScopeClassName();\n  const { t } = useTranslationContext();\n  const { IconXmarkSmall } = useComponentContextIcons();\n  const title = option.shortTitle ?? option.title;\n  return (\n    <ThemeScopeProvider>\n      <span\n        className={clsx(\n          themeScopeClassName,\n          'str-chat__ai-chat-options__chip',\n          className,\n        )}\n      >\n        {option.icon && (\n          <span className='str-chat__ai-chat-options__chip-icon'>{option.icon}</span>\n        )}\n        <span className='str-chat__ai-chat-options__chip-title'>{title}</span>\n        <button\n          aria-label={t(\n            'aiComponents.chatOptions.removeOption.ariaLabel',\n            'Remove option: {{ option }}',\n            { option: title },\n          )}\n          className='str-chat__ai-chat-options__chip-remove'\n          disabled={disabled}\n          onClick={onRemove}\n          type='button'\n        >\n          <IconXmarkSmall />\n        </button>\n      </span>\n    </ThemeScopeProvider>\n  );\n};\n\nexport type ChatOptionsProps = {\n  options: AIChatOption[];\n  className?: string;\n  /** The option picked at first, when uncontrolled. */\n  defaultSelectedId?: string | null;\n  disabled?: boolean;\n  /** The menu button's icon. Defaults to a plus. */\n  icon?: ReactNode;\n  /** Submits the picked option's id with the composer's form under this name. */\n  name?: string;\n  /** Called when the person picks an option, or removes it with `null`. */\n  onSelectedChange?: (option: AIChatOption | null) => void;\n  /** The picked option's id, when controlled. */\n  selectedId?: string | null;\n};\n\n// What the menu button needs beyond what `Dropdown` hands its trigger.\nconst ChatOptionsTriggerContext = createContext<{ disabled?: boolean; icon?: ReactNode }>(\n  {},\n);\n\n// Defined once, since `Dropdown` closes whenever its trigger component changes.\nconst ChatOptionsTrigger = ({\n  onClick,\n  referenceRef,\n  ...props\n}: DropdownTriggerProps) => {\n  const { t } = useTranslationContext();\n  const { IconPlus } = useComponentContextIcons();\n  const { disabled, icon } = useContext(ChatOptionsTriggerContext);\n  return (\n    <button\n      {...props}\n      aria-haspopup='menu'\n      aria-label={t('aiComponents.chatOptions.chatOptions.ariaLabel', 'Chat options')}\n      className='str-chat__ai-message-composer__round-button'\n      disabled={disabled}\n      onClick={onClick}\n      ref={referenceRef as React.Ref<HTMLButtonElement>}\n      type='button'\n    >\n      {icon ?? <IconPlus />}\n    </button>\n  );\n};\n\n/**\n * A menu of ways to ask, such as \"Think harder\", and a chip for the one picked, with a button\n * that removes it. Place it inside `AIMessageComposer`; with `name`, the picked option's id is\n * submitted with the form. The counterpart of the chat options of `AIComposerView`.\n */\nexport const ChatOptions = ({\n  className,\n  defaultSelectedId = null,\n  disabled,\n  icon,\n  name,\n  onSelectedChange,\n  options,\n  selectedId: selectedIdProp,\n}: ChatOptionsProps) => {\n  const themeScopeClassName = useAIThemeScopeClassName();\n  const { t } = useTranslationContext();\n  const [uncontrolledId, setUncontrolledId] = useState(defaultSelectedId);\n  const selectedId = selectedIdProp === undefined ? uncontrolledId : selectedIdProp;\n  const selected = options.find((option) => option.id === selectedId);\n\n  const select = (option: AIChatOption | null) => {\n    setUncontrolledId(option?.id ?? null);\n    onSelectedChange?.(option);\n  };\n\n  return (\n    <ThemeScopeProvider>\n      <div className={clsx(themeScopeClassName, 'str-chat__ai-chat-options', className)}>\n        <ChatOptionsTriggerContext.Provider value={{ disabled, icon }}>\n          <Dropdown\n            className='str-chat__ai-chat-options__list'\n            placement='top-start'\n            TriggerComponent={ChatOptionsTrigger}\n          >\n            <div\n              aria-label={t(\n                'aiComponents.chatOptions.chatOptions.ariaLabel',\n                'Chat options',\n              )}\n              role='group'\n            >\n              <ChatOptionsList\n                onSelect={(option) => select(option.id === selectedId ? null : option)}\n                options={options}\n                selectedId={selectedId}\n              />\n            </div>\n          </Dropdown>\n        </ChatOptionsTriggerContext.Provider>\n        {selected && (\n          <SelectedChatOptionChip\n            disabled={disabled}\n            onRemove={() => select(null)}\n            option={selected}\n          />\n        )}\n        {name && <input name={name} type='hidden' value={selected?.id ?? ''} />}\n      </div>\n    </ThemeScopeProvider>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;AAyCA,IAAa,mBAAmB,EAC9B,UACA,SACA,iBAGA,oBAAC,cAAD,EAAA,UACG,QAAQ,KAAK,WACZ,qBAAC,UAAD;CACE,gBAAc,OAAO,OAAO;CAC5B,WAAU;CAEV,eAAe,SAAS,MAAM;CAC9B,MAAK;CACL,MAAK;WANP,CAQG,OAAO,QACN,oBAAC,QAAD;EAAM,WAAU;YAAwC,OAAO;CAAW,CAAA,GAE5E,qBAAC,QAAD;EAAM,WAAU;YAAhB,CACE,oBAAC,QAAD;GAAM,WAAU;aAAyC,OAAO;EAAY,CAAA,GAC3E,OAAO,eACN,oBAAC,QAAD;GAAM,WAAU;aACb,OAAO;EACJ,CAAA,CAEJ;GACA;GAhBD,OAAO,EAgBN,CACT,EACW,CAAA;;AAWhB,IAAa,0BAA0B,EACrC,WACA,UACA,UACA,aACiC;CACjC,MAAM,sBAAsB,yBAAyB;CACrD,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,EAAE,mBAAmB,yBAAyB;CACpD,MAAM,QAAQ,OAAO,cAAc,OAAO;CAC1C,OACE,oBAAC,oBAAD,EAAA,UACE,qBAAC,QAAD;EACE,WAAW,KACT,qBACA,mCACA,SACF;YALF;GAOG,OAAO,QACN,oBAAC,QAAD;IAAM,WAAU;cAAwC,OAAO;GAAW,CAAA;GAE5E,oBAAC,QAAD;IAAM,WAAU;cAAyC;GAAY,CAAA;GACrE,oBAAC,UAAD;IACE,cAAY,EACV,mDACA,+BACA,EAAE,QAAQ,MAAM,CAClB;IACA,WAAU;IACA;IACV,SAAS;IACT,MAAK;cAEL,oBAAC,gBAAD,C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