import React from 'react'; import { FormModal, Option, Select, css, spacing, } from '@mongodb-js/compass-components'; import { connect } from 'react-redux'; import type { MapDispatchToProps, MapStateToProps } from 'react-redux'; import type { RootState } from '../stores'; import { closeModal, openSelectedItem, selectCollection, selectDatabase, } from '../stores/open-item'; type AsyncItemsSelectProps = { name: string; label: string; items: string[]; selectedItem: string | null; isLoading: boolean; onChange(item: string): void; }; const select = css({ // Working around leafygreen Select issues // See https://jira.mongodb.org/browse/PD-1677 and https://jira.mongodb.org/browse/PD-1764 button: { zIndex: 999, }, }); const AsyncItemsSelect: React.FunctionComponent = ({ name, label, items, selectedItem, isLoading, onChange, }) => { return ( ); }; const mapDatabaseState: MapStateToProps< Pick, Pick, RootState > = ({ openItem: { databases, databasesStatus, selectedDatabase } }) => { return { items: databases, selectedItem: selectedDatabase, isLoading: ['initial', 'fetching'].includes(databasesStatus), }; }; const DatabaseSelect = connect(mapDatabaseState, { onChange: selectDatabase })( AsyncItemsSelect ); const mapCollectionState: MapStateToProps< Pick, Pick, RootState > = ({ openItem: { collections, collectionsStatus, selectedCollection } }) => { return { items: collections, selectedItem: selectedCollection, isLoading: ['initial', 'fetching'].includes(collectionsStatus), }; }; const CollectionSelect = connect(mapCollectionState, { onChange: selectCollection, })(AsyncItemsSelect); type OpenItemModalProps = { namespace: string; itemType: string; itemName: string; isModalOpen: boolean; isSubmitDisabled: boolean; onSubmit(): void; onClose(): void; }; const modalContent = css({ display: 'grid', gridTemplateAreas: ` 'description description' 'database collection' `, gridAutoColumns: '1fr', rowGap: spacing[4], columnGap: spacing[3], }); const description = css({ gridArea: 'description', }); const databaseSelect = css({ gridArea: 'database', }); const collectionSelect = css({ gridArea: 'collection', }); const OpenItemModal: React.FunctionComponent = ({ namespace, itemType, itemName, isModalOpen, isSubmitDisabled, onClose, onSubmit, }) => { return (
The namespace {namespace} for the saved {itemType}{' '} {itemName} doesn’t exist in the current cluster. Please select another namespace to open saved {itemType} .
); }; const mapState: MapStateToProps< Pick< OpenItemModalProps, 'isModalOpen' | 'isSubmitDisabled' | 'namespace' | 'itemType' | 'itemName' >, Record, RootState > = ({ openItem: { isModalOpen, selectedDatabase, selectedCollection, selectedItem: item, }, }) => { return { isModalOpen, isSubmitDisabled: !(selectedDatabase && selectedCollection), namespace: `${item?.database ?? ''}.${item?.collection ?? ''}`, itemName: item?.name ?? '', itemType: item?.type ?? '', }; }; const mapDispatch: MapDispatchToProps< Pick, Record > = { onSubmit: openSelectedItem, onClose: closeModal, }; export default connect(mapState, mapDispatch)(OpenItemModal);