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

Created a Asset Selector for Send page with Search #401

Merged
merged 3 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/app/sections/LCDSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useForm } from "react-hook-form"
import { Form, FormItem, Input } from "components/form"
import { useTranslation } from "react-i18next"
import { useNetworks } from "app/InitNetworks"
import ChainSelector from "components/form/ChainSelector"
import ChainSelector from "components/form/Selectors/ChainSelector/ChainSelector"
import { useEffect, useMemo, useState } from "react"
import { Button } from "components/general"
import styles from "./LCDSetting.module.scss"
Expand Down
60 changes: 60 additions & 0 deletions src/components/form/Selectors/AssetSelector/AssetList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import styles from "../../ChainSelector.module.scss"
import WithSearchInput from "pages/custom/WithSearchInput"
import classNames from "classnames"

interface AssetType {
denom: string
balance: string
icon: string
symbol: string
price: number
chains: string[]
}

interface Props {
list: AssetType[]
onChange: (symbol: string, index: number) => void
value: string
small?: boolean
noSearch?: boolean
}

const ChainList = ({ list, onChange, value, small, noSearch }: Props) => {
return (
<div className={styles.options}>
<WithSearchInput disabled={noSearch} inline gap={4}>
{(search) => (
<div
className={classNames(
styles.options__container,
small && styles.options__container__small
)}
>
{list
.filter(
({ denom, symbol }) =>
denom.toLowerCase().includes(search.toLowerCase()) ||
symbol.toLowerCase().includes(search.toLowerCase())
)
.map(({ denom, symbol, icon }, index) => (
<button
className={symbol === value ? styles.active : ""}
key={denom}
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
onChange(denom, index)
}}
>
<img src={icon} alt={denom} />
{symbol}
</button>
))}
</div>
)}
</WithSearchInput>
</div>
)
}

export default ChainList
74 changes: 74 additions & 0 deletions src/components/form/Selectors/AssetSelector/AssetSelector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useState, useRef, useEffect } from "react"
import styles from "../../ChainSelector.module.scss"
import ArrowDropDownIcon from "@mui/icons-material/ArrowDropDown"
import AssetList from "./AssetList"

interface AssetType {
denom: string
balance: string
icon: string
symbol: string
price: number
chains: string[]
}

interface Props {
assetList: AssetType[]
onChange: (chain: string) => void
value: string
assetsByDenom: Record<string, AssetType>
}

const AssetSelector = ({
assetList,
onChange,
value,
assetsByDenom,
}: Props) => {
const [open, setOpen] = useState(false)
const ref = useRef<HTMLDivElement>(null)

const handleClickOutside = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
setOpen(false)
}
}
useEffect(() => {
document.addEventListener("mousedown", handleClickOutside)
return () => {
document.removeEventListener("mousedown", handleClickOutside)
}
}, [])

const handleSelection = (denom: string) => {
onChange(denom)
setOpen(false)
}

return (
<div className={styles.container} ref={ref}>
<button
type="button"
className={styles.selector}
onClick={(e) => {
e.stopPropagation()
if (e.screenX && e.screenY) setOpen((o) => !o) // negate onClick triggered by enter key press
}}
>
<span>
<img
src={assetsByDenom[value]?.icon}
alt={assetsByDenom[value]?.denom}
/>{" "}
{assetsByDenom[value]?.symbol}
</span>{" "}
<ArrowDropDownIcon style={{ fontSize: 20 }} className={styles.caret} />
</button>
{open && (
<AssetList list={assetList} onChange={handleSelection} value={value} />
)}
</div>
)
}

export default AssetSelector
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import styles from "./ChainSelector.module.scss"
import styles from "../../ChainSelector.module.scss"
import WithSearchInput from "pages/custom/WithSearchInput"
import classNames from "classnames"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo, useState, useRef, useEffect } from "react"
import styles from "./ChainSelector.module.scss"
import styles from "../../ChainSelector.module.scss"
import ArrowDropDownIcon from "@mui/icons-material/ArrowDropDown"
import { useNetworks } from "app/InitNetworks"
import ChainList from "./ChainList"
Expand Down
2 changes: 1 addition & 1 deletion src/components/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export { default as Checkbox } from "./Checkbox"
export * from "./Checkbox"
export { default as Toggle } from "./Toggle"
export { default as Upload } from "./Upload"
export { default as ChainSelector } from "./ChainSelector"
export { default as ChainSelector } from "./Selectors/ChainSelector/ChainSelector"

/* modules */
export { default as Paste } from "./Paste"
Expand Down
4 changes: 3 additions & 1 deletion src/pages/stake/QuickStake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import {
import { combineState } from "data/query"
import { Tooltip, TooltipIcon } from "components/display"
import { Delegation } from "@terra-money/feather.js"
import TokenSelector, { TokenInterface } from "components/form/TokenSelector"
import TokenSelector, {
TokenInterface,
} from "components/form/Selectors/TokenSelector/TokenSelector"
import { useState } from "react"
import { useAuth } from "auth"
import is from "auth/scripts/is"
Expand Down
4 changes: 3 additions & 1 deletion src/pages/stake/Validators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import styles from "./Validators.module.scss"
import ValidatorsList from "./ValidatorsList"
import { Flex, Grid, InlineFlex, Page, Table } from "components/layout"
import { useTranslation } from "react-i18next"
import TokenSelector, { TokenInterface } from "components/form/TokenSelector"
import TokenSelector, {
TokenInterface,
} from "components/form/Selectors/TokenSelector/TokenSelector"
import { useNetwork } from "data/wallet"
import { useState } from "react"
import {
Expand Down
47 changes: 30 additions & 17 deletions src/pages/wallet/SendPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from "@terra-money/feather.js"
import { isDenom, toAmount } from "@terra-money/terra-utils"
import { useInterchainAddresses } from "auth/hooks/useAddress"
import { Form, FormItem, FormWarning, Input, Select } from "components/form"
import ChainSelector from "components/form/ChainSelector"
import { Form, FormItem, FormWarning, Input } from "components/form"
import ChainSelector from "components/form/Selectors/ChainSelector/ChainSelector"
import { Flex, Grid } from "components/layout"
import { SAMPLE_ADDRESS } from "config/constants"
import { useBankBalance } from "data/queries/bank"
Expand All @@ -31,9 +31,10 @@ import { queryKey } from "data/query"
import Tx from "txs/Tx"
import AddressBookList from "txs/AddressBook/AddressBookList"
import { ModalButton } from "components/feedback"
import AssetSelector from "components/form/Selectors/AssetSelector/AssetSelector"

interface TxValues {
asset: string
asset?: string
chain?: string
recipient?: string // AccAddress | TNS
address?: AccAddress // hidden input
Expand Down Expand Up @@ -131,6 +132,13 @@ const SendPage = () => {
readNativeDenom(denom).token === watch("asset")
)

/* resolve asset */
useEffect(() => {
if (!asset) {
setValue("asset", defaultAsset)
}
}, [form, asset, defaultAsset, setValue])

/* resolve recipient */
useEffect(() => {
if (!recipient) {
Expand Down Expand Up @@ -325,6 +333,19 @@ const SendPage = () => {
}
}, [chain, trigger, recipient])

const filteredAssets = useMemo(
() => availableAssets.filter(({ symbol }) => !symbol.endsWith("...")),
[availableAssets]
)

const assetsByDenom = filteredAssets.reduce(
(acc: Record<string, AssetType>, item: AssetType) => {
acc[item.denom] = item
return acc
},
{}
)

return (
// @ts-expect-error
<Tx {...tx}>
Expand All @@ -339,20 +360,12 @@ const SendPage = () => {
label={t("Asset")}
error={errors.asset?.message ?? errors.address?.message}
>
<Select
{...register("asset", {
value: defaultAsset,
})}
autoFocus
>
{availableAssets
.filter(({ symbol }) => !symbol.endsWith("..."))
.map(({ denom, symbol }, i) => (
<option value={denom} key={i}>
{symbol}
</option>
))}
</Select>
<AssetSelector
value={asset ?? ""}
onChange={(asset) => setValue("asset", asset)}
assetList={filteredAssets}
assetsByDenom={assetsByDenom}
/>
</FormItem>
{availableChains && (
<FormItem label={t("Source chain")}>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/wallet/TransferPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { isDenom, toAmount } from "@terra-money/terra-utils"
import { useInterchainAddresses } from "auth/hooks/useAddress"
import { Form, FormItem, Input, Select } from "components/form"
import ChainSelector from "components/form/ChainSelector"
import ChainSelector from "components/form/Selectors/ChainSelector/ChainSelector"
import { Flex } from "components/layout"
import { useBankBalance } from "data/queries/bank"
import { useExchangeRates } from "data/queries/coingecko"
Expand Down