import { useMutation } from 'react-query';

import { queryClient } from 'src/config/react-query';
import { markLastReadComment } from 'src/pages/dashboard/serviceManagement/services';

import { serviceManagementKeys } from '../queries';

type ReadCommentMutationParams = {
  taskId: string;
  commentId: string;
};

const useMarkLastReadComment = () => {
  const markAsReadMutation = useMutation(
    ({ taskId, commentId }: ReadCommentMutationParams) =>
      markLastReadComment(taskId, commentId),
    {
      onSuccess: (_, { taskId }) => {
        queryClient.invalidateQueries(
          serviceManagementKeys.comments.list(taskId),
        );
      },
      onError: () => {
        // do nothing, avoid global error handling
      },
    },
  );

  return markAsReadMutation;
};

export default useMarkLastReadComment;
