2022-03-21 11:09:01 -07:00
|
|
|
import { Map as ImmutableMap } from 'immutable';
|
|
|
|
import * as React from 'react';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
2022-05-30 11:23:55 -07:00
|
|
|
import { fetchTrends } from 'soapbox/actions/trends';
|
|
|
|
import Hashtag from 'soapbox/components/hashtag';
|
2022-03-25 12:08:12 -07:00
|
|
|
import { Widget } from 'soapbox/components/ui';
|
2022-03-21 11:09:01 -07:00
|
|
|
import { useAppSelector } from 'soapbox/hooks';
|
|
|
|
|
|
|
|
interface ITrendsPanel {
|
|
|
|
limit: number
|
|
|
|
}
|
|
|
|
|
|
|
|
const TrendsPanel = ({ limit }: ITrendsPanel) => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
const trends: any = useAppSelector((state) => state.trends.get('items'));
|
|
|
|
|
|
|
|
const sortedTrends = React.useMemo(() => {
|
|
|
|
return trends.sort((a: ImmutableMap<string, any>, b: ImmutableMap<string, any>) => {
|
|
|
|
const num_a = Number(a.getIn(['history', 0, 'accounts']));
|
|
|
|
const num_b = Number(b.getIn(['history', 0, 'accounts']));
|
|
|
|
return num_b - num_a;
|
|
|
|
}).slice(0, limit);
|
|
|
|
}, [trends, limit]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
dispatch(fetchTrends());
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (sortedTrends.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-03-25 12:08:12 -07:00
|
|
|
<Widget title={<FormattedMessage id='trends.title' defaultMessage='Trends' />}>
|
|
|
|
{sortedTrends.map((hashtag: ImmutableMap<string, any>) => (
|
|
|
|
<Hashtag key={hashtag.get('name')} hashtag={hashtag} />
|
|
|
|
))}
|
|
|
|
</Widget>
|
2022-03-21 11:09:01 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TrendsPanel;
|