import React, { useState } from 'react' import PropTypes from 'prop-types' import { Button, Modal } from 'react-bootstrap' import { useTranslation } from 'react-i18next' import { useRefWithAutoFocus } from '../../../../infrastructure/auto-focus' import { useFileTreeActionable } from '../../contexts/file-tree-actionable' function FileTreeModalCreateFolder() { const { t } = useTranslation() const [name, setName] = useState('') const { isCreatingFolder, inFlight, finishCreatingFolder, cancel, error } = useFileTreeActionable() if (!isCreatingFolder) return null // the modal will not be rendered; return early function handleHide() { cancel() } function handleCreateFolder() { finishCreatingFolder(name) } return ( {t('new_folder')} {error && ( {t('generic_something_went_wrong')} )} {inFlight ? ( {t('creating')}… ) : ( <> {t('cancel')} {t('create')} > )} ) } function InputName({ name, setName, handleCreateFolder }) { const { autoFocusedRef } = useRefWithAutoFocus() function handleFocus(ev) { ev.target.setSelectionRange(0, -1) } function handleChange(ev) { setName(ev.target.value) } function handleKeyDown(ev) { if (ev.key === 'Enter') { handleCreateFolder() } } return ( ) } InputName.propTypes = { name: PropTypes.string.isRequired, setName: PropTypes.func.isRequired, handleCreateFolder: PropTypes.func.isRequired } export default FileTreeModalCreateFolder