crush-level-web/src/hooks/services/common.ts

37 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-12-23 11:35:48 +00:00
import { getLikeStatus, thmubObject } from '@/services/editor';
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useState } from 'react';
import { authKeys } from '@/lib/query-keys';
import { LikeObjectParamsType, LikeType } from '@/services/editor/type';
import { useAsyncFn } from '../tools';
export function useThmubObject(props: Pick<LikeObjectParamsType, 'objectId' | 'objectType'>) {
const queryClient = useQueryClient();
const [thumb, setThumb] = useState<LikeType>();
const user = queryClient.getQueryData(authKeys.currentUser()) as any;
useQuery({
queryKey: ['likeStatus', props.objectId, user.userId],
enabled: !!props.objectId && !!user.userId,
queryFn: () => {
return getLikeStatus({ objectId: props.objectId, userId: user.userId });
},
});
const { run: handleThumb, loading } = useAsyncFn(async (likeType: LikeType) => {
setThumb(likeType);
const user = queryClient.getQueryData(authKeys.currentUser()) as any;
const { data } = await thmubObject({ ...props, likeType, userId: user?.userId });
if (data.code === 200) {
} else {
setThumb(undefined);
}
});
return {
thumb,
loading,
handleThumb,
};
}