import React, { useCallback } from 'react'; import { UserPicture } from '@growi/ui/dist/components'; import { useTranslation } from 'next-i18next'; import Image from 'next/image'; import prettyBytes from 'pretty-bytes'; import { useIsGuestUser, useIsReadOnlyUser, useIsSharedUser } from '~/stores-universal/context'; 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 { data: isGuestUser } = useIsGuestUser(); const { data: isSharedUser } = useIsSharedUser(); const { data: isReadOnlyUser } = useIsReadOnlyUser(); const showTrashButton = isGuestUser === false && isSharedUser === false && isReadOnlyUser === false; 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 (
attachment icon
{/* Since we need to include the "referer" to view the attachment on the shared page */} {/* eslint-disable-next-line react/jsx-no-target-blank */} {attachmentName || originalName} cloud_download {showTrashButton && ( delete )}
{new Date(createdAt).toLocaleString('en-US')} {prettyBytes(fileSize)}
); }); RichAttachment.displayName = 'RichAttachment';