diff --git a/src/components/AssetDialog.tsx b/src/components/AssetDialog.tsx index b8bda39..8205f13 100644 --- a/src/components/AssetDialog.tsx +++ b/src/components/AssetDialog.tsx @@ -4,14 +4,14 @@ import ListItem from 'components/ListItem'; import SkeletonListItem from 'components/SkeletonListItem'; import { displayFunctionsAtom } from 'store/app'; import type { Brand } from '@agoric/ertp/src/types'; +import { anchorBrandsAtom } from 'store/swap'; const AssetDialog = ({ - brands, handleBrandSelected, }: { - brands: Array; handleBrandSelected: (brand: Brand) => void; }) => { + const brands = useAtomValue(anchorBrandsAtom); const { displayBrandPetname } = useAtomValue(displayFunctionsAtom); const brandSections = brands && brands.length diff --git a/src/components/DialogSwap.tsx b/src/components/DialogSwap.tsx index dd39c16..35bb299 100644 --- a/src/components/DialogSwap.tsx +++ b/src/components/DialogSwap.tsx @@ -4,33 +4,40 @@ import { AnimatePresence, motion } from 'framer-motion'; import { FiX, FiExternalLink } from 'react-icons/fi'; import AssetDialog from 'components/AssetDialog'; import type { Brand } from '@agoric/ertp/src/types'; +import { useAtom, useAtomValue } from 'jotai'; +import { + assetSelectionDialogOpenAtom, + selectedAnchorPetnameAtom, +} from 'store/swap'; +import { displayFunctionsAtom } from 'store/app'; + +const DialogSwap = () => { + const { displayBrandPetname } = useAtomValue(displayFunctionsAtom); + const [selectedAnchorBrandPetname, setSelectedAnchorBrandPetname] = useAtom( + selectedAnchorPetnameAtom + ); + const [assetSelectionDialogOpen, setAssetSelectionDialogOpen] = useAtom( + assetSelectionDialogOpenAtom + ); + + const handleBrandSelected = (brand: Brand | null) => { + setSelectedAnchorBrandPetname(displayBrandPetname(brand)); + setAssetSelectionDialogOpen(false); + }; + const close = () => setAssetSelectionDialogOpen(false); -const DialogSwap = ({ - open, - handleClose, - brands, - selectedBrand, - handleBrandSelected, -}: { - open: boolean; - handleClose: () => void; - brands: Array; - handleBrandSelected: (brand: Brand | null) => void; - selectedBrand?: Brand | null; - purseOnly?: boolean; -}) => { return ( - {open && ( + {assetSelectionDialogOpen && ( -
+
- + )} diff --git a/src/components/SectionSwap.tsx b/src/components/SectionSwap.tsx index 5a2219d..2099ec3 100644 --- a/src/components/SectionSwap.tsx +++ b/src/components/SectionSwap.tsx @@ -1,12 +1,10 @@ /* eslint-disable jsx-a11y/no-static-element-interactions */ /* eslint-disable jsx-a11y/click-events-have-key-events */ -import { useState } from 'react'; import { motion } from 'framer-motion'; import { FiChevronDown } from 'react-icons/fi'; -import { useAtomValue, useAtom } from 'jotai'; +import { useAtomValue, useAtom, useSetAtom } from 'jotai'; import clsx from 'clsx'; import CustomInput from 'components/CustomInput'; -import DialogSwap from 'components/DialogSwap'; import { displayFunctionsAtom, previewEnabledAtom } from 'store/app'; import { displayPetname } from 'utils/displayFunctions'; import { @@ -16,11 +14,9 @@ import { toPurseAtom, swapDirectionAtom, SwapDirection, - anchorBrandsAtom, - selectedAnchorPetnameAtom, + assetSelectionDialogOpenAtom, } from 'store/swap'; import { AmountMath } from '@agoric/ertp'; -import type { Brand } from '@agoric/ertp/src/types'; export enum SectionSwapType { FROM = 'FROM', @@ -34,13 +30,9 @@ const SectionSwap = ({ type }: { type: SectionSwapType }) => { const toPurse = useAtomValue(toPurseAtom); const [toAmount, setToAmount] = useAtom(toAmountAtom); const [fromAmount, setFromAmount] = useAtom(fromAmountAtom); - const [open, setOpen] = useState(false); + const setAssetSelectionDialogOpen = useSetAtom(assetSelectionDialogOpenAtom); const swapDirection = useAtomValue(swapDirectionAtom); const previewEnabled = useAtomValue(previewEnabledAtom); - const brands = useAtomValue(anchorBrandsAtom); - const [_selectedAnchorBrandPetname, setSelectedAnchorBrandPetname] = useAtom( - selectedAnchorPetnameAtom - ); const isMinted = (swapDirection === SwapDirection.WantMinted && @@ -51,11 +43,6 @@ const SectionSwap = ({ type }: { type: SectionSwapType }) => { const value = type === SectionSwapType.TO ? toAmount?.value : fromAmount?.value; - const handleBrandSelected = (brand: Brand | null) => { - setSelectedAnchorBrandPetname(displayBrandPetname(brand)); - setOpen(false); - }; - const purse = type === SectionSwapType.TO ? toPurse : fromPurse; const brand = purse?.brand; @@ -71,70 +58,60 @@ const SectionSwap = ({ type }: { type: SectionSwapType }) => { }; return ( - <> - setOpen(false)} - brands={brands} - selectedBrand={brand} - handleBrandSelected={handleBrandSelected} - /> - -

- Swap {type} -

-
-
- brand icon -
- {purse ? ( -
{ - !isMinted && setOpen(true); - }} - > -
-

- {displayBrandPetname(brand)} -

- {!isMinted && } -
- {previewEnabled && ( -

- Purse:{' '} - {displayPetname(purse?.pursePetname ?? '')}{' '} -

- )} -
- ) : ( - - )} - +

+ Swap {type} +

+
+
+ brand icon
- - + {purse ? ( +
{ + !isMinted && setAssetSelectionDialogOpen(true); + }} + > +
+

+ {displayBrandPetname(brand)} +

+ {!isMinted && } +
+ {previewEnabled && ( +

+ Purse: {displayPetname(purse?.pursePetname ?? '')}{' '} +

+ )} +
+ ) : ( + + )} + +
+ ); }; diff --git a/src/components/Swap.tsx b/src/components/Swap.tsx index 4dc10fa..e63ea5e 100644 --- a/src/components/Swap.tsx +++ b/src/components/Swap.tsx @@ -32,6 +32,7 @@ import { } from 'store/swap'; import { makeSwapOffer } from 'services/swap'; import { provisionSmartWallet } from 'services/wallet'; +import DialogSwap from './DialogSwap'; const Swap = () => { const chainConnection = useAtomValue(chainConnectionAtom); @@ -197,54 +198,57 @@ const Swap = () => { ); return ( - - -

IST Swap

- - - - - -
- {chainConnection && isSmartWalletProvisioned !== undefined ? ( - form - ) : ( - - )} - - + - -
-
- {isSmartWalletProvisioned === false - ? 'Provision Smart Wallet' - : 'Swap'} -
+ +

IST Swap

+ + + + +
- - {errorsToRender} - + {chainConnection && isSmartWalletProvisioned !== undefined ? ( + form + ) : ( + + )} + + + +
+
+ {isSmartWalletProvisioned === false + ? 'Provision Smart Wallet' + : 'Swap'} +
+ + + {errorsToRender} + + + ); }; diff --git a/src/store/swap.ts b/src/store/swap.ts index 0408418..9c10fa7 100644 --- a/src/store/swap.ts +++ b/src/store/swap.ts @@ -32,6 +32,8 @@ export enum SwapDirection { export const selectedAnchorPetnameAtom = atom(null); +export const assetSelectionDialogOpenAtom = atom(false); + export const anchorBrandAtom = atom( get => get(metricsAtom)?.anchorPoolBalance?.brand );