import { MouseEvent } from "react"; import copyToClipboard from "../../lib/copyToClipboard"; import AttachmentEditor from "./AttachmentEditor"; import useModalStore from "../../stores/useModalStore"; import useToastStore from "../../stores/useToastStore"; import * as Attachments from "../../types/Attachments"; import * as Drag from "../../types/Drag"; import { Locale } from "../../types"; import { useDraggable } from "../drag"; type Props = { attributeName: string; placeholder: boolean; draggable: Drag.Draggable; locale: string; locales: { [index: string]: Locale }; deleteRecord: () => void; showEmbed: boolean; position: number; onUpdate: (attachment: Partial) => void; startDrag: ( evt: MouseEvent, draggable: Drag.Draggable ) => void; }; export default function Attachment(props: Props) { const { attributeName, draggable, locales, locale } = props; const { record } = draggable; const { attachment, uploading } = record; const openModal = useModalStore((state) => state.open); const notice = useToastStore((state) => state.notice); const listeners = useDraggable( draggable, props.startDrag ); const copyEmbed = (evt: MouseEvent) => { evt.preventDefault(); copyToClipboard(`[attachment:${attachment.id}]`); notice("Embed code copied to clipboard"); }; const deleteRecord = (evt: MouseEvent) => { evt.preventDefault(); if (props.deleteRecord) { props.deleteRecord(); } }; const description = () => { if (attachment.description && attachment.description[locale]) { return attachment.description[locale]; } return null; }; const name = () => { if (attachment.name && attachment.name[locale]) { return attachment.name[locale]; } return null; }; const editAttachment = (evt: MouseEvent) => { evt.preventDefault(); openModal( ); }; const classes = ["attachment"]; if (props.placeholder) { classes.push("placeholder"); } if (record.uploading) { classes.push("uploading"); } const icon = uploading ? "cloud-arrow-up" : "paperclip"; let localeDir = "ltr"; if (locale in locales && locales[locale].dir) { localeDir = locales[locale].dir; } return (
{!uploading && (
{props.showEmbed && } {props.deleteRecord && }
)} {attachment && (

{name() || Untitled}

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

{description()}

)}
)}
); }