Support adding files through the VFS

This commit is contained in:
Alex Gleason committed 2025-09-14 12:50:28 -05:00
1 parent be8309b503
commit a106a6e10b
6 files changed
+478 -6

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { FileAttachment } from './file-attachment';
describe('FileAttachment', () => {
it('renders the attachment button', () => {
const onFileSelect = vi.fn();
render(
<FileAttachment onFileSelect={onFileSelect} />
);
const button = screen.getByRole('button');
expect(button).toBeInTheDocument();
});
it('calls onFileSelect when file is selected', async () => {
const onFileSelect = vi.fn();
render(
<FileAttachment onFileSelect={onFileSelect} />
);
const button = screen.getByRole('button');
fireEvent.click(button);
// Get the hidden file input
const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
expect(fileInput).toBeInTheDocument();
// Create a mock file
const file = new File(['test content'], 'test.txt', { type: 'text/plain' });
// Mock the file input change event
Object.defineProperty(fileInput, 'files', {
value: [file],
writable: false,
});
fireEvent.change(fileInput);
expect(onFileSelect).toHaveBeenCalledWith(file);
});
it('displays selected files', () => {
const onFileSelect = vi.fn();
const onFileRemove = vi.fn();
const selectedFiles = [
new File(['content1'], 'file1.txt', { type: 'text/plain' }),
new File(['content2'], 'file2.txt', { type: 'text/plain' }),
];
render(
<FileAttachment
onFileSelect={onFileSelect}
onFileRemove={onFileRemove}
selectedFiles={selectedFiles}
/>
);
expect(screen.getByText('file1.txt')).toBeInTheDocument();
expect(screen.getByText('file2.txt')).toBeInTheDocument();
});
it('calls onFileRemove when remove button is clicked', () => {
const onFileSelect = vi.fn();
const onFileRemove = vi.fn();
const selectedFiles = [
new File(['content'], 'test.txt', { type: 'text/plain' }),
];
render(
<FileAttachment
onFileSelect={onFileSelect}
onFileRemove={onFileRemove}
selectedFiles={selectedFiles}
/>
);
const removeButtons = screen.getAllByRole('button');
// First button is the attachment button, second is the remove button
const removeButton = removeButtons[1];
fireEvent.click(removeButton);
expect(onFileRemove).toHaveBeenCalledWith(selectedFiles[0]);
});
it('disables interaction when disabled prop is true', () => {
const onFileSelect = vi.fn();
render(
<FileAttachment onFileSelect={onFileSelect} disabled={true} />
);
const button = screen.getByRole('button');
expect(button).toBeDisabled();
const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
expect(fileInput).toBeDisabled();
});
});
+100
View File
@@ -0,0 +1,100 @@
import React, { useRef } from 'react';
import { Button } from '@/components/ui/button';
import { Paperclip, X } from 'lucide-react';
import { cn } from '@/lib/utils';
interface FileAttachmentProps {
onFileSelect: (file: File) => void;
onFileRemove?: (file: File) => void;
selectedFiles?: File[];
disabled?: boolean;
accept?: string;
multiple?: boolean;
className?: string;
}
export function FileAttachment({
onFileSelect,
onFileRemove,
selectedFiles = [],
disabled = false,
accept,
multiple = false,
className
}: FileAttachmentProps) {
const fileInputRef = useRef<HTMLInputElement>(null);
const handleButtonClick = () => {
fileInputRef.current?.click();
};
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const files = event.target.files;
if (files) {
Array.from(files).forEach(file => {
onFileSelect(file);
});
}
// Reset input so the same file can be selected again
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
};
const handleRemoveFile = (file: File) => {
if (onFileRemove) {
onFileRemove(file);
}
};
return (
<div className={cn("flex items-center gap-2", className)}>
<input
ref={fileInputRef}
type="file"
onChange={handleFileChange}
accept={accept}
multiple={multiple}
className="hidden"
disabled={disabled}
/>
<Button
type="button"
variant="ghost"
size="sm"
onClick={handleButtonClick}
disabled={disabled}
className="h-8 w-8 p-0 hover:bg-muted/50"
>
<Paperclip className="h-4 w-4" />
</Button>
{selectedFiles.length > 0 && (
<div className="flex flex-wrap gap-1 max-w-48">
{selectedFiles.map((file, index) => (
<div
key={`${file.name}-${index}`}
className="flex items-center gap-1 px-2 py-1 bg-muted/50 rounded-md text-xs"
>
<span className="truncate max-w-24" title={file.name}>
{file.name}
</span>
{onFileRemove && (
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => handleRemoveFile(file)}
className="h-3 w-3 p-0 hover:bg-muted/70"
>
<X className="h-2 w-2" />
</Button>
)}
</div>
))}
</div>
)}
</div>
);
}