44 createContext ,
55 useContext ,
66 useMemo ,
7- useCallback ,
87} from 'react' ;
98import { Address } from 'viem' ;
109import invariant from 'tiny-invariant' ;
@@ -13,9 +12,27 @@ import { calculateOverview } from '@lidofinance/lsv-cli/dist/utils/calculate-ove
1312import { formatBalance , formatPercent } from 'utils' ;
1413
1514import { useVaultInfo } from 'modules/vaults/vault-context' ;
16- import { VAULT_TOTAL_BASIS_POINTS , VAULTS_ALL_ROLES } from 'modules/vaults' ;
15+ import {
16+ VAULT_TOTAL_BASIS_POINTS ,
17+ VAULTS_ALL_ROLES ,
18+ vaultTexts ,
19+ } from 'modules/vaults' ;
20+
21+ export type SectionData = {
22+ key : VaultOverviewContextKeys ;
23+ actionRole ?: VAULTS_ALL_ROLES ;
24+ actionLink ?: ( vaultAddress : Address ) => string ;
25+ } ;
26+
27+ export type SectionPayload = SectionData & {
28+ title : string ;
29+ hint ?: string ;
30+ action ?: string ;
31+ isLoading ?: boolean ;
32+ payload : string | Address | number ;
33+ } ;
1734
18- export interface VaultOverviewContextType {
35+ export type VaultOverviewContextType = {
1936 values : {
2037 address : Address ;
2138 nodeOperator : Address ;
@@ -37,28 +54,33 @@ export interface VaultOverviewContextType {
3754 pendingUnlockEth : string ;
3855 } ;
3956 isLoadingVault ?: boolean ;
40- getVaultDataToRender : (
41- payload : SectionPayload [ ] ,
42- ) => ( SectionPayload & { payload : string | Address | number } ) [ ] ;
43- }
57+ getVaultDataToRender : ( payload : SectionData ) => SectionPayload ;
58+ } ;
4459
4560export type VaultOverviewContextKeys = keyof VaultOverviewContextType [ 'values' ] ;
46- export type SectionPayload = {
61+
62+ type MetricText = {
4763 title : string ;
48- key : VaultOverviewContextKeys ;
49- actionText ?: string ;
50- actionRole ?: VAULTS_ALL_ROLES ;
51- actionLink ?: ( vaultAddress : Address ) => string ;
52- isLoading ?: boolean ;
64+ hint ?: string ;
65+ action ?: string ;
5366} ;
5467
5568const VaultOverviewContext = createContext < VaultOverviewContextType | null > (
5669 null ,
5770) ;
71+ VaultOverviewContext . displayName = 'VaultOverviewContext' ;
5872
5973const toEthValue = ( value : bigint ) => `${ formatBalance ( value ) . trimmed } ETH` ;
6074const toStethValue = ( value : bigint ) => `${ formatBalance ( value ) . trimmed } stETH` ;
6175
76+ const getMetricTexts = ( key : VaultOverviewContextKeys ) : MetricText => {
77+ const metric = vaultTexts . metrics [
78+ key as keyof typeof vaultTexts . metrics
79+ ] as MetricText ;
80+ invariant ( metric , `Metric text for ${ key } not found` ) ;
81+ return metric ;
82+ } ;
83+
6284export const VaultOverviewProvider : FC < PropsWithChildren > = ( { children } ) => {
6385 const { activeVault, isLoadingVault } = useVaultInfo ( ) ;
6486
@@ -147,21 +169,20 @@ export const VaultOverviewProvider: FC<PropsWithChildren> = ({ children }) => {
147169 return { } as VaultOverviewContextType [ 'values' ] ;
148170 } , [ activeVault , isLoadingVault ] ) ;
149171
150- const getVaultDataToRender = useCallback (
151- ( sectionPayloadList : SectionPayload [ ] ) => {
152- return sectionPayloadList . map ( ( item ) => {
153- return {
154- ...item ,
155- payload : values [ item . key ] ,
156- isLoading : isLoadingVault ,
157- } ;
158- } ) ;
159- } ,
160- [ values , isLoadingVault ] ,
161- ) ;
172+ const value = useMemo ( ( ) => {
173+ return {
174+ values,
175+ getVaultDataToRender : ( sectionEntry : SectionData ) => ( {
176+ ...sectionEntry ,
177+ ...getMetricTexts ( sectionEntry . key ) ,
178+ payload : values [ sectionEntry . key ] ,
179+ isLoading : isLoadingVault ,
180+ } ) ,
181+ } ;
182+ } , [ isLoadingVault , values ] ) ;
162183
163184 return (
164- < VaultOverviewContext . Provider value = { { values , getVaultDataToRender } } >
185+ < VaultOverviewContext . Provider value = { value } >
165186 { children }
166187 </ VaultOverviewContext . Provider >
167188 ) ;
0 commit comments