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

36 lines
817 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';
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.
const data = result.data?.filter(ad => {
const now = new Date();
const isExpired = ad.expires && (now.getTime() > ad.expires.getTime());
return !isExpired;
});
return {
...result,
data,
};
2022-08-01 20:43:28 -07:00
}