import React, { useCallback, useEffect, useMemo, useRef } from 'react'; import { connect } from 'react-redux'; import { Button, Modal, ModalBody, ModalFooter, ModalHeader, css, spacing, FormFieldContainer, } from '@mongodb-js/compass-components'; import type { Document } from 'mongodb'; import { FINISHED_STATUSES, COMPLETED_STATUSES, STARTED, COMPLETED, COMPLETED_WITH_ERRORS, CANCELED, FAILED, UNSPECIFIED, } from '../constants/process-status'; import type { ProcessStatus } from '../constants/process-status'; import ProgressBar from './progress-bar'; import { ImportPreview } from './import-preview'; import { ImportOptions } from './import-options'; import type { AcceptedFileType } from '../constants/file-types'; import formatNumber from '../utils/format-number'; import { startImport, cancelImport, selectImportFileType, selectImportFileName, setDelimiter, setStopOnErrors, setIgnoreBlanks, closeImport, toggleIncludeField, setFieldType, } from '../modules/import'; import { ImportErrorList } from './import-error-list'; import type { RootImportState } from '../stores/import-store'; import type { CSVDelimiter, FieldFromCSV } from '../modules/import'; /** * Progress messages. */ const MESSAGES = { [STARTED]: 'Importing documents...', [CANCELED]: 'Import canceled', [COMPLETED]: 'Import completed', [COMPLETED_WITH_ERRORS]: 'Import completed with following errors:', [FAILED]: 'Failed to import with the following error:', [UNSPECIFIED]: '', } as const; const closeButtonStyles = css({ marginRight: spacing[2], }); type ImportModalProps = { isOpen: boolean; ns: string; startImport: () => void; cancelImport: () => void; closeImport: () => void; errors: Error[]; status: ProcessStatus; /** * See `` */ selectImportFileType: (fileType: AcceptedFileType) => void; selectImportFileName: (fileName: string) => void; setDelimiter: (delimiter: CSVDelimiter) => void; delimiter: CSVDelimiter; fileType: AcceptedFileType | ''; fileName: string; stopOnErrors: boolean; setStopOnErrors: (stopOnErrors: boolean) => void; ignoreBlanks: boolean; setIgnoreBlanks: (ignoreBlanks: boolean) => void; /** * See `` */ docsTotal: number; docsProcessed: number; docsWritten: number; guesstimatedDocsTotal: number; guesstimatedDocsProcessed: number; /** * See `` */ fields: { path: string; checked: boolean; type?: string; // Only on csv imports. }[]; values: Document[]; toggleIncludeField: (path: string) => void; setFieldType: (path: string, bsonType: string) => void; previewLoaded: boolean; }; function ImportModal({ isOpen, ns, startImport, cancelImport, closeImport, errors, status, selectImportFileType, selectImportFileName, setDelimiter, delimiter, fileType, fileName, stopOnErrors, setStopOnErrors, ignoreBlanks, setIgnoreBlanks, docsTotal, docsProcessed, docsWritten, guesstimatedDocsTotal, guesstimatedDocsProcessed, fields, values, toggleIncludeField, setFieldType, previewLoaded, }: ImportModalProps) { const modalBodyRef = useRef(null); const handleCancel = useCallback(() => { cancelImport(); }, [cancelImport]); const handleClose = useCallback(() => { handleCancel(); closeImport(); }, [closeImport, handleCancel]); const handleImportBtnClicked = useCallback(() => { startImport(); }, [startImport]); // docsTotal is set to actual value only at the very end of processing a // stream of documents const isGuesstimated = useMemo(() => docsTotal === -1, [docsTotal]); const importWasSuccessful = useMemo( () => COMPLETED_STATUSES.includes(status), [status] ); useEffect(() => { // When the errors change and there are now errors, we auto scroll // to the end of the modal body to ensure folks see the new errors. if (isOpen && errors && modalBodyRef.current) { const contentDiv = modalBodyRef.current; contentDiv.scrollTop = contentDiv.scrollHeight; } }, [errors, isOpen]); return ( {fileType === 'csv' && ( )} 0} cancel={cancelImport} docsWritten={docsWritten} docsProcessed={Math.max(docsProcessed, guesstimatedDocsProcessed)} docsTotal={ // When guesstimating, guessed total might be too low, in that // case the most reasonable thing to do would be to fallback to // currently processed number isGuesstimated ? Math.max(docsProcessed, guesstimatedDocsTotal) : docsTotal } progressLabel={(written: number, total: number) => { return `${formatNumber(written)}\u00a0/\u00a0${ isGuesstimated ? '~' : '' }${formatNumber(total)}`; }} progressTitle={(written: number, total: number) => { return `Imported ${formatNumber(written)} out of ${ isGuesstimated ? 'approximately ' : '' }${formatNumber(total)} documents`; }} message={MESSAGES[status]} /> {importWasSuccessful ? ( ) : ( <> )} ); } /** * Map the state of the store to component properties. */ const mapStateToProps = (state: RootImportState) => ({ ns: state.ns, isOpen: state.importData.isOpen, errors: state.importData.errors, fileType: state.importData.fileType, fileName: state.importData.fileName, status: state.importData.status, docsTotal: state.importData.docsTotal, docsProcessed: state.importData.docsProcessed, docsWritten: state.importData.docsWritten, guesstimatedDocsTotal: state.importData.guesstimatedDocsTotal, guesstimatedDocsProcessed: state.importData.guesstimatedDocsProcessed, delimiter: state.importData.delimiter, stopOnErrors: state.importData.stopOnErrors, ignoreBlanks: state.importData.ignoreBlanks, fields: state.importData.fields, values: state.importData.values, previewLoaded: state.importData.previewLoaded, }); /** * Export the connected component as the default. */ export default connect(mapStateToProps, { startImport, cancelImport, selectImportFileType, selectImportFileName, setDelimiter, setStopOnErrors, setIgnoreBlanks, closeImport, toggleIncludeField, setFieldType, })(ImportModal);