{"version":3,"file":"WithDragAndDropUpload.mjs","names":[],"sources":["../../../../src/components/MessageComposer/WithDragAndDropUpload.tsx"],"sourcesContent":["import type { CSSProperties, ElementType, PropsWithChildren } from 'react';\nimport React, { useCallback, useContext, useEffect, useMemo, useRef } from 'react';\nimport { useDropzone } from 'react-dropzone';\nimport clsx from 'clsx';\nimport type { MessageComposerConfig } from 'stream-chat';\n\nimport {\n  useComponentContext,\n  useComponentContextIcons,\n  useIsWithinMessageComposerContext,\n  useTranslationContext,\n} from '../../context';\nimport { useAttachmentManagerState, useMessageComposerController } from './hooks';\nimport { useStateStore } from '../../store';\nimport { useIsCooldownActive } from './hooks/useIsCooldownActive';\n\nconst DragAndDropUploadContext = React.createContext<{\n  subscribeToDrop: ((fn: (files: File[]) => void) => () => void) | null;\n}>({\n  subscribeToDrop: null,\n});\n\nexport const useDragAndDropUploadContext = () => useContext(DragAndDropUploadContext);\n\n/**\n * Registers the composer's `attachmentManager.uploadFiles` as a drop handler of the enclosing\n * `WithDragAndDropUpload`. Call it once, directly in `MessageComposerProvider`.\n *\n * @private\n */\nexport const useRegisterDropHandlers = () => {\n  const { subscribeToDrop } = useDragAndDropUploadContext();\n\n  const messageComposer = useMessageComposerController();\n\n  useEffect(() => {\n    const unsubscribe = subscribeToDrop?.(messageComposer.attachmentManager.uploadFiles);\n\n    return unsubscribe;\n  }, [subscribeToDrop, messageComposer]);\n};\n\nconst attachmentManagerConfigStateSelector = (state: MessageComposerConfig) => ({\n  acceptedFiles: state.attachments.acceptedFiles,\n  multipleUploads: state.attachments.maxNumberOfFilesPerMessage > 1,\n});\n\n/**\n * Wraps its children in a drop zone that uploads dropped files to the message composers inside it.\n * The accepted file types come from the composer config (`attachments.acceptedFiles`), and the\n * overlay shown while dragging is the `FileDragAndDropContent` slot.\n *\n * @example\n * ```tsx\n * <Channel channel={channel}>\n *  <WithDragAndDropUpload component=\"section\" className=\"message-list-dnd-wrapper\">\n *    <MessageList />\n *    <MessageComposer />\n *  </WithDragAndDropUpload>\n * </Channel>\n * ```\n */\nexport const WithDragAndDropUpload = ({\n  children,\n  className,\n  component: Component = 'div',\n  style,\n}: PropsWithChildren<{\n  /**\n   * @description Element rendered as the wrapper that handles drag and drop.\n   * @default 'div'\n   */\n  component?: ElementType;\n  className?: string;\n  style?: CSSProperties;\n}>) => {\n  const dropHandlersRef = useRef<Set<(f: File[]) => void>>(new Set());\n  const {\n    FileDragAndDropContent: FileDragAndDropContentComponent = FileDragAndDropContent,\n  } = useComponentContext();\n  const dragAndDropUploadContext = useDragAndDropUploadContext();\n  const messageComposer = useMessageComposerController();\n  const { isUploadEnabled } = useAttachmentManagerState();\n  const { acceptedFiles, multipleUploads } = useStateStore(\n    messageComposer.configState,\n    attachmentManagerConfigStateSelector,\n  );\n\n  const isCooldownActive = useIsCooldownActive();\n  // Within a message composer context, that composer handles the drop itself.\n  const isWithinMessageComposerContext = useIsWithinMessageComposerContext();\n\n  const accept = useMemo(\n    () =>\n      acceptedFiles.reduce<Record<string, Array<string>>>((mediaTypeMap, mediaType) => {\n        mediaTypeMap[mediaType] ??= [];\n        return mediaTypeMap;\n      }, {}),\n    [acceptedFiles],\n  );\n\n  const subscribeToDrop = useCallback((fn: (files: File[]) => void) => {\n    dropHandlersRef.current.add(fn);\n\n    return () => {\n      dropHandlersRef.current.delete(fn);\n    };\n  }, []);\n\n  const handleDrop = useCallback((files: File[]) => {\n    dropHandlersRef.current.forEach((fn) => fn(files));\n  }, []);\n\n  const {\n    getRootProps,\n    isDragActive,\n    isDragReject: isDragRejected,\n  } = useDropzone({\n    accept,\n    // Outside a composer context the zone is never disabled; each composer's `uploadFiles`\n    // enforces the limitations internally.\n    disabled: isWithinMessageComposerContext\n      ? !isUploadEnabled || isCooldownActive\n      : false,\n    multiple: multipleUploads,\n    noClick: true,\n    noKeyboard: true,\n    onDrop: isWithinMessageComposerContext\n      ? messageComposer.attachmentManager.uploadFiles\n      : handleDrop,\n  });\n\n  // A nested WithDragAndDropUpload, e.g. the one `MessageComposerUI` always renders, is a plain\n  // wrapper: the outermost one handles the drop.\n  if (dragAndDropUploadContext.subscribeToDrop !== null) {\n    return (\n      <Component className={className} style={style}>\n        {children}\n      </Component>\n    );\n  }\n\n  const rootClassName = clsx('str-chat__dropzone-root', className);\n\n  return (\n    <DragAndDropUploadContext.Provider value={{ subscribeToDrop }}>\n      <Component {...getRootProps({ className: rootClassName, style })}>\n        {isDragActive && (\n          <div\n            className={clsx('str-chat__dropzone-container', {\n              'str-chat__dropzone-container--not-accepted': isDragRejected,\n            })}\n            role='presentation'\n          >\n            <FileDragAndDropContentComponent isDragRejected={isDragRejected} />\n          </div>\n        )}\n        {children}\n      </Component>\n    </DragAndDropUploadContext.Provider>\n  );\n};\n\nexport type FileDragAndDropContentProps = {\n  isDragRejected: boolean;\n};\n\nexport const FileDragAndDropContent = ({\n  isDragRejected,\n}: FileDragAndDropContentProps) => {\n  const { IconUpload } = useComponentContextIcons();\n  const { t } = useTranslationContext();\n  return (\n    <div className='str-chat__dropzone-container__content'>\n      {isDragRejected ? (\n        <p>\n          {t(\n            'messageComposer.dragDropUpload.someFilesNotAccepted.text',\n            'Some of the files will not be accepted',\n          )}\n        </p>\n      ) : (\n        <>\n          <IconUpload />\n          <p>\n            {t('messageComposer.dragDropUpload.dragFiles.text', 'Drag your files here')}\n          </p>\n        </>\n      )}\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;AAgBA,IAAM,2BAA2B,MAAM,cAEpC,EACD,iBAAiB,KACnB,CAAC;AAED,IAAa,oCAAoC,WAAW,wBAAwB;;;;;;;AAQpF,IAAa,gCAAgC;CAC3C,MAAM,EAAE,oBAAoB,4BAA4B;CAExD,MAAM,kBAAkB,6BAA6B;CAErD,gBAAgB;EAGd,OAFoB,kBAAkB,gBAAgB,kBAAkB,WAAW;CAGrF,GAAG,CAAC,iBAAiB,eAAe,CAAC;AACvC;AAEA,IAAM,wCAAwC,WAAkC;CAC9E,eAAe,MAAM,YAAY;CACjC,iBAAiB,MA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