1
- import useSWR from "swr " ;
1
+ import { ArrowPathIcon } from "@heroicons/react/24/outline " ;
2
2
3
3
import {
4
4
type AccountHistoryAction ,
5
5
type StakeDetails ,
6
6
AccountHistoryItemType ,
7
7
StakeType ,
8
- loadAccountHistory ,
9
8
} from "../../api" ;
10
- import { useApiContext } from "../../hooks/use-api-context" ;
11
- import { LoadingSpinner } from "../LoadingSpinner" ;
9
+ import { StateType , useAccountHistory } from "../../hooks/use-account-history" ;
12
10
import { Tokens } from "../Tokens" ;
13
11
14
- const ONE_SECOND_IN_MS = 1000 ;
15
- const ONE_MINUTE_IN_MS = 60 * ONE_SECOND_IN_MS ;
16
- const REFRESH_INTERVAL = 1 * ONE_MINUTE_IN_MS ;
17
-
18
12
export const AccountHistory = ( ) => {
19
- const history = useAccountHistoryData ( ) ;
13
+ const history = useAccountHistory ( ) ;
20
14
21
15
switch ( history . type ) {
22
- case DataStateType . NotLoaded :
23
- case DataStateType . Loading : {
24
- return < LoadingSpinner /> ;
16
+ case StateType . NotLoaded :
17
+ case StateType . Loading : {
18
+ return < ArrowPathIcon className = "size-6 animate-spin" /> ;
25
19
}
26
- case DataStateType . Error : {
20
+ case StateType . Error : {
27
21
return < p > Uh oh, an error occured!</ p > ;
28
22
}
29
- case DataStateType . Loaded : {
23
+ case StateType . Loaded : {
30
24
return (
31
25
< table className = "text-sm" >
32
26
< thead className = "font-medium" >
@@ -56,7 +50,9 @@ export const AccountHistory = () => {
56
50
) => (
57
51
< tr key = { i } >
58
52
< td className = "pr-4" > { timestamp . toLocaleString ( ) } </ td >
59
- < td className = "pr-4" > { mkDescription ( action ) } </ td >
53
+ < td className = "pr-4" >
54
+ < Description > { action } </ Description >
55
+ </ td >
60
56
< td className = "pr-4" >
61
57
< Tokens > { amount } </ Tokens >
62
58
</ td >
@@ -82,37 +78,37 @@ export const AccountHistory = () => {
82
78
}
83
79
} ;
84
80
85
- const mkDescription = ( action : AccountHistoryAction ) : string => {
86
- switch ( action . type ) {
81
+ const Description = ( { children } : { children : AccountHistoryAction } ) => {
82
+ switch ( children . type ) {
87
83
case AccountHistoryItemType . Claim : {
88
84
return "Rewards claimed" ;
89
85
}
90
- case AccountHistoryItemType . Deposit : {
91
- return "Tokens deposited " ;
86
+ case AccountHistoryItemType . AddTokens : {
87
+ return "Tokens added " ;
92
88
}
93
89
case AccountHistoryItemType . LockedDeposit : {
94
- return `Locked tokens deposited, unlocking ${ action . unlockDate . toLocaleString ( ) } ` ;
90
+ return `Locked tokens deposited, unlocking ${ children . unlockDate . toLocaleString ( ) } ` ;
95
91
}
96
92
case AccountHistoryItemType . RewardsCredited : {
97
93
return "Rewards credited" ;
98
94
}
99
95
case AccountHistoryItemType . Slash : {
100
- return `Staked tokens slashed from ${ action . publisherName } ` ;
96
+ return `Staked tokens slashed from ${ children . publisherName } ` ;
101
97
}
102
98
case AccountHistoryItemType . StakeCreated : {
103
- return `Created stake position for ${ getStakeDetails ( action . details ) } ` ;
99
+ return `Created stake position for ${ getStakeDetails ( children . details ) } ` ;
104
100
}
105
101
case AccountHistoryItemType . StakeFinishedWarmup : {
106
- return `Warmup complete for position for ${ getStakeDetails ( action . details ) } ` ;
102
+ return `Warmup complete for position for ${ getStakeDetails ( children . details ) } ` ;
107
103
}
108
104
case AccountHistoryItemType . Unlock : {
109
105
return "Locked tokens unlocked" ;
110
106
}
111
107
case AccountHistoryItemType . UnstakeCreated : {
112
- return `Requested unstake for position for ${ getStakeDetails ( action . details ) } ` ;
108
+ return `Requested unstake for position for ${ getStakeDetails ( children . details ) } ` ;
113
109
}
114
110
case AccountHistoryItemType . UnstakeExitedCooldown : {
115
- return `Cooldown completed for ${ getStakeDetails ( action . details ) } ` ;
111
+ return `Cooldown completed for ${ getStakeDetails ( children . details ) } ` ;
116
112
}
117
113
case AccountHistoryItemType . Withdrawal : {
118
114
return "Tokens withdrawn to wallet" ;
@@ -130,46 +126,3 @@ const getStakeDetails = (details: StakeDetails): string => {
130
126
}
131
127
}
132
128
} ;
133
-
134
- const useAccountHistoryData = ( ) => {
135
- const apiContext = useApiContext ( ) ;
136
-
137
- const { data, isLoading, ...rest } = useSWR (
138
- `${ apiContext . stakeAccount . address . toBase58 ( ) } /history` ,
139
- ( ) => loadAccountHistory ( apiContext ) ,
140
- {
141
- refreshInterval : REFRESH_INTERVAL ,
142
- } ,
143
- ) ;
144
- const error = rest . error as unknown ;
145
-
146
- if ( error ) {
147
- return DataState . ErrorState ( error ) ;
148
- } else if ( isLoading ) {
149
- return DataState . Loading ( ) ;
150
- } else if ( data ) {
151
- return DataState . Loaded ( data ) ;
152
- } else {
153
- return DataState . NotLoaded ( ) ;
154
- }
155
- } ;
156
-
157
- enum DataStateType {
158
- NotLoaded ,
159
- Loading ,
160
- Loaded ,
161
- Error ,
162
- }
163
- const DataState = {
164
- NotLoaded : ( ) => ( { type : DataStateType . NotLoaded as const } ) ,
165
- Loading : ( ) => ( { type : DataStateType . Loading as const } ) ,
166
- Loaded : ( data : Awaited < ReturnType < typeof loadAccountHistory > > ) => ( {
167
- type : DataStateType . Loaded as const ,
168
- data,
169
- } ) ,
170
- ErrorState : ( error : unknown ) => ( {
171
- type : DataStateType . Error as const ,
172
- error,
173
- } ) ,
174
- } ;
175
- type DataState = ReturnType < ( typeof DataState ) [ keyof typeof DataState ] > ;
0 commit comments