soapbox/src/queries/trends.ts

31 wiersze
793 B
TypeScript
Czysty Zwykły widok Historia

2022-08-10 13:35:07 +00:00
import { useQuery } from '@tanstack/react-query';
import { fetchTrendsSuccess } from 'soapbox/actions/trends';
import { useApi, useAppDispatch } from 'soapbox/hooks';
import { normalizeTag } from 'soapbox/normalizers';
import type { Tag } from 'soapbox/types/entities';
export default function useTrends() {
const api = useApi();
const dispatch = useAppDispatch();
const getTrends = async() => {
2022-08-10 17:27:09 +00:00
const { data } = await api.get<any[]>('/api/v1/trends');
2022-08-10 13:35:07 +00:00
dispatch(fetchTrendsSuccess(data));
const normalizedData = data.map((tag) => normalizeTag(tag));
return normalizedData;
};
2023-10-17 20:19:05 +00:00
const result = useQuery<ReadonlyArray<Tag>>({
queryKey: ['trends'],
queryFn: getTrends,
2022-08-10 13:35:07 +00:00
placeholderData: [],
staleTime: 600000, // 10 minutes
});
return result;
2022-08-10 17:27:09 +00:00
}