2021-06-23 12:09:19 -07:00
|
|
|
import { MangoAccount } from '@blockworks-foundation/mango-client'
|
2021-05-07 12:41:26 -07:00
|
|
|
import { useEffect, useState } from 'react'
|
2021-04-25 22:20:05 -07:00
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
|
|
|
import Select from './Select'
|
2021-05-01 07:02:21 -07:00
|
|
|
import { abbreviateAddress } from '../utils'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2021-05-01 07:02:21 -07:00
|
|
|
|
2021-06-23 08:32:33 -07:00
|
|
|
type MangoAccountSelectProps = {
|
2021-04-25 22:20:05 -07:00
|
|
|
className?: string
|
|
|
|
onChange?: (x) => void
|
2021-06-23 08:32:33 -07:00
|
|
|
value?: MangoAccount | null
|
2021-04-25 22:20:05 -07:00
|
|
|
disabled?: boolean
|
|
|
|
}
|
|
|
|
|
2021-06-23 08:32:33 -07:00
|
|
|
const MangoAccountSelect = ({
|
2021-04-25 22:20:05 -07:00
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
disabled = false,
|
|
|
|
className = '',
|
2021-06-23 08:32:33 -07:00
|
|
|
}: MangoAccountSelectProps) => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-06-23 08:32:33 -07:00
|
|
|
const mangoAccounts = useMangoStore((s) => s.mangoAccounts)
|
|
|
|
const [selectedMangoAccount, setSelectedMangoAccount] = useState(
|
|
|
|
value || mangoAccounts[0]
|
2021-04-25 22:20:05 -07:00
|
|
|
)
|
2021-05-07 12:41:26 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (value) {
|
2021-06-23 08:32:33 -07:00
|
|
|
setSelectedMangoAccount(value)
|
2021-05-07 12:41:26 -07:00
|
|
|
}
|
|
|
|
}, [value])
|
2021-04-25 22:20:05 -07:00
|
|
|
|
2021-06-23 08:32:33 -07:00
|
|
|
const handleSelectMangoAccount = (value) => {
|
|
|
|
const mangoAccount = mangoAccounts.find(
|
2021-04-25 22:20:05 -07:00
|
|
|
(ma) => ma.publicKey.toString() === value
|
|
|
|
)
|
2022-03-28 07:18:35 -07:00
|
|
|
if (!mangoAccount) {
|
|
|
|
return
|
|
|
|
}
|
2021-06-23 08:32:33 -07:00
|
|
|
setSelectedMangoAccount(mangoAccount)
|
2021-04-25 22:20:05 -07:00
|
|
|
if (onChange) {
|
2021-06-23 08:32:33 -07:00
|
|
|
onChange(mangoAccount)
|
2021-04-25 22:20:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
disabled={disabled}
|
2021-05-01 07:02:21 -07:00
|
|
|
value={
|
|
|
|
<div className="text-left">
|
2021-06-23 08:32:33 -07:00
|
|
|
{abbreviateAddress(selectedMangoAccount?.publicKey)}
|
2021-05-01 07:02:21 -07:00
|
|
|
</div>
|
|
|
|
}
|
2021-06-23 08:32:33 -07:00
|
|
|
onChange={handleSelectMangoAccount}
|
2021-10-20 05:42:40 -07:00
|
|
|
placeholder={t('select-margin')}
|
2021-04-25 22:20:05 -07:00
|
|
|
className={className}
|
|
|
|
>
|
2021-06-23 08:32:33 -07:00
|
|
|
{mangoAccounts.length ? (
|
|
|
|
mangoAccounts.map((ma, index) => (
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
<Select.Option key={index} value={ma.publicKey.toString()}>
|
|
|
|
{abbreviateAddress(ma.publicKey)}
|
|
|
|
</Select.Option>
|
|
|
|
))
|
2021-04-25 22:20:05 -07:00
|
|
|
) : (
|
|
|
|
<Select.Option value className="text-th-fgd-4">
|
2021-10-20 05:42:40 -07:00
|
|
|
{t('no-margin')}
|
2021-04-25 22:20:05 -07:00
|
|
|
</Select.Option>
|
|
|
|
)}
|
|
|
|
</Select>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-23 08:32:33 -07:00
|
|
|
export default MangoAccountSelect
|