|
| 1 | +import { |
| 2 | + Body, |
| 3 | + css, |
| 4 | + palette, |
| 5 | + spacing, |
| 6 | + TextInput, |
| 7 | +} from '@mongodb-js/compass-components'; |
| 8 | +import React, { useMemo } from 'react'; |
| 9 | +import { connect } from 'react-redux'; |
| 10 | +import { CollectionState } from '../../modules/collection-tab'; |
| 11 | +import { SchemaAnalysisState } from '../../schema-analysis-types'; |
| 12 | +import numeral from 'numeral'; |
| 13 | +import { DEFAULT_DOCUMENT_COUNT, MAX_DOCUMENT_COUNT } from './constants'; |
| 14 | + |
| 15 | +const titleStyles = css({ |
| 16 | + fontWeight: 600, |
| 17 | +}); |
| 18 | + |
| 19 | +const descriptionStyles = css({ |
| 20 | + color: palette.gray.dark1, |
| 21 | + fontStyle: 'italic', |
| 22 | +}); |
| 23 | + |
| 24 | +const inputContainerStyles = css({ |
| 25 | + display: 'flex', |
| 26 | + flexDirection: 'row', |
| 27 | + gap: spacing[600], |
| 28 | + marginTop: spacing[200], |
| 29 | +}); |
| 30 | + |
| 31 | +const estimatedDiskSizeStyles = css({ |
| 32 | + fontSize: '13px', |
| 33 | + marginTop: spacing[100], |
| 34 | +}); |
| 35 | + |
| 36 | +const boldStyles = css({ |
| 37 | + fontWeight: 600, |
| 38 | +}); |
| 39 | + |
| 40 | +const formatBytes = (bytes: number) => { |
| 41 | + const precision = bytes <= 1000 ? '0' : '0.0'; |
| 42 | + return numeral(bytes).format(precision + 'b'); |
| 43 | +}; |
| 44 | + |
| 45 | +interface OwnProps { |
| 46 | + documentCount: number; |
| 47 | + onDocumentCountChange: (documentCount: number) => void; |
| 48 | +} |
| 49 | + |
| 50 | +interface Props extends OwnProps { |
| 51 | + schemaAnalysisState: SchemaAnalysisState; |
| 52 | +} |
| 53 | + |
| 54 | +const DocumentCountScreen = ({ |
| 55 | + documentCount, |
| 56 | + onDocumentCountChange, |
| 57 | + schemaAnalysisState, |
| 58 | +}: Props) => { |
| 59 | + const estimatedDiskSize = useMemo(() => { |
| 60 | + return schemaAnalysisState.status === 'complete' |
| 61 | + ? schemaAnalysisState.schemaMetadata.avgDocumentSize * documentCount |
| 62 | + : 0; |
| 63 | + }, [schemaAnalysisState, documentCount]); |
| 64 | + |
| 65 | + const errorState = useMemo(() => { |
| 66 | + return documentCount < 1 || documentCount > MAX_DOCUMENT_COUNT |
| 67 | + ? 'error' |
| 68 | + : 'none'; |
| 69 | + }, [documentCount]); |
| 70 | + |
| 71 | + const errorMessage = useMemo(() => { |
| 72 | + return documentCount < 1 || documentCount > MAX_DOCUMENT_COUNT |
| 73 | + ? 'Document count must be between 1 and 100000' |
| 74 | + : undefined; |
| 75 | + }, [documentCount]); |
| 76 | + |
| 77 | + return schemaAnalysisState.status === 'complete' ? ( |
| 78 | + <div> |
| 79 | + <Body className={titleStyles}> |
| 80 | + Specify Number of Documents to Generate |
| 81 | + </Body> |
| 82 | + <Body className={descriptionStyles}> |
| 83 | + Indicate the amount of documents you want to generate below. |
| 84 | + <br /> |
| 85 | + Note: We have defaulted to {DEFAULT_DOCUMENT_COUNT}. |
| 86 | + </Body> |
| 87 | + <div className={inputContainerStyles}> |
| 88 | + <TextInput |
| 89 | + label="Documents to generate in current collection" |
| 90 | + type="number" |
| 91 | + value={documentCount.toString()} |
| 92 | + onChange={(e) => onDocumentCountChange(Number(e.target.value))} |
| 93 | + min={1} |
| 94 | + max={MAX_DOCUMENT_COUNT} |
| 95 | + state={errorState} |
| 96 | + errorMessage={errorMessage} |
| 97 | + /> |
| 98 | + <div> |
| 99 | + <Body className={boldStyles}>Estimated Disk Size</Body> |
| 100 | + <Body className={estimatedDiskSizeStyles}> |
| 101 | + {formatBytes(estimatedDiskSize)} |
| 102 | + </Body> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + ) : ( |
| 107 | + // Not reachable since schema analysis must be finished before the modal can be opened |
| 108 | + <div>We are analyzing your collection.</div> |
| 109 | + ); |
| 110 | +}; |
| 111 | + |
| 112 | +const mapStateToProps = (state: CollectionState, _ownProps: OwnProps) => { |
| 113 | + const schemaAnalysisState = state.schemaAnalysis; |
| 114 | + |
| 115 | + return { |
| 116 | + schemaAnalysisState, |
| 117 | + }; |
| 118 | +}; |
| 119 | + |
| 120 | +const ConnectedDocumentCountScreen = connect( |
| 121 | + mapStateToProps, |
| 122 | + {} |
| 123 | +)(DocumentCountScreen); |
| 124 | + |
| 125 | +export default ConnectedDocumentCountScreen; |
0 commit comments