SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.
JavaScript a2aagentic-aiai-agentsdevtoolsmcp-clientn8n-alternativeno-codeopen-sourceorchestrationsmythosvisual-builder
import { errorToast } from '@src/shared/components/toast';
import { ChangeEvent, useRef, useState } from 'react';
import { MAX_ATTACHMENT_SIZE } from '../../../shared/constants/general';
interface UseFileAttachmentProps {
onFileSelect?: (file: File) => void;
maxSize?: number;
acceptedTypes?: readonly string[];
}
export const useFileAttachment = ({
onFileSelect,
maxSize = MAX_ATTACHMENT_SIZE,
acceptedTypes = ['image/*'],
}: UseFileAttachmentProps = {}) => {
const [attachmentFile, setAttachmentFile] = useState<File | null>(null);
const [isDragging, setIsDragging] = useState(false);
const dragCounterRef = useRef(0);
const fileInputRef = useRef<HTMLInputElement>(null);
const handleFileSelect = (file: File) => {
if (file.size > maxSize) {
errorToast('File exceeds 5MB size limit');
return false;
}
if (
!acceptedTypes.some((type) => {
const [category, ext] = type.split('/');
return ext === '*' ? file.type.startsWith(category) : file.type === type;
})
) {
errorToast('Unsupported file type');
return false;
}
setAttachmentFile(file);
onFileSelect?.(file);
return true;
};
const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
handleFileSelect(file);
}
};
const handleDragEnter = (e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
dragCounterRef.current += 1;
if (dragCounterRef.current === 1) {
setIsDragging(true);
}
};
const handleDragLeave = (e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
dragCounterRef.current -= 1;
if (dragCounterRef.current === 0) {
setIsDragging(false);
}
};
const handleDragOver = (e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
};
const handleDrop = (e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
dragCounterRef.current = 0;
setIsDragging(false);
const file = e.dataTransfer.files[0];
if (file) {
handleFileSelect(file);
}
};
const handlePaste = (e: ClipboardEvent) => {
const items = e.clipboardData?.items;
if (!items) return;
for (const item of items) {
if (item.type.startsWith('image/')) {
const file = item.getAsFile();
if (file) {
handleFileSelect(file);
e.preventDefault();
break;
}
}
}
};
const clearAttachment = () => {
setAttachmentFile(null);
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
};
return {
attachmentFile,
fileInputRef,
isDragging,
handlers: {
handleDragEnter,
handleDragLeave,
handleDragOver,
handleDrop,
handleFileChange,
handlePaste,
handleFileSelect,
},
clearAttachment,
};
};