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 { useAccountLookup } from 'soapbox/api/hooks'; import MissingIndicator from 'soapbox/components/missing-indicator'; import StatusList from 'soapbox/components/status-list'; import { Column } from 'soapbox/components/ui'; import { useAppDispatch, useAppSelector, useOwnAccount } from 'soapbox/hooks'; 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 = ({ params }) => { const intl = useIntl(); const dispatch = useAppDispatch(); const { account: ownAccount } = useOwnAccount(); const { account, isUnavailable } = useAccountLookup(params?.username, { withRelationship: true }); const username = params?.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 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;