-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
223 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { useCallback, useEffect, useMemo } from "react"; | ||
import { useLocalStorage } from "usehooks-ts"; | ||
|
||
import { | ||
DELEGATION_STATUSES, | ||
DelegationLike, | ||
DelegationV2, | ||
DelegationV2StakingState as State, | ||
} from "@/app/types/delegationsV2"; | ||
|
||
export function useDelegationStorage( | ||
key: string, | ||
delegations?: DelegationV2[], | ||
) { | ||
const [pendingDelegations = {}, setPendingDelegations] = useLocalStorage< | ||
Record<string, DelegationLike> | ||
>(`${key}_pending`, {}); | ||
const [delegationStatuses = {}, setDelegationStatuses] = useLocalStorage< | ||
Record<string, State> | ||
>(`${key}_statuses`, {}); | ||
|
||
const delegationMap = useMemo(() => { | ||
return (delegations ?? []).reduce( | ||
(acc, delegation) => ({ | ||
...acc, | ||
[delegation.stakingTxHashHex]: delegation, | ||
}), | ||
{} as Record<string, DelegationV2>, | ||
); | ||
}, [delegations]); | ||
|
||
const formattedDelegations = useMemo(() => { | ||
const pendingDelegationArr = Object.values(pendingDelegations).map( | ||
(d) => | ||
({ | ||
...d, | ||
stakingTxHex: "", | ||
paramsVersion: 0, | ||
finalityProviderBtcPksHex: [], | ||
stakerBtcPkHex: "", | ||
stakingTime: 0, | ||
endHeight: 0, | ||
unbondingTime: 0, | ||
unbondingTxHex: "", | ||
}) as DelegationV2, | ||
); | ||
|
||
return pendingDelegationArr.concat( | ||
(delegations ?? []) | ||
.filter((d) => !pendingDelegations[d.stakingTxHashHex]) | ||
.map((d) => ({ | ||
...d, | ||
state: delegationStatuses[d.stakingTxHashHex] ?? d.state, | ||
})), | ||
); | ||
}, [delegations, pendingDelegations, delegationStatuses]); | ||
|
||
useEffect( | ||
function syncPendingDelegations() { | ||
if (!key) return; | ||
|
||
setPendingDelegations((delegations) => { | ||
const result = Object.values(delegations) | ||
.filter((d) => !delegationMap[d.stakingTxHashHex]) | ||
.reduce( | ||
(acc, d) => ({ ...acc, [d.stakingTxHashHex]: d }), | ||
{} as Record<string, DelegationLike>, | ||
); | ||
|
||
return result; | ||
}); | ||
}, | ||
[key, delegationMap, setPendingDelegations], | ||
); | ||
|
||
useEffect( | ||
function syncDelegationStatuses() { | ||
if (!key) return; | ||
|
||
setDelegationStatuses((statuses) => | ||
Object.entries(statuses) | ||
.filter( | ||
([hash, status]) => | ||
DELEGATION_STATUSES[status] < | ||
DELEGATION_STATUSES[delegationMap[hash].state], | ||
) | ||
.reduce( | ||
(acc, [hash, status]) => ({ ...acc, [hash]: status }), | ||
{} as Record<string, State>, | ||
), | ||
); | ||
}, | ||
[key, delegationMap, setDelegationStatuses], | ||
); | ||
|
||
const addPendingDelegation = useCallback( | ||
(delegation: DelegationLike) => { | ||
if (!key) return; | ||
|
||
setPendingDelegations((delegations) => ({ | ||
...delegations, | ||
[delegation.stakingTxHashHex]: { | ||
...delegation, | ||
state: State.INTERMEDIATE_PENDING_VERIFICATION, | ||
}, | ||
})); | ||
}, | ||
[key, setPendingDelegations], | ||
); | ||
|
||
const updateDelegationStatus = useCallback( | ||
(id: string, status: State) => { | ||
if (!key) return; | ||
|
||
setDelegationStatuses((statuses) => ({ ...statuses, [id]: status })); | ||
}, | ||
[key, setDelegationStatuses], | ||
); | ||
|
||
return { | ||
delegations: formattedDelegations, | ||
addPendingDelegation, | ||
updateDelegationStatus, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.