Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/refactor ux #213

Merged
merged 15 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
🩹Refactor ConfirmDeployModal and InteractWithVault components to enha…
…nce state management and integrate vault data handling
  • Loading branch information
MattPoblete committed Nov 19, 2024
commit 2e94614a65a535c45532ec0c38b3ae6f72813b24
28 changes: 23 additions & 5 deletions apps/dapp/src/components/DeployVault/ConfirmDelpoyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { randomBytes } from "crypto";

import { useAppDispatch, useAppSelector } from "@/store/lib/storeHooks"
import { pushVault } from '@/store/lib/features/walletStore'
import { Asset, NewVaultState } from "@/store/lib/types";
import { Asset, NewVaultState, VaultData } from "@/store/lib/types";

import { useFactoryCallback, FactoryMethod } from '@/hooks/useFactory'
import { ModalContext, TransactionStatusModalStatus } from "@/contexts";
Expand All @@ -19,6 +19,7 @@ import { AccordionItems, FormControlInterface, VaultPreview } from "./VaultPrevi
import { DialogBody, DialogCloseTrigger, DialogFooter, DialogHeader, DialogTitle } from "../ui/dialog";
import { Button } from "@chakra-ui/react"
import { resetNewVault } from "@/store/lib/features/vaultStore";
import { useVault } from "@/hooks/useVault";

interface Status {
isSuccess: boolean,
Expand Down Expand Up @@ -47,8 +48,9 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
const managerString = useAppSelector(state => state.newVault.manager)
const emergencyManagerString = useAppSelector(state => state.newVault.emergencyManager)
const feeReceiverString = useAppSelector(state => state.newVault.feeReceiver)
const { transactionStatusModal: txModal } = useContext(ModalContext);
const { transactionStatusModal: txModal, deployVaultModal: deployModal } = useContext(ModalContext);
const dispatch = useAppDispatch();
const { getIdleFunds, getInvestedFunds, getTVL, getUserBalance } = useVault()

const [deployDisabled, setDeployDisabled] = useState(true);

Expand Down Expand Up @@ -118,6 +120,7 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
setAccordionValue([AccordionItems.FEES_CONFIGS])
return
}
deployModal.setIsOpen(false)
txModal.initModal();

const vaultName = nativeToScVal(indexName, { type: "string" })
Expand Down Expand Up @@ -190,7 +193,8 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
*/
let result: any;

if (amountsScVal.length === 0) {

if (newVault.amounts.length === 0) {
const createDefindexParams: xdr.ScVal[] = [
emergencyManager.toScVal(),
feeReceiver.toScVal(),
Expand Down Expand Up @@ -245,10 +249,24 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
}
const parsedResult: string = scValToNative(result.returnValue);
if (parsedResult.length !== 56) throw new Error('Invalid result')
const tempVault: any = {
const idleFunds = newVault.assets.map((asset, index) => {
return {
address: asset.address,
amount: newVault.amounts[index] || 0
}
})
const tempVault: VaultData = {
...newVault,
address: parsedResult
address: parsedResult,
emergencyManager: emergencyManagerString,
feeReceiver: feeReceiverString,
manager: managerString,
TVL: 0,
totalSupply: 0,
idleFunds: idleFunds,
investedFunds: [{ address: '', amount: 0 }],
}
console.log(tempVault)
await txModal.handleSuccess(result.txHash);
dispatch(pushVault(tempVault));
dispatch(resetNewVault());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ export const InteractWithVault = () => {
selectedVault?.address!,
params,
true,
).then((res) =>
statusModal.handleSuccess(res.txHash)
).then((res) => {
interactModal.setIsOpen(false),
inspectModal.setIsOpen(false),
statusModal.handleSuccess(res.txHash)
}
).finally(async () => {
const newTVL = await vault.getTVL(selectedVault?.address!)
const parsedNewTVL = Number(newTVL) / 10 ** 7
Expand All @@ -76,6 +79,7 @@ export const InteractWithVault = () => {
}
catch (error: any) {
console.error('Error:', error)
interactModal.setIsOpen(false)
statusModal.handleError(error.toString())
}
}
Expand Down
5 changes: 2 additions & 3 deletions apps/dapp/src/components/ManageVaults/InspectVault.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useSorobanReact } from "@soroban-react/core"

import { shortenAddress } from "@/helpers/address"
import { VaultMethod } from "@/hooks/useVault"
import { useVault, VaultMethod } from "@/hooks/useVault"

import { useAppSelector } from "@/store/lib/storeHooks"
import { Asset, AssetAmmount, VaultData } from "@/store/lib/types"
Expand All @@ -12,7 +12,6 @@ import { DialogBody, DialogContent, DialogFooter, DialogHeader } from "../ui/dia
import { FaRegEdit } from "react-icons/fa"
import { IoClose } from "react-icons/io5"


export const InspectVault = ({
handleOpenDeployVault,
handleOpenInteract,
Expand All @@ -24,8 +23,8 @@ export const InspectVault = ({
}) => {
const selectedVault: VaultData | undefined = useAppSelector(state => state.wallet.vaults.selectedVault)
const { address } = useSorobanReact()
if (!selectedVault) return null

if (!selectedVault) return null
return (
<DialogContent>
<DialogHeader>
Expand Down
13 changes: 5 additions & 8 deletions apps/dapp/src/providers/modal-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export const ModalProvider = ({
const [txHash, setTxHash] = React.useState<string>('')

const handleResetModal = () => {
console.log('resetting modal')
setIsDeployVaultModalOpen(false)
setIsInspectVaultModalOpen(false)
setIsInteractWithVaultModalOpen(false)
setIsTransactionStatusModalOpen(false)
setTransactionStatusModalStep(0)
setTransactionStatusModalStatus(TransactionStatusModalStatus.PENDING)
Expand All @@ -38,30 +34,31 @@ export const ModalProvider = ({
setTxHash('')
}

const handleFirstStep = setTimeout(() => setTransactionStatusModalStep(1), 3000)
useEffect(() => {
const handleFirstStep = setTimeout(() => setTransactionStatusModalStep(1), 3000)
if (isTransactionStatusModalOpen && transactionStatusModalStep === 0 && transactionStatusModalStatus === TransactionStatusModalStatus.PENDING) {
console.log('modal is open')
handleFirstStep
} else {
} else if (transactionStatusModalStatus !== TransactionStatusModalStatus.PENDING) {
clearTimeout(handleFirstStep)
setTransactionStatusModalStep(2)
}
}, [isTransactionStatusModalOpen, transactionStatusModalStep, transactionStatusModalStatus])

const handleInitModal = () => {
console.log('init modal')
handleResetModal()
setIsTransactionStatusModalOpen(true)
}

const handleError = (error: string) => {
clearTimeout(handleFirstStep)
setTransactionStatusModalError(error)
setTransactionStatusModalStatus(TransactionStatusModalStatus.ERROR)
setTransactionStatusModalStep(2)
setTimeout(() => handleResetModal(), 8000)
}

const handleSuccess = (txHash: string) => {
clearTimeout(handleFirstStep)
setTxHash(txHash)
setTransactionStatusModalStatus(TransactionStatusModalStatus.SUCCESS)
setTransactionStatusModalStep(2)
Expand Down
25 changes: 24 additions & 1 deletion apps/dapp/src/store/lib/features/walletStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ChainMetadata } from '@soroban-react/types'
import vaults from '@/constants/constants.json'
import { Networks } from '@stellar/stellar-sdk'
import { SelectedVault, VaultData, WalletState } from '../types'
import { VaultMethod } from '@/hooks/useVault'

const getDefaultVaults = async (network: string) => {
const filteredVaults = vaults.filter(vault => {
Expand Down Expand Up @@ -105,6 +106,27 @@ export const walletSlice = createSlice({
}
})
},
updateVaultData: (state, action: PayloadAction<Partial<VaultData>>) => {
state.vaults.createdVaults.forEach(vault => {
if (vault.address === action.payload.address) {
Object.assign(vault, action.payload)
}
})
state.vaults.selectedVault = {
address: action.payload.address ?? state.vaults.selectedVault?.address ?? '',
name: action.payload.name ?? state.vaults.selectedVault?.name ?? '',
manager: action.payload.manager ?? state.vaults.selectedVault?.manager ?? '',
emergencyManager: action.payload.emergencyManager ?? state.vaults.selectedVault?.emergencyManager ?? '',
feeReceiver: action.payload.feeReceiver ?? state.vaults.selectedVault?.feeReceiver ?? '',
TVL: action.payload.TVL ?? state.vaults.selectedVault?.TVL ?? 0,
userBalance: action.payload.userBalance ?? state.vaults.selectedVault?.userBalance ?? 0,
assets: action.payload.assets ?? state.vaults.selectedVault?.assets ?? [],
idleFunds: action.payload.idleFunds ?? state.vaults.selectedVault?.idleFunds ?? [],
investedFunds: action.payload.investedFunds ?? state.vaults.selectedVault?.investedFunds ?? [],
totalSupply: action.payload.totalSupply ?? state.vaults.selectedVault?.totalSupply ?? 0,
method: state.vaults.selectedVault?.method || VaultMethod.DEPOSIT,
}
},
},
extraReducers(builder) {
builder.addCase(fetchDefaultAddresses.pending, (state) => {
Expand All @@ -131,7 +153,8 @@ export const {
setVaults,
setVaultTVL,
resetSelectedVault,
setVaultUserBalance
setVaultUserBalance,
updateVaultData
} = walletSlice.actions

// Other code such as selectors can use the imported `RootState` type
Expand Down