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 { formatLinkName, validateLinkNameField } from '../../store';
25 onCreateDone?: (folderId: string) => void;
26 folder: { shareId: string; linkId: string };
27 createFolder: (abortSignal: AbortSignal, shareId: string, parentLinkId: string, name: string) => Promise<string>;
30 const CreateFolderModal = ({ createFolder, onClose, folder, onCreateDone, ...modalProps }: Props & ModalStateProps) => {
31 const [folderName, setFolderName] = useState('');
32 const [loading, withLoading] = useLoading();
33 const { validator, onFormSubmit } = useFormErrors();
35 const handleBlur = ({ target }: FocusEvent<HTMLInputElement>) => {
36 setFolderName(formatLinkName(target.value));
39 const handleChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
40 setFolderName(target.value);
43 const handleSubmit = async (e: React.FormEvent) => {
46 if (!onFormSubmit()) {
50 const formattedName = formatLinkName(folderName);
51 setFolderName(formattedName);
53 const parentFolder = folder;
58 const folderId = await createFolder(
59 new AbortController().signal,
64 onCreateDone?.(folderId);
71 disableCloseOnEscape={loading}
73 onSubmit={(e: React.FormEvent) => withLoading(handleSubmit(e)).catch(noop)}
77 <ModalTwoHeader closeButtonProps={{ disabled: loading }} title={c('Title').t`Create a new folder`} />
83 label={c('Label').t`Folder name`}
84 placeholder={c('Placeholder').t`Enter a new folder name`}
85 onChange={handleChange}
87 error={validator([validateLinkNameField(folderName) || ''])}
88 data-testid="input-new-folder-name"
93 <Button type="button" onClick={onClose} disabled={loading}>
94 {c('Action').t`Cancel`}
96 <PrimaryButton type="submit" loading={loading}>
97 {c('Action').t`Create`}
104 export default CreateFolderModal;
105 export const useCreateFolderModal = () => {
106 return useModalTwoStatic(CreateFolderModal);