{"version":3,"file":"Dropzone.mjs","names":["XCircleSolidIcon","DocumentArrowUpSolidIcon","DocumentArrowUpOutlineIcon"],"sources":["../../../src/dropzone/Dropzone.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport { useMergeRefs } from '@floating-ui/react';\nimport classNames from 'classnames';\nimport { type RefObject, useMemo, useRef, useState } from 'react';\nimport {\n  type DropEvent,\n  type DropzoneOptions,\n  type FileRejection,\n  useDropzone,\n} from 'react-dropzone';\nimport { useResizeObserver } from 'usehooks-ts';\n\nimport { type CommonProps } from '../_common/types';\nimport { Banner } from '../banner';\nimport DocumentArrowUpOutlineIcon from '../icons/generated/custom/DocumentArrowUpOutline';\nimport DocumentArrowUpSolidIcon from '../icons/generated/custom/DocumentArrowUpSolid';\nimport XCircleSolidIcon from '../icons/generated/custom/XCircleSolid';\nimport { ProgressBar } from '../progress-bar';\nimport { Typography } from '../typography';\n\n/**\n *\n *\n * Types\n *\n *\n */\n/**\n * @remarks\n * Where `DropzoneOptions` is the props for the `react-dropzone` library. For more information, see the [react-dropzone documentation](https://react-dropzone.js.org/#src).\n */\ntype DropzoneProps = {\n  /** Props for the underlying `react-dropzone` component. Don't use `accept`. If using custom file extensions as this option is broken. Instead use the `acceptedFileExtensions` property. */\n  dropZoneOptions: DropzoneOptions;\n\n  /** Informational text that highlights the supported file types, will be displayed in the footer. */\n  supportedFilesDescription?: React.ReactNode;\n\n  /** Element to display when uploading the file. */\n  loadingElement?: React.ReactNode;\n\n  /** Use when wanting a custom heading. */\n  heading?: React.ReactNode;\n\n  /** Message shown when a file is rejected. Defaults to \"File extension not supported.\" when omitted. */\n  rejectedText?: React.ReactNode;\n\n  /** The accepted file extensions for the Drag-and-Drop. */\n  acceptedFileExtensions?: string[];\n\n  /** Whether to display the dashed border around the dropzone. */\n  hasBorder?: boolean;\n};\n\ntype DropzoneLoadingProgressBarProps = {\n  /**  The percentage of the progress bar. */\n  progressBarPrecentage: number;\n\n  /** The time in minute left to upload the file. */\n  progressBarMinute?: number;\n};\n\n/**\n *\n *\n * Helpers\n *\n *\n */\n\nconst DropzoneComponent = ({\n  dropZoneOptions,\n  supportedFilesDescription,\n  loadingElement,\n  heading,\n  rejectedText,\n  acceptedFileExtensions,\n  hasBorder = true,\n  htmlAttributes,\n  style,\n  className,\n  ref,\n  ...restProps\n}: CommonProps<'div', DropzoneProps>) => {\n  /**\n   * We intercept the onDrop function in order to enforce\n   * custom file extensions. Before calling the onDrop props we check\n   * if the custom file extensions is acceptable. If the extension\n   * conforms to acceptedFileExtensions, we call onDrop. Otherwise, we\n   * present an error message.\n   */\n  function onDropInterceptor<T extends File>(\n    acceptedFiles: T[],\n    fileRejections: FileRejection[],\n    event: DropEvent,\n  ) {\n    if (acceptedFileExtensions && acceptedFileExtensions.length > 0) {\n      for (const file of acceptedFiles) {\n        const isAccepted = acceptedFileExtensions.some((extension) =>\n          file.name.endsWith(extension),\n        );\n        if (!isAccepted) {\n          setIsRejected(true);\n          return;\n        }\n      }\n    }\n    setIsRejected(false);\n    dropZoneOptions.onDrop?.(acceptedFiles, fileRejections, event);\n  }\n\n  const [isRejected, setIsRejected] = useState(false);\n  const dropZoneRef = useRef<HTMLDivElement>(null);\n  const mergedRef = useMergeRefs([dropZoneRef, ref]);\n  const isDisabled = dropZoneOptions?.disabled || Boolean(loadingElement);\n\n  const {\n    getRootProps,\n    getInputProps,\n    isDragActive,\n    isDragAccept,\n    isDragReject,\n    open,\n  } = useDropzone({\n    ...dropZoneOptions,\n    disabled: isDisabled,\n    noClick: true,\n    onDrop: onDropInterceptor,\n  });\n\n  const { width } = useResizeObserver({\n    ref: dropZoneRef as RefObject<HTMLElement>,\n  });\n\n  const isUploading = Boolean(loadingElement);\n\n  const titleText =\n    isDragActive && isDragReject\n      ? 'Format not supported'\n      : isDragActive && !isDragReject\n        ? 'Drop files'\n        : null;\n\n  const activeDragIcon = isDragReject ? (\n    <XCircleSolidIcon className=\"ndl-dropzone-drag-icon ndl-rejected\" />\n  ) : isDragAccept ? (\n    <DocumentArrowUpSolidIcon className=\"ndl-dropzone-drag-icon ndl-accepted\" />\n  ) : (\n    <DocumentArrowUpOutlineIcon className=\"ndl-dropzone-upload-icon\" />\n  );\n\n  const wrapperClasses = classNames('ndl-dropzone', className, {\n    'ndl-drag-active': isDragActive,\n    'ndl-drag-disabled': dropZoneOptions?.disabled,\n    'ndl-dropzone-borderless': !hasBorder,\n    'ndl-file-invalid': isDragActive && isDragReject,\n  });\n\n  useMemo(() => {\n    if (isDragActive && isRejected) {\n      setIsRejected(false);\n    }\n  }, [isDragActive, isRejected]);\n\n  return (\n    <>\n      {isRejected && (\n        <Banner className=\"ndl-dropzone-error-alert\" variant=\"danger\">\n          <Banner.Header>Error</Banner.Header>\n          <Banner.Description>\n            {rejectedText ?? 'File extension not supported.'}\n          </Banner.Description>\n        </Banner>\n      )}\n      <div\n        ref={mergedRef}\n        className={wrapperClasses}\n        style={style}\n        {...restProps}\n        {...htmlAttributes}\n      >\n        <div className=\"ndl-dropzone-inner-wrapper\" {...getRootProps()}>\n          <input aria-label=\"Drag and drop area\" {...getInputProps()} />\n          <div className=\"ndl-dropzone-inner\">\n            {/* Illustration should be visible only after arbitrary width */}\n            {width !== undefined && width > 230 && (\n              <>\n                {isDragActive ? (\n                  activeDragIcon\n                ) : (\n                  <DocumentArrowUpOutlineIcon className=\"ndl-dropzone-upload-icon\" />\n                )}\n              </>\n            )}\n            {!isUploading && (\n              <div className=\"ndl-dnd-title-container\">\n                <Typography variant=\"subheading-medium\">\n                  {titleText ||\n                    (heading ?? (\n                      <>\n                        Drag <span className=\"ndl-dnd-title-ampersand\">&</span>{' '}\n                        Drop\n                      </>\n                    ))}\n                </Typography>\n                {!isDragActive && !isDragReject && (\n                  <div className=\"ndl-dnd-subtitle\">\n                    <Typography variant=\"body-medium\">or</Typography>{' '}\n                    <button\n                      className=\"ndl-dnd-browse-link\"\n                      onClick={() => open()}\n                      disabled={isDisabled}\n                      type=\"button\"\n                    >\n                      Browse\n                    </button>\n                  </div>\n                )}\n              </div>\n            )}\n            <div className=\"ndl-dropzone-footer\">\n              {isUploading && <>{loadingElement}</>}\n              {!isUploading && !isDragActive && (\n                <div className=\"ndl-file-support-text\">\n                  {supportedFilesDescription}\n                </div>\n              )}\n            </div>\n          </div>\n        </div>\n      </div>\n    </>\n  );\n};\n\nconst DropzoneLoadingProgressBarComponent = ({\n  progressBarPrecentage,\n  progressBarMinute,\n  htmlAttributes,\n  className,\n  style,\n  ref,\n  ...restProps\n}: CommonProps<'div', DropzoneLoadingProgressBarProps>) => {\n  return (\n    <div className=\"ndl-dropzone-loading-progress-bar-wrapper\">\n      <div className=\"ndl-dropzone-loading-progress-bar-indicators\">\n        <Typography\n          className=\"ndl-dropzone-loading-progress-bar-precentage\"\n          variant=\"subheading-small\"\n        >\n          {`${progressBarPrecentage}%`}\n        </Typography>\n        {Boolean(progressBarMinute) && (\n          <Typography\n            className=\"ndl-dropzone-loading-progress-bar-minute\"\n            variant=\"body-medium\"\n          >{`(${progressBarMinute} minutes left)`}</Typography>\n        )}\n      </div>\n      <ProgressBar\n        className={classNames('ndl-dropzone-loading-progress-bar', className)}\n        value={progressBarPrecentage}\n        size=\"large\"\n        htmlAttributes={htmlAttributes}\n        ref={ref}\n        style={style}\n        {...restProps}\n      />\n    </div>\n  );\n};\n\nconst Dropzone = Object.assign(DropzoneComponent, {\n  LoadingProgressBar: DropzoneLoad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