1 import type { ChangeEvent, FocusEvent } from 'react';
2 import React, { useState } from 'react';
4 import { c } from 'ttag';
6 import { Button } from '@proton/atoms';
7 import type { ModalStateProps } from '@proton/components';
17 } from '@proton/components';
18 import { useLoading } from '@proton/hooks';
19 import noop from '@proton/utils/noop';
21 import useActiveShare from '../../hooks/drive/useActiveShare';
22 import { formatLinkName, useActions, validateLinkNameField } from '../../store';
26 onCreateDone?: (folderId: string) => void;
27 folder?: { shareId: string; linkId: string };
30 const CreateFolderModal = ({ onClose, folder, onCreateDone, ...modalProps }: Props & ModalStateProps) => {
31 const { activeFolder } = useActiveShare();
32 const { createFolder } = useActions();
33 const [folderName, setFolderName] = useState('');
34 const [loading, withLoading] = useLoading();
35 const { validator, onFormSubmit } = useFormErrors();
37 const handleBlur = ({ target }: FocusEvent<HTMLInputElement>) => {
38 setFolderName(formatLinkName(target.value));
41 const handleChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
42 setFolderName(target.value);
45 const handleSubmit = async (e: React.FormEvent) => {
48 if (!onFormSubmit()) {
52 const formattedName = formatLinkName(folderName);
53 setFolderName(formattedName);
55 const parentFolder = folder || activeFolder;
60 const folderId = await createFolder(
61 new AbortController().signal,
66 onCreateDone?.(folderId);
73 disableCloseOnEscape={loading}
75 onSubmit={(e: React.FormEvent) => withLoading(handleSubmit(e)).catch(noop)}
79 <ModalTwoHeader closeButtonProps={{ disabled: loading }} title={c('Title').t`Create a new folder`} />
85 label={c('Label').t`Folder name`}
86 placeholder={c('Placeholder').t`Enter a new folder name`}
87 onChange={handleChange}
89 error={validator([validateLinkNameField(folderName) || ''])}
90 data-testid="input-new-folder-name"
95 <Button type="button" onClick={onClose} disabled={loading}>
96 {c('Action').t`Cancel`}
98 <PrimaryButton type="submit" loading={loading}>
99 {c('Action').t`Create`}
106 export default CreateFolderModal;
107 export const useCreateFolderModal = () => {
108 return useModalTwoStatic(CreateFolderModal);