11/* eslint-disable react/jsx-wrap-multilines */
2- import { FC , useEffect , useState } from 'react'
2+ import { FC , useMemo } from 'react'
33
44import { UserProfile } from '~/libs/core'
55import { IconOutline , LinkButton , LoadingCircles } from '~/libs/ui'
66
7- import { Balance } from '../../../lib/models/WalletDetails'
87import { InfoRow , PayoutGuard } from '../../../lib'
98import { BannerImage , BannerText } from '../../../lib/assets/home'
109import { nullToZero } from '../../../lib/util'
@@ -17,21 +16,136 @@ interface HomeTabProps {
1716 profile : UserProfile
1817}
1918
19+ type WalletDetailsData = NonNullable < WalletDetailsResponse [ 'data' ] >
20+ interface WalletInfoRowsProps {
21+ walletDetails : WalletDetailsData
22+ profile : UserProfile
23+ balanceSum : number
24+ }
25+
26+ const WalletInfoRows : FC < WalletInfoRowsProps > = props => (
27+ < div className = { styles [ 'info-row-container' ] } >
28+ < InfoRow
29+ title = 'Account Balance'
30+ value = { `$${ props . balanceSum } ` }
31+ action = {
32+ < LinkButton
33+ label = 'MANAGE YOUR WINNINGS'
34+ iconToRight
35+ icon = { IconOutline . ArrowRightIcon }
36+ size = 'md'
37+ link
38+ to = '#winnings'
39+ />
40+ }
41+ />
42+
43+ < PayoutGuard profile = { props . profile } >
44+ { props . walletDetails . withdrawalMethod . isSetupComplete && (
45+ < InfoRow
46+ title = 'Est. Payment Fees'
47+ value = { `$${ nullToZero ( props . walletDetails . estimatedFees ) } USD` }
48+ action = {
49+ < LinkButton
50+ label = 'ADJUST YOUR PAYOUT SETTINGS'
51+ iconToRight
52+ icon = { IconOutline . ArrowRightIcon }
53+ size = 'md'
54+ link
55+ to = '#payout'
56+ />
57+ }
58+ />
59+ ) }
60+ { props . walletDetails . taxForm . isSetupComplete && (
61+ < InfoRow
62+ title = 'Est. Tax Withholding %'
63+ value = { `${ nullToZero ( props . walletDetails . taxWithholdingPercentage ) } %` }
64+ action = {
65+ < LinkButton
66+ label = 'ADJUST YOUR PAYOUT SETTINGS'
67+ iconToRight
68+ icon = { IconOutline . ArrowRightIcon }
69+ size = 'md'
70+ link
71+ to = '#payout'
72+ />
73+ }
74+ />
75+ ) }
76+
77+ { ! props . walletDetails . withdrawalMethod . isSetupComplete && (
78+ < InfoRow
79+ title = 'Withdrawal Method'
80+ value = { < Chip text = 'Setup Required' /> }
81+ action = {
82+ < LinkButton
83+ label = 'SETUP WITHDRAWAL METHOD'
84+ iconToRight
85+ icon = { IconOutline . ArrowRightIcon }
86+ size = 'md'
87+ link
88+ to = '#payout'
89+ />
90+ }
91+ />
92+ ) }
93+
94+ { ! props . walletDetails . taxForm . isSetupComplete && (
95+ < InfoRow
96+ title = 'Tax Form'
97+ value = { < Chip text = 'Setup Required' /> }
98+ action = {
99+ < LinkButton
100+ label = 'COMPLETE TAX FORM'
101+ iconToRight
102+ icon = { IconOutline . ArrowRightIcon }
103+ size = 'md'
104+ link
105+ to = '#payout'
106+ />
107+ }
108+ />
109+ ) }
110+ { ! props . walletDetails . identityVerification . isSetupComplete && (
111+ < InfoRow
112+ title = 'ID Verification'
113+ value = { < Chip text = 'Setup Required' /> }
114+ action = {
115+ < LinkButton
116+ label = 'COMPLETE VERIFICATION'
117+ iconToRight
118+ icon = { IconOutline . ArrowRightIcon }
119+ size = 'md'
120+ link
121+ to = '#payout'
122+ />
123+ }
124+ />
125+ ) }
126+ </ PayoutGuard >
127+ </ div >
128+ )
129+
20130const HomeTab : FC < HomeTabProps > = props => {
21131
22132 const { data : walletDetails , isLoading, error } : WalletDetailsResponse = useWalletDetails ( )
23- const [ balanceSum , setBalanceSum ] = useState ( 0 )
24133
25- useEffect ( ( ) => {
26- if ( walletDetails ) {
27- setBalanceSum (
28- walletDetails . account . balances . reduce ( ( sum : number , balance : Balance ) => sum + balance . amount , 0 ) ,
29- )
30- }
31- } , [ walletDetails ] )
134+ const balanceSum = useMemo (
135+ ( ) => ( walletDetails ? walletDetails . account . balances . reduce ( ( sum , balance ) => sum + balance . amount , 0 ) : 0 ) ,
136+ [ walletDetails ] ,
137+ )
32138
33139 if ( error ) {
34- return < div > { error } </ div >
140+ let errorMessage = 'Unable to load wallet details.'
141+
142+ if ( typeof error === 'string' ) {
143+ errorMessage = error
144+ } else if ( error && typeof error === 'object' && 'message' in error ) {
145+ errorMessage = ( error as Error ) . message || errorMessage
146+ }
147+
148+ return < div > { errorMessage } </ div >
35149 }
36150
37151 return (
@@ -42,117 +156,7 @@ const HomeTab: FC<HomeTabProps> = props => {
42156 </ div >
43157 { isLoading && < LoadingCircles /> }
44158 { ! isLoading && walletDetails && (
45- < div className = { styles [ 'info-row-container' ] } >
46- < InfoRow
47- title = 'Account Balance'
48- value = { `$${ balanceSum } ` }
49- action = {
50- < LinkButton
51- label = 'MANAGE YOUR WINNINGS'
52- iconToRight
53- icon = { IconOutline . ArrowRightIcon }
54- size = 'md'
55- link
56- to = '#winnings'
57- />
58- }
59- />
60-
61- < PayoutGuard profile = { props . profile } >
62- { walletDetails . withdrawalMethod . isSetupComplete && (
63- < InfoRow
64- title = 'Est. Payment Fees'
65- value = { `$${ nullToZero ( walletDetails . estimatedFees ) } USD` }
66- action = {
67- < LinkButton
68- label = 'ADJUST YOUR PAYOUT SETTINGS'
69- iconToRight
70- icon = { IconOutline . ArrowRightIcon }
71- size = 'md'
72- link
73- to = '#payout'
74- />
75- }
76- />
77- ) }
78- { walletDetails . taxForm . isSetupComplete && (
79- < InfoRow
80- title = 'Est. Tax Withholding %'
81- value = { `${ nullToZero ( walletDetails . taxWithholdingPercentage ) } %` }
82- action = {
83- < LinkButton
84- label = 'ADJUST YOUR PAYOUT SETTINGS'
85- iconToRight
86- icon = { IconOutline . ArrowRightIcon }
87- size = 'md'
88- link
89- to = '#payout'
90- />
91- }
92- />
93- ) }
94-
95- { ! walletDetails ?. withdrawalMethod . isSetupComplete && (
96- < InfoRow
97- title = 'Withdrawal Method'
98- value = {
99- walletDetails ?. withdrawalMethod . isSetupComplete ? (
100- 'Your preferred method'
101- ) : (
102- < Chip text = 'Setup Required' />
103- )
104- }
105- action = {
106- < LinkButton
107- label = 'SETUP WITHDRAWAL METHOD'
108- iconToRight
109- icon = { IconOutline . ArrowRightIcon }
110- size = 'md'
111- link
112- to = '#payout'
113- />
114- }
115- />
116- ) }
117-
118- { ! walletDetails ?. taxForm . isSetupComplete && (
119- < InfoRow
120- title = 'Tax Form'
121- value = {
122- walletDetails ?. taxForm . isSetupComplete
123- ? 'All set'
124- : < Chip text = 'Setup Required' />
125- }
126- action = {
127- < LinkButton
128- label = 'COMPLETE TAX FORM'
129- iconToRight
130- icon = { IconOutline . ArrowRightIcon }
131- size = 'md'
132- link
133- to = '#payout'
134- />
135- }
136- />
137- ) }
138- { ! walletDetails ?. identityVerification . isSetupComplete && (
139- < InfoRow
140- title = 'ID Verification'
141- value = { < Chip text = 'Setup Required' /> }
142- action = {
143- < LinkButton
144- label = 'COMPLETE VERIFICATION'
145- iconToRight
146- icon = { IconOutline . ArrowRightIcon }
147- size = 'md'
148- link
149- to = '#payout'
150- />
151- }
152- />
153- ) }
154- </ PayoutGuard >
155- </ div >
159+ < WalletInfoRows walletDetails = { walletDetails } profile = { props . profile } balanceSum = { balanceSum } />
156160 ) }
157161 </ div >
158162 )
0 commit comments