const AttachmentManager = ({ articleId, attachments, onAttachmentsChange, readOnly = false }) => { const [uploading, setUploading] = React.useState(false); const fileInputRef = React.useRef(null); const handleUpload = async (files) => { if (!files || files.length === 0) return; setUploading(true); const newAttachments = []; for (const file of files) { try { const formData = new FormData(); formData.append('file', file); const response = await fetch(`/api/knowledge-base/${articleId}/attachments/upload`, { method: 'POST', headers: { 'X-OTO-User': localStorage.getItem('oto_username') }, body: formData }); const result = await response.json(); if (result.success) { newAttachments.push(result.attachment); } } catch (error) { console.error('Upload failed:', error); window.otoNotify?.toast(`Failed to upload ${file.name}`, 'error'); } } setUploading(false); if (newAttachments.length > 0) { onAttachmentsChange([...attachments, ...newAttachments]); } }; const handleDelete = async (attachmentId) => { const ok = await window.otoNotify?.confirm({ title: 'Delete attachment', message: 'Delete this attachment?', confirmLabel: 'Delete', danger: true }); if (!ok) return; try { const response = await fetch(`/api/knowledge-base/${articleId}/attachments/${attachmentId}`, { method: 'DELETE', headers: { 'X-OTO-User': localStorage.getItem('oto_username') } }); const result = await response.json(); if (result.success) { onAttachmentsChange(attachments.filter(a => a.id !== attachmentId)); } } catch (error) { console.error('Delete failed:', error); window.otoNotify?.toast('Failed to delete attachment', 'error'); } }; const formatFileSize = (bytes) => { if (bytes < 1024) return bytes + ' B'; if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; }; return (
{!readOnly && (
handleUpload(Array.from(e.target.files))} className="hidden" />
)} {attachments && attachments.length > 0 && (
{attachments.map((att) => (
📎
{att.original_filename}
{formatFileSize(att.file_size)} • Uploaded by {att.uploaded_by}
Download {!readOnly && ( )}
))}
)}
); }; if (typeof window !== 'undefined') window.AttachmentManager = AttachmentManager;