Convert trends to React Query

This commit is contained in:
Justin
2022-08-10 09:35:07 -04:00
parent 819c62fc3c
commit b2530dadd5
3 changed files with 75 additions and 13 deletions

View File

@@ -1,40 +1,32 @@
import * as React from 'react';
import { FormattedMessage } from 'react-intl';
import { useDispatch } from 'react-redux';
import { fetchTrends } from 'soapbox/actions/trends';
import Hashtag from 'soapbox/components/hashtag';
import { Widget } from 'soapbox/components/ui';
import { useAppSelector } from 'soapbox/hooks';
import useTrends from 'soapbox/queries/trends';
interface ITrendsPanel {
limit: number
}
const TrendsPanel = ({ limit }: ITrendsPanel) => {
const dispatch = useDispatch();
const trends = useAppSelector((state) => state.trends.items);
const { data: trends, isFetching } = useTrends();
const sortedTrends = React.useMemo(() => {
return trends.sort((a, b) => {
return trends?.sort((a, b) => {
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()) {
if (trends?.length === 0 || isFetching) {
return null;
}
return (
<Widget title={<FormattedMessage id='trends.title' defaultMessage='Trends' />}>
{sortedTrends.map((hashtag) => (
{sortedTrends?.slice(0, limit).map((hashtag) => (
<Hashtag key={hashtag.name} hashtag={hashtag} />
))}
</Widget>