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 folder?: { shareId: string; linkId: string };
29 const CreateFileModal = ({ onClose, folder, ...modalProps }: Props & ModalStateProps) => {
30 const { activeFolder } = useActiveShare();
31 const { createFile } = useActions();
32 const [fileName, setFileName] = useState('');
33 const [loading, withLoading] = useLoading();
34 const { validator, onFormSubmit } = useFormErrors();
36 const handleBlur = ({ target }: FocusEvent<HTMLInputElement>) => {
37 setFileName(formatLinkName(target.value));
40 const handleChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
41 setFileName(target.value);
44 const handleSubmit = async (e: React.FormEvent) => {
47 if (!onFormSubmit()) {
51 let formattedName = formatLinkName(fileName);
52 if (!formattedName.includes('.')) {
53 formattedName = `${formattedName}.txt`;
56 const parentFolder = folder || activeFolder;
61 await createFile(parentFolder.shareId, parentFolder.linkId, formattedName);
68 disableCloseOnEscape={loading}
70 onSubmit={(e: React.FormEvent) => withLoading(handleSubmit(e)).catch(noop)}
74 <ModalTwoHeader closeButtonProps={{ disabled: loading }} title={c('Title').t`Create a new file`} />
80 label={c('Label').t`File name`}
81 placeholder={c('Placeholder').t`Enter a new file name`}
82 onChange={handleChange}
84 error={validator([validateLinkNameField(fileName) || ''])}
89 <Button type="button" onClick={onClose} disabled={loading}>
90 {c('Action').t`Cancel`}
92 <PrimaryButton type="submit" loading={loading}>
93 {c('Action').t`Create`}
100 export default CreateFileModal;
101 export const useCreateFileModal = () => {
102 return useModalTwoStatic(CreateFileModal);