-
Notifications
You must be signed in to change notification settings - Fork 38
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
Alexandros Kalogerakis
committed
Nov 5, 2024
1 parent
f0a96b3
commit 94c8d67
Showing
14 changed files
with
426 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
import { useAddressBook } from '@/composables/addressBook'; | ||
import { computed, ref, watch } from 'vue'; | ||
|
||
import { | ||
IAddressBookEntry, | ||
type ICommonTransaction, | ||
type ITransaction, | ||
} from '@/types'; | ||
import { | ||
ACCOUNT_SELECT_TYPE_FILTER, | ||
AccountSelectTypeFilter, | ||
PROTOCOLS, | ||
TX_DIRECTION, | ||
} from '@/constants'; | ||
import { createCustomScopedComposable, getDefaultAccountLabel, pipe } from '@/utils'; | ||
import { useAccounts, useLatestTransactionList } from '@/composables'; | ||
|
||
import { useAeMiddleware } from '@/protocols/aeternity/composables'; | ||
import { useAeNames } from '@/protocols/aeternity/composables/aeNames'; | ||
import { | ||
getInnerTransaction, | ||
getOwnershipStatus, | ||
getTxDirection, | ||
getTxOwnerAddress, | ||
} from '@/protocols/aeternity/helpers'; | ||
import { AE_TRANSACTION_OWNERSHIP_STATUS } from '@/protocols/aeternity/config'; | ||
|
||
export const useAccountSelector = createCustomScopedComposable(() => { | ||
const { getName } = useAeNames(); | ||
const { getMiddleware } = useAeMiddleware(); | ||
const latestTransactions = ref<IAddressBookEntry[]>([]); | ||
const searchQuery = ref<string>(''); | ||
const { | ||
addressBookFiltered, | ||
addressBookFilteredByProtocol, | ||
protocolFilter, | ||
showBookmarked, | ||
getAddressBookEntryByAddress, | ||
setProtocolFilter, | ||
setShowBookmarked, | ||
clearFilters: addressBookClearFilters, | ||
} = useAddressBook(); | ||
const { | ||
accounts, | ||
activeAccount, | ||
accountsGroupedByProtocol, | ||
getAccountByAddress, | ||
} = useAccounts(); | ||
const { accountsTransactionsLatest } = useLatestTransactionList(); | ||
const accountSelectType = ref<AccountSelectTypeFilter>(ACCOUNT_SELECT_TYPE_FILTER.addressBook); | ||
const prevAccountSelectType = ref<AccountSelectTypeFilter>(accountSelectType.value); | ||
const ownAddresses = computed(() => { | ||
if (protocolFilter.value) { | ||
return accountsGroupedByProtocol.value[protocolFilter.value]?.map((account) => ( | ||
{ | ||
name: getName(account.address).value || getDefaultAccountLabel(account), | ||
address: account.address, | ||
isBookmarked: false, | ||
protocol: protocolFilter.value ?? PROTOCOLS.aeternity, | ||
isOwnAddress: true, | ||
type: account.type, | ||
} | ||
)); | ||
} | ||
return []; | ||
}); | ||
const accountsFilteredByType = computed( | ||
() => { | ||
switch (accountSelectType.value) { | ||
case ACCOUNT_SELECT_TYPE_FILTER.bookmarked: | ||
return addressBookFiltered.value; | ||
case ACCOUNT_SELECT_TYPE_FILTER.addressBook: | ||
return addressBookFiltered.value; | ||
case ACCOUNT_SELECT_TYPE_FILTER.recent: | ||
return latestTransactions.value; | ||
case ACCOUNT_SELECT_TYPE_FILTER.owned: | ||
return ownAddresses.value; | ||
case ACCOUNT_SELECT_TYPE_FILTER.all: | ||
return [...(addressBookFiltered.value ?? []), ...(ownAddresses.value ?? [])]; | ||
default: | ||
return []; | ||
} | ||
}, | ||
); | ||
function filterAccountsBookBySearchPhrase(entries: IAddressBookEntry[]) { | ||
const searchQueryLower = searchQuery.value.toLowerCase(); | ||
return entries.filter(({ name, address }) => ( | ||
[name, address].some((val) => val.toLowerCase().includes(searchQueryLower)) | ||
)); | ||
} | ||
function sortAccounts(entries: IAddressBookEntry[]) { | ||
return entries.sort((a, b) => { | ||
if (a === b) return 0; | ||
return a ? 1 : -1; | ||
}); | ||
} | ||
function removeDuplicates(entries: IAddressBookEntry[]) { | ||
return entries.filter( | ||
(addr1, i, addresses) => addresses.findIndex( | ||
(addr2) => addr2.address === addr1.address, | ||
) === i, | ||
); | ||
} | ||
const accountsFiltered = computed( | ||
() => pipe([ | ||
filterAccountsBookBySearchPhrase, | ||
sortAccounts, | ||
removeDuplicates, | ||
])(accountsFilteredByType.value ?? []), | ||
); | ||
|
||
function setAccountSelectType(type: AccountSelectTypeFilter, resetProtocolFilter = false) { | ||
accountSelectType.value = type; | ||
setShowBookmarked(type === ACCOUNT_SELECT_TYPE_FILTER.bookmarked, resetProtocolFilter); | ||
} | ||
function clearFilters(resetProtocolFilter = false) { | ||
accountSelectType.value = ACCOUNT_SELECT_TYPE_FILTER.addressBook; | ||
addressBookClearFilters(resetProtocolFilter); | ||
} | ||
|
||
watch( | ||
() => [protocolFilter.value], | ||
async () => { | ||
if (protocolFilter.value) { | ||
setAccountSelectType(ACCOUNT_SELECT_TYPE_FILTER.addressBook); | ||
} | ||
}, | ||
); | ||
watch( | ||
() => [accountsTransactionsLatest.value, activeAccount.value.address], | ||
async () => { | ||
const filteredTransactions = (accountsTransactionsLatest | ||
.value[activeAccount.value.address] || []).filter( | ||
(transaction: ICommonTransaction) => { | ||
const outerTx = transaction.tx!; | ||
const innerTx = transaction.tx ? getInnerTransaction(transaction.tx) : null; | ||
const txOwnerAddress = getTxOwnerAddress(innerTx); | ||
|
||
const direction = getTxDirection( | ||
outerTx?.payerId ? outerTx : innerTx, | ||
(transaction as ITransaction).transactionOwner | ||
|| ( | ||
( | ||
getOwnershipStatus(activeAccount.value, accounts.value, innerTx) | ||
!== AE_TRANSACTION_OWNERSHIP_STATUS.current | ||
) | ||
&& txOwnerAddress | ||
) | ||
|| activeAccount.value.address, | ||
); | ||
|
||
return ( | ||
direction === TX_DIRECTION.sent | ||
&& (outerTx?.payerId ? outerTx : innerTx).recipientId | ||
); | ||
}, | ||
); | ||
|
||
latestTransactions.value = await Promise.all( | ||
filteredTransactions | ||
.map(async (transaction: ICommonTransaction): Promise<IAddressBookEntry> => { | ||
const outerTx = transaction.tx!; | ||
const innerTx = transaction.tx ? getInnerTransaction(transaction.tx) : null; | ||
const { recipientId } = outerTx?.payerId ? outerTx : innerTx; | ||
const middleware = await getMiddleware(); | ||
|
||
let address = recipientId; | ||
const accountFound = getAccountByAddress(recipientId!); | ||
let name = getName(accountFound?.address).value | ||
|| getDefaultAccountLabel(accountFound); | ||
if (recipientId?.startsWith('nm_')) { | ||
address = (await middleware.getName(recipientId)).name; | ||
name = address; | ||
} | ||
const addressBookEntryByAddress = getAddressBookEntryByAddress(address); | ||
if (addressBookEntryByAddress) { | ||
return addressBookEntryByAddress; | ||
} | ||
return { | ||
name, | ||
address, | ||
isBookmarked: false, | ||
protocol: protocolFilter.value ?? PROTOCOLS.aeternity, | ||
}; | ||
}), | ||
); | ||
}, | ||
{ immediate: true }, // Run immediately on initialization | ||
); | ||
|
||
// Storing the previous type in order to revert to it when the input is cleared | ||
let savedPrevAccountSelectType = false; | ||
watch(searchQuery, (newSearch) => { | ||
if (newSearch !== '' && !savedPrevAccountSelectType) { | ||
savedPrevAccountSelectType = true; | ||
prevAccountSelectType.value = accountSelectType.value; | ||
accountSelectType.value = ACCOUNT_SELECT_TYPE_FILTER.all; | ||
} else if (newSearch === '') { | ||
savedPrevAccountSelectType = false; | ||
accountSelectType.value = prevAccountSelectType.value; | ||
} | ||
}); | ||
|
||
return { | ||
accountSelectType, | ||
accountsFiltered, | ||
addressBookFilteredByProtocol, | ||
protocolFilter, | ||
showBookmarked, | ||
searchQuery, | ||
|
||
setAccountSelectType, | ||
setProtocolFilter, | ||
setShowBookmarked, | ||
clearFilters, | ||
}; | ||
}); |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.