|
| 1 | +import { |
| 2 | + computed, |
| 3 | + ref, |
| 4 | + watch, |
| 5 | +} from 'vue'; |
| 6 | + |
| 7 | +import type { |
| 8 | + IAccountSelectorEntry, |
| 9 | + ITransaction, |
| 10 | +} from '@/types'; |
| 11 | +import { |
| 12 | + ACCOUNT_SELECT_TYPE_FILTER, |
| 13 | + AccountSelectTypeFilter, |
| 14 | + PROTOCOLS, |
| 15 | + TX_DIRECTION, |
| 16 | +} from '@/constants'; |
| 17 | +import { |
| 18 | + createCustomScopedComposable, |
| 19 | + excludeFalsy, |
| 20 | + getDefaultAccountLabel, |
| 21 | +} from '@/utils'; |
| 22 | +import { |
| 23 | + useAccounts, |
| 24 | + useAddressBook, |
| 25 | + useLatestTransactionList, |
| 26 | +} from '@/composables'; |
| 27 | + |
| 28 | +import { useAeNames } from '@/protocols/aeternity/composables/aeNames'; |
| 29 | +import { |
| 30 | + getInnerTransaction, |
| 31 | + getOwnershipStatus, |
| 32 | + getTxDirection, |
| 33 | + getTxOwnerAddress, |
| 34 | +} from '@/protocols/aeternity/helpers'; |
| 35 | + |
| 36 | +import { tg } from '@/popup/plugins/i18n'; |
| 37 | +import { AE_TRANSACTION_OWNERSHIP_STATUS } from '@/protocols/aeternity/config'; |
| 38 | +import { Encoding, isAddressValid } from '@aeternity/aepp-sdk'; |
| 39 | + |
| 40 | +export const useAccountSelector = createCustomScopedComposable(() => { |
| 41 | + const { getName, ownedNames } = useAeNames(); |
| 42 | + |
| 43 | + const searchQuery = ref(''); |
| 44 | + const { |
| 45 | + addressBookFiltered, |
| 46 | + addressBookFilteredByProtocol, |
| 47 | + protocolFilter, |
| 48 | + showBookmarked, |
| 49 | + getAddressBookEntryByAddress, |
| 50 | + setProtocolFilter, |
| 51 | + setShowBookmarked, |
| 52 | + clearFilters: addressBookClearFilters, |
| 53 | + } = useAddressBook(); |
| 54 | + const { |
| 55 | + activeAccount, |
| 56 | + accounts, |
| 57 | + accountsGroupedByProtocol, |
| 58 | + getAccountByAddress, |
| 59 | + } = useAccounts(); |
| 60 | + const { accountsTransactionsLatest } = useLatestTransactionList(); |
| 61 | + const accountSelectType = ref<AccountSelectTypeFilter>(ACCOUNT_SELECT_TYPE_FILTER.addressBook); |
| 62 | + |
| 63 | + const latestTransactions = computed( |
| 64 | + () => (accountsTransactionsLatest.value[activeAccount.value.address] || []) |
| 65 | + .map((transaction: ITransaction): IAccountSelectorEntry | null => { |
| 66 | + const outerTx = transaction.tx!; |
| 67 | + const innerTx = transaction.tx ? getInnerTransaction(transaction.tx) : null; |
| 68 | + const { recipientId } = outerTx?.payerId ? outerTx : innerTx; |
| 69 | + const direction = getTxDirection( |
| 70 | + outerTx?.payerId ? outerTx : innerTx, |
| 71 | + ( |
| 72 | + getOwnershipStatus(activeAccount.value, accounts.value, innerTx) |
| 73 | + !== AE_TRANSACTION_OWNERSHIP_STATUS.current |
| 74 | + && getTxOwnerAddress(innerTx) |
| 75 | + ) || activeAccount.value.address, |
| 76 | + ); |
| 77 | + if (direction !== TX_DIRECTION.sent || !recipientId) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + const addressBookRecord = getAddressBookEntryByAddress(recipientId); |
| 82 | + if (addressBookRecord) { |
| 83 | + return addressBookRecord; |
| 84 | + } |
| 85 | + |
| 86 | + let name = tg('modals.send.recipientLabel'); |
| 87 | + let localChainNameAddress: string | undefined; |
| 88 | + const isSentToChainName = isAddressValid(recipientId, Encoding.Name); |
| 89 | + |
| 90 | + if (isSentToChainName) { |
| 91 | + const ownedName = ownedNames.value.find((entry) => entry.hash === recipientId); |
| 92 | + if (ownedName) { |
| 93 | + name = ownedName.name; |
| 94 | + localChainNameAddress = ownedName.pointers.accountPubkey; |
| 95 | + } |
| 96 | + } else { |
| 97 | + const account = getAccountByAddress(recipientId!); |
| 98 | + if (account) { |
| 99 | + name = getName(account.address).value || getDefaultAccountLabel(account); |
| 100 | + } else { |
| 101 | + name = getName(recipientId).value || name; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return { |
| 106 | + name, |
| 107 | + address: isSentToChainName ? localChainNameAddress : recipientId, |
| 108 | + nameAddress: (localChainNameAddress || !isSentToChainName) ? undefined : recipientId, |
| 109 | + isBookmarked: false, |
| 110 | + protocol: protocolFilter.value ?? PROTOCOLS.aeternity, |
| 111 | + }; |
| 112 | + }) |
| 113 | + .filter(excludeFalsy), |
| 114 | + ); |
| 115 | + const ownAddresses = computed( |
| 116 | + () => (protocolFilter.value) |
| 117 | + ? (accountsGroupedByProtocol.value[protocolFilter.value] ?? []).map((account) => ( |
| 118 | + { |
| 119 | + name: getName(account.address).value || getDefaultAccountLabel(account), |
| 120 | + address: account.address, |
| 121 | + isBookmarked: false, |
| 122 | + protocol: protocolFilter.value ?? PROTOCOLS.aeternity, |
| 123 | + type: account.type, |
| 124 | + } |
| 125 | + )) |
| 126 | + : [], |
| 127 | + ); |
| 128 | + const accountsFilteredByType = computed(() => { |
| 129 | + switch (accountSelectType.value) { |
| 130 | + case ACCOUNT_SELECT_TYPE_FILTER.bookmarked: |
| 131 | + case ACCOUNT_SELECT_TYPE_FILTER.addressBook: |
| 132 | + return addressBookFiltered.value; |
| 133 | + case ACCOUNT_SELECT_TYPE_FILTER.recent: |
| 134 | + return latestTransactions.value; |
| 135 | + case ACCOUNT_SELECT_TYPE_FILTER.owned: |
| 136 | + return ownAddresses.value; |
| 137 | + case ACCOUNT_SELECT_TYPE_FILTER.all: |
| 138 | + return [...addressBookFiltered.value, ...ownAddresses.value]; |
| 139 | + default: |
| 140 | + return []; |
| 141 | + } |
| 142 | + }); |
| 143 | + const accountsFiltered = computed(() => { |
| 144 | + const entries: IAccountSelectorEntry[] = accountsFilteredByType.value ?? []; |
| 145 | + const searchQueryLower = searchQuery.value.toLowerCase(); |
| 146 | + |
| 147 | + return entries |
| 148 | + .filter( // Filter by searchQuery |
| 149 | + ({ name, address }) => [name, address].some( |
| 150 | + (val) => val.toLowerCase().includes(searchQueryLower), |
| 151 | + ), |
| 152 | + ).filter( // Remove duplicates |
| 153 | + (entry, index, self) => self.findIndex( |
| 154 | + (e) => ( |
| 155 | + (e.address !== undefined && e.address === entry.address) |
| 156 | + || (e.address === undefined && e.nameAddress === entry.nameAddress) |
| 157 | + ), |
| 158 | + ) === index, |
| 159 | + ).map( // Add flag for own addresses |
| 160 | + (entry) => ({ |
| 161 | + ...entry, |
| 162 | + isOwnAddress: ownAddresses.value.some( |
| 163 | + (account) => account.address === entry.address, |
| 164 | + ), |
| 165 | + }), |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + function setAccountSelectType(type: AccountSelectTypeFilter, resetProtocolFilter = false) { |
| 170 | + accountSelectType.value = type; |
| 171 | + setShowBookmarked(type === ACCOUNT_SELECT_TYPE_FILTER.bookmarked, resetProtocolFilter); |
| 172 | + } |
| 173 | + function clearFilters(resetProtocolFilter = false) { |
| 174 | + accountSelectType.value = ACCOUNT_SELECT_TYPE_FILTER.addressBook; |
| 175 | + addressBookClearFilters(resetProtocolFilter); |
| 176 | + } |
| 177 | + |
| 178 | + watch( |
| 179 | + protocolFilter, |
| 180 | + () => { |
| 181 | + if (protocolFilter.value) { |
| 182 | + setAccountSelectType(ACCOUNT_SELECT_TYPE_FILTER.addressBook); |
| 183 | + } |
| 184 | + }, |
| 185 | + ); |
| 186 | + |
| 187 | + // Storing the previous type in order to revert to it when the input is cleared |
| 188 | + let savedPrevAccountSelectType = false; |
| 189 | + let prevAccountSelectType = accountSelectType.value; |
| 190 | + watch(searchQuery, (newSearch) => { |
| 191 | + if (newSearch !== '' && !savedPrevAccountSelectType) { |
| 192 | + savedPrevAccountSelectType = true; |
| 193 | + prevAccountSelectType = accountSelectType.value; |
| 194 | + accountSelectType.value = ACCOUNT_SELECT_TYPE_FILTER.all; |
| 195 | + } else if (newSearch === '') { |
| 196 | + savedPrevAccountSelectType = false; |
| 197 | + accountSelectType.value = prevAccountSelectType; |
| 198 | + } |
| 199 | + }); |
| 200 | + |
| 201 | + return { |
| 202 | + accountSelectType, |
| 203 | + accountsFiltered, |
| 204 | + addressBookFilteredByProtocol, |
| 205 | + protocolFilter, |
| 206 | + showBookmarked, |
| 207 | + searchQuery, |
| 208 | + |
| 209 | + setAccountSelectType, |
| 210 | + setProtocolFilter, |
| 211 | + setShowBookmarked, |
| 212 | + clearFilters, |
| 213 | + }; |
| 214 | +}); |
0 commit comments