import React, { useCallback } from 'react'; import { UserPicture } from '@growi/ui/dist/components'; import { useTranslation } from 'next-i18next'; import prettyBytes from 'pretty-bytes'; import { useSWRxAttachment } from '~/stores/attachment'; import { useDeleteAttachmentModal } from '~/stores/modal'; import styles from './RichAttachment.module.scss'; type RichAttachmentProps = { attachmentId: string, url: string, attachmentName: string, } export const RichAttachment = React.memo((props: RichAttachmentProps) => { const { attachmentId, attachmentName } = props; const { t } = useTranslation(); const { data: attachment, remove } = useSWRxAttachment(attachmentId); const { open: openDeleteAttachmentModal } = useDeleteAttachmentModal(); const onClickTrashButtonHandler = useCallback(() => { if (attachment == null) { return; } openDeleteAttachmentModal(attachment, remove); }, [attachment, openDeleteAttachmentModal, remove]); if (attachment == null) { return {t('rich_attachment.attachment_not_be_found')}; } const { filePathProxied, originalName, downloadPathProxied, creator, createdAt, fileSize, } = attachment; // Guard here because attachment properties might be deleted in turn when an attachment is removed if (filePathProxied == null || originalName == null || downloadPathProxied == null || creator == null || createdAt == null || fileSize == null ) { return {t('rich_attachment.attachment_not_be_found')}; } return (