import { List as ImmutableList } from 'immutable'; import React, { useState, useEffect } from 'react'; import { FormattedMessage } from 'react-intl'; import { openModal } from 'soapbox/actions/modals'; import { expandAccountMediaTimeline } from 'soapbox/actions/timelines'; import { Spinner, Text, Widget } from 'soapbox/components/ui'; import { useAppDispatch, useAppSelector } from 'soapbox/hooks'; import { getAccountGallery } from 'soapbox/selectors'; import MediaItem from '../../account-gallery/components/media-item'; import type { Account, Attachment } from 'soapbox/types/entities'; interface IProfileMediaPanel { account?: Account, } const ProfileMediaPanel: React.FC = ({ account }) => { const dispatch = useAppDispatch(); const [loading, setLoading] = useState(true); const attachments: ImmutableList = useAppSelector((state) => account ? getAccountGallery(state, account?.id) : ImmutableList()); const handleOpenMedia = (attachment: Attachment): void => { if (attachment.type === 'video') { dispatch(openModal('VIDEO', { media: attachment, status: attachment.status })); } else { const media = attachment.getIn(['status', 'media_attachments']) as ImmutableList; const index = media.findIndex(x => x.id === attachment.id); dispatch(openModal('MEDIA', { media, index, status: attachment.status })); } }; useEffect(() => { setLoading(true); if (account) { dispatch(expandAccountMediaTimeline(account.id)) // @ts-ignore yes it does .then(() => setLoading(false)) .catch(() => {}); } }, [account?.id]); const renderAttachments = () => { const publicAttachments = attachments.filter(attachment => attachment.getIn(['status', 'visibility']) === 'public'); const nineAttachments = publicAttachments.slice(0, 9); if (!nineAttachments.isEmpty()) { return (
{nineAttachments.map((attachment, _index) => ( ))}
); } else { return ( ); } }; return ( }> {account && (
{loading ? ( ) : ( renderAttachments() )}
)}
); }; export default ProfileMediaPanel;