From 46954430cdee7a7aad665b97eb1cfb7083a5062a Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Wed, 20 Nov 2024 14:25:27 -0500 Subject: [PATCH] Corrected Pools to not depend on chain sync+Added Action List --- apps/box/src/components/Cards/TasksCard.tsx | 141 ++++++++++++++++++ apps/box/src/components/WalletDetails.tsx | 2 +- apps/box/src/screens/Blox/Blox.screen.tsx | 57 ++++++- .../box/src/screens/Settings/Pools.screen.tsx | 7 +- apps/box/src/screens/Users/UserHeader.tsx | 2 +- apps/box/src/stores/index.ts | 2 + 6 files changed, 200 insertions(+), 11 deletions(-) create mode 100644 apps/box/src/components/Cards/TasksCard.tsx diff --git a/apps/box/src/components/Cards/TasksCard.tsx b/apps/box/src/components/Cards/TasksCard.tsx new file mode 100644 index 00000000..fc5b495a --- /dev/null +++ b/apps/box/src/components/Cards/TasksCard.tsx @@ -0,0 +1,141 @@ +import React, { useEffect, useState } from 'react'; +import { + FxBox, + FxCard, + FxText, + FxRadioButton, + FxRadioButtonWithLabel, + FxSpacer, + useFxTheme, + FxRefreshIcon, +} from '@functionland/component-library'; +import { chainApi } from '@functionland/react-native-fula'; +import { useNavigation } from '@react-navigation/native'; +import { Pressable, ActivityIndicator } from 'react-native'; + +type ValueType = string | number; + +type TaskListProps = { + pools: Array<{ joined: boolean; requested: boolean }>; + getPools: () => Promise; + currentBloxPeerId: string; + accountId: string; + routes: any; +}; + +export type Task = { + id: ValueType; + title: string; + route: any; +}; + +export const TasksCard = ({ + pools, + getPools, + currentBloxPeerId, + accountId, + routes, +}: TaskListProps) => { + const navigation = useNavigation(); + const tasks: Task[] = [ + { id: 1, title: 'Join Testnet', route: routes.UsersTab }, + { id: 2, title: 'Join Closest Pool', route: routes.Pools }, + ]; + + const [completedTasks, setCompletedTasks] = useState([]); + const { colors } = useFxTheme(); + const [refreshCard, setRefreshCard] = useState(false); + const [loading, setLoading] = useState(false); + const handleTaskPress = (route: string) => { + navigation.navigate(route, { + screen: route, + }); + }; + + const checkTasks = async () => { + if (!loading) { + setLoading(true); + await checkTestnetTask(); + await checkPoolTask(); + setLoading(false); + } + }; + + const checkTestnetTask = async () => { + try { + const api = await chainApi.init(); + const gasBalanceStr = await chainApi.checkAccountBalance(api, accountId); + + if (gasBalanceStr && !gasBalanceStr.includes('Error')) { + console.log(gasBalanceStr); + const gasBalance = parseInt(gasBalanceStr, 10); + console.log(gasBalance); + if (gasBalance > 0) { + setCompletedTasks((prev) => (prev.includes(1) ? prev : [...prev, 1])); + } else { + setCompletedTasks((prev) => prev.filter((id) => id !== 1)); + } + } + } catch (error) { + console.error('Error checking testnet balance:', error); + } + }; + + const checkPoolTask = async () => { + try { + const isPoolJoined = + pools.some((pool) => pool.joined || pool.requested) && + currentBloxPeerId; + + if (isPoolJoined) { + setCompletedTasks((prev) => (prev.includes(2) ? prev : [...prev, 2])); + } else { + setCompletedTasks((prev) => prev.filter((id) => id !== 2)); + } + } catch (error) { + console.error('Error checking pool status:', error); + } + }; + + useEffect(() => { + getPools(); + }, [currentBloxPeerId]); + + useEffect(() => { + if (currentBloxPeerId && accountId) { + checkTasks(); + } + }, [pools, currentBloxPeerId, accountId, refreshCard]); + + return ( + + + Action List + {loading ? ( + + ) : ( + setRefreshCard(!refreshCard)} + /> + )} + + + {}}> + {tasks.map((task, index) => ( + + handleTaskPress(task.route)}> + + + {index < tasks.length - 1 && } + + ))} + + + + ); +}; diff --git a/apps/box/src/components/WalletDetails.tsx b/apps/box/src/components/WalletDetails.tsx index e0d90d02..9cd54fd5 100644 --- a/apps/box/src/components/WalletDetails.tsx +++ b/apps/box/src/components/WalletDetails.tsx @@ -289,7 +289,7 @@ export const WalletDetails = ({ paddingHorizontal="32" size="large" > - {`PeerId:${appPeerId}`} + {`App PeerId:${appPeerId}`} )} diff --git a/apps/box/src/screens/Blox/Blox.screen.tsx b/apps/box/src/screens/Blox/Blox.screen.tsx index de1d1ca6..afa75ac3 100644 --- a/apps/box/src/screens/Blox/Blox.screen.tsx +++ b/apps/box/src/screens/Blox/Blox.screen.tsx @@ -17,7 +17,7 @@ import { DeviceCard, UsageBar, } from '../../components'; -import { UsersCard } from '../../components/Cards/UsersCard'; +import { TasksCard } from '../../components/Cards/TasksCard'; import { EarningCard } from '../../components/Cards/EarningCard'; import { BloxHeader } from './components/BloxHeader'; import { BloxInteraction } from './components/BloxInteraction'; @@ -25,7 +25,6 @@ import { BloxInteractionModal } from './modals/BloxInteractionModal'; import { EDeviceStatus } from '../../api/hub'; import { EBloxInteractionType, TBloxInteraction } from '../../models'; import { ProfileBottomSheet } from '../../components/ProfileBottomSheet'; -import { useUserProfileStore } from '../../stores/useUserProfileStore'; import { ConnectionOptionsSheet, ConnectionOptionsType, @@ -33,9 +32,14 @@ import { import { useLogger } from '../../hooks'; import { Routes } from '../../navigation/navigationConfig'; import { useNavigation } from '@react-navigation/native'; -import { useBloxsStore } from '../../stores'; +import { + useBloxsStore, + usePoolsStore, + useUserProfileStore, +} from '../../stores'; import { blockchain, fxblox } from '@functionland/react-native-fula'; import { Helper } from '../../utils'; + const DEFAULT_DIVISION = 30; export const BloxScreen = () => { @@ -52,6 +56,7 @@ export const BloxScreen = () => { const [resettingChain, setResettingChain] = useState(false); const [loadingBloxSpace, setLoadingBloxSpace] = useState(false); const [loadingFulaEarnings, setLoadingFulaEarnings] = useState(false); + const [bloxAccountId, setBloxAccountId] = useState(''); const [selectedMode, setSelectedMode] = useState( EBloxInteractionType.OfficeBloxUnit @@ -87,9 +92,38 @@ export const BloxScreen = () => { state.removeBlox, state.update, ]); - useEffect(() => { - console.log('here'); - }, [fulaIsReady]); + + const [pools, getPools] = usePoolsStore((state) => [ + state.pools, + state.getPools, + ]); + + const updateBloxAccount = async () => { + try { + if (fulaIsReady) { + const connectionStatus = await checkBloxConnection(); + if (connectionStatus) { + const bloxAccount = await blockchain.getAccount(); + setBloxAccountId(bloxAccount.account); + } else { + setBloxAccountId('Not Connected to blox'); + } + } else { + setBloxAccountId('Fula is not ready'); + } + } catch (e) { + console.error('Error updating blox account:', e); + const err = e.message || e.toString(); + if (err.includes('failed to dial')) { + setBloxAccountId('Connection to Blox not established'); + } else if (err.includes('blockchain call error')) { + setBloxAccountId('Error with blockchain.'); + } else { + setBloxAccountId(e.message || e.toString()); + } + } + }; + const bloxInteractions = Object.values(bloxs || {}).map( (blox) => ({ peerId: blox.peerId, @@ -116,6 +150,7 @@ export const BloxScreen = () => { updateBloxSpace(); updateFulaEarnings(); checkBloxConnection(); + updateBloxAccount(); } else if (fulaIsReady && !bloxsConnectionStatus[currentBloxPeerId]) { checkBloxConnection(); } @@ -463,6 +498,16 @@ export const BloxScreen = () => { totalCapacity={currentBloxSpaceInfo?.size || 1000} /> )} + {bloxAccountId && bloxAccountId.startsWith('5') && ( + + )} + { const wrappedJoinPool = async (poolID: number) => { try { - if (syncProgress==0 || syncProgress > 90){ + if (syncProgress==0 || syncProgress > 10){ setRefreshing(true); await joinPool(poolID); } else { @@ -124,7 +124,8 @@ export const PoolsScreen = () => { await getPools(); console.log('enableInteraction: ', enableInteraction); setAllowJoin( - pools.filter((pool) => pool.joined || pool.requested).length === 0 && isChainSynced && + pools.filter((pool) => pool.joined || pool.requested).length === 0 && + syncProgress > 0 && enableInteraction ); } catch (e) { @@ -178,7 +179,7 @@ export const PoolsScreen = () => { ListHeaderComponent={ - { syncProgress > 0 && syncProgress < 99 && + { syncProgress > 0 && syncProgress < 10 && { showDID={true} showNetwork={false} showPeerId={true} - showBloxPeerIds={false} + showBloxPeerIds={true} /> {/* diff --git a/apps/box/src/stores/index.ts b/apps/box/src/stores/index.ts index fd06a00c..e2172337 100644 --- a/apps/box/src/stores/index.ts +++ b/apps/box/src/stores/index.ts @@ -1,2 +1,4 @@ export * from './useSettingsStore'; export * from './useBloxsStore'; +export * from './usePoolsStore'; +export * from './useUserProfileStore';