import React , { useEffect } from 'react' ; import { Box , useStdin } from 'ink' ; import useStdoutDimensions from 'ink-use-stdout-dimensions' ; import { Response } from 'node-fetch' ; import { base_app_width , config_endpoint , error_tolarance , error_reconnect_delay_ms } from '../app-config.json' ; import { useStore , AppStatus } from '../../store-provider' ; import { Entry , doRequest , findMirror } from '../../search-api' ; import Loader from './Loader' ; import Header from './Header' ; import Stage from './Stage' ; import Downloader from './Downloader' ; import BulkDownloader from './BulkDownloader' ; const App = ( ) => { const [ cols ] = useStdoutDimensions ( ) ; const { setRawMode } = useStdin ( ) ; const status = useStore ( state => state . globals . status ) ; const initialStatus = useStore ( state => state . globals . initialStatus ) ; const downloadQueue = useStore ( state => state . globals . downloadQueue ) ; const setConfig = useStore ( state => state . set . config ) ; const setErrorCounter = useStore ( state => state . set . errorCounter ) ; const seetLastFailedAction = useStore ( state => state . set . lastFailedAction ) ; const setMirror = useStore ( state => state . set . mirror ) ; const setStatus = useStore ( state => state . set . status ) ; useEffect ( ( ) => { const setupTheApp = async ( ) => { const onErr = ( attempt , _ ) => { setErrorCounter ( attempt ) ; } ; setStatus ( 'fetchingConfig' ) ; const configResponse = await doRequest ( config_endpoint , onErr , error_tolarance , error_reconnect_delay_ms ) ; if ( configResponse == null ) { seetLastFailedAction ( setupTheApp ) ; setStatus ( 'failed' ) ; return ; } const configJSON = await configResponse . json ( ) ; setConfig ( configJSON ) ; setStatus ( 'findingMirror' ) ; const mirrorList = configJSON . mirrors || [ ] ; const mirror = await findMirror ( mirrorList ) ; if ( mirror == null ) { seetLastFailedAction ( setupTheApp ) ; setStatus ( 'failed' ) ; return ; } setMirror ( mirror ) ; setStatus ( initialStatus ) ; } ; setupTheApp ( ) ; setRawMode ( true ) ; ( ) => setRawMode ( false ) ; } , [ ] ) ; return ( < Box marginLeft = { 1 } flexDirection = 'column' > < Box width = { cols - 5 > base_app_width ? base_app_width : '95%' } flexDirection = 'column' > < Header width = { cols - 5 > 40 ? 40 : '95%' } /> < Loader / > < Stage /> < Downloader / > < /Box> { status == 'bulkDownloadingID' && < BulkDownloader mode = 'id' / > } { status == 'bulkDownloadingMD5' && < BulkDownloader mode = 'md5' / > } < /Box>); } ; export default App ;