import { useCallback } from 'react'; import type { IAttachmentHasId } from '@growi/core'; import { Attachment, LoadingSpinner } from '@growi/ui/dist/components'; import { ExtractedAttachments } from './ExtractedAttachments'; import { RefsContext } from './util/refs-context'; import styles from './AttachmentList.module.scss'; const AttachmentLink = Attachment; type Props = { refsContext: RefsContext isLoading: boolean error?: Error attachments: IAttachmentHasId[] }; export const AttachmentList = ({ refsContext, isLoading, error, attachments, }: Props): JSX.Element => { const renderNoAttachmentsMessage = useCallback(() => { return (
{ refsContext.options?.prefix != null ? `${refsContext.options.prefix} and descendant pages have no attachments` : `${refsContext.pagePath} has no attachments` }
); }, [refsContext]); const renderContents = useCallback(() => { if (isLoading) { return (
{refsContext.toString()}
); } if (error != null) { return (
warning {refsContext.toString()} (-> {error.message})
); } // no attachments if (attachments.length === 0) { return renderNoAttachmentsMessage(); } return (refsContext.isExtractImage) ? : attachments.map((attachment) => { return ; }); }, [isLoading, error, attachments, refsContext, renderNoAttachmentsMessage]); return
{renderContents()}
; };