import clsx from 'clsx'; import { List as ImmutableList } from 'immutable'; import React from 'react'; import { FormattedMessage } from 'react-intl'; import { useDispatch } from 'react-redux'; import { useHistory } from 'react-router-dom'; import { openModal } from 'soapbox/actions/modals'; import { HStack, Text, Emoji } from 'soapbox/components/ui'; import { useAppSelector, useSoapboxConfig, useFeatures } from 'soapbox/hooks'; import { reduceEmoji } from 'soapbox/utils/emoji-reacts'; import { shortNumberFormat } from 'soapbox/utils/numbers'; import type { Status } from 'soapbox/types/entities'; interface IStatusInteractionBar { status: Status } const StatusInteractionBar: React.FC = ({ status }): JSX.Element | null => { const history = useHistory(); const me = useAppSelector(({ me }) => me); const { allowedEmoji } = useSoapboxConfig(); const dispatch = useDispatch(); const features = useFeatures(); const { account } = status; if (!account || typeof account !== 'object') return null; const onOpenUnauthorizedModal = () => { dispatch(openModal('UNAUTHORIZED')); }; const onOpenReblogsModal = (username: string, statusId: string): void => { dispatch(openModal('REBLOGS', { username, statusId, })); }; const onOpenFavouritesModal = (username: string, statusId: string): void => { dispatch(openModal('FAVOURITES', { username, statusId, })); }; const onOpenReactionsModal = (username: string, statusId: string): void => { dispatch(openModal('REACTIONS', { username, statusId, })); }; const getNormalizedReacts = () => { return reduceEmoji( ImmutableList(status.pleroma.get('emoji_reactions') as any), status.favourites_count, status.favourited, allowedEmoji, ); }; const handleOpenReblogsModal: React.EventHandler = (e) => { e.preventDefault(); if (!me) onOpenUnauthorizedModal(); else onOpenReblogsModal(account.acct, status.id); }; const getReposts = () => { if (status.reblogs_count) { return ( ); } return null; }; const navigateToQuotes: React.EventHandler = (e) => { e.preventDefault(); history.push(`/@${status.getIn(['account', 'acct'])}/posts/${status.id}/quotes`); }; const getQuotes = () => { if (status.quotes_count) { return ( ); } return null; }; const handleOpenFavouritesModal: React.EventHandler> = (e) => { e.preventDefault(); if (!me) onOpenUnauthorizedModal(); else onOpenFavouritesModal(account.acct, status.id); }; const getFavourites = () => { if (status.favourites_count) { return ( ); } return null; }; const handleOpenReactionsModal = () => { if (!me) { return onOpenUnauthorizedModal(); } onOpenReactionsModal(account.acct, status.id); }; const getEmojiReacts = () => { const emojiReacts = getNormalizedReacts(); const count = emojiReacts.reduce((acc, cur) => ( acc + cur.get('count') ), 0); if (count) { return ( {emojiReacts.take(3).map((e, i) => { return ( ); })} ); } return null; }; return ( {getReposts()} {getQuotes()} {features.emojiReacts ? getEmojiReacts() : getFavourites()} ); }; interface IInteractionCounter { count: number onClick?: React.MouseEventHandler children: React.ReactNode } const InteractionCounter: React.FC = ({ count, onClick, children }) => { const features = useFeatures(); return ( ); }; export default StatusInteractionBar;