Skip to content

Commit

Permalink
fix loop bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CoNETProject committed Nov 20, 2023
1 parent 0066ddf commit 86db00e
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 103 deletions.
46 changes: 32 additions & 14 deletions src/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,37 +103,44 @@ type StartWorkerResolveForAPI = [WorkerCallStatus, any []]
const channelWrokerListenName = 'toMainWroker'


const postUrl: (url: string, data: string) => Promise<null|boolean> = (url, data) => {
export const postUrl: (url: string, data: string, post?: boolean) => Promise<any> = (url, data, post = true) => {
return new Promise( async (resolve, reject )=> {
const timeout = 1000
const controller = new AbortController()
const id = setTimeout(() => controller.abort(), timeout)
let status:null|boolean = null
await fetch (url, {
method: "POST",

const opt: RequestInit = {
method: post ? "POST" : 'GET',
headers: {
Accept: "text/event-stream",
"Content-Type": 'application/json;charset=UTF-8'
},
body: data,
cache: 'no-store',
referrerPolicy: 'no-referrer',
signal: controller.signal
})
}
if (post) {
opt.body = data
}
await fetch (url, opt)
.then ( async res => {

if (!res.ok) {

console.log (`postUrl return resolve (false) res.status = [${res.status}]`)
return res.status
}

status = true
const returnData = await res.json()
let returnData = res.text.length ? await res.text(): ''
if (res.headers.get('content-type')?.includes('application/json')) {
returnData = await res.json()

if (!returnData) {
return true
if (!returnData) {
return true
}
}

console.log (`postUrl status = [${res.status}] returnData = `, returnData)
console.log (`postUrl return JSON data`, returnData)
return returnData
Expand All @@ -142,20 +149,31 @@ const postUrl: (url: string, data: string) => Promise<null|boolean> = (url, data
.then(_data => {
return resolve (_data)
})
.catch (ex => {
.catch ((ex) => {
return resolve (status)
})

clearTimeout(id)
})
}

export const testLocalServer = () => {
return postUrl(`http://localhost:3001/loginRequest`, '')

export const postPasscode: (passcode: string) => Promise<null|boolean|WorkerCommand> = async (passcode) => {
return await postUrl(`http://localhost:3001/loginRequest`, JSON.stringify({data:passcode}))
}

export const postPasscode: (passcode: string) => Promise<null|boolean|WorkerCommand> = (passcode) => {
return postUrl(`http://localhost:3001/loginRequest`, JSON.stringify({data:passcode}))
export const testLocalServer = async () => {
const ver = '0.0.6'
const result = await postUrl(`http://localhost:3001/ver`, '', false)
if (result) {
if (result.ver === ver) {
return true
}
return false
}
console.log (`[${!result}]`)
console.log (result? 'result true ': 'result false')
return null
}

const postMessage = (cmd: WorkerCommand, resolve: (value: StartWorkerResolveForAPI | PromiseLike<StartWorkerResolveForAPI>) => void) => {
Expand Down
13 changes: 9 additions & 4 deletions src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ const App = () => {
showAppStore,
localDaemon,
setlocalDaemon,
showMiner
showMiner,
dAPPInitialize
} = useAppState()
const drawerDragControls = useDragControls()

Expand Down Expand Up @@ -104,10 +105,14 @@ const App = () => {

const testDeamon = async() => {
const test = await testLocalServer ()
if (test === null) {
if (test === true) {
setlocalDaemon(true)
dAPPInitialize().then(() => {

})
}
}

testDeamon().catch((ex) => {
console.log(`APP useEffect testDeamon error`, ex)
})
Expand All @@ -117,8 +122,8 @@ const App = () => {
const ShowApp = () => {
return (
<>
{localDaemon && <NoDeamon />}
{!localDaemon && <ConetAPP />}
{!localDaemon && <NoDeamon />}
{localDaemon && <ConetAPP />}
</>

)
Expand Down
64 changes: 6 additions & 58 deletions src/components/App/LaunchAPP.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {OnboardingPageProvider} from '../Providers/OnboardingPageProvider'
import OnboardingScreen from "./OnboardingScreen/OnboardingScreen"
import UnlockScreen from "./UnlockScreen/UnlockScreen"
import LaunchScreen from "./LaunchScreen/LaunchScreen"
import {US, CN,JP, TW } from 'country-flag-icons/react/3x2'

const StyledContainer = styled.div`
height: 100vh;
Expand All @@ -22,76 +21,25 @@ const StyledContainer = styled.div`

const App = () => {
const {
dAPPInitialize,

isInitializing,
isPlatformLoading,
setNetworkStrength,

setWindowInnerSize,
setClientDevices,
setIsTouchDevice,
showJoinUS,
setShowAppStore,
setIsModalOpen,
setIsShowOverlay,
showOverlay,
hasContainer,
isUnlocked
} = useAppState()

const windowResizeHandler = () => {
setWindowInnerSize(detectWindowInnerSize())
}

const [reload, setReload] = useState(false)
useEffect(() => {

dAPPInitialize().then(() => {
if (!isInitializing) {
setReload (true)
}
})

const randomDeviceIds = Array.from({length: 3}, (_, i) => (Date.now() + Math.round(Math.random() * 100)).toString())

setClientDevices({
[randomDeviceIds[0]]: {
id: randomDeviceIds[0],
type: 'mobile',
name: 'iPhone-S4GD0S'
},
[randomDeviceIds[1]]: {
id: randomDeviceIds[1],
type: 'desktop',
name: 'Mac Mini-C0S3M8VN'
},
[randomDeviceIds[2]]: {
id: randomDeviceIds[2],
type: 'tablet',
name: 'Samsung TAB-LX30SMA'
},
})

setIsTouchDevice(detectTouchDevice())

// Test network connection icon
const rndInt = Math.floor(Math.random() * 4) + 1;
// @ts-ignore
setNetworkStrength(rndInt)

window.addEventListener('resize', windowResizeHandler)
return () => {
window.removeEventListener('resize', windowResizeHandler)
}

}, [])

const getContent = () => {

switch (true) {
case isInitializing:
return (
<LaunchScreen reload = {reload}/>
)
// case isInitializing:
// return (
// <LaunchScreen reload = {reload}/>
// )
case hasContainer && isUnlocked:
return (
<MainScreen/>
Expand Down
16 changes: 4 additions & 12 deletions src/components/App/MainScreen/MainScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ const MainScreen = () => {

const {
windowInnerSize: {width},
setIsDrawerOpen,
isDrawerOpen,
isTouchDevice,
isModalOpen,
showGuide,
setClientProfiles,
showAppStore,
showBlockScan,
showJoinUS,
setIsModalOpen,
showMiner
} = useAppState()
Expand Down Expand Up @@ -75,9 +68,9 @@ const MainScreen = () => {
}
}

useEffect(() => {
setInitialProfiles()
}, [])
// useEffect(() => {
// setInitialProfiles()
// }, [])

const startDrag = (event: any) => {
drawerDragControls.start(event)
Expand Down Expand Up @@ -114,9 +107,8 @@ const MainScreen = () => {
<GlobalBar/> */}

<LayoutGroup id="a">
{ showGuide && <LaunchPage/>}

{ showAppStore && <CONETProxy/>}
{ showBlockScan && <BlockScan />}
{ showMiner && <Miner />}
{/* <StyledContents>
{!showGuide && !showAppStore && <Messenger/>}
Expand Down
57 changes: 54 additions & 3 deletions src/components/App/NoDaemon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import Container from '@mui/material/Container'
import Stack from '@mui/material/Stack'
import Button from '@mui/material/Button'
import React, {HTMLAttributes, useState, useEffect} from "react"
import CircularProgress from '@mui/material/CircularProgress'
import BottomNavigationAction from '@mui/material/BottomNavigationAction'
import LoadingButton from '@mui/lab/LoadingButton'
import {testLocalServer} from '../../../API/index'
import useAppState from '../../../store/appState/useAppState'

import BottomNavigation from '@mui/material/BottomNavigation'
import WindowSharpIcon from '@mui/icons-material/WindowSharp'
import AppleIcon from '@mui/icons-material/Apple'
import Paper from '@mui/material/Paper'
import Link from '@mui/material/Link'
import GitHubIcon from '@mui/icons-material/GitHub'
const themeTopArea1 = createTheme ({
typography: {
h3: {
Expand Down Expand Up @@ -39,6 +44,48 @@ const RootContainer = styled(Container)(() => ({
height: '100vh'
}))

const downloadConet = ((event: React.SyntheticEvent<Element, Event>, newValue: any) =>{

switch (newValue) {
default:
case 0: {
return window.open(`https://github.com/CoNET-project/seguro-platform/releases/download/0.0.1/CONET-0.61.0.exe`)
}
case 1: {
return window.open(`https://github.com/CoNET-project/seguro-platform/releases/download/0.0.1/CONET-0.61.0-Apple-M-Series-cpu.dmg`)
}
case 2: {
return window.open(`https://github.com/CoNET-project/seguro-platform/releases/download/0.0.1/CONET-0.61.0-Inter-CPU.dmg`)
}
case 3: {
return window.open(`https://github.com/CoNET-project/seguro-platform/releases/tag/0.0.1`)
}
}
})
const DownloadArea = () => {
const intl = useIntl()
return (
<Paper sx={{ borderRadius: '20px', textAlign: 'center', width: '30rem' }} elevation={10} >
<Link underline="hover" component="button" onClick={() => window.open(`https://github.com/CoNET-project/CONET-Proxy`)} >
<Typography variant="h6" sx={{color: '#448aff', textAlign: 'center'}}>
{ intl.formatMessage({id: 'platform.api.daemon.openSource'})}
</Typography>
</Link>

<BottomNavigation
showLabels
onChange = {downloadConet}
>
<BottomNavigationAction label="Windows" icon={<WindowSharpIcon />} />
<BottomNavigationAction label="Apple M" icon={<AppleIcon />} />
<BottomNavigationAction label="Apple" icon={<AppleIcon />} />
<BottomNavigationAction label="More" icon={<GitHubIcon/>} />
</BottomNavigation>

</Paper>
)

}

const deamon = () => {
const intl = useIntl()
Expand All @@ -58,6 +105,8 @@ const deamon = () => {
}
setlocalDaemon(true)
}


return (
<ThemeProvider theme={themeTopArea1}>
<RootContainer maxWidth="md">
Expand All @@ -79,10 +128,12 @@ const deamon = () => {
color={error ? "error": 'info'}
>

{ intl.formatMessage({id: 'platform.api.daemon.testButton'})}
{ intl.formatMessage({id: 'platform.api.daemon.testButton'})}
</LoadingButton>

</Box>

<DownloadArea />

</StackContainer>
</RootContainer>
Expand Down
7 changes: 2 additions & 5 deletions src/components/UI/Common/AlertDialog/AlertDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,10 @@ const StyledDialogButton = styled.button<StyledDialogButtonProps>`

const AlertDialog = ({icon, message, dialogActions}: AlertDialogProps) => {

const appState = useAppState()
const {setIsShowOverlay} = useAppState()

useEffect(() => {
appState.setIsShowOverlay(true)
return () => {
appState.setIsShowOverlay(false)
}

}, [])
return (
<StyledDialog>
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'

import { createRoot } from "react-dom/client"
import 'modern-normalize'
import App from './components/App/APP'
import App from './components/App/App'
import RootProvider from './components/Providers/RootProvider'
import GlobalStyle from './components/UI/Global/Styles'
import './index.css'
Expand Down
1 change: 1 addition & 0 deletions src/localization/messages/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ const en_US: Messages = {
'platform.api.daemon.title': 'No CONET Daemon Activity Detected',
'platform.api.daemon.detail': 'Please start CONET Daemon, or download and start it',
'platform.api.daemon.testButton': 'test daemon',
'platform.api.daemon.openSource': 'CONET is Open Source',

'platform.miner.header.title': 'Welcome to CONET DePIN',
'platform.miner.register.title': 'Node Register',
Expand Down
Loading

0 comments on commit 86db00e

Please sign in to comment.