bolt.diy/app/components/chat/ImportFolderButton.tsx

141 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-11-26 08:18:46 +00:00
import React, { useState } from 'react';
2024-11-25 08:24:03 +00:00
import type { Message } from 'ai';
import { toast } from 'react-toastify';
import { MAX_FILES, isBinaryFile, shouldIncludeFile } from '~/utils/fileUtils';
import { createChatFromFolder } from '~/utils/folderImport';
import { logStore } from '~/lib/stores/logs'; // Assuming logStore is imported from this location
2025-01-28 10:39:12 +00:00
import { Button } from '~/components/ui/Button';
import { cn } from '~/lib/utils';
2024-11-25 08:24:03 +00:00
interface ImportFolderButtonProps {
className?: string;
importChat?: (description: string, messages: Message[]) => Promise<void>;
}
export const ImportFolderButton: React.FC<ImportFolderButtonProps> = ({ className, importChat }) => {
2024-11-26 08:18:46 +00:00
const [isLoading, setIsLoading] = useState(false);
2024-11-26 08:18:46 +00:00
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
const allFiles = Array.from(e.target.files || []);
2024-11-25 08:24:03 +00:00
2024-12-30 02:21:49 +00:00
const filteredFiles = allFiles.filter((file) => {
const path = file.webkitRelativePath.split('/').slice(1).join('/');
const include = shouldIncludeFile(path);
return include;
});
if (filteredFiles.length === 0) {
const error = new Error('No valid files found');
logStore.logError('File import failed - no valid files', error, { folderName: 'Unknown Folder' });
toast.error('No files found in the selected folder');
return;
}
if (filteredFiles.length > MAX_FILES) {
const error = new Error(`Too many files: ${filteredFiles.length}`);
logStore.logError('File import failed - too many files', error, {
2024-12-30 02:21:49 +00:00
fileCount: filteredFiles.length,
maxFiles: MAX_FILES,
});
2024-11-26 08:18:46 +00:00
toast.error(
2024-12-30 02:21:49 +00:00
`This folder contains ${filteredFiles.length.toLocaleString()} files. This product is not yet optimized for very large projects. Please select a folder with fewer than ${MAX_FILES.toLocaleString()} files.`,
2024-11-26 08:18:46 +00:00
);
2024-11-26 08:18:46 +00:00
return;
}
2024-12-30 02:21:49 +00:00
const folderName = filteredFiles[0]?.webkitRelativePath.split('/')[0] || 'Unknown Folder';
2024-11-26 08:18:46 +00:00
setIsLoading(true);
2024-11-26 08:18:46 +00:00
const loadingToast = toast.loading(`Importing ${folderName}...`);
try {
const fileChecks = await Promise.all(
filteredFiles.map(async (file) => ({
file,
isBinary: await isBinaryFile(file),
})),
);
const textFiles = fileChecks.filter((f) => !f.isBinary).map((f) => f.file);
const binaryFilePaths = fileChecks
.filter((f) => f.isBinary)
.map((f) => f.file.webkitRelativePath.split('/').slice(1).join('/'));
if (textFiles.length === 0) {
const error = new Error('No text files found');
logStore.logError('File import failed - no text files', error, { folderName });
2024-11-26 08:18:46 +00:00
toast.error('No text files found in the selected folder');
2024-11-26 08:18:46 +00:00
return;
}
if (binaryFilePaths.length > 0) {
logStore.logWarning(`Skipping binary files during import`, {
folderName,
binaryCount: binaryFilePaths.length,
});
2024-11-26 08:18:46 +00:00
toast.info(`Skipping ${binaryFilePaths.length} binary files`);
}
const messages = await createChatFromFolder(textFiles, binaryFilePaths, folderName);
2024-11-26 08:18:46 +00:00
if (importChat) {
await importChat(folderName, [...messages]);
2024-11-26 08:18:46 +00:00
}
logStore.logSystem('Folder imported successfully', {
folderName,
textFileCount: textFiles.length,
binaryFileCount: binaryFilePaths.length,
});
2024-11-26 08:18:46 +00:00
toast.success('Folder imported successfully');
} catch (error) {
logStore.logError('Failed to import folder', error, { folderName });
2024-11-26 08:18:46 +00:00
console.error('Failed to import folder:', error);
toast.error('Failed to import folder');
} finally {
setIsLoading(false);
toast.dismiss(loadingToast);
e.target.value = ''; // Reset file input
2024-11-25 08:24:03 +00:00
}
};
return (
<>
<input
type="file"
id="folder-import"
className="hidden"
webkitdirectory=""
directory=""
2024-11-26 08:18:46 +00:00
onChange={handleFileChange}
{...({} as any)}
2024-11-25 08:24:03 +00:00
/>
2025-01-28 10:39:12 +00:00
<Button
2024-11-25 08:24:03 +00:00
onClick={() => {
const input = document.getElementById('folder-import');
input?.click();
}}
2025-01-28 10:39:12 +00:00
variant="outline"
size="lg"
className={cn(
'gap-2 bg-[#F5F5F5] dark:bg-[#252525]',
'text-bolt-elements-textPrimary dark:text-white',
'hover:bg-[#E5E5E5] dark:hover:bg-[#333333]',
'border-[#E5E5E5] dark:border-[#333333]',
'h-10 px-4 py-2 min-w-[120px] justify-center',
'transition-all duration-200 ease-in-out',
className,
)}
2024-11-26 08:18:46 +00:00
disabled={isLoading}
2024-11-25 08:24:03 +00:00
>
2025-01-28 10:39:12 +00:00
<span className="i-ph:upload-simple w-4 h-4" />
2024-11-26 08:18:46 +00:00
{isLoading ? 'Importing...' : 'Import Folder'}
2025-01-28 10:39:12 +00:00
</Button>
2024-11-25 08:24:03 +00:00
</>
);
};