pleroma/app/soapbox/features/account-gallery/index.tsx

147 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-01-30 08:49:34 -08:00
import React, { useEffect, useRef } from 'react';
import { FormattedMessage } from 'react-intl';
import { useParams } from 'react-router-dom';
import { openModal } from 'soapbox/actions/modals';
import { expandAccountMediaTimeline } from 'soapbox/actions/timelines';
2023-06-25 10:24:21 -07:00
import { useAccountLookup } from 'soapbox/api/hooks';
2022-11-15 08:00:49 -08:00
import LoadMore from 'soapbox/components/load-more';
import MissingIndicator from 'soapbox/components/missing-indicator';
import { Column, Spinner } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
2023-06-25 10:24:21 -07:00
import { getAccountGallery } from 'soapbox/selectors';
2022-11-15 09:23:36 -08:00
import MediaItem from './components/media-item';
import type { List as ImmutableList } from 'immutable';
import type { Attachment, Status } from 'soapbox/types/entities';
interface ILoadMoreMedia {
maxId: string | null
onLoadMore: (value: string | null) => void
}
const LoadMoreMedia: React.FC<ILoadMoreMedia> = ({ maxId, onLoadMore }) => {
const handleLoadMore = () => {
onLoadMore(maxId);
};
return (
<LoadMore onClick={handleLoadMore} />
);
};
const AccountGallery = () => {
const dispatch = useAppDispatch();
const { username } = useParams<{ username: string }>();
2023-06-25 10:24:21 -07:00
const {
account,
isLoading: accountLoading,
isUnavailable,
2023-06-25 10:24:21 -07:00
} = useAccountLookup(username, { withRelationship: true });
2023-06-25 10:24:21 -07:00
const attachments: ImmutableList<Attachment> = useAppSelector((state) => getAccountGallery(state, account!.id));
const isLoading = useAppSelector((state) => state.timelines.get(`account:${account?.id}:media`)?.isLoading);
const hasMore = useAppSelector((state) => state.timelines.get(`account:${account?.id}:media`)?.hasMore);
const next = useAppSelector(state => state.timelines.get(`account:${account?.id}:media`)?.next);
const node = useRef<HTMLDivElement>(null);
const handleScrollToBottom = () => {
if (hasMore) {
handleLoadMore(attachments.size > 0 ? attachments.last()!.status.id : undefined);
}
};
const handleLoadMore = (maxId: string | null) => {
2023-06-25 10:24:21 -07:00
if (account) {
dispatch(expandAccountMediaTimeline(account.id, { url: next, maxId }));
}
};
const handleLoadOlder: React.MouseEventHandler = e => {
e.preventDefault();
handleScrollToBottom();
};
const handleOpenMedia = (attachment: Attachment) => {
if (attachment.type === 'video') {
dispatch(openModal('VIDEO', { media: attachment, status: attachment.status, account: attachment.account }));
} else {
const media = (attachment.status as Status).media_attachments;
const index = media.findIndex((x) => x.id === attachment.id);
2022-12-29 06:17:19 -08:00
dispatch(openModal('MEDIA', { media, index, status: attachment.status }));
}
};
useEffect(() => {
2023-06-25 10:24:21 -07:00
if (account) {
dispatch(expandAccountMediaTimeline(account.id));
}
2023-06-25 10:24:21 -07:00
}, [account?.id]);
2023-06-25 10:24:21 -07:00
if (accountLoading || (!attachments && isLoading)) {
return (
2023-06-25 10:24:21 -07:00
<Column>
<Spinner />
</Column>
);
}
2023-06-25 10:24:21 -07:00
if (!account) {
return (
2023-06-25 10:24:21 -07:00
<MissingIndicator />
);
}
let loadOlder = null;
if (hasMore && !(isLoading && attachments.size === 0)) {
loadOlder = <LoadMore className='my-auto' visible={!isLoading} onClick={handleLoadOlder} />;
}
if (isUnavailable) {
return (
<Column>
<div className='empty-column-indicator'>
<FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />
</div>
</Column>
);
}
return (
2023-06-25 10:24:21 -07:00
<Column label={`@${account.acct}`} transparent withHeader={false}>
2023-01-30 08:49:34 -08:00
<div role='feed' className='grid grid-cols-2 gap-2 sm:grid-cols-3' ref={node}>
{attachments.map((attachment, index) => attachment === null ? (
<LoadMoreMedia key={'more:' + attachments.get(index + 1)?.id} maxId={index > 0 ? (attachments.get(index - 1)?.id || null) : null} onLoadMore={handleLoadMore} />
) : (
<MediaItem
key={`${attachment.status.id}+${attachment.id}`}
attachment={attachment}
onOpenMedia={handleOpenMedia}
/>
))}
{!isLoading && attachments.size === 0 && (
<div className='empty-column-indicator col-span-2 sm:col-span-3'>
<FormattedMessage id='account_gallery.none' defaultMessage='No media to show.' />
</div>
)}
{loadOlder}
</div>
{isLoading && attachments.size === 0 && (
<div className='slist__append'>
<Spinner />
</div>
)}
</Column>
);
};
export default AccountGallery;