import React from 'react'; import {DatabaseFill, FolderFill} from '@gravity-ui/icons'; import {Breadcrumbs, Dialog, TextInput} from '@gravity-ui/uikit'; import {ResponseError} from '../../../../components/Errors/ResponseError'; import {useClusterWithProxy} from '../../../../store/reducers/cluster/cluster'; import {schemaApi} from '../../../../store/reducers/schema/schema'; import {cn} from '../../../../utils/cn'; import {BRAND_BUTTON_CLASS} from '../../../../utils/constants'; import i18n from '../../i18n'; import {transformPath} from '../transformPath'; import './CreateDirectoryDialog.scss'; const b = cn('ydb-schema-create-directory-dialog'); const relativePathInputId = 'relativePath'; interface CreateDirectoryDialogProps { open: boolean; onClose: VoidFunction; database: string; databaseFullPath: string; parentPath: string; onSuccess: (value: string) => void; } function validateRelativePath(value: string) { if (!value) { return i18n('alert_empty-path'); } if (/\s/.test(value)) { return i18n('alert_whitespace-not-allowed'); } return ''; } export function CreateDirectoryDialog({ open, onClose, database, databaseFullPath, parentPath, onSuccess, }: CreateDirectoryDialogProps) { const useMetaProxy = useClusterWithProxy(); const [validationError, setValidationError] = React.useState(''); const [relativePath, setRelativePath] = React.useState(''); const [create, response] = schemaApi.useCreateDirectoryMutation(); const inputRef = React.useRef(null); const normalizedDatabasePath = databaseFullPath.replace(/^\/+|\/+$/g, ''); const normalizedParentPath = parentPath.replace(/^\/+|\/+$/g, ''); const relativeParentPath = transformPath(parentPath, databaseFullPath).replace( /^\/+|\/+$/g, '', ); const isDatabaseRoot = normalizedParentPath === normalizedDatabasePath; const resetErrors = () => { setValidationError(''); response.reset(); }; const handleUpdate = (updated: string) => { setRelativePath(updated); resetErrors(); }; const handleClose = () => { onClose(); setRelativePath(''); resetErrors(); }; const handleSubmit = () => { const path = `${parentPath}/${relativePath}`; create({ database, databaseFullPath, path, useMetaProxy, }) .unwrap() .then(() => { handleClose(); onSuccess(relativePath); }); }; return (
{ e.preventDefault(); const nextValidationError = validateRelativePath(relativePath); setValidationError(nextValidationError); if (!nextValidationError) { handleSubmit(); } }} >
{normalizedDatabasePath} {!isDatabaseRoot && ( {relativeParentPath} )}
{response.isError && ( )}
); }