mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-07 04:20:12 +02:00
92 lines
3.5 KiB
TypeScript
92 lines
3.5 KiB
TypeScript
import { useIsMutating, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { AxiosError } from 'axios';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { api } from '/@/renderer/api';
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
|
import { eventEmitter } from '/@/renderer/events/event-emitter';
|
|
import { PreviousQueryData } from '/@/renderer/features/shared/mutations/favorite-optimistic-updates';
|
|
import {
|
|
applyRatingOptimisticUpdates,
|
|
restoreRatingQueryData,
|
|
} from '/@/renderer/features/shared/mutations/rating-optimistic-updates';
|
|
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
|
import { LogCategory, logFn } from '/@/renderer/utils/logger';
|
|
import { toast } from '/@/shared/components/toast/toast';
|
|
import { LibraryItem, RatingResponse, SetRatingArgs } from '/@/shared/types/domain-types';
|
|
|
|
const setRatingMutationKey = ['set-rating'];
|
|
|
|
export const useSetRatingMutation = (args: MutationHookArgs) => {
|
|
const { options } = args || {};
|
|
const queryClient = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation<RatingResponse, AxiosError, SetRatingArgs, PreviousQueryData[]>({
|
|
mutationFn: (args) => {
|
|
return api.controller.setRating({
|
|
...args,
|
|
apiClientProps: { serverId: args.apiClientProps.serverId },
|
|
});
|
|
},
|
|
mutationKey: setRatingMutationKey,
|
|
onError: (_error, _variables, context) => {
|
|
logFn.error('Set rating failed', {
|
|
category: LogCategory.API,
|
|
meta: {
|
|
id: _variables.query.id,
|
|
message: _error?.message,
|
|
rating: _variables.query.rating,
|
|
serverId: _variables.apiClientProps.serverId,
|
|
type: _variables.query.type,
|
|
},
|
|
});
|
|
if (context) {
|
|
restoreRatingQueryData(queryClient, context);
|
|
}
|
|
|
|
toast.show({
|
|
message: _error.message,
|
|
title: t('error.genericError', { postProcess: 'sentenceCase' }) as string,
|
|
type: 'error',
|
|
});
|
|
|
|
eventEmitter.emit('USER_RATING', {
|
|
id: _variables.query.id,
|
|
itemType: _variables.query.type,
|
|
rating: _variables.query.rating,
|
|
serverId: _variables.apiClientProps.serverId,
|
|
});
|
|
},
|
|
onMutate: (variables) => {
|
|
eventEmitter.emit('USER_RATING', {
|
|
id: variables.query.id,
|
|
itemType: variables.query.type,
|
|
rating: variables.query.rating,
|
|
serverId: variables.apiClientProps.serverId,
|
|
});
|
|
|
|
return applyRatingOptimisticUpdates(queryClient, variables, variables.query.rating);
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
if (
|
|
variables.query.type === LibraryItem.SONG ||
|
|
variables.query.type === LibraryItem.PLAYLIST_SONG ||
|
|
variables.query.type === LibraryItem.QUEUE_SONG
|
|
) {
|
|
queryClient.invalidateQueries({
|
|
queryKey: queryKeys.albumArtists.favoriteSongs(
|
|
variables.apiClientProps.serverId,
|
|
),
|
|
});
|
|
}
|
|
},
|
|
...options,
|
|
});
|
|
};
|
|
|
|
export const useIsMutatingRating = () => {
|
|
const mutatingCount = useIsMutating({ mutationKey: setRatingMutationKey });
|
|
return mutatingCount > 0;
|
|
};
|