bigbuffet-rw/app/soapbox/queries/ads.ts

33 lines
746 B
TypeScript
Raw Normal View History

2022-08-01 20:43:28 -07:00
import { useQuery } from '@tanstack/react-query';
import { Ad, getProvider } from 'soapbox/features/ads/providers';
import { useAppDispatch } from 'soapbox/hooks';
import { isExpired } from 'soapbox/utils/ads';
2022-08-01 20:43:28 -07:00
export default function useAds() {
const dispatch = useAppDispatch();
const getAds = async() => {
return dispatch(async(_, getState) => {
const provider = await getProvider(getState);
if (provider) {
return provider.getAds(getState);
} else {
return [];
}
});
};
2022-08-26 07:48:49 -07:00
const result = useQuery<Ad[]>(['ads'], getAds, {
2022-08-01 20:43:28 -07:00
placeholderData: [],
});
2022-08-26 07:48:49 -07:00
// Filter out expired ads.
2022-08-30 06:57:44 -07:00
const data = result.data?.filter(ad => !isExpired(ad));
2022-08-26 07:48:49 -07:00
return {
...result,
data,
};
2022-08-01 20:43:28 -07:00
}