import { FC } from 'react';
import { QueryClient, setLogger, QueryClientProvider } from 'react-query';

import useGeneralError from 'src/hooks/useGeneralError';
import { useRevokeBlobFileInConversations } from 'src/pages/dashboard/Conversations/hooks/useConversationsQueries';

setLogger({
  log: () => {},
  warn: () => {},
  error: () => {},
});

export const queryClient = new QueryClient();

export const ReactQueryClientProvider: FC<React.PropsWithChildren> = props => {
  const { children } = props;
  const showGeneralError = useGeneralError({ showEnqueueSnackbar: false });

  useRevokeBlobFileInConversations(queryClient);

  queryClient.setDefaultOptions({
    queries: {
      retry: false,
      refetchOnWindowFocus: false,
      staleTime: 500, // data stays fresh for 500 milliseconds
      onError: err => showGeneralError(err),
    },
    mutations: {
      onError: err => showGeneralError(err),
    },
  });

  return (
    <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  );
};
