import * as React from "react" ; import { CivilContext , buildCivilContext } from "./CivilContext" ; import { KirbyEthereum , KirbyEthereumProvider , KirbyEthereumContext } from "@kirby-web3/ethereum-react" ; import { ApolloConsumer } from "react-apollo" ; import ApolloClient from "apollo-client" ; import { IdentityParentPlugin } from "./IdentityParentPlugin" ; export interface CivilInnerProviderProps { config : any ; featureFlags : string [ ] ; apolloClient ? : ApolloClient < any > ; } const CivilInnerProvider = ( { children , featureFlags , config , apolloClient , } ) => { const kirbyCtx = React . useContext < KirbyEthereum > ( KirbyEthereumContext ) ; const web3 = kirbyCtx . web3 ; if ( kirbyCtx === null ) { return < div > < /div>; } const [ currentUser , setCurrentUser ] = React . useState ( null ) ; const baseContext = React . useMemo ( ( ) => { function onAuthChange ( nextUser ) { console . log ( "auth change" , nextUser ) ; setCurrentUser ( nextUser ) ; } const ctx = buildCivilContext ( { web3 , apolloClient : apolloClient ! , featureFlags , config , onAuthChange } ) ; return ctx ; } , [ ] ) ; const civilContext = React . useMemo ( ( ) => { return { ... baseContext , currentUser } ; } , [ baseContext , currentUser ] ) ; return < CivilContext . Provider value = { civilContext } > { children } < /CivilContext.Provider>; } ; export interface CivilProviderProps extends CivilInnerProviderProps { plugins ? : any [ ] ; pluginConfig : any ; } export const CivilProvider = ( { children , featureFlags , pluginConfig , config , } ) => { const plugins = React . useMemo ( ( ) => { return [ new IdentityParentPlugin ( ) ] ; } , [ ] ) ; return ( < ApolloConsumer > { client => ( < KirbyEthereumProvider config = { pluginConfig } plugins = { plugins } > < CivilInnerProvider featureFlags = { featureFlags } config = { config } apolloClient = { client } > { children } < /CivilInnerProvider> < /KirbyEthereumProvider>)} < /ApolloConsumer>); } ;