import React from "react"; import PropTypes from "prop-types"; import copyToClipboard from "../../lib/copyToClipboard"; import AttachmentEditor from "./AttachmentEditor"; import ModalStore from "../ModalStore"; import ToastStore from "../ToastStore"; import { useDraggable } from "../drag"; export default function Attachment(props) { const { attributeName, draggable } = props; const { record } = draggable; const { attachment, uploading } = record; const listeners = useDraggable(draggable, props.startDrag); const copyEmbed = (evt) => { evt.preventDefault(); copyToClipboard(`[attachment:${attachment.id}]`); ToastStore.dispatch({ type: "NOTICE", message: "Embed code copied to clipboard" }); }; const deleteRecord = (evt) => { evt.preventDefault(); if (props.deleteRecord) { props.deleteRecord(); } }; const description = () => { if (attachment.description && attachment.description[props.locale]) { return attachment.description[props.locale]; } return null; }; const name = () => { if (attachment.name && attachment.name[props.locale]) { return attachment.name[props.locale]; } return null; }; const editAttachment = (evt) => { evt.preventDefault(); ModalStore.dispatch({ type: "OPEN", payload: }); }; const classes = ["attachment"]; if (props.placeholder) { classes.push("placeholder"); } if (record.uploading) { classes.push("uploading"); } const icon = uploading ? "cloud-upload" : "paperclip"; return (
{!uploading &&
{props.showEmbed && ( )} {props.deleteRecord && ( )}
} {attachment &&

{name() || Untitled}

{!uploading && {attachment.filename}} {!uploading && description() &&

{description()}

}
}
); } Attachment.propTypes = { locale: PropTypes.string, locales: PropTypes.object, draggable: PropTypes.object, deleteRecord: PropTypes.func, startDrag: PropTypes.func, showEmbed: PropTypes.bool, onUpdate: PropTypes.func, attributeName: PropTypes.string, placeholder: PropTypes.bool, position: PropTypes.number, ref: PropTypes.object };