import { observer } from "mobx-react"; import { ImportIcon } from "outline-icons"; import * as React from "react"; import Dropzone from "react-dropzone"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import styled from "styled-components"; import { breakpoint } from "@shared/styles/breakpoint"; import { DropzoneContainer, dropzoneIcon, } from "~/components/DropzoneContainer"; import Flex from "~/components/Flex"; import Text from "~/components/Text"; import useImportDocument from "~/hooks/useImportDocument"; import useStores from "~/hooks/useStores"; type Props = { /** The collection to import the documents into. */ collectionId?: string | null; /** The document to import the documents as children of. */ documentId?: string; /** Called once files have been chosen and the import has started. */ onSubmit: () => void; }; /** * A dialog that accepts documents to import, either dropped onto it or chosen * with the system file picker, and lists the file formats that are supported. */ export const ImportDocumentDialog = observer(function ImportDocumentDialog({ collectionId, documentId, onSubmit, }: Props) { const { t } = useTranslation(); const { documents } = useStores(); const { handleFiles } = useImportDocument(collectionId, documentId); const formats = React.useMemo( () => [ { name: t("Markdown"), extensions: ".md, .markdown", }, { name: t("Word"), extensions: ".docx", }, { name: "HTML", extensions: ".html, .htm, .mhtml, .mht", }, { name: "PDF", extensions: ".pdf", }, { name: t("Plain text"), extensions: ".txt", }, { name: "CSV", extensions: ".csv, .tsv", }, { name: t("Email"), extensions: ".eml", }, { name: "TextPack", extensions: ".textpack", }, ], [t] ); const handleDropAccepted = React.useCallback( (files: File[]) => { // Close the dialog before importing, progress is reported with a toast // and a successful import navigates to the new document. onSubmit(); void handleFiles(files); }, [handleFiles, onSubmit] ); const handleDropRejected = React.useCallback(() => { toast.error(t("This file type is not supported")); }, [t]); return ( {({ getRootProps, getInputProps, isDragActive }) => ( {t( "Drag and drop files here, or click to choose from your computer" )} )} {t("Supported formats")} {formats.map((format) => ( {format.name} {format.extensions} ))} ); }); const Icon = styled(ImportIcon)` ${dropzoneIcon} `; /** Below tablet width there is no room for two columns, so they stack. */ const Columns = styled(Flex)` flex-direction: column; ${breakpoint("tablet")` flex-direction: row; `} `; /** Fills its half so that the drop target is as tall as the format list. */ const DropTarget = styled(DropzoneContainer)` flex: 1 1 0; min-width: 0; display: flex; align-items: center; justify-content: center; min-height: 200px; `; const Formats = styled(Flex)` flex: 1 1 0; `;