import { OrderedSet as ImmutableOrderedSet } from 'immutable'; import debounce from 'lodash/debounce'; import React, { useCallback, useEffect } from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { fetchAccount, fetchAccountByUsername } from 'soapbox/actions/accounts'; import { fetchFavouritedStatuses, expandFavouritedStatuses, fetchAccountFavouritedStatuses, expandAccountFavouritedStatuses } from 'soapbox/actions/favourites'; import MissingIndicator from 'soapbox/components/missing_indicator'; import StatusList from 'soapbox/components/status_list'; import { useAppDispatch, useAppSelector, useFeatures, useOwnAccount } from 'soapbox/hooks'; import { findAccountByUsername } from 'soapbox/selectors'; import Column from '../ui/components/column'; const messages = defineMessages({ heading: { id: 'column.favourited_statuses', defaultMessage: 'Liked posts' }, }); interface IFavourites { params?: { username?: string, } } /** Timeline displaying a user's favourited statuses. */ const Favourites: React.FC = (props) => { const intl = useIntl(); const dispatch = useAppDispatch(); const features = useFeatures(); const ownAccount = useOwnAccount(); const username = props.params?.username || ''; const account = useAppSelector(state => findAccountByUsername(state, username)); const isOwnAccount = username.toLowerCase() === ownAccount?.username?.toLowerCase(); const timelineKey = isOwnAccount ? 'favourites' : `favourites:${account?.id}`; const statusIds = useAppSelector(state => state.status_lists.get(timelineKey)?.items || ImmutableOrderedSet()); const isLoading = useAppSelector(state => state.status_lists.get(timelineKey)?.isLoading === true); const hasMore = useAppSelector(state => !!state.status_lists.get(timelineKey)?.next); const isUnavailable = useAppSelector(state => { const blockedBy = state.relationships.getIn([account?.id, 'blocked_by']) === true; return isOwnAccount ? false : (blockedBy && !features.blockersVisible); }); const handleLoadMore = useCallback(debounce(() => { if (isOwnAccount) { dispatch(expandFavouritedStatuses()); } else if (account) { dispatch(expandAccountFavouritedStatuses(account.id)); } }, 300, { leading: true }), [account?.id]); useEffect(() => { if (isOwnAccount) dispatch(fetchFavouritedStatuses()); else { if (account) { dispatch(fetchAccount(account.id)); dispatch(fetchAccountFavouritedStatuses(account.id)); } else { dispatch(fetchAccountByUsername(username)); } } }, []); useEffect(() => { if (account && !isOwnAccount) { dispatch(fetchAccount(account.id)); dispatch(fetchAccountFavouritedStatuses(account.id)); } }, [account?.id]); if (isUnavailable) { return (
); } if (!account) { return ( ); } const emptyMessage = isOwnAccount ? : ; return ( ); }; export default Favourites;