import React , { FC , useReducer , Dispatch , Reducer } from 'react' ; import { awsLambdaApiRef } from '../api' ; import { State , Action } from './types' ; export const AppContext = React . createContext < [ State , Dispatch < Action > ] > ( [ ] as any ) ; export const STORAGE_KEY = `${ awsLambdaApiRef . id }.settings` ; const initialState = { region : '' , identityPoolId : '' , showSettings : false , authMethod : 'aws' , awsAccessKeyId : '' , awsAccessKeySecret : '' , } ; const reducer = ( state , action ) => { switch ( action . type ) { case 'setCredentials' : return { ... state , ... action . payload , } ; case 'showSettings' : return { ... state , showSettings : true } ; case 'hideSettings' : return { ... state , showSettings : false } ; default : return state ; } } ; export const AppStateProvider = ( { children } ) => { const [ state , dispatch ] = useReducer ( reducer , initialState ) ; return ( < AppContext . Provider value = { [ state , dispatch ] } > < > { children } < /> < /AppContext.Provider>); } ;