|
| 1 | +// Copyright (c) Cosmo Tech. |
| 2 | +// Licensed under the MIT license. |
| 3 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 4 | +import { useTranslation } from 'react-i18next'; |
| 5 | +import { Edit as EditIcon } from '@mui/icons-material'; |
| 6 | +import { FormControl, FormHelperText, IconButton, OutlinedInput, Stack, Typography } from '@mui/material'; |
| 7 | +import { FadingTooltip, PermissionsGate } from '@cosmotech/ui'; |
| 8 | +import { ACL_PERMISSIONS } from '../../../../../../services/config/accessControl'; |
| 9 | +import { useEditableDatasetName } from './EditableDatasetNameHook'; |
| 10 | + |
| 11 | +const sanitizeValue = (value) => value.trim(); |
| 12 | + |
| 13 | +export const EditableDatasetName = () => { |
| 14 | + const { t } = useTranslation(); |
| 15 | + const { dataset, datasetName, renameDataset } = useEditableDatasetName(); |
| 16 | + |
| 17 | + const [error, setError] = useState(null); |
| 18 | + const [isEditing, setEditing] = useState(false); |
| 19 | + const [textFieldValue, setTextFieldValue] = useState(datasetName ?? ''); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + setTextFieldValue(datasetName); |
| 23 | + }, [datasetName]); |
| 24 | + |
| 25 | + const startEdition = (event) => { |
| 26 | + event.stopPropagation(); |
| 27 | + event.preventDefault(); |
| 28 | + setEditing(true); |
| 29 | + }; |
| 30 | + |
| 31 | + const editableTextField = useMemo(() => { |
| 32 | + const stopEdition = (event) => { |
| 33 | + event && event.stopPropagation(); |
| 34 | + setEditing(false); |
| 35 | + setError(null); |
| 36 | + }; |
| 37 | + |
| 38 | + const cancelEdition = (event) => { |
| 39 | + setTextFieldValue(datasetName); |
| 40 | + stopEdition(event); |
| 41 | + }; |
| 42 | + |
| 43 | + const confirmChanges = (event) => { |
| 44 | + const sanitizedValue = sanitizeValue(event.target.value); |
| 45 | + if (error == null && sanitizedValue.length !== 0 && sanitizedValue !== datasetName) { |
| 46 | + renameDataset(sanitizedValue); |
| 47 | + setTextFieldValue(sanitizedValue); |
| 48 | + } else setTextFieldValue(datasetName); |
| 49 | + |
| 50 | + stopEdition(event); |
| 51 | + }; |
| 52 | + |
| 53 | + const processNewValue = (event) => { |
| 54 | + const newValue = event.target.value; |
| 55 | + setTextFieldValue(newValue); |
| 56 | + |
| 57 | + const sanitizedValue = sanitizeValue(newValue); |
| 58 | + if (sanitizedValue.length !== 0) setError(null); |
| 59 | + else |
| 60 | + setError( |
| 61 | + t('commoncomponents.datasetmanager.overview.actions.emptyDatasetNameError', 'Dataset name cannot be empty') |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + return ( |
| 66 | + <FormControl data-cy="dataset-name-editable-text-field"> |
| 67 | + <OutlinedInput |
| 68 | + autoFocus |
| 69 | + value={textFieldValue} |
| 70 | + error={error != null} |
| 71 | + onChange={processNewValue} |
| 72 | + onBlur={confirmChanges} |
| 73 | + onClick={(event) => event.stopPropagation()} |
| 74 | + onKeyDown={(event) => { |
| 75 | + if (event.key === 'Escape') cancelEdition(event); |
| 76 | + else if (event.key === 'Enter') confirmChanges(event); |
| 77 | + }} |
| 78 | + onFocus={(event) => event.stopPropagation()} |
| 79 | + size="small" |
| 80 | + sx={{ minWidth: '400px', marginLeft: 0.5, padding: 0.5 }} |
| 81 | + /> |
| 82 | + <FormHelperText error>{error}</FormHelperText> |
| 83 | + </FormControl> |
| 84 | + ); |
| 85 | + }, [datasetName, error, renameDataset, setTextFieldValue, t, textFieldValue]); |
| 86 | + |
| 87 | + const datasetNameElement = useMemo( |
| 88 | + () => ( |
| 89 | + <Typography data-cy="dataset-name" variant="h6"> |
| 90 | + {datasetName} |
| 91 | + </Typography> |
| 92 | + ), |
| 93 | + [datasetName] |
| 94 | + ); |
| 95 | + |
| 96 | + const datasetNameWithEditButton = useMemo(() => { |
| 97 | + return ( |
| 98 | + <Stack direction="row" spacing={1} alignItems="stretch" justifyContent="flex-start"> |
| 99 | + {datasetNameElement} |
| 100 | + <FadingTooltip |
| 101 | + title={t('commoncomponents.datasetmanager.overview.actions.renameButtonTooltip', 'Rename')} |
| 102 | + disableInteractive={true} |
| 103 | + > |
| 104 | + <IconButton onClick={startEdition} data-cy="rename-dataset-button"> |
| 105 | + <EditIcon color="primary" /> |
| 106 | + </IconButton> |
| 107 | + </FadingTooltip> |
| 108 | + </Stack> |
| 109 | + ); |
| 110 | + }, [datasetNameElement, t]); |
| 111 | + |
| 112 | + const userPermissions = dataset?.security?.currentUserPermissions ?? []; |
| 113 | + return ( |
| 114 | + <PermissionsGate |
| 115 | + userPermissions={userPermissions} |
| 116 | + necessaryPermissions={[ACL_PERMISSIONS.DATASET.WRITE]} |
| 117 | + RenderNoPermissionComponent={() => datasetNameElement} |
| 118 | + > |
| 119 | + {isEditing ? editableTextField : datasetNameWithEditButton} |
| 120 | + </PermissionsGate> |
| 121 | + ); |
| 122 | +}; |
0 commit comments