Skip to content

Commit

Permalink
Sort addresses by index
Browse files Browse the repository at this point in the history
Closes #1120
  • Loading branch information
nop33 committed Feb 27, 2025
1 parent a24e657 commit a31dbb4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/desktop-wallet/locales/en-US/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Address details": "Address details",
"Address group": "Address group",
"Address label": "Address label",
"Address index": "Address index",
"Address NFTs": "Address NFTs",
"Address options": "Address options",
"Address tokens": "Address tokens",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const LockTimes = {
export enum AddressOrder {
LastUse = 'lastUse',
AlphBalance = 'alphBalance',
Label = 'label'
Label = 'label',
Index = 'index'
}

export const locktimeInMinutes = Object.values(LockTimes)
Expand Down
33 changes: 33 additions & 0 deletions apps/desktop-wallet/src/hooks/useAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TokenId } from '@/types/tokens'
export const useFetchAddressesHashesSortedByPreference = () => {
const orderPreference = useAppSelector((s) => s.settings.addressOrderPreference)
const addressesSortedByLabel = useAddressesHashesSortedByLabel()
const addressesSortedByIndex = useAddressesHashesSortedByIndex()
const { data: addressesSortedByLastUse, isLoading: isLoadingLastUse } = useFetchAddressesHashesSortedByLastUse()
const { data: addressesSortedByAlphBalance, isLoading: isLoadingAlphBalance } =
useFetchAddressesHashesSortedByAlphBalance({
Expand All @@ -33,6 +34,11 @@ export const useFetchAddressesHashesSortedByPreference = () => {
data: addressesSortedByAlphBalance,
isLoading: isLoadingAlphBalance
}
case AddressOrder.Index:
return {
data: addressesSortedByIndex,
isLoading: false
}
default:
return {
data: addressesSortedByLastUse,
Expand Down Expand Up @@ -153,3 +159,30 @@ export const useAddressesHashesSortedByLabel = (): AddressHash[] => {
[allAddressHashes, addressEntities, defaultAddressHash]
)
}

export const useAddressesHashesSortedByIndex = (): AddressHash[] => {
const allAddressHashes = useUnsortedAddressesHashes()
const defaultAddressHash = useAppSelector((s) => selectDefaultAddress(s).hash)
const addressEntities = useAppSelector((s) => s.addresses.entities)

return useMemo(
() =>
[...allAddressHashes].sort((a, b) => {
if (a === defaultAddressHash) return -1
if (b === defaultAddressHash) return 1

const indexA = addressEntities[a]?.index
const indexB = addressEntities[b]?.index

if (indexA && !indexB) return -1
if (!indexA && indexB) return 1

if (indexA && indexB) {
return indexA > indexB ? 1 : -1
}

return a.localeCompare(b)
}),
[allAddressHashes, addressEntities, defaultAddressHash]
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const AddressSortingSelect = () => {
const orderOptions: SelectOption<AddressOrder>[] = [
{ value: AddressOrder.LastUse, label: t('Last used') },
{ value: AddressOrder.AlphBalance, label: t('ALPH balance') },
{ value: AddressOrder.Label, label: t('Address label') }
{ value: AddressOrder.Label, label: t('Address label') },
{ value: AddressOrder.Index, label: t('Address index') }
]

const onSelect = (value: AddressOrder) => {
Expand Down

0 comments on commit a31dbb4

Please sign in to comment.