{"version":3,"file":"AttachmentPreviewRoot.mjs","names":[],"sources":["../../../../../../src/components/MessageComposer/AttachmentPreviewList/utils/AttachmentPreviewRoot.tsx"],"sourcesContent":["import React, {\n  type ComponentProps,\n  type KeyboardEvent,\n  type MouseEvent,\n  useState,\n} from 'react';\nimport { useTranslationContext } from '../../../../context';\nimport {\n  isImageAttachment,\n  isVideoAttachment,\n  type LocalUploadAttachment,\n} from 'stream-chat';\n\ntype AttachmentPreviewRootProps = Omit<ComponentProps<'div'>, 'onClick' | 'onKeyDown'> & {\n  attachment: LocalUploadAttachment;\n  /**\n   * Called when the preview is pressed and the attachment can be previewed. The parent opens the\n   * gallery at the correct index.\n   */\n  openPreview?: () => void;\n  /** Runs before the default press handling. Return false to skip it. */\n  onPressed?: (e: MouseEvent<Element> | KeyboardEvent<Element>) => boolean;\n};\n\nconst INTERACTIVE_SELECTOR =\n  'button, a, input, textarea, select, [role=\"button\"], [role=\"link\"], [data-interactive=\"true\"]';\n\nfunction hasInteractiveAncestorBeforeRoot(\n  target: EventTarget | null,\n  root: HTMLElement | null,\n): boolean {\n  if (!(target instanceof Element) || !root) return false;\n\n  let el: Element | null = target;\n  while (el && el !== root) {\n    if (el.matches(INTERACTIVE_SELECTOR)) return true;\n    el = el.parentElement;\n  }\n  return false;\n}\n\n// todo: use this component for all the attachment previews\nexport const AttachmentPreviewRoot = ({\n  attachment,\n  onPressed,\n  openPreview,\n  tabIndex = 0,\n  ...props\n}: AttachmentPreviewRootProps) => {\n  const { t } = useTranslationContext();\n  const [root, setRoot] = useState<HTMLDivElement | null>(null);\n  const url =\n    attachment.asset_url || attachment.image_url || attachment.localMetadata.previewUri;\n\n  const canDownloadAttachment = false; //!!url;\n\n  const canPreviewAttachment =\n    !!openPreview &&\n    ((!!url && isImageAttachment(attachment)) || isVideoAttachment(attachment));\n\n  const handlePressed = (e: MouseEvent<Element> | KeyboardEvent<Element>) => {\n    if (e.defaultPrevented) return;\n\n    if (hasInteractiveAncestorBeforeRoot(e.target as Element, root)) return;\n\n    if (onPressed) {\n      const shouldContinue = onPressed(e);\n      if (!shouldContinue) return;\n    }\n\n    if (canPreviewAttachment) {\n      openPreview();\n      return;\n    }\n\n    if (canDownloadAttachment) {\n      window.open(url, '_blank', 'noopener,noreferrer');\n    }\n  };\n\n  const isInteractive = canPreviewAttachment || canDownloadAttachment;\n\n  return (\n    <div\n      aria-label={\n        !isInteractive\n          ? undefined\n          : canPreviewAttachment\n            ? t(\n                'messageComposer.attachmentPreviewRoot.showPreview.ariaLabel',\n                'Show preview',\n              )\n            : t('common.downloadAttachment.ariaLabel', 'Download attachment')\n      }\n      {...props}\n      onClick={handlePressed}\n      onKeyDown={\n        isInteractive\n          ? (e) => {\n              if (e.key !== 'Enter' && e.key !== ' ') return;\n              handlePressed(e);\n              e.preventDefault();\n            }\n          : undefined\n      }\n      ref={setRoot}\n      role={isInteractive ? 'button' : undefined}\n      tabIndex={isInteractive ? tabIndex : -1}\n    >\n      {props.children}\n    </div>\n  );\n};\n"],"mappings":";;;;;AAwBA,IAAM,uBACJ;AAEF,SAAS,iCACP,QACA,MACS;CACT,IAAI,EAAE,kBAAkB,YAAY,CAAC,MAAM,OAAO;CAElD,IAAI,KAAqB;CACzB,OAAO,MAAM,OAAO,MAAM;EACxB,IAAI,GAAG,QAAQ,oBAAoB,GAAG,OAAO;EAC7C,KAAK,GAAG;CACV;CACA,OAAO;AACT;AAGA,IAAa,yBAAyB,EACpC,YACA,WACA,aACA,WAAW,GACX,GAAG,YAC6B;CAChC,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,CAAC,MAAM,WAAW,SAAgC,IAAI;CAC5D,MAAM,MACJ,WAAW,aAAa,WAAW,aAAa,WAAW,cAAc;CAE3E,MAAM,wBAAwB;;CAE9B,MAAM,uBACJ,CAAC,CAAC,gBACA,CAAC,CAAC,OAAO,kBAAkB,UAAU,KAAM,kBAAkB,UAAU;CAE3E,MAAM,iBAAiB,MAAoD;EACzE,IAAI,EAAE,kBAAkB;EAExB,IAAI,iCAAiC,EAAE,QAAmB,IAAI,GAAG;EAEjE,IAAI;OAEE,CADmB,UAAU,CAC5B,GAAgB;EAAA;EAGvB,IAAI,sBAAsB;GACxB,YAAY;GACZ;EACF;CAKF;CAEA,MAAM,gBAAgB,wBAAwB;CAE9C,OACE,oBAAC,OAAD;EACE,cACE,CAAC,gBACG,KAAA,IACA,uBACE,EACE,+DACA,cACF,IACA,EAAE,uCAAuC,qBAAqB;EAEtE,GAAI;EACJ,SAAS;EACT,WACE,iBACK,MAAM;GACL,IAAI,EAAE,QAAQ,WAAW,EAAE,QAAQ,KAAK;GACxC,cAAc,CAAC;GACf,EAAE,eAAe;EACnB,IACA,KAAA;EAEN,KAAK;EACL,MAAM,gBAAgB,WAAW,KAAA;EACjC,UAAU,gBAAgB,WAAW;YAEpC,MAAM;CACJ,CAAA;AAET"}