{"version":3,"file":"AIReplyActions.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/Message/AIReplyActions.tsx"],"sourcesContent":["import React, { useEffect, useRef, useState } from 'react';\nimport clsx from 'clsx';\n\nimport { useTranslationContext } from '../../../context/TranslationContext';\nimport { useComponentContextIcons } from '../../../context/useComponentContextIcons';\nimport {\n  IconAIShare,\n  IconAIThumbsDown,\n  IconAIThumbsDownFill,\n  IconAIThumbsUp,\n  IconAIThumbsUpFill,\n} from '../icons';\nimport { ThemeScopeProvider, useAIThemeScopeClassName } from '../themeScope';\n\n/** How the person rated a reply. */\nexport type AIMessageRating = 'positive' | 'negative';\n\nexport type AIReplyActionsProps = {\n  /** The reply's text, which copying and sharing hand on. */\n  text: string;\n  className?: string;\n  /** Rates the reply, or takes the rating back with `null`. Without it, no rating is offered. */\n  onRate?: (rating: AIMessageRating | null) => void;\n  /** Asks the agent for a new reply in its place. Without it, no regenerate button. */\n  onRegenerate?: () => void;\n  /** How the person rated the reply, if they did. */\n  rating?: AIMessageRating | null;\n};\n\nconst copyText = async (text: string) => {\n  if (navigator.clipboard?.writeText) {\n    await navigator.clipboard.writeText(text);\n    return;\n  }\n  // Clipboard API unavailable (insecure context): fall back to a selection copy.\n  const area = document.createElement('textarea');\n  area.value = text;\n  area.setAttribute('readonly', '');\n  area.style.position = 'fixed';\n  area.style.opacity = '0';\n  document.body.appendChild(area);\n  area.select();\n  document.execCommand('copy');\n  area.remove();\n};\n\n/**\n * The actions under a finished reply: copy, share (the system share sheet where the browser\n * has one, else a copy), regenerate, and a thumbs up or down rating. The counterpart of\n * `AIReplyActionsView`.\n */\nexport const AIReplyActions = ({\n  className,\n  onRate,\n  onRegenerate,\n  rating,\n  text,\n}: AIReplyActionsProps) => {\n  const themeScopeClassName = useAIThemeScopeClassName();\n  const { t } = useTranslationContext();\n  const { IconCheckmark, IconCopy, IconRefresh } = useComponentContextIcons();\n  const [isCopied, setIsCopied] = useState(false);\n  const timer = useRef<ReturnType<typeof setTimeout>>(undefined);\n  useEffect(() => () => clearTimeout(timer.current), []);\n\n  const copy = () => {\n    copyText(text).then(\n      () => {\n        setIsCopied(true);\n        clearTimeout(timer.current);\n        timer.current = setTimeout(() => setIsCopied(false), 1500);\n      },\n      () => undefined,\n    );\n  };\n\n  const share = () => {\n    if (typeof navigator.share === 'function') {\n      navigator.share({ text }).catch((error: unknown) => {\n        // The person closing the share sheet is not a failure.\n        if (error instanceof Error && error.name === 'AbortError') return;\n        copy();\n      });\n      return;\n    }\n    copy();\n  };\n\n  const positive = rating === 'positive';\n  const negative = rating === 'negative';\n\n  return (\n    <ThemeScopeProvider>\n      <div\n        aria-label={t(\n          'aiComponents.replyActions.replyActions.ariaLabel',\n          'Reply actions',\n        )}\n        className={clsx(themeScopeClassName, 'str-chat__ai-reply-actions', className)}\n        role='toolbar'\n      >\n        <button\n          aria-label={\n            isCopied\n              ? t('aiComponents.replyActions.copied.ariaLabel', 'Copied')\n              : t('aiComponents.replyActions.copy.ariaLabel', 'Copy')\n          }\n          className='str-chat__ai-reply-actions__button'\n          onClick={copy}\n          title={\n            isCopied\n              ? t('aiComponents.replyActions.copied.ariaLabel', 'Copied')\n              : t('aiComponents.replyActions.copy.ariaLabel', 'Copy')\n          }\n          type='button'\n        >\n          {isCopied ? <IconCheckmark /> : <IconCopy />}\n        </button>\n        <button\n          aria-label={t('aiComponents.replyActions.share.ariaLabel', 'Share')}\n          className='str-chat__ai-reply-actions__button'\n          onClick={share}\n          title={t('aiComponents.replyActions.share.ariaLabel', 'Share')}\n          type='button'\n        >\n          <IconAIShare />\n        </button>\n        {onRegenerate && (\n          <button\n            aria-label={t('aiComponents.replyActions.regenerate.ariaLabel', 'Regenerate')}\n            className='str-chat__ai-reply-actions__button'\n            onClick={onRegenerate}\n            title={t('aiComponents.replyActions.regenerate.ariaLabel', 'Regenerate')}\n            type='button'\n          >\n            <IconRefresh />\n          </button>\n        )}\n        {onRate && (\n          <>\n            <button\n              aria-label={t(\n                'aiComponents.replyActions.goodResponse.ariaLabel',\n                'Good response',\n              )}\n              aria-pressed={positive}\n              className='str-chat__ai-reply-actions__button'\n              onClick={() => onRate(positive ? null : 'positive')}\n              title={t(\n                'aiComponents.replyActions.goodResponse.ariaLabel',\n                'Good response',\n              )}\n              type='button'\n            >\n              {positive ? <IconAIThumbsUpFill /> : <IconAIThumbsUp />}\n            </button>\n            <button\n              aria-label={t(\n                'aiComponents.replyActions.badResponse.ariaLabel',\n                'Bad response',\n              )}\n              aria-pressed={negative}\n              className='str-chat__ai-reply-actions__button'\n              onClick={() => onRate(negative ? null : 'negative')}\n              title={t('aiComponents.replyActions.badResponse.ariaLabel', 'Bad response')}\n              type='button'\n            >\n              {negative ? <IconAIThumbsDownFill /> : <IconAIThumbsDown />}\n            </button>\n          </>\n        )}\n      </div>\n    </ThemeScopeProvider>\n  );\n};\n"],"mappings":";;;;;;;;;AA6BA,IAAM,WAAW,OAAO,SAAiB;CACvC,IAAI,UAAU,WAAW,WAAW;EAClC,MAAM,UAAU,UAAU,UAAU,IAAI;EACxC;CACF;CAEA,MAAM,OAAO,SAAS,cAAc,UAAU;CAC9C,KAAK,QAAQ;CACb,KAAK,aAAa,YAAY,EAAE;CAChC,KAAK,MAAM,WAAW;CACtB,KAAK,MAAM,UAAU;CACrB,SAAS,KAAK,YAAY,IAAI;CAC9B,KAAK,OAAO;CACZ,SAAS,YAAY,MAAM;CAC3B,KAAK,OAAO;AACd;;;;;;AAOA,IAAa,kBAAkB,EAC7B,WACA,QACA,cACA,QACA,WACyB;CACzB,MAAM,sBAAsB,yBAAyB;CACrD,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,EAAE,eAAe,UAAU,gBAAgB,yBAAyB;CAC1E,MAAM,CAAC,UAAU,eAAe,SAAS,KAAK;CAC9C,MAAM,QAAQ,OAAsC,KAAA,CAAS;CAC7D,sBAAsB,aAAa,MAAM,OAAO,GAAG,CAAC,CAAC;CAErD,MAAM,aAAa;EACjB,SAAS,IAAI,CAAC,CAAC,WAC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