|
| 1 | +import BAIModal, { type BAIModalProps } from '../../BAIModal'; |
| 2 | +import useConnectedBAIClient from '../../provider/BAIClientProvider/hooks/useConnectedBAIClient'; |
| 3 | +import { FolderInfoContext } from './BAIFileExplorer'; |
| 4 | +import { useMutation } from '@tanstack/react-query'; |
| 5 | +import { App, Form, Input, type FormInstance } from 'antd'; |
| 6 | +import _ from 'lodash'; |
| 7 | +import React, { use, useRef } from 'react'; |
| 8 | +import { useTranslation } from 'react-i18next'; |
| 9 | + |
| 10 | +interface CreateFileModalProps extends BAIModalProps { |
| 11 | + onRequestClose: (success: boolean) => void; |
| 12 | +} |
| 13 | + |
| 14 | +const CreateFileModal: React.FC<CreateFileModalProps> = ({ |
| 15 | + onRequestClose, |
| 16 | + ...modalProps |
| 17 | +}) => { |
| 18 | + 'use memo'; |
| 19 | + const { t } = useTranslation(); |
| 20 | + const { message, modal } = App.useApp(); |
| 21 | + const { targetVFolderId, currentPath } = use(FolderInfoContext); |
| 22 | + const baiClient = useConnectedBAIClient(); |
| 23 | + const formRef = useRef<FormInstance>(null); |
| 24 | + |
| 25 | + const createFileMutation = useMutation({ |
| 26 | + mutationFn: async ({ |
| 27 | + filePath, |
| 28 | + vfolderId, |
| 29 | + }: { |
| 30 | + filePath: string; |
| 31 | + vfolderId: string; |
| 32 | + }) => { |
| 33 | + // Use a newline (1 byte) since tus servers may reject 0-byte uploads. |
| 34 | + // A newline is preferred over a space as it makes the file show line numbers (up to 2), |
| 35 | + // making it clearer that the file has initial content. |
| 36 | + const blob = new Blob(['\n'], { type: 'application/octet-stream' }); |
| 37 | + const tusUrl: string = await baiClient.vfolder.create_upload_session( |
| 38 | + filePath, |
| 39 | + blob, |
| 40 | + vfolderId, |
| 41 | + ); |
| 42 | + // Complete the tus upload by sending the file content via PATCH |
| 43 | + const res = await fetch(tusUrl, { |
| 44 | + method: 'PATCH', |
| 45 | + headers: { |
| 46 | + 'Tus-Resumable': '1.0.0', |
| 47 | + 'Upload-Offset': '0', |
| 48 | + 'Content-Type': 'application/offset+octet-stream', |
| 49 | + }, |
| 50 | + body: blob, |
| 51 | + }); |
| 52 | + if (!res.ok) { |
| 53 | + throw new Error(`Failed to create file: ${res.statusText}`); |
| 54 | + } |
| 55 | + return res; |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + const createFile = () => { |
| 60 | + formRef.current |
| 61 | + ?.validateFields() |
| 62 | + .then(async (values) => { |
| 63 | + const filePath = [currentPath, values.fileName].join('/'); |
| 64 | + |
| 65 | + // Check for duplicate file name before creating |
| 66 | + const isDuplicate = await baiClient.vfolder |
| 67 | + .list_files(currentPath, targetVFolderId) |
| 68 | + .then((res) => _.some(res.items, (f) => f.name === values.fileName)) |
| 69 | + .catch(() => false); |
| 70 | + |
| 71 | + if (isDuplicate) { |
| 72 | + modal.confirm({ |
| 73 | + title: t('comp:FileExplorer.DuplicatedFiles'), |
| 74 | + content: t('comp:FileExplorer.DuplicatedFilesDesc'), |
| 75 | + onOk: () => { |
| 76 | + createFileMutation |
| 77 | + .mutateAsync({ filePath, vfolderId: targetVFolderId }) |
| 78 | + .then(() => { |
| 79 | + onRequestClose(true); |
| 80 | + message.success( |
| 81 | + t('comp:FileExplorer.FileCreatedSuccessfully'), |
| 82 | + ); |
| 83 | + }) |
| 84 | + .catch((err) => { |
| 85 | + message.error(err?.message || err?.title); |
| 86 | + }); |
| 87 | + }, |
| 88 | + }); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + createFileMutation |
| 93 | + .mutateAsync({ filePath, vfolderId: targetVFolderId }) |
| 94 | + .then(() => { |
| 95 | + onRequestClose(true); |
| 96 | + message.success(t('comp:FileExplorer.FileCreatedSuccessfully')); |
| 97 | + }) |
| 98 | + .catch((err) => { |
| 99 | + message.error(err?.message || err?.title); |
| 100 | + }); |
| 101 | + }) |
| 102 | + .catch(() => {}); |
| 103 | + }; |
| 104 | + |
| 105 | + return ( |
| 106 | + <BAIModal |
| 107 | + title={t('comp:FileExplorer.CreateANewFile')} |
| 108 | + onCancel={() => onRequestClose(false)} |
| 109 | + okText={t('general.button.Create')} |
| 110 | + onOk={createFile} |
| 111 | + okButtonProps={{ loading: createFileMutation.isPending }} |
| 112 | + {...modalProps} |
| 113 | + width={400} |
| 114 | + > |
| 115 | + <Form ref={formRef} layout="vertical"> |
| 116 | + <Form.Item |
| 117 | + name="fileName" |
| 118 | + label={t('comp:FileExplorer.FileName')} |
| 119 | + rules={[ |
| 120 | + { |
| 121 | + required: true, |
| 122 | + message: t('comp:FileExplorer.PleaseEnterAFileName'), |
| 123 | + }, |
| 124 | + { |
| 125 | + max: 255, |
| 126 | + message: t('comp:FileExplorer.MaxFileNameLength'), |
| 127 | + }, |
| 128 | + { |
| 129 | + validator: (_, value) => { |
| 130 | + if (value && (value.includes('/') || value.includes('\\'))) { |
| 131 | + return Promise.reject( |
| 132 | + new Error(t('comp:FileExplorer.InvalidFileNameCharacters')), |
| 133 | + ); |
| 134 | + } |
| 135 | + return Promise.resolve(); |
| 136 | + }, |
| 137 | + }, |
| 138 | + ]} |
| 139 | + > |
| 140 | + <Input placeholder={t('comp:FileExplorer.FileNamePlaceholder')} /> |
| 141 | + </Form.Item> |
| 142 | + </Form> |
| 143 | + </BAIModal> |
| 144 | + ); |
| 145 | +}; |
| 146 | + |
| 147 | +export default CreateFileModal; |
0 commit comments