This repository was archived by the owner on Sep 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathuseExpansionVisibilityService.ts
More file actions
153 lines (136 loc) · 5.34 KB
/
Copy pathuseExpansionVisibilityService.ts
File metadata and controls
153 lines (136 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { useCallback } from "react";
import { useDelegationStorage } from "@/ui/common/hooks/storage/useDelegationStorage";
import { useStakingExpansionState } from "@/ui/common/state/StakingExpansionState";
import {
DelegationV2,
DelegationV2StakingState,
} from "@/ui/common/types/delegationsV2";
import { getExpansionsLocalStorageKey } from "@/ui/common/utils/local_storage/getExpansionsLocalStorageKey";
/**
* Hook providing expansion visibility services.
* Centralizes complex logic for determining which delegations should be visible
* in different UI parts (Activity tab vs Verified Expansion Modal).
*/
export function useExpansionVisibilityService(
publicKeyNoCoord: string | undefined,
) {
const { expansions } = useStakingExpansionState();
// Access expansion localStorage for broadcast status detection
const expansionStorageKey = getExpansionsLocalStorageKey(publicKeyNoCoord);
// Get pending delegations from localStorage to check broadcast status
const { delegations: expansionStorageDelegations } = useDelegationStorage(
expansionStorageKey,
expansions ?? [],
);
/**
* Checks if a delegation is a broadcasted expansion.
* A broadcasted expansion must meet both criteria:
* 1. Has VERIFIED status in the API data (delegation.state)
* 2. Has INTERMEDIATE_PENDING_VERIFICATION status in localStorage (user broadcasted it)
*/
const isBroadcastedExpansion = useCallback(
(delegation: DelegationV2): boolean => {
// First, verify this is actually VERIFIED in the API data
if (delegation.state !== DelegationV2StakingState.VERIFIED) {
return false;
}
// Then check if this delegation exists in localStorage with INTERMEDIATE_PENDING_VERIFICATION status
const storedDelegation = (expansionStorageDelegations ?? []).find(
(stored) =>
stored.stakingTxHashHex.toLowerCase() ===
delegation.stakingTxHashHex.toLowerCase(),
);
return (
storedDelegation?.state ===
DelegationV2StakingState.INTERMEDIATE_PENDING_VERIFICATION
);
},
[expansionStorageDelegations],
);
/**
* Checks if an original staking transaction should be hidden.
* Original staking transactions are hidden when they have broadcasted expansions.
*/
const isOriginalTransactionHidden = useCallback(
(stakeHashHex: string, delegations: DelegationV2[]): boolean => {
// Find any expansion that references this original transaction
const relatedExpansions = delegations.filter(
(delegation) =>
delegation.previousStakingTxHashHex === stakeHashHex &&
isBroadcastedExpansion(delegation),
);
return relatedExpansions.length > 0;
},
[isBroadcastedExpansion],
);
/**
* Returns delegations that should be visible in the Activity tab.
* Applies the following rules:
* 1. Exclude VERIFIED expansions that are not broadcasted (show in modal only)
* 2. Include VERIFIED expansions that are broadcasted (INTERMEDIATE_PENDING_VERIFICATION)
* 3. Exclude original transactions that have broadcasted expansions
* 4. Include all other regular transactions
*/
const getActivityTabDelegations = useCallback(
(delegations: DelegationV2[]): DelegationV2[] => {
// Performance optimization: Pre-compute hidden transaction IDs to avoid O^2 complexity
const hiddenOriginalTxIds = new Set<string>();
// First pass: identify all original transactions that should be hidden
delegations.forEach((delegation) => {
if (
delegation.previousStakingTxHashHex &&
isBroadcastedExpansion(delegation)
) {
hiddenOriginalTxIds.add(
delegation.previousStakingTxHashHex.toLowerCase(),
);
}
});
// Second pass: filter delegations using O(1) Set lookups
return delegations.filter((delegation) => {
// Always exclude EXPANDED state delegations (existing logic)
if (delegation.state === DelegationV2StakingState.EXPANDED) {
return false;
}
// Handle VERIFIED expansions
if (
delegation.state === DelegationV2StakingState.VERIFIED &&
delegation.previousStakingTxHashHex
) {
// Only include if it's been broadcasted by the user
return isBroadcastedExpansion(delegation);
}
// Check if this is an original transaction that should be hidden (O(1) lookup)
if (
hiddenOriginalTxIds.has(delegation.stakingTxHashHex.toLowerCase())
) {
return false;
}
// Include all other transactions
return true;
});
},
[isBroadcastedExpansion],
);
/**
* Returns delegations that should be visible in the Verified Expansion Modal.
* These are VERIFIED expansions with previousStakingTxHashHex that haven't been broadcasted yet.
*/
const getVerifiedExpansionModalDelegations = useCallback(
(delegations: DelegationV2[]): DelegationV2[] => {
return delegations.filter(
(delegation) =>
delegation.state === DelegationV2StakingState.VERIFIED &&
delegation.previousStakingTxHashHex &&
!isBroadcastedExpansion(delegation),
);
},
[isBroadcastedExpansion],
);
return {
getActivityTabDelegations,
getVerifiedExpansionModalDelegations,
isOriginalTransactionHidden,
isBroadcastedExpansion,
};
}