import { GlobalStateT , CHAINS , PROTOCOL_STEPS_ID , PROTOCOL_INNER_STATES_ID , NETWORKS , ProtocolStepsT , ProtocolStepT , LocalStorageStateT , } from 'types' ; export const prepareGlobalStateForStorage = ( globalState ) : LocalStorageStateT => { const chains = Object . keys ( globalState . protocols ) as CHAINS [ ] ; return chains . reduce ( ( acc , el ) => { const stepsId = Object . keys ( globalState . protocols [ el ] . steps ) as PROTOCOL_STEPS_ID [ ] ; const newSteps = stepsId . reduce ( ( acc0 , stepId ) => { acc0 [ stepId ] = { isCompleted : globalState . protocols [ el ] . steps [ stepId ] . isCompleted , } ; return acc0 ; } , { } ) ; acc [ el ] = { currentStepId : globalState . protocols [ el ] . currentStepId , steps : newSteps , innerState : globalState . protocols [ el ] . innerState , } ; return acc ; } , { } as LocalStorageStateT ) ; } ; export const prepareGlobalState = ( localStorage , initialGlobalState , chainId ) : GlobalStateT => { const initialLocalStorageState = prepareGlobalStateForStorage ( initialGlobalState ) ; const chains = Object . keys ( initialLocalStorageState ) as CHAINS [ ] ; const newProtocols = chains . reduce ( ( protocols , chain ) => { const stepsId = Object . keys ( protocols [ chain ] . steps ) as PROTOCOL_STEPS_ID [ ] ; const newSteps = stepsId . reduce ( ( steps , stepId ) => { let newStep ; try { newStep = localStorage [ chain ] ?. steps [ stepId ] ; } catch ( error ) { newStep = initialLocalStorageState [ chain ] ?. steps [ stepId ] ; } steps [ stepId ] = { ... steps [ stepId ] , ... newStep , } ; return steps ; } , protocols [ chain ] . steps ) ; protocols [ chain ] = { ... initialGlobalState . protocols [ chain ] , ... ( localStorage ? localStorage [ chain ] : initialLocalStorageState [ chain ] ) , steps : newSteps , } ; return protocols ; } , initialGlobalState . protocols ) ; return { currentChainId : chainId , protocols : newProtocols , } ; } ; export const getChainId = ( state ) : CHAINS => { return state . currentChainId as CHAINS ; } ; export const getChainLabel = ( state ) : string => { const chainId = getChainId ( state ) ; return state . protocols [ chainId ] . label ; } ; export const getNetwork = ( state ) : NETWORKS => { const chainId = getChainId ( state ) ; return state . protocols [ chainId ] . network ; } ; export const getSteps = ( state ) : ProtocolStepsT => { const chainId = getChainId ( state ) ; return state . protocols [ chainId ] . steps ; } ; export const getStepId = ( state ) : PROTOCOL_STEPS_ID => { const chainId = getChainId ( state ) ; return state . protocols [ chainId ] . currentStepId ; } ; export const getFirstStepId = ( state ) : PROTOCOL_STEPS_ID => { const chainId = getChainId ( state ) ; return state . protocols [ chainId ] . firstStepId ; } ; export const getLastStepId = ( state ) : PROTOCOL_STEPS_ID => { const chainId = getChainId ( state ) ; return state . protocols [ chainId ] . lastStepId ; } ; export const getPreviousStepId = ( state ) : PROTOCOL_STEPS_ID => { const chainId = getChainId ( state ) ; const currentStepId = getStepId ( state ) ; const previousStepId = state . protocols [ chainId ] . steps [ currentStepId ] . previousStepId ; return previousStepId ? previousStepId : getFirstStepId ( state ) ; } ; export const getNextStepId = ( state ) : PROTOCOL_STEPS_ID => { const chainId = getChainId ( state ) ; const currentStepId = getStepId ( state ) ; const nextStepId = state . protocols [ chainId ] . steps [ currentStepId ] . nextStepId ; return nextStepId ? nextStepId : getLastStepId ( state ) ; } ; export const getPreviousStep = ( state ) : ProtocolStepT | null => { const chainId = getChainId ( state ) ; const previousStep = getPreviousStepId ( state ) ; return previousStep === null ? null : state . protocols [ chainId ] . steps [ previousStep ] ; } ; export const getNextStep = ( state ) : ProtocolStepT | null => { const chainId = getChainId ( state ) ; const nextStepId = getNextStepId ( state ) ; return nextStepId === null ? null : state . protocols [ chainId ] . steps [ nextStepId ] ; } ; export const getStepIndex = ( state ) : number => { const chainId = getChainId ( state ) ; const currentStepId = getStepId ( state ) ; return state . protocols [ chainId ] . steps [ currentStepId ] . position ; } ; export const getStepTitle = ( state ) : string => { const chainId = getChainId ( state ) ; const currentStepId = getStepId ( state ) ; return state . protocols [ chainId ] . steps [ currentStepId ] . title ; } ; export const isConnectionStep = ( state ) : boolean => { return getStepId ( state ) === PROTOCOL_STEPS_ID . CHAIN_CONNECTION ; } ; export const isOneColumnStep = ( state ) : boolean => { const chainId = getChainId ( state ) ; const currentStepId = getStepId ( state ) ; return state . protocols [ chainId ] . steps [ currentStepId ] . isOneColumn ; } ; export const isFirstStep = ( state ) => { return getStepId ( state ) === getFirstStepId ( state ) ; } ; export const isLastStep = ( state ) : boolean => { return getStepId ( state ) === getLastStepId ( state ) ; } ; export const isCompletedStep = ( state ) : boolean => { const chainId = getChainId ( state ) ; const currentStepId = getStepId ( state ) ; return ( isOneColumnStep ( state ) || isSkippableStep ( state ) || state . protocols [ chainId ] . steps [ currentStepId ] . isCompleted ) ; } ; export const isSkippableStep = ( state ) : boolean => { const chainId = getChainId ( state ) ; const currentStepId = getStepId ( state ) ; return state . protocols [ chainId ] . steps [ currentStepId ] . isSkippable ; } ; export const getChainInnerState = ( state , stateId ) : string | null => { const chainId = getChainId ( state ) ; return state . protocols [ chainId ] . innerState ?. [ stateId ] as string | null ; } ; export const getInnerState = ( state ) => { const innerState = { } ; const network = getNetwork ( state ) ; const secret = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . SECRET ) ; if ( secret ) { innerState . secret = secret ; } const privateKey = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . PRIVATE_KEY ) ; if ( privateKey ) { innerState . privateKey = privateKey ; } const publicKey = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . PUBLIC_KEY ) ; if ( publicKey ) { innerState . publicKey = publicKey ; } const address = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . ADDRESS ) ; if ( address ) { innerState . address = address ; } const contractId = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . CONTRACT_ID ) ; if ( contractId ) { innerState . contractId = contractId ; } const mnemonic = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . MNEMONIC ) ; if ( mnemonic ) { innerState . mnemonic = mnemonic ; } const accountId = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . ACCOUNT_ID ) ; if ( accountId ) { innerState . accountId = accountId ; } const password = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . PASSWORD ) ; if ( password ) { innerState . password = password ; } const email = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . EMAIL ) ; if ( email ) { innerState . email = email ; } const programId = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . PROGRAM_ID ) ; if ( programId ) { innerState . programId = programId ; } const greeter = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . GREETER ) ; if ( greeter ) { innerState . greeter = greeter ; } const metamaskNetworkName = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . METAMASK_NETWORK_NAME ) ; if ( metamaskNetworkName ) { innerState . metamaskNetworkName = metamaskNetworkName ; } const did = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . DID ) ; if ( did ) { innerState . did = did ; } const userName = getChainInnerState ( state , PROTOCOL_INNER_STATES_ID . USER_NAME ) ; if ( userName ) { innerState . userName = userName ; } return { network , ... innerState } ; } ;