{"version":3,"file":"ReactionSelector.mjs","names":[],"sources":["../../../../src/components/Reactions/ReactionSelector.tsx"],"sourcesContent":["import React, { type ReactNode, useMemo, useState } from 'react';\nimport clsx from 'clsx';\n\nimport { useDialogOnNearestManager } from '../Dialog';\nimport { defaultReactionOptions, getHasExtendedReactions } from './reactionOptions';\nimport { useComponentContext } from '../../context/ComponentContext';\nimport { useMessageContext } from '../../context/MessageContext';\nimport { useTranslationContext } from '../../context/TranslationContext';\nimport { Button } from '../Button';\n\nimport type { ReactionResponse } from 'stream-chat';\nimport { useComponentContextIcons } from '../../context';\n\nexport type ReactionSelectorProps = {\n  /** Dialog ID of the selector popover. Defaults to the ID from `ReactionSelector.getDialogId`. */\n  dialogId?: string;\n  /** Adds or removes a reaction on the message. Defaults to the `MessageContext` handler. */\n  handleReaction?: (\n    reactionType: string,\n    event: React.BaseSyntheticEvent,\n  ) => Promise<void>;\n  /** Own reactions, whose types show as pressed. Defaults to the message's own reactions. */\n  own_reactions?: ReactionResponse[];\n};\n\ninterface ReactionSelectorInterface {\n  (props: ReactionSelectorProps): ReactNode;\n  getDialogId: (_: { messageId: string }) => string;\n  displayName: string;\n  ExtendedList: React.ComponentType<ReactionSelectorProps & { dialogId?: string }>;\n}\n\nconst stableOwnReactions: ReactionResponse[] = [];\n\nexport const ReactionSelector: ReactionSelectorInterface = (props) => {\n  const { IconPlus } = useComponentContextIcons();\n  const {\n    dialogId: propDialogId,\n    handleReaction: propHandleReaction,\n    own_reactions: propOwnReactions,\n  } = props;\n  const [extendedListOpen, setExtendedListOpen] = useState(false);\n\n  const {\n    reactionOptions = defaultReactionOptions,\n    ReactionSelectorExtendedList = ReactionSelector.ExtendedList,\n  } = useComponentContext();\n\n  const {\n    closeReactionSelectorOnClick,\n    handleReaction: contextHandleReaction,\n    message,\n  } = useMessageContext();\n  const { t } = useTranslationContext();\n  const dialogId =\n    propDialogId ?? ReactionSelector.getDialogId({ messageId: message.id });\n  const { dialog } = useDialogOnNearestManager({ id: dialogId });\n\n  const handleReaction = propHandleReaction ?? contextHandleReaction;\n  const ownReactions = propOwnReactions ?? message?.own_reactions ?? stableOwnReactions;\n\n  const ownReactionByType = useMemo(() => {\n    const map: { [key: string]: ReactionResponse } = {};\n\n    for (const reaction of ownReactions) {\n      map[reaction.type] ??= reaction;\n    }\n\n    return map;\n  }, [ownReactions]);\n\n  const adjustedQuickReactionOptions = useMemo(() => {\n    if (Array.isArray(reactionOptions)) return reactionOptions;\n\n    return Object.entries(reactionOptions.quick).map(\n      ([type, { Component, name, unicode }]) => ({\n        Component,\n        name,\n        type,\n        unicode,\n      }),\n    );\n  }, [reactionOptions]);\n\n  const hasExtendedReactions = getHasExtendedReactions(reactionOptions);\n\n  return (\n    <div\n      aria-label={t('reactions.messageReactions.reactionList.ariaLabel', 'Reaction list')}\n      className='str-chat__reaction-selector'\n      data-testid='reaction-selector'\n      role='group'\n    >\n      {!extendedListOpen ? (\n        <>\n          <ul\n            className='str-chat__reaction-selector-list'\n            data-testid='reaction-selector-list'\n          >\n            {adjustedQuickReactionOptions.map(\n              ({ Component, name: reactionName, type: reactionType }) => (\n                <li className='str-chat__reaction-list-selector__item' key={reactionType}>\n                  <button\n                    aria-label={t(\n                      'reactions.messageReactions.selectReaction.ariaLabel',\n                      'Select Reaction: {{ reactionName }}',\n                      {\n                        reactionName: reactionName || reactionType,\n                      },\n                    )}\n                    aria-pressed={typeof ownReactionByType[reactionType] !== 'undefined'}\n                    className={clsx('str-chat__reaction-selector-list__item-button')}\n                    data-testid='select-reaction-button'\n                    data-text={reactionType}\n                    onClick={(event) => {\n                      handleReaction(reactionType, event);\n                      if (closeReactionSelectorOnClick) {\n                        dialog.close();\n                      }\n                    }}\n                    type='button'\n                  >\n                    <span className='str-chat__reaction-icon'>\n                      <Component />\n                    </span>\n                  </button>\n                </li>\n              ),\n            )}\n          </ul>\n          {hasExtendedReactions && (\n            <Button\n              appearance='outline'\n              aria-label={t(\n                'common.openReactionSelector.ariaLabel',\n                'Open Reaction Selector',\n              )}\n              circular\n              className='str-chat__reaction-selector__add-button'\n              data-testid='reaction-selector-add-button'\n              onClick={() => {\n                setExtendedListOpen(true);\n              }}\n              size='sm'\n              variant='secondary'\n            >\n              <IconPlus />\n            </Button>\n          )}\n        </>\n      ) : (\n        <ReactionSelectorExtendedList\n          {...props}\n          dialogId={dialogId}\n          handleReaction={async (reactionType, event) => {\n            await handleReaction(reactionType, event);\n            if (closeReactionSelectorOnClick) {\n              dialog.close();\n            }\n          }}\n        />\n      )}\n    </div>\n  );\n};\n\nReactionSelector.getDialogId = ({ messageId }) => `reaction-selector-${messageId}`;\n\nReactionSelector.displayName = 'ReactionSelector';\n\nReactionSelector.ExtendedList = function ReactionSelectorExtendedList({\n  dialogId,\n  handleReaction: propHandleReaction,\n  own_reactions: propOwnReactions,\n}) {\n  const { reactionOptions = defaultReactionOptions } = useComponentContext();\n  const {\n    closeReactionSelectorOnClick,\n    handleReaction: contextHandleReaction,\n    message,\n  } = useMessageContext();\n  const { t } = useTranslationContext();\n\n  const handleReaction = propHandleReaction ?? contextHandleReaction;\n  const ownReactions = propOwnReactions ?? message?.own_reactions ?? stableOwnReactions;\n\n  const { dialog } = useDialogOnNearestManager({ id: dialogId || '' });\n\n  const ownReactionByType = useMemo(() => {\n    const map: { [key: string]: ReactionResponse } = {};\n\n    for (const reaction of ownReactions) {\n      map[reaction.type] ??= reaction;\n    }\n\n    return map;\n  }, [ownReactions]);\n\n  if (Array.isArray(reactionOptions) || !reactionOptions.extended) {\n    return null;\n  }\n\n  return (\n    <div\n      aria-label={t('reactions.messageReactions.reactionList.ariaLabel', 'Reaction list')}\n      className='str-chat__reaction-selector-extended-list'\n      data-testid='reaction-selector-extended-list'\n      role='group'\n    >\n      {Object.entries(reactionOptions.extended).map(\n        ([reactionType, { Component, name: reactionName }]) => (\n          <button\n            aria-label={t(\n              'reactions.messageReactions.selectReaction.ariaLabel',\n              'Select Reaction: {{ reactionName }}',\n              {\n                reactionName: reactionName || reactionType,\n              },\n            )}\n            aria-pressed={typeof ownReactionByType[reactionType] !== 'undefined'}\n            className='str-chat__reaction-selector-extended-list__button str-chat__button str-chat__button--ghost str-chat__button--size-sm str-chat__button--circular'\n            data-testid='select-reaction-button'\n            data-text={reactionType}\n            key={reactionType}\n            onClick={(event) => {\n              handleReaction(reactionType, event);\n              if (closeReactionSelectorOnClick) {\n                dialog.close();\n              }\n            }}\n            type='button'\n          >\n            <span className='str-chat__reaction-icon'>\n              <Component />\n            </span>\n          </button>\n        ),\n      )}\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;;AAgCA,IAAM,qBAAyC,CAAC;AAEhD,IAAa,oBAA+C,UAAU;CACpE,MAAM,EAAE,aAAa,yBAAyB;CAC9C,MAAM,EACJ,UAAU,cACV,gBAAgB,oBAChB,eAAe,qBACb;CACJ,MAAM,CAAC,kBAAkB,uBAAuB,SAAS,KAAK;CAE9D,MAAM,EACJ,kBAAkB,wBAClB,+BAA+B,iBAAiB,iBAC9C,oBAAoB;CAExB,MAAM,EACJ,8BACA,gBAAgB,uBAChB,YACE,kBAAkB;CACtB,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,WACJ,gBAAgB,iBAAiB,YAAY,EAAE,WAAW,QAAQ,GAAG,CAAC;CACxE,MAAM,EAAE,WAAW,0BAA0B,EAAE,IAAI,SAAS,CAAC;CAE7D,MAAM,iBAAiB,sBAAsB;CAC7C,MAAM,eAAe,oBAAoB,SAAS,iBAAiB;CAEnE,MAAM,oBAAoB,cAAc;EACtC,MAAM,MAA2C,CAAC;EAElD,KAAK,MAAM,YAAY,cACrB,IAAI,SAAS,UAAU;EAGzB,OAAO;CACT,GAAG,CAAC,YAAY,CAAC;CAEjB,MAAM,+BAA+B,cAAc;EACjD,IAAI,MAAM,QAAQ,eAAe,GAAG,OAAO;EAE3C,OAAO,OAAO,QAAQ,gBAAgB,KAAK,CAAC,CAAC,KAC1C,CAAC,MAAM,EAAE,WAAW,MAAM,gBAAgB;GACzC;GACA;GACA;GAC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