@huma-finance/widgets 0.0.35 → 0.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +1534 -1114
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.d.ts +42 -10
- package/dist/index.js +1536 -1117
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
- package/src/components/Activity.tsx +1 -5
- package/src/components/ApproveAllowanceModal.tsx +24 -10
- package/src/components/CreditLine/approve/1-ApproveAllowance.tsx +34 -0
- package/src/components/CreditLine/approve/2-Success.tsx +43 -0
- package/src/components/CreditLine/approve/index.tsx +85 -0
- package/src/components/CreditLine/borrow/2-ChooseAmount.tsx +8 -2
- package/src/components/CreditLine/borrow/{4-ApproveAllowance.tsx → 3-ApproveAllowance.tsx} +1 -1
- package/src/components/CreditLine/borrow/{3-Transfer.tsx → 4-Transfer.tsx} +4 -14
- package/src/components/CreditLine/borrow/index.tsx +2 -2
- package/src/components/HumaTable.tsx +111 -66
- package/src/components/Lend/supply/1-Evaluation/EvaluationKYC.tsx +52 -19
- package/src/components/Notifi/NotifiSubscriptionModal.tsx +7 -3
- package/src/components/SuperfluidFactoring/SuperfluidFactoredList.tsx +0 -1
- package/src/components/SuperfluidFactoring/SuperfluidUpcomingList.tsx +0 -1
- package/src/components/ViewOnExplorer.tsx +4 -7
- package/src/hooks/useElementDimensions.ts +30 -0
- package/src/index.tsx +40 -58
- package/src/utilTypes.d.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huma-finance/widgets",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.37",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"@ethersproject/experimental": "^5.7.0",
|
|
31
31
|
"@ethersproject/providers": "^5.6.0",
|
|
32
32
|
"@ethersproject/units": "^5.6.0",
|
|
33
|
-
"@huma-finance/sdk": "^0.0.
|
|
34
|
-
"@huma-finance/shared": "^0.0.
|
|
33
|
+
"@huma-finance/sdk": "^0.0.37",
|
|
34
|
+
"@huma-finance/shared": "^0.0.37",
|
|
35
35
|
"@mui/icons-material": "^5.3.0",
|
|
36
36
|
"@mui/material": "^5.0.6",
|
|
37
37
|
"@mui/styles": "^5.0.2",
|
|
@@ -83,6 +83,7 @@
|
|
|
83
83
|
"prebuild": "tsc -p tsconfig.build.json",
|
|
84
84
|
"build": "rollup -c",
|
|
85
85
|
"cosmos": "cosmos",
|
|
86
|
+
"lint": "eslint --max-warnings=0 \"src/**/*.{js,ts,tsx,graphql}\"",
|
|
86
87
|
"lint-staged": "lint-staged",
|
|
87
88
|
"e2e": "cypress run --config baseUrl=http://localhost:8000 --headed --browser chrome --e2e",
|
|
88
89
|
"generateDocs": "jsdoc2md --files 'src/**/*.tsx' --configure ./jsdoc2md.json > API.md",
|
|
@@ -192,5 +193,5 @@
|
|
|
192
193
|
"optionalDependencies": {
|
|
193
194
|
"encoding": "^0.1.13"
|
|
194
195
|
},
|
|
195
|
-
"gitHead": "
|
|
196
|
+
"gitHead": "7ec34fafed3ca19acf9e1be6e3de27d6a03460a5"
|
|
196
197
|
}
|
|
@@ -47,7 +47,7 @@ type ActivityDataType = {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
const columns = [
|
|
50
|
-
{ title: 'Action', dataIndex: 'eventText'
|
|
50
|
+
{ title: 'Action', dataIndex: 'eventText' },
|
|
51
51
|
{
|
|
52
52
|
title: 'Amount',
|
|
53
53
|
action: (record: { amount: string }) => {
|
|
@@ -117,10 +117,6 @@ export function Activity({
|
|
|
117
117
|
`,
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
if (!events) {
|
|
121
|
-
return null
|
|
122
|
-
}
|
|
123
|
-
|
|
124
120
|
return (
|
|
125
121
|
<Box css={styles.wrapper}>
|
|
126
122
|
<Box css={styles.title}>Recent Activity</Box>
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MaxUint256 } from '@ethersproject/constants'
|
|
2
2
|
import { Box, css, useTheme } from '@mui/material'
|
|
3
3
|
import { useWeb3React } from '@web3-react/core'
|
|
4
|
-
import { BigNumber } from 'ethers'
|
|
4
|
+
import { BigNumber, BigNumberish } from 'ethers'
|
|
5
5
|
import {
|
|
6
6
|
PoolInfoType,
|
|
7
7
|
sendTxAtom,
|
|
@@ -23,8 +23,9 @@ import { WrapperModal } from './WrapperModal'
|
|
|
23
23
|
type Props = {
|
|
24
24
|
poolInfo: PoolInfoType
|
|
25
25
|
showAutoPayback?: boolean
|
|
26
|
-
allowanceAmount?:
|
|
26
|
+
allowanceAmount?: BigNumberish
|
|
27
27
|
spender?: string
|
|
28
|
+
resetAfterSuccess?: boolean
|
|
28
29
|
handleSuccess: () => void
|
|
29
30
|
}
|
|
30
31
|
|
|
@@ -33,10 +34,11 @@ export function ApproveAllowanceModal({
|
|
|
33
34
|
showAutoPayback = false,
|
|
34
35
|
allowanceAmount,
|
|
35
36
|
spender,
|
|
37
|
+
resetAfterSuccess = true,
|
|
36
38
|
handleSuccess,
|
|
37
39
|
}: Props): React.ReactElement | null {
|
|
38
40
|
const theme = useTheme()
|
|
39
|
-
const { provider } = useWeb3React()
|
|
41
|
+
const { provider, account } = useWeb3React()
|
|
40
42
|
const { poolUnderlyingToken } = poolInfo
|
|
41
43
|
const { symbol } = poolUnderlyingToken
|
|
42
44
|
const poolUnderlyingTokenContract = usePoolUnderlyingTokenContract(
|
|
@@ -49,10 +51,12 @@ export function ApproveAllowanceModal({
|
|
|
49
51
|
|
|
50
52
|
useEffect(() => {
|
|
51
53
|
if (state === TxStateType.Success) {
|
|
52
|
-
|
|
54
|
+
if (resetAfterSuccess) {
|
|
55
|
+
reset()
|
|
56
|
+
}
|
|
53
57
|
handleSuccess()
|
|
54
58
|
}
|
|
55
|
-
}, [handleSuccess, reset, state, symbol])
|
|
59
|
+
}, [handleSuccess, reset, resetAfterSuccess, state, symbol])
|
|
56
60
|
|
|
57
61
|
const styles = {
|
|
58
62
|
iconWrapper: css`
|
|
@@ -71,14 +75,24 @@ export function ApproveAllowanceModal({
|
|
|
71
75
|
`,
|
|
72
76
|
}
|
|
73
77
|
|
|
74
|
-
const
|
|
78
|
+
const sendIncreaseAllowance = useCallback(async () => {
|
|
79
|
+
const allowanceSpender = spender ?? poolInfo.pool
|
|
80
|
+
const targetAllowanceAmount = allowanceAmount ?? MaxUint256
|
|
81
|
+
const currentAllowance = await poolUnderlyingTokenContract?.allowance(
|
|
82
|
+
account!,
|
|
83
|
+
allowanceSpender,
|
|
84
|
+
)
|
|
85
|
+
const amountToIncrease = BigNumber.from(targetAllowanceAmount).sub(
|
|
86
|
+
currentAllowance ?? 0,
|
|
87
|
+
)
|
|
75
88
|
send({
|
|
76
89
|
contract: poolUnderlyingTokenContract!,
|
|
77
|
-
method: '
|
|
78
|
-
params: [
|
|
90
|
+
method: 'increaseAllowance',
|
|
91
|
+
params: [allowanceSpender, amountToIncrease],
|
|
79
92
|
provider,
|
|
80
93
|
})
|
|
81
94
|
}, [
|
|
95
|
+
account,
|
|
82
96
|
allowanceAmount,
|
|
83
97
|
poolInfo.pool,
|
|
84
98
|
poolUnderlyingTokenContract,
|
|
@@ -89,7 +103,7 @@ export function ApproveAllowanceModal({
|
|
|
89
103
|
|
|
90
104
|
useMount(() => {
|
|
91
105
|
if (!showAutoPayback) {
|
|
92
|
-
|
|
106
|
+
sendIncreaseAllowance()
|
|
93
107
|
}
|
|
94
108
|
})
|
|
95
109
|
|
|
@@ -106,7 +120,7 @@ export function ApproveAllowanceModal({
|
|
|
106
120
|
This allowance transaction removes the need to confirm follow up{' '}
|
|
107
121
|
{symbol} transactions. Gas is on us.
|
|
108
122
|
</Box>
|
|
109
|
-
<BottomButton variant='contained' onClick={
|
|
123
|
+
<BottomButton variant='contained' onClick={sendIncreaseAllowance}>
|
|
110
124
|
APPROVE {symbol} PAYMENTS
|
|
111
125
|
</BottomButton>
|
|
112
126
|
</WrapperModal>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { PoolInfoType } from '@huma-finance/shared'
|
|
2
|
+
import { BigNumberish } from 'ethers'
|
|
3
|
+
import React, { useCallback } from 'react'
|
|
4
|
+
|
|
5
|
+
import { useAppDispatch } from '../../../hooks/useRedux'
|
|
6
|
+
import { setStep } from '../../../store/widgets.reducers'
|
|
7
|
+
import { WIDGET_STEP } from '../../../store/widgets.store'
|
|
8
|
+
import { ApproveAllowanceModal } from '../../ApproveAllowanceModal'
|
|
9
|
+
|
|
10
|
+
type Props = {
|
|
11
|
+
poolInfo: PoolInfoType
|
|
12
|
+
amount?: BigNumberish
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function ApproveAllowance({
|
|
16
|
+
poolInfo,
|
|
17
|
+
amount,
|
|
18
|
+
}: Props): React.ReactElement {
|
|
19
|
+
const dispatch = useAppDispatch()
|
|
20
|
+
|
|
21
|
+
const handleSuccess = useCallback(() => {
|
|
22
|
+
dispatch(setStep(WIDGET_STEP.Done))
|
|
23
|
+
}, [dispatch])
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<ApproveAllowanceModal
|
|
27
|
+
poolInfo={poolInfo}
|
|
28
|
+
allowanceAmount={amount}
|
|
29
|
+
handleSuccess={handleSuccess}
|
|
30
|
+
resetAfterSuccess={false}
|
|
31
|
+
showAutoPayback
|
|
32
|
+
/>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PoolInfoType,
|
|
3
|
+
decodeLogs,
|
|
4
|
+
downScale,
|
|
5
|
+
formatMoney,
|
|
6
|
+
sendTxAtom,
|
|
7
|
+
} from '@huma-finance/shared'
|
|
8
|
+
import erc20Abi from '@huma-finance/shared/dist/abis/erc20.json'
|
|
9
|
+
import React, { useEffect, useState } from 'react'
|
|
10
|
+
|
|
11
|
+
import { useAtom } from 'jotai'
|
|
12
|
+
import { TxDoneModal } from '../../TxDoneModal'
|
|
13
|
+
|
|
14
|
+
type Props = {
|
|
15
|
+
poolInfo: PoolInfoType
|
|
16
|
+
handleAction: () => void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function Success({ poolInfo, handleAction }: Props): React.ReactElement {
|
|
20
|
+
const { symbol, decimals } = poolInfo.poolUnderlyingToken
|
|
21
|
+
const [{ txReceipt }] = useAtom(sendTxAtom)
|
|
22
|
+
const [approvedAmount, setApprovedAmount] = useState<string | undefined>()
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (txReceipt) {
|
|
26
|
+
const [event] = decodeLogs(txReceipt.logs, erc20Abi)
|
|
27
|
+
if (event) {
|
|
28
|
+
const payedAmount = downScale(event.args.value.toString(), decimals)
|
|
29
|
+
setApprovedAmount(payedAmount)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}, [decimals, txReceipt])
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<TxDoneModal
|
|
36
|
+
handleAction={handleAction}
|
|
37
|
+
content={[
|
|
38
|
+
`You successfully approved ${formatMoney(approvedAmount)} ${symbol}.`,
|
|
39
|
+
`Your pool will automatically pay off the due amount on the credit line before the end of each billing cycle.`,
|
|
40
|
+
]}
|
|
41
|
+
/>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
POOL_NAME,
|
|
3
|
+
POOL_TYPE,
|
|
4
|
+
useAccountStats,
|
|
5
|
+
usePoolInfo,
|
|
6
|
+
} from '@huma-finance/shared'
|
|
7
|
+
import { useWeb3React } from '@web3-react/core'
|
|
8
|
+
import { BigNumberish } from 'ethers'
|
|
9
|
+
import React, { useEffect } from 'react'
|
|
10
|
+
import { useDispatch } from 'react-redux'
|
|
11
|
+
|
|
12
|
+
import { useAppSelector } from '../../../hooks/useRedux'
|
|
13
|
+
import { setStep } from '../../../store/widgets.reducers'
|
|
14
|
+
import { selectWidgetState } from '../../../store/widgets.selectors'
|
|
15
|
+
import { WIDGET_STEP } from '../../../store/widgets.store'
|
|
16
|
+
import { ErrorModal } from '../../ErrorModal'
|
|
17
|
+
import { WidgetWrapper } from '../../WidgetWrapper'
|
|
18
|
+
import { ApproveAllowance } from './1-ApproveAllowance'
|
|
19
|
+
import { Success } from './2-Success'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Credit line pool approve allowance props
|
|
23
|
+
* @typedef {Object} CreditLineApproveProps
|
|
24
|
+
* @property {POOL_NAME} poolName The name of the pool.
|
|
25
|
+
* @property {POOL_TYPE} poolType The type of the pool.
|
|
26
|
+
* @property {BigNumberish|undefined} amount The amount to approve.
|
|
27
|
+
* @property {function():void} handleClose Function to notify to close the widget modal when user clicks the 'x' close button.
|
|
28
|
+
* @property {function((number|undefined)):void|undefined} handleSuccess Optional function to notify that the credit line pool approve allowance action is successful.
|
|
29
|
+
*/
|
|
30
|
+
export type CreditLineApproveProps = {
|
|
31
|
+
poolName: keyof typeof POOL_NAME
|
|
32
|
+
poolType: keyof typeof POOL_TYPE
|
|
33
|
+
amount?: BigNumberish
|
|
34
|
+
handleClose: () => void
|
|
35
|
+
handleSuccess?: (blockNumber?: number) => void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function CreditLineApprove({
|
|
39
|
+
poolName: poolNameStr,
|
|
40
|
+
poolType: poolTypeStr,
|
|
41
|
+
amount,
|
|
42
|
+
handleClose,
|
|
43
|
+
handleSuccess,
|
|
44
|
+
}: CreditLineApproveProps): React.ReactElement | null {
|
|
45
|
+
const dispatch = useDispatch()
|
|
46
|
+
const { account } = useWeb3React()
|
|
47
|
+
const poolName = POOL_NAME[poolNameStr]
|
|
48
|
+
const poolType = POOL_TYPE[poolTypeStr]
|
|
49
|
+
const poolInfo = usePoolInfo(poolName, poolType)
|
|
50
|
+
const { step, errorMessage } = useAppSelector(selectWidgetState)
|
|
51
|
+
const [accountStats] = useAccountStats(poolName, poolType, account)
|
|
52
|
+
const { creditRecord } = accountStats
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
dispatch(setStep(WIDGET_STEP.ApproveAllowance))
|
|
56
|
+
}, [dispatch])
|
|
57
|
+
|
|
58
|
+
if (!poolInfo) {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<WidgetWrapper
|
|
64
|
+
isOpen
|
|
65
|
+
isLoading={!creditRecord}
|
|
66
|
+
loadingTitle='Approve Allowance'
|
|
67
|
+
handleClose={handleClose}
|
|
68
|
+
handleSuccess={handleSuccess}
|
|
69
|
+
>
|
|
70
|
+
{step === WIDGET_STEP.ApproveAllowance && (
|
|
71
|
+
<ApproveAllowance poolInfo={poolInfo} amount={amount} />
|
|
72
|
+
)}
|
|
73
|
+
{step === WIDGET_STEP.Done && (
|
|
74
|
+
<Success poolInfo={poolInfo} handleAction={handleClose} />
|
|
75
|
+
)}
|
|
76
|
+
{step === WIDGET_STEP.Error && (
|
|
77
|
+
<ErrorModal
|
|
78
|
+
title='Approve Allowance'
|
|
79
|
+
errorMessage={errorMessage}
|
|
80
|
+
handleOk={handleClose}
|
|
81
|
+
/>
|
|
82
|
+
)}
|
|
83
|
+
</WidgetWrapper>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
@@ -3,7 +3,9 @@ import {
|
|
|
3
3
|
PoolInfoType,
|
|
4
4
|
upScale,
|
|
5
5
|
useCLFeeManager,
|
|
6
|
+
useCLPoolAllowance,
|
|
6
7
|
} from '@huma-finance/shared'
|
|
8
|
+
import { useWeb3React } from '@web3-react/core'
|
|
7
9
|
import React, { useCallback, useState } from 'react'
|
|
8
10
|
|
|
9
11
|
import { useAppDispatch } from '../../../hooks/useRedux'
|
|
@@ -21,6 +23,7 @@ export function ChooseAmount({
|
|
|
21
23
|
accountStats,
|
|
22
24
|
}: Props): React.ReactElement | null {
|
|
23
25
|
const dispatch = useAppDispatch()
|
|
26
|
+
const { account } = useWeb3React()
|
|
24
27
|
const { poolUnderlyingToken } = poolInfo
|
|
25
28
|
const { symbol, decimals } = poolUnderlyingToken
|
|
26
29
|
const { getFeesCharged } = useCLFeeManager(poolInfo.poolName)
|
|
@@ -28,6 +31,7 @@ export function ChooseAmount({
|
|
|
28
31
|
const [currentAmount, setCurrentAmount] = useState(0)
|
|
29
32
|
const { creditRecordStatic, creditAvailableAmount } = accountStats
|
|
30
33
|
|
|
34
|
+
const { approved } = useCLPoolAllowance(poolInfo.poolName, account)
|
|
31
35
|
const handleChangeAmount = useCallback(
|
|
32
36
|
(newAmount: number) => {
|
|
33
37
|
setCurrentAmount(newAmount)
|
|
@@ -38,7 +42,9 @@ export function ChooseAmount({
|
|
|
38
42
|
)
|
|
39
43
|
|
|
40
44
|
const handleAction = useCallback(() => {
|
|
41
|
-
const nextStep =
|
|
45
|
+
const nextStep = approved
|
|
46
|
+
? WIDGET_STEP.Transfer
|
|
47
|
+
: WIDGET_STEP.ApproveAllowance
|
|
42
48
|
dispatch(
|
|
43
49
|
setBorrowInfo({
|
|
44
50
|
borrowAmount: currentAmount,
|
|
@@ -47,7 +53,7 @@ export function ChooseAmount({
|
|
|
47
53
|
nextStep,
|
|
48
54
|
}),
|
|
49
55
|
)
|
|
50
|
-
}, [chargedFees, currentAmount, decimals, dispatch])
|
|
56
|
+
}, [approved, chargedFees, currentAmount, decimals, dispatch])
|
|
51
57
|
|
|
52
58
|
return (
|
|
53
59
|
<ChooseAmountModal
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
toBigNumber,
|
|
4
|
-
txAtom,
|
|
5
|
-
useCLPoolAllowance,
|
|
6
|
-
} from '@huma-finance/shared'
|
|
1
|
+
import { PoolInfoType, txAtom } from '@huma-finance/shared'
|
|
2
|
+
import { useResetAtom } from 'jotai/utils'
|
|
7
3
|
import React, { useCallback } from 'react'
|
|
8
4
|
|
|
9
|
-
import { useResetAtom } from 'jotai/utils'
|
|
10
|
-
import { useWeb3React } from '@web3-react/core'
|
|
11
5
|
import { useAppDispatch, useAppSelector } from '../../../hooks/useRedux'
|
|
12
6
|
import { setStep } from '../../../store/widgets.reducers'
|
|
13
7
|
import { selectWidgetState } from '../../../store/widgets.selectors'
|
|
@@ -21,17 +15,13 @@ type Props = {
|
|
|
21
15
|
export function Transfer({ poolInfo }: Props): React.ReactElement {
|
|
22
16
|
const dispatch = useAppDispatch()
|
|
23
17
|
const reset = useResetAtom(txAtom)
|
|
24
|
-
const { account } = useWeb3React()
|
|
25
18
|
const { borrowAmountBN } = useAppSelector(selectWidgetState)
|
|
26
|
-
const { allowance } = useCLPoolAllowance(poolInfo.poolName, account)
|
|
27
19
|
|
|
28
20
|
const handleSuccess = useCallback(() => {
|
|
29
21
|
reset()
|
|
30
|
-
const nextStep =
|
|
31
|
-
? WIDGET_STEP.Done
|
|
32
|
-
: WIDGET_STEP.ApproveAllowance
|
|
22
|
+
const nextStep = WIDGET_STEP.Done
|
|
33
23
|
dispatch(setStep(nextStep))
|
|
34
|
-
}, [
|
|
24
|
+
}, [dispatch, reset])
|
|
35
25
|
|
|
36
26
|
return (
|
|
37
27
|
<TxSendModal
|
|
@@ -17,8 +17,8 @@ import { ErrorModal } from '../../ErrorModal'
|
|
|
17
17
|
import { WidgetWrapper } from '../../WidgetWrapper'
|
|
18
18
|
import { Evaluation } from './1-Evaluation'
|
|
19
19
|
import { ChooseAmount } from './2-ChooseAmount'
|
|
20
|
-
import { ApproveAllowance } from './
|
|
21
|
-
import { Transfer } from './
|
|
20
|
+
import { ApproveAllowance } from './3-ApproveAllowance'
|
|
21
|
+
import { Transfer } from './4-Transfer'
|
|
22
22
|
import { Success } from './5-Success'
|
|
23
23
|
import { Notifications } from './6-Notifications'
|
|
24
24
|
import {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Box, css, TablePagination, useTheme } from '@mui/material'
|
|
1
|
+
import { Box, css, Skeleton, TablePagination, useTheme } from '@mui/material'
|
|
2
2
|
import Table from '@mui/material/Table'
|
|
3
3
|
import TableBody from '@mui/material/TableBody'
|
|
4
4
|
import TableCell from '@mui/material/TableCell'
|
|
@@ -7,6 +7,7 @@ import TableHead from '@mui/material/TableHead'
|
|
|
7
7
|
import TableRow from '@mui/material/TableRow'
|
|
8
8
|
import * as React from 'react'
|
|
9
9
|
|
|
10
|
+
import { useElementDimensions } from '../hooks/useElementDimensions'
|
|
10
11
|
import { ColumnType } from '../utilTypes'
|
|
11
12
|
import { SeparatorIcon } from './icons'
|
|
12
13
|
|
|
@@ -17,16 +18,20 @@ type Props<T> = {
|
|
|
17
18
|
hideRowsPerPage?: boolean
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
// @TODO: Move HumaTable along with Superfluid widget to Dapp package
|
|
20
22
|
export function HumaTable<T>({
|
|
21
23
|
columns,
|
|
22
24
|
rows,
|
|
23
25
|
hideRowsPerPage = false,
|
|
24
26
|
}: Props<T>): React.ReactElement | null {
|
|
25
27
|
const theme = useTheme()
|
|
28
|
+
const ref = React.useRef()
|
|
26
29
|
const [page, setPage] = React.useState(0)
|
|
27
30
|
const [rowsPerPage, setRowsPerPage] = React.useState(5)
|
|
28
31
|
const noPagination = rows ? rows.length <= 5 : true
|
|
29
|
-
const
|
|
32
|
+
const rowPerPageOptions = hideRowsPerPage ? [] : [5, 10, 25]
|
|
33
|
+
const { width } = useElementDimensions(ref)
|
|
34
|
+
const isLoading = !columns || !rows
|
|
30
35
|
|
|
31
36
|
const styles = {
|
|
32
37
|
tableHeader: css`
|
|
@@ -40,6 +45,14 @@ export function HumaTable<T>({
|
|
|
40
45
|
font-size: 14px;
|
|
41
46
|
color: rgba(0, 0, 0, 0.87);
|
|
42
47
|
`,
|
|
48
|
+
empty: css`
|
|
49
|
+
display: flex;
|
|
50
|
+
flex-direction: column;
|
|
51
|
+
align-items: center;
|
|
52
|
+
margin-top: 20px;
|
|
53
|
+
font-family: 'Uni-Neue-Regular';
|
|
54
|
+
font-size: 14px;
|
|
55
|
+
`,
|
|
43
56
|
}
|
|
44
57
|
|
|
45
58
|
const handleChangePage = (event: unknown, newPage: number) => {
|
|
@@ -53,77 +66,96 @@ export function HumaTable<T>({
|
|
|
53
66
|
setPage(0)
|
|
54
67
|
}
|
|
55
68
|
|
|
56
|
-
|
|
57
|
-
|
|
69
|
+
const getTableHeader = () => (
|
|
70
|
+
<TableHead>
|
|
71
|
+
<TableRow>
|
|
72
|
+
{columns.map((column, index) => (
|
|
73
|
+
<TableCell key={column.title} width={column.width}>
|
|
74
|
+
<Box css={styles.tableHeader}>
|
|
75
|
+
{index !== 0 && <SeparatorIcon />}
|
|
76
|
+
<Box component='span' sx={{ marginLeft: index ? '12px' : '0' }}>
|
|
77
|
+
{column.title}
|
|
78
|
+
</Box>
|
|
79
|
+
</Box>
|
|
80
|
+
</TableCell>
|
|
81
|
+
))}
|
|
82
|
+
</TableRow>
|
|
83
|
+
</TableHead>
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
if (isLoading) {
|
|
87
|
+
return (
|
|
88
|
+
<Box ref={ref}>
|
|
89
|
+
<Box sx={{ width }}>
|
|
90
|
+
<Skeleton />
|
|
91
|
+
<Skeleton />
|
|
92
|
+
<Skeleton />
|
|
93
|
+
</Box>
|
|
94
|
+
</Box>
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (rows.length === 0) {
|
|
99
|
+
return (
|
|
100
|
+
<TableContainer>
|
|
101
|
+
<Table aria-label='simple table'>{getTableHeader()}</Table>
|
|
102
|
+
<Box css={styles.empty}>
|
|
103
|
+
<Box>No Data</Box>
|
|
104
|
+
</Box>
|
|
105
|
+
</TableContainer>
|
|
106
|
+
)
|
|
58
107
|
}
|
|
59
108
|
|
|
60
109
|
return (
|
|
61
110
|
<>
|
|
62
111
|
<TableContainer>
|
|
63
112
|
<Table aria-label='simple table'>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
113
|
+
{getTableHeader()}
|
|
114
|
+
<TableBody>
|
|
115
|
+
{rows
|
|
116
|
+
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
|
|
117
|
+
.map((row) => (
|
|
118
|
+
<TableRow key={row.id || row.name}>
|
|
119
|
+
{columns.map((column, index) => (
|
|
120
|
+
<TableCell
|
|
121
|
+
// eslint-disable-next-line react/no-array-index-key
|
|
122
|
+
key={`${column.dataIndex}-${index}`}
|
|
123
|
+
component='th'
|
|
124
|
+
scope='row'
|
|
125
|
+
css={styles.tableBody}
|
|
126
|
+
width={column.width}
|
|
127
|
+
sx={column.style}
|
|
73
128
|
>
|
|
74
|
-
{column.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
129
|
+
{column.dataIndex && (
|
|
130
|
+
<Box
|
|
131
|
+
component='span'
|
|
132
|
+
sx={{
|
|
133
|
+
display: 'flex',
|
|
134
|
+
alignItems: 'center',
|
|
135
|
+
marginLeft: index ? '12px' : '0',
|
|
136
|
+
}}
|
|
137
|
+
>
|
|
138
|
+
{row[column.dataIndex]}
|
|
139
|
+
</Box>
|
|
140
|
+
)}
|
|
141
|
+
{column.action && (
|
|
142
|
+
<Box
|
|
143
|
+
component='span'
|
|
144
|
+
sx={{
|
|
145
|
+
display: 'flex',
|
|
146
|
+
alignItems: 'center',
|
|
147
|
+
marginLeft: index ? '12px' : '0',
|
|
148
|
+
lineHeight: 0,
|
|
149
|
+
height: 0,
|
|
150
|
+
}}
|
|
151
|
+
>
|
|
152
|
+
{column.action(row)}
|
|
153
|
+
</Box>
|
|
154
|
+
)}
|
|
155
|
+
</TableCell>
|
|
156
|
+
))}
|
|
157
|
+
</TableRow>
|
|
78
158
|
))}
|
|
79
|
-
</TableRow>
|
|
80
|
-
</TableHead>
|
|
81
|
-
<TableBody>
|
|
82
|
-
{rows &&
|
|
83
|
-
rows
|
|
84
|
-
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
|
|
85
|
-
.map((row) => (
|
|
86
|
-
<TableRow key={row.id || row.name}>
|
|
87
|
-
{columns.map((column, index) => (
|
|
88
|
-
<TableCell
|
|
89
|
-
// eslint-disable-next-line react/no-array-index-key
|
|
90
|
-
key={`${column.dataIndex}-${index}`}
|
|
91
|
-
component='th'
|
|
92
|
-
scope='row'
|
|
93
|
-
css={styles.tableBody}
|
|
94
|
-
width={column.width}
|
|
95
|
-
sx={column.style}
|
|
96
|
-
>
|
|
97
|
-
{column.dataIndex && (
|
|
98
|
-
<Box
|
|
99
|
-
component='span'
|
|
100
|
-
sx={{
|
|
101
|
-
display: 'flex',
|
|
102
|
-
alignItems: 'center',
|
|
103
|
-
marginLeft: column.noSeparator ? '0' : '12px',
|
|
104
|
-
}}
|
|
105
|
-
>
|
|
106
|
-
{row[column.dataIndex]}
|
|
107
|
-
</Box>
|
|
108
|
-
)}
|
|
109
|
-
{column.action && (
|
|
110
|
-
<Box
|
|
111
|
-
component='span'
|
|
112
|
-
sx={{
|
|
113
|
-
display: 'flex',
|
|
114
|
-
alignItems: 'center',
|
|
115
|
-
marginLeft: column.noSeparator ? '0' : '12px',
|
|
116
|
-
lineHeight: 0,
|
|
117
|
-
height: 0,
|
|
118
|
-
}}
|
|
119
|
-
>
|
|
120
|
-
{column.action(row)}
|
|
121
|
-
</Box>
|
|
122
|
-
)}
|
|
123
|
-
</TableCell>
|
|
124
|
-
))}
|
|
125
|
-
</TableRow>
|
|
126
|
-
))}
|
|
127
159
|
</TableBody>
|
|
128
160
|
</Table>
|
|
129
161
|
</TableContainer>
|
|
@@ -144,13 +176,26 @@ export function HumaTable<T>({
|
|
|
144
176
|
fontSize: '14px',
|
|
145
177
|
},
|
|
146
178
|
}}
|
|
147
|
-
rowsPerPageOptions={
|
|
179
|
+
rowsPerPageOptions={rowPerPageOptions}
|
|
148
180
|
component='div'
|
|
149
181
|
count={rows ? rows.length : 0}
|
|
150
182
|
rowsPerPage={rowsPerPage}
|
|
151
183
|
page={page}
|
|
152
184
|
onPageChange={handleChangePage}
|
|
153
185
|
onRowsPerPageChange={handleChangeRowsPerPage}
|
|
186
|
+
SelectProps={{
|
|
187
|
+
MenuProps: {
|
|
188
|
+
sx: {
|
|
189
|
+
'.MuiTablePagination-menuItem': {
|
|
190
|
+
padding: '4px 0',
|
|
191
|
+
display: 'flex',
|
|
192
|
+
justifyContent: 'center',
|
|
193
|
+
fontSize: '14px',
|
|
194
|
+
lineHeight: '1.2',
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
}}
|
|
154
199
|
/>
|
|
155
200
|
)}
|
|
156
201
|
</>
|