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

50 lines
1 KiB
TypeScript
Raw Normal View History

import { useMutation, useQuery } from '@tanstack/react-query';
2022-08-02 06:20:07 -07:00
import { useApi, useFeatures } from 'soapbox/hooks';
2022-08-02 06:20:07 -07:00
export type Avatar = {
2022-08-02 06:20:07 -07:00
account_id: string
account_avatar: string
acct: string
seen?: boolean
2022-08-02 06:20:07 -07:00
}
const CarouselKeys = {
avatars: ['carouselAvatars'] as const,
};
function useCarouselAvatars() {
const api = useApi();
const getCarouselAvatars = async() => {
const { data } = await api.get('/api/v1/truth/carousels/avatars');
return data;
};
const result = useQuery<Avatar[]>(CarouselKeys.avatars, getCarouselAvatars, {
2022-08-02 06:20:07 -07:00
placeholderData: [],
keepPreviousData: true,
2022-08-02 06:20:07 -07:00
});
2022-08-08 12:53:21 -07:00
const avatars = result.data;
2022-08-02 06:20:07 -07:00
return {
2022-08-08 12:53:21 -07:00
...result,
data: avatars || [],
2022-08-02 06:20:07 -07:00
};
}
function useMarkAsSeen() {
const api = useApi();
const features = useFeatures();
return useMutation(async (accountId: string) => {
if (features.carouselSeen) {
await void api.post('/api/v1/truth/carousels/avatars/seen', {
account_id: accountId,
});
}
});
}
export { useCarouselAvatars, useMarkAsSeen };