11"use client" ;
22
3+ import { removeNullBytes } from "@/lib/utils" ;
34import { useGovernance } from "@/providers/GovernanceProvider" ;
45import { useSolanaUnixNow } from "@helium/helium-react-hooks" ;
56import {
67 PositionWithMeta ,
78 SubDaoWithMeta ,
8- useDataBurnSplit ,
9- useSubDaoDelegationSplit ,
109} from "@helium/voter-stake-registry-hooks" ;
1110import { PublicKey } from "@solana/web3.js" ;
1211import BN from "bn.js" ;
1312import classNames from "classnames" ;
1413import { Loader2 , X } from "lucide-react" ;
1514import React , { FC , useCallback , useMemo , useState } from "react" ;
1615import { FaCircleArrowRight } from "react-icons/fa6" ;
16+ import { AutomationSettings } from "../AutomationSettings" ;
17+ import { DataSplitBars } from "../DataSplitBars" ;
1718import { StepIndicator } from "../StepIndicator" ;
1819import { SubDaoSelection } from "../SubDaoSelection" ;
1920import { Button } from "../ui/button" ;
2021import { ConfirmationItem } from "./ConfirmationItem" ;
21- import { DataSplitBars } from "../DataSplitBars" ;
22- import { AutomationSettings } from "../AutomationSettings" ;
23- import { humanReadable } from "@/lib/utils" ;
2422
2523export const UpdatePositionDelegationPrompt : FC < {
2624 position : PositionWithMeta ;
2725 isSubmitting ?: boolean ;
2826 onCancel : ( ) => void ;
2927 onConfirm : ( subDao ?: SubDaoWithMeta ) => Promise < void > ;
28+ onUndelegate : ( ) => Promise < void > ;
3029 automationEnabled : boolean ;
3130 setAutomationEnabled : ( automationEnabled : boolean ) => void ;
3231 subDao : SubDaoWithMeta | null ;
@@ -38,6 +37,7 @@ export const UpdatePositionDelegationPrompt: FC<{
3837 isSubmitting,
3938 onCancel,
4039 onConfirm,
40+ onUndelegate,
4141 subDao,
4242 setSubDao,
4343 automationEnabled,
@@ -46,7 +46,8 @@ export const UpdatePositionDelegationPrompt: FC<{
4646 prepaidTxFees = 0 ,
4747} ) => {
4848 const [ step , setStep ] = useState ( 1 ) ;
49- const { subDaos, voteService } = useGovernance ( ) ;
49+ const [ isUndelegating , setIsUndelegating ] = useState ( false ) ;
50+ const { subDaos } = useGovernance ( ) ;
5051 const unixNow = useSolanaUnixNow ( ) || Date . now ( ) / 1000 ;
5152 const { lockup, isDelegated, delegatedSubDao } = position ;
5253 const lockupKind = Object . keys ( lockup . kind ) [ 0 ] as string ;
@@ -71,50 +72,12 @@ export const UpdatePositionDelegationPrompt: FC<{
7172 subDaos ?. find ( ( sd ) => sd . pubkey . equals ( position . delegatedSubDao ! ) )
7273 ?. dntMetadata ;
7374
74- const { data : revData , isLoading : revLoading } = useDataBurnSplit ( {
75- voteService,
76- } ) ;
77- const { iot : iotDataUsageRev = 0 , mobile : mobileDataUsageRev = 0 } =
78- ( revData || { } ) as { iot : number ; mobile : number } ;
79- const { data : delegationData , isLoading : delegationLoading } =
80- useSubDaoDelegationSplit ( {
81- voteService,
82- } ) ;
83- const {
84- iot : iotDelegation = new BN ( 0 ) ,
85- mobile : mobileDelegation = new BN ( 0 ) ,
86- } = ( delegationData || { } ) as { iot : BN ; mobile : BN } ;
87-
88- const totalDataUsage = Number ( mobileDataUsageRev ) + Number ( iotDataUsageRev ) ;
89- const totalVetokens =
90- mobileDelegation && iotDelegation
91- ? new BN ( mobileDelegation ) . add ( new BN ( iotDelegation ) )
92- : new BN ( 0 ) ;
93-
94- const mobileDelegationPercentage =
95- mobileDelegation && totalVetokens . gt ( new BN ( 0 ) )
96- ? new BN ( mobileDelegation )
97- . mul ( new BN ( 10000 ) )
98- . div ( totalVetokens )
99- . toNumber ( ) / 100
100- : 0 ;
101-
102- const iotDelegationPercentage =
103- iotDelegation && totalVetokens . gt ( new BN ( 0 ) )
104- ? new BN ( iotDelegation ) . mul ( new BN ( 10000 ) ) . div ( totalVetokens ) . toNumber ( ) /
105- 100
106- : 0 ;
107-
108- const mobileDataUsagePercentage =
109- totalDataUsage > 0
110- ? ( Number ( mobileDataUsageRev ) / totalDataUsage ) * 100
111- : 0 ;
112-
113- const iotDataUsagePercentage =
114- totalDataUsage > 0 ? ( Number ( iotDataUsageRev ) / totalDataUsage ) * 100 : 0 ;
115-
11675 const handleSubmit = async ( ) => {
117- await onConfirm ( selectedSubDao ) ;
76+ if ( isUndelegating ) {
77+ await onUndelegate ( ) ;
78+ } else {
79+ await onConfirm ( selectedSubDao ) ;
80+ }
11881 } ;
11982
12083 const setSelectedSubDaoPk = useCallback (
@@ -135,7 +98,13 @@ export const UpdatePositionDelegationPrompt: FC<{
13598 < div className = "flex flex-col gap-4" >
13699 < div className = "flex flex-col gap-1" >
137100 < div className = "flex flex-row justify-between items-center" >
138- < h3 > { step === 1 ? "Update Position" : "Update Delegation" } </ h3 >
101+ < h3 >
102+ { step === 1
103+ ? "Update Position"
104+ : isUndelegating
105+ ? "Undelegate Position"
106+ : "Update Delegation" }
107+ </ h3 >
139108 < StepIndicator steps = { 2 } currentStep = { step } />
140109 </ div >
141110 { step === 1 ? (
@@ -177,7 +146,7 @@ export const UpdatePositionDelegationPrompt: FC<{
177146 disabled = { ! isDelegated }
178147 className = "flex-1"
179148 onClick = { ( ) => {
180- setSelectedSubDaoPk ( undefined ) ;
149+ setIsUndelegating ( true ) ;
181150 setStep ( step + 1 ) ;
182151 } }
183152 >
@@ -214,7 +183,9 @@ export const UpdatePositionDelegationPrompt: FC<{
214183 src : delegatedSubDaoMetadata ?. json ?. image ,
215184 } }
216185 title = "Delegating from..."
217- description = { delegatedSubDaoMetadata ?. symbol . toUpperCase ( ) }
186+ description = { removeNullBytes (
187+ delegatedSubDaoMetadata ?. symbol . toUpperCase ( )
188+ ) }
218189 />
219190 < FaCircleArrowRight className = "size-6 absolute left-[calc(50%-0.75rem)] top-[calc(50%-0.75rem)]" />
220191 </ >
@@ -229,7 +200,9 @@ export const UpdatePositionDelegationPrompt: FC<{
229200 src : selectedSubDaoMetadata ?. json ?. image ,
230201 } }
231202 title = "Delegating to..."
232- description = { selectedSubDaoMetadata ?. symbol . toUpperCase ( ) }
203+ description = { removeNullBytes (
204+ selectedSubDaoMetadata ?. symbol . toUpperCase ( )
205+ ) }
233206 />
234207 </ div >
235208 ) }
@@ -240,8 +213,14 @@ export const UpdatePositionDelegationPrompt: FC<{
240213 alt : delegatedSubDaoMetadata ?. json ?. name ,
241214 src : delegatedSubDaoMetadata ?. json ?. image ,
242215 } }
243- title = "Updating delegation"
244- description = { delegatedSubDaoMetadata ?. symbol . toUpperCase ( ) }
216+ title = {
217+ isUndelegating
218+ ? "Undelegating from..."
219+ : "Updating delegation"
220+ }
221+ description = { removeNullBytes (
222+ delegatedSubDaoMetadata ?. symbol . toUpperCase ( )
223+ ) }
245224 />
246225 ) }
247226 </ div >
@@ -253,6 +232,7 @@ export const UpdatePositionDelegationPrompt: FC<{
253232 disabled = { isSubmitting }
254233 onClick = { ( ) => {
255234 setSelectedSubDaoPk ( undefined ) ;
235+ setIsUndelegating ( false ) ;
256236 setStep ( step - 1 ) ;
257237 } }
258238 >
@@ -266,7 +246,11 @@ export const UpdatePositionDelegationPrompt: FC<{
266246 { isSubmitting && (
267247 < Loader2 className = "size-5 animate-spin" />
268248 ) }
269- { isSubmitting ? "Updating Delegation..." : "Confirm" }
249+ { isSubmitting
250+ ? isUndelegating
251+ ? "Undelegating..."
252+ : "Updating delegation..."
253+ : "Confirm" }
270254 </ Button >
271255 </ div >
272256 < p className = "text-xs text-muted-foreground text-center" >
0 commit comments