{"version":3,"file":"AIMessageAttachment.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/Message/AIMessageAttachment.tsx"],"sourcesContent":["import React from 'react';\nimport clsx from 'clsx';\nimport { sanitizeUrl } from '@braintree/sanitize-url';\n\nimport { useTranslationContext } from '../../../context/TranslationContext';\nimport { useComponentContextIcons } from '../../../context/useComponentContextIcons';\nimport { prettifyFileSize } from '../../../components/MessageComposer/hooks/utils';\nimport { useAIThemeScopeClassName } from '../themeScope';\n\n/**\n * What a message carries besides its text, such as an image or a file. The counterpart of\n * `AIMessageAttachment` in StreamChatAI.\n */\nexport type AIMessageAttachmentData = {\n  /** `image` shows a thumbnail; `file` shows its name, type and size. An open set. */\n  kind: 'image' | 'file' | (string & {});\n  /** Data of its own, such as what an attachment of a kind of your own needs. */\n  extraData?: Record<string, unknown>;\n  /** Its media type, such as `application/pdf`. Without it, the type comes from its name or URL. */\n  mimeType?: string;\n  /** Its name, such as the file's. */\n  name?: string;\n  /** Its size, in bytes. */\n  size?: number;\n  /** Where it is: a web URL, or an object or data URL of something the app holds, such as a photo just picked. */\n  url?: string;\n};\n\nconst extensionOf = (attachment: AIMessageAttachmentData) => {\n  for (const candidate of [attachment.name, attachment.url]) {\n    if (!candidate) continue;\n    const path = candidate.split(/[?#]/)[0] ?? '';\n    const file = path.split('/').pop() ?? '';\n    const dot = file.lastIndexOf('.');\n    if (dot > 0 && dot < file.length - 1) return file.slice(dot + 1);\n  }\n  return attachment.mimeType?.split('/')[1]?.split(/[+;]/)[0];\n};\n\n// A web or blob URL as it is, and an image's data URL for its thumbnail (a photo just picked);\n// anything else, such as `javascript:`, is blanked as the rest of the SDK's links are.\nconst safeUrl = (url: string, kind: AIMessageAttachmentData['kind']) =>\n  /^blob:/i.test(url) || (kind === 'image' && /^data:image\\//i.test(url))\n    ? url\n    : sanitizeUrl(url);\n\nconst BLANK_URL = 'about:blank';\n\n/** Its type and size, such as \"PDF · 2.34 MB\". */\nexport const attachmentDetails = (attachment: AIMessageAttachmentData) =>\n  [\n    extensionOf(attachment)?.toUpperCase(),\n    attachment.size !== undefined ? prettifyFileSize(attachment.size) : undefined,\n  ]\n    .filter(Boolean)\n    .join(' · ');\n\nexport type AIMessageAttachmentProps = {\n  attachment: AIMessageAttachmentData;\n  /** Who sent it, which sets the side it leans to. */\n  role?: 'user' | 'assistant' | (string & {});\n  className?: string;\n};\n\n/**\n * An image or a file on a message: an image as a thumbnail that opens it, a file with its\n * name, type and size. The counterpart of `AIMessageAttachmentView`.\n */\nexport const AIMessageAttachment = ({\n  attachment,\n  className,\n  role = 'assistant',\n}: AIMessageAttachmentProps) => {\n  const themeScopeClassName = useAIThemeScopeClassName();\n  const { t } = useTranslationContext();\n  const { IconFile } = useComponentContextIcons();\n  const url = attachment.url ? safeUrl(attachment.url, attachment.kind) : undefined;\n  const name =\n    attachment.name ||\n    (attachment.url && !attachment.url.startsWith('data:')\n      ? attachment.url.split(/[?#]/)[0]?.split('/').pop()\n      : undefined) ||\n    (attachment.kind === 'image'\n      ? t('aiComponents.messageAttachment.image.text', 'Image')\n      : t('aiComponents.messageAttachment.file.text', 'File'));\n\n  const classes = clsx(\n    themeScopeClassName,\n    'str-chat__ai-message-attachment',\n    `str-chat__ai-message-attachment--${attachment.kind}`,\n    `str-chat__ai-message-attachment--${role}`,\n    className,\n  );\n\n  if (attachment.kind === 'image' && url && url !== BLANK_URL) {\n    return (\n      <a className={classes} href={url} rel='noreferrer' target='_blank'>\n        <img alt={name} className='str-chat__ai-message-attachment__image' src={url} />\n      </a>\n    );\n  }\n\n  const details = attachmentDetails(attachment);\n  const content = (\n    <>\n      <span className='str-chat__ai-message-attachment__file-icon'>\n        <IconFile />\n      </span>\n      <span className='str-chat__ai-message-attachment__file-text'>\n        <span className='str-chat__ai-message-attachment__file-name'>{name}</span>\n        {details && (\n          <span className='str-chat__ai-message-attachment__file-details'>{details}</span>\n        )}\n      </span>\n    </>\n  );\n\n  return url && url !== BLANK_URL ? (\n    <a\n      className={classes}\n      download={attachment.name}\n      href={url}\n      rel='noreferrer'\n      target='_blank'\n    >\n      {content}\n    </a>\n  ) : (\n    <div className={classes}>{content}</div>\n  );\n};\n"],"mappings":";;;;;;;;;AA4BA,IAAM,eAAe,eAAwC;CAC3D,KAAK,MAAM,aAAa,CAAC,WAAW,MAAM,WAAW,GAAG,GAAG;EACzD,IAAI,CAAC,WAAW;EAEhB,MAAM,QADO,UAAU,MAAM,MAAM,CAAC,CAAC,MAAM,GAAA,CACzB,MAAM,GAAG,CAAC,CAAC,IAAI,KAAK;EACtC,MAAM,MAAM,KAAK,YAAY,GAAG;EAChC,IAAI,MAAM,KAAK,MAAM,KAAK,SAAS,GAAG,OAAO,KAAK,MAAM,MAAM,CAAC;CACjE;CACA,OAAO,WAAW,UAAU,MAAM,GAAG,CAAC,CAAC,EAAE,EAAE,MAAM,MAAM,CAAC,CAAC;AAC3D;AAIA,IAAM,WAAW,KAAa,SAC5B,UAAU,KAAK,GAAG,KAAM,SAAS,WAAW,iBAAiB,KAAK,GAAG,IACjE,MACA,YAAY,GAAG;AAErB,IAAM,YAAY;;AAGlB,IAAa,qBAAqB,eAChC,CACE,YAAY,UAAU,CAAC,EAAE,YAAY,GACrC,WAAW,SAAS,KAAA,IAAY,iBAAiB,WAAW,IAAI,IAAI,KAAA,CACtE,CAAC,CACE,OAAO,OAAO,CAAC,CACf,KAAK,KAAK;;;;;AAaf,IAAa,uBAAuB,EAClC,YACA,WACA,OAAO,kBACuB;CAC9B,MAAM,sBAAsB,yBAAyB;CACrD,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,EAAE,aAAa,yBAAyB;CAC9C,MAAM,MAAM,WAAW,MAAM,QAAQ,WAAW,KAAK,WAAW,IAAI,IAAI,KAAA;CACxE,MAAM,OACJ,WAAW,SACV,WAAW,OAAO,CAAC,WAAW,IAAI,WAAW,OAAO,IACjD,WAAW,IAAI,MAAM,MAAM,CAAC,CAAC,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,IACh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