import App from 'next/app'
import { ApolloProvider } from '@apollo/client'

import Page from '../src/app/components/Page'
import client from '../src/lib/apollo'
import { StoreProvider } from '../src/lib/store'
import { UserProvider } from '../src/user/hooks'
import { useCurrentUserQuery } from '../src/gql'

class MyApp extends App {
  static async getInitialProps({ Component, ctx }: any) {
    let pageProps: any = {}

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx)
    }

    // Add the query object to the pageProps
    // https://github.com/wesbos/Advanced-React/blob/master/finished-application/frontend/pages/_app.js
    pageProps.query = ctx.query
    return { pageProps }
  }

  render() {
    const { Component, pageProps } = this.props

    return (
      <ApolloProvider client={client}>
        <UserProvider>
          <Page>
            <Component {...pageProps} />
          </Page>
        </UserProvider>
      </ApolloProvider>
    )
  }
}

export default MyApp
