diff --git a/src/App.js b/src/App.js index 53d545d..3d9d912 100644 --- a/src/App.js +++ b/src/App.js @@ -3,201 +3,105 @@ */ // Global npm libraries -import React, { useState, useEffect } from 'react' -import { useQueryParam, StringParam } from 'use-query-params' +import React, { useEffect, useCallback } from 'react' // Local libraries import './App.css' import LoadScripts from './components/load-scripts' -import WaitingModal from './components/waiting-modal' import AsyncLoad from './services/async-load' import SelectServerButton from './components/servers/select-server-button' import Footer from './components/footer' import NavMenu from './components/nav-menu' -import AppBody from './components/app-body' +import useAppState from './hooks/state' +import { UninitializedView, InitializedView } from './components/starter-views' function App (props) { - // BEGIN STATE - - // Get the CashStack URL from a query parameter, if it exists. - let [restURL] = useQueryParam('restURL', StringParam) - // Otherwise default to free-bch.fullstack.cash - if (!restURL) restURL = 'https://free-bch.fullstack.cash' - // console.log('restURL: ', restURL) - const [serverUrl, setServerUrl] = useState(restURL) - - const [menuState, setMenuState] = useState(0) - const [wallet, setWallet] = useState(false) - const [servers, setServers] = useState([]) - - // Startup state - // When the page is loaded, it goes through a series of async network calls - // to initialize the app. These variables track the state of this startup - // process. - const [asyncInitStarted, setAsyncInitStarted] = useState(false) - const [asyncInitFinished, setAsyncInitFinished] = useState(false) - const [asyncInitSucceeded, setAsyncInitSucceeded] = useState(null) - - // Startup Modal - const [showStartModal, setShowStartModal] = useState(true) - const [modalBody, setModalBody] = useState([]) - const [hideSpinner, setHideSpinner] = useState(false) - const [denyClose, setDenyClose] = useState(false) // Deny modal to be closed - // Load all the app state into a single object that can be passed to child // components. - const appData = { - wallet, - setWallet, - serverUrl, - setServerUrl, - servers, - setServers, - showStartModal, - setShowStartModal, - menuState, - setMenuState, - asyncInitFinished, - setAsyncInitFinished, - asyncInitSucceeded, - setAsyncInitSucceeded, - modalBody, - setModalBody, - hideSpinner, - setHideSpinner, - denyClose, - setDenyClose - } + const appData = useAppState() - // END STATE - - // Encasulate dependencies - const asyncLoad = new AsyncLoad() + // Add a new line to the waiting modal. + const addToModal = useCallback((inStr, appData) => { + // console.log('addToModal() inStr: ', inStr) + appData.setModalBody(prevBody => { + // console.log('prevBody: ', prevBody) + prevBody.push(inStr) + return prevBody + }) + }, []) + /** Load all required data before component start. */ useEffect(() => { async function asyncEffect () { - // console.log('asyncInitStarted: ', asyncInitStarted) - if (!asyncInitStarted) { + console.log('asyncInitStarted: ', appData.asyncInitStarted) + + if (!appData.asyncInitStarted) { try { - setAsyncInitStarted(true) + // Instantiate the async load object. + const asyncLoad = new AsyncLoad() + appData.setAsyncInitStarted(true) addToModal('Loading minimal-slp-wallet', appData) - - setDenyClose(true) + appData.setDenyClose(true) await asyncLoad.loadWalletLib() // console.log('Wallet: ', Wallet) addToModal('Getting alternative servers', appData) const gistServers = await asyncLoad.getServers() - setServers(gistServers) + appData.setServers(gistServers) // console.log('servers: ', servers) - // throw new Error('test error') - addToModal('Initializing wallet', appData) - console.log(`Initializing wallet with back end server ${serverUrl}`) + console.log(`Initializing wallet with back end server ${appData.serverUrl}`) - const walletTemp = await asyncLoad.initWallet(serverUrl) - setWallet(walletTemp) + const walletTemp = await asyncLoad.initWallet(appData.serverUrl) + appData.setWallet(walletTemp) // Update state - setShowStartModal(false) - setDenyClose(false) + appData.setShowStartModal(false) + appData.setDenyClose(false) // Update the startup state. - setAsyncInitFinished(true) - setAsyncInitSucceeded(true) + appData.setAsyncInitFinished(true) + appData.setAsyncInitSucceeded(true) console.log('App.js useEffect() startup finished successfully') } catch (err) { const errModalBody = [ `Error: ${err.message}`, 'Try selecting a different back end server using the drop-down menu at the bottom of the app.' ] - setModalBody(errModalBody) + appData.setModalBody(errModalBody) // Update Modal State - setHideSpinner(true) - setShowStartModal(true) - setDenyClose(false) + appData.setHideSpinner(true) + appData.setShowStartModal(true) + appData.setDenyClose(false) // Update the startup state. - setAsyncInitFinished(true) - setAsyncInitSucceeded(false) + appData.setAsyncInitFinished(true) + appData.setAsyncInitSucceeded(false) } } } asyncEffect() - }) + }, [appData, addToModal]) return ( <> - - + + {/** Define View to show */} { - showStartModal + appData.showStartModal ? () - : () + : () } - +