CommonProviders.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { ApolloProvider } from '@apollo/client'
  2. import { FC, PropsWithChildren } from 'react'
  3. import { QueryClient, QueryClientProvider } from 'react-query'
  4. import { BrowserRouter } from 'react-router-dom'
  5. import { Maintenance } from '@/Maintenance'
  6. import { createApolloClient } from '@/api'
  7. import { useGetKillSwitch } from '@/api/hooks/admin'
  8. import { AdminModal } from '@/components/_overlays/AdminModal'
  9. import { OperatorsContextProvider } from '@/providers/assets/assets.provider'
  10. import { ConfirmationModalProvider } from '@/providers/confirmationModal'
  11. import { OverlayManagerProvider } from '@/providers/overlayManager'
  12. import { SegmentAnalyticsProvider } from '@/providers/segmentAnalytics/segment.provider'
  13. import { UserProvider } from '@/providers/user/user.provider'
  14. import { GlobalStyles } from '@/styles'
  15. import { FORCE_MAINTENANCE } from './config/env'
  16. const queryClient = new QueryClient({
  17. defaultOptions: {
  18. queries: {
  19. refetchOnWindowFocus: false,
  20. },
  21. },
  22. })
  23. export const CommonProviders: FC<PropsWithChildren> = ({ children }) => {
  24. // App doesn't accept props and doesn't contain state so should never rerender
  25. const apolloClient = createApolloClient()
  26. return (
  27. <>
  28. <GlobalStyles />
  29. <ApolloProvider client={apolloClient}>
  30. <QueryClientProvider client={queryClient}>
  31. <UserProvider>
  32. <OverlayManagerProvider>
  33. <SegmentAnalyticsProvider>
  34. <ConfirmationModalProvider>
  35. <BrowserRouter>
  36. <AdminModal />
  37. <MaintenanceWrapper>
  38. <OperatorsContextProvider>{children}</OperatorsContextProvider>
  39. </MaintenanceWrapper>
  40. </BrowserRouter>
  41. </ConfirmationModalProvider>
  42. </SegmentAnalyticsProvider>
  43. </OverlayManagerProvider>
  44. </UserProvider>
  45. </QueryClientProvider>
  46. </ApolloProvider>
  47. </>
  48. )
  49. }
  50. const MaintenanceWrapper: FC<PropsWithChildren> = ({ children }) => {
  51. const isMaintenanceForced = FORCE_MAINTENANCE === 'true'
  52. const { isKilled, wasKilledLastTime, error, loading } = useGetKillSwitch({
  53. context: { delay: 1000 },
  54. skip: isMaintenanceForced,
  55. })
  56. if (isKilled || (error && wasKilledLastTime) || (loading && wasKilledLastTime) || isMaintenanceForced) {
  57. return <Maintenance />
  58. } else {
  59. return <>{children}</>
  60. }
  61. }