-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): Do not show all-zero state in Hero by default (#3354)
# Motivation We have 3 different states for the Hero: A. **Loading**: initial state when the user join. B. **Balance**: state that is shown when the user has at least some token. C. **No-balance**: state that is shown when all the tokens have zero balance or cannot be loaded. The idea is to have only two transitions: from A to B, or from A to C, with no middle glitch. That means that: - for state B, the code shall wait for AT LEAST one positive token balance; - for state C, the code shall wait for all tokens to be zero balance (or not available). So, we create a new derived that monitors all tokens balance to be zero, once all of them are loaded. This is based not only on checking for nullish or zero values, but even to have a minimum number of tokens loaded in the balance store. # Changes - Create store to check for all balances to be zero. - Include condition in the loading context for the Hero: after the exchange is initialized, it can stop loading IF either any token has positive balance OR all tokens have zero balance. - Substitute condition for the Send Button in the Hero: always visible, hidden only when all balances are Zero. - Same for the alternative info string below the USD balance. # Tests I provided some tests for the new util. However, for the stores/derived, I shall do a separate PR, since it requires to mock the `page` store too, and it is not quite trivial. https://github.com/user-attachments/assets/89da6086-04a6-41a2-889f-e5db74333f5d https://github.com/user-attachments/assets/3598a006-ed0d-45fd-9e0c-9cbb8ac7ddbd --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
09a3f68
commit bec3ace
Showing
8 changed files
with
164 additions
and
10 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
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 |
---|---|---|
@@ -1,10 +1,38 @@ | ||
import { type BalancesData } from '$lib/stores/balances.store'; | ||
import type { CertifiedStoreData } from '$lib/stores/certified.store'; | ||
import type { TokenId } from '$lib/types/token'; | ||
import type { Option } from '$lib/types/utils'; | ||
import { nonNullish } from '@dfinity/utils'; | ||
|
||
export const checkAnyNonZeroBalance = ($balancesStore: CertifiedStoreData<BalancesData>): boolean => | ||
nonNullish($balancesStore) && | ||
Object.getOwnPropertySymbols($balancesStore).some( | ||
(tokenId) => !($balancesStore[tokenId as TokenId]?.data?.isZero() ?? true) | ||
); | ||
|
||
/** | ||
* Check if all balances are zero. | ||
* | ||
* It requires a minimum length of the balance data to be considered valid. | ||
* This is to avoid false positives when, for example, the list of tokens is still loading, | ||
* and the number of tokens in the balance store is not the same as the number of tokens in the UI. | ||
* | ||
* @param $balancesStore - Certified store of balances. | ||
* @param minLength - Minimum length of the store to be considered valid. | ||
* @returns `true` if all balances are zero and the conditions are met, `false` otherwise. | ||
*/ | ||
|
||
export const checkAllBalancesZero = ({ | ||
$balancesStore, | ||
minLength | ||
}: { | ||
$balancesStore: CertifiedStoreData<BalancesData>; | ||
minLength: number; | ||
}): boolean => | ||
nonNullish($balancesStore) && | ||
Object.getOwnPropertySymbols($balancesStore).length >= Math.max(minLength, 1) && | ||
Object.getOwnPropertySymbols($balancesStore).every((tokenId) => { | ||
const balance: Option<BalancesData> = $balancesStore[tokenId as TokenId]; | ||
|
||
return balance === null || (balance?.data?.isZero() ?? false) || balance?.data === null; | ||
}); |
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