import {useMutation} from '@tanstack/react-query';
import {EXPORT_ERROR_TIPS, exportFile} from '@utils';
import {message} from 'antd';

export function useExportFile<P>(fn: (params: P) => Promise<any>) {
  const {mutate, isLoading} = useMutation(
    fn,
    {
      onSuccess(data) {
        !process.env.IS_E2E && exportFile(data as Blob);
      },
      onError() {
        message.error(EXPORT_ERROR_TIPS);
      },
    },
  );

  return [isLoading, mutate] as const;
}
