Skip to content

Commit

Permalink
chore: replace subject type in all project with an enum (#7525)
Browse files Browse the repository at this point in the history
Co-authored-by: Begoña Álvarez de la Cruz <balvarez@boxfish.studio>
  • Loading branch information
evavirseda and begonaalvarezd authored Oct 10, 2023
1 parent 15a1ca6 commit aec90b1
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 21 deletions.
6 changes: 3 additions & 3 deletions packages/shared/components/SubjectBox.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<script lang="ts">
import { localize } from '@core/i18n'
import { Subject } from '@core/wallet'
import { Subject, SubjectType } from '@core/wallet'
import { Box, AddressBox, Text, AccountLabel, TextType, FontWeight } from 'shared/components'
export let subject: Subject | null = null
</script>

{#if subject?.type === 'account'}
{#if subject?.type === SubjectType.Account}
<Box row clearBackground clearPadding classes="justify-center">
<AccountLabel account={subject?.account} />
</Box>
{:else if subject?.type === 'address'}
{:else if subject?.type === SubjectType.Address}
<AddressBox clearBackground clearPadding isCopyable address={subject?.address} />
{:else}
<Box row clearBackground clearPadding classes="justify-center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
NewTransactionDetails,
NewTransactionType,
Subject,
SubjectType,
getAssetById,
getUnitFromTokenMetadata,
selectedAccountAssets,
Expand Down Expand Up @@ -58,7 +59,7 @@ function parseSendConfirmationOperation(searchParams: URLSearchParams): NewTrans
throw new InvalidAddressError()
}

const recipient: Subject = { type: 'address', address }
const recipient: Subject = { type: SubjectType.Address, address }

const assetId = searchParams.get(SendOperationParameter.AssetId)
assetId && validateAssetId(assetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getAssetById,
NewTransactionType,
getUnitFromTokenMetadata,
SubjectType,
} from '@core/wallet'
import { openPopup, PopupId } from '@auxiliary/popup'
import { get } from 'svelte/store'
Expand Down Expand Up @@ -51,7 +52,7 @@ function parseSendFormOperation(searchParams: URLSearchParams): NewTransactionDe
const rawAmount = getRawAmountFromSearchParam(searchParams)
const metadata = searchParams.get(SendOperationParameter.Metadata)
const tag = searchParams.get(SendOperationParameter.Tag)
const recipient: Subject = address ? { type: 'address', address } : undefined
const recipient: Subject = address ? { type: SubjectType.Address, address } : undefined

return {
type: NewTransactionType.TokenTransfer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
setNewTransactionDetails,
NewTransactionType,
NewTokenTransactionDetails,
SubjectType,
} from '@core/wallet'
import { logAndNotifyError } from '@core/error/actions'

Expand Down Expand Up @@ -66,7 +67,7 @@ async function claimShimmerRewardsForShimmerClaimingAccount(

const newTransactionDetails: NewTokenTransactionDetails = {
recipient: {
type: 'address',
type: SubjectType.Address,
address: recipientAddress,
},
type: NewTransactionType.TokenTransfer,
Expand Down
1 change: 1 addition & 0 deletions packages/shared/lib/core/wallet/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './verified-status.enum'
export * from './return-strategy.enum'
export * from './inclusion-state.enum'
export * from './irc27-version.enum'
export * from './subject.enum'
4 changes: 4 additions & 0 deletions packages/shared/lib/core/wallet/enums/subject.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum SubjectType {
Account = 'account',
Address = 'address',
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IAccountState } from '@core/account'
import { SubjectType } from '../enums'

export interface IAccountSubject {
type: 'account'
type: SubjectType.Account
account: IAccountState
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SubjectType } from '../enums'

export interface IAddressSubject {
type: 'address'
type: SubjectType.Address
address: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isValidIrc30Token } from '@core/token'

import { selectedAccount } from '../../account/stores/selected-account.store'
import { Activity } from '../types/activity.type'
import { ActivityType } from '../enums'
import { ActivityType, SubjectType } from '../enums'
import { ActivityFilter } from '../interfaces/activity-filter.interface'
import { getAssetFromPersistedAssets, getFormattedAmountFromActivity } from '../utils'
import { isVisibleActivity } from '../utils/isVisibleActivity'
Expand Down Expand Up @@ -78,9 +78,9 @@ function getFieldsToSearchFromActivity(activity: Activity): string[] {
fieldsToSearch.push(getFormattedAmountFromActivity(activity, false)?.toLowerCase())
}

if (activity.subject?.type === 'account') {
if (activity.subject?.type === SubjectType.Account) {
fieldsToSearch.push(activity.subject?.account?.name)
} else if (activity.subject?.type === 'address') {
} else if (activity.subject?.type === SubjectType.Address) {
fieldsToSearch.push(activity.subject?.address)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parseLayer2Metadata, getDestinationNetworkFromAddress } from '@core/layer-2/utils'
import { Layer2Metadata } from '@core/layer-2/types'
import { SenderInfo } from '../../../types'
import { SubjectType } from '@core/wallet/enums'

export function getLayer2ActivityInformation(
metadata: string,
Expand All @@ -14,7 +15,7 @@ export function getLayer2ActivityInformation(
try {
parsedLayer2Metadata = parseLayer2Metadata(metadata)
destinationNetwork = getDestinationNetworkFromAddress(
sendingInfo.subject?.type === 'address' ? sendingInfo.subject.address : undefined
sendingInfo.subject?.type === SubjectType.Address ? sendingInfo.subject.address : undefined
)
} catch (_err) {
parsedLayer2Metadata = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { localize } from '@core/i18n'
import { getLayer2NetworkFromAddress } from '@core/layer-2/utils'
import { truncateString } from '@core/utils'
import { ActivityType } from '@core/wallet/enums'
import { ActivityType, SubjectType } from '@core/wallet/enums'
import type { Activity, Subject } from '@core/wallet/types'

export function getSubjectFromActivity(activity: Activity): Subject {
if (activity.parsedLayer2Metadata) {
return {
...activity.subject,
...(activity.subject?.type === 'address' && {
...(activity.subject?.type === SubjectType.Address && {
address: activity.parsedLayer2Metadata?.ethereumAddress,
}),
}
} else if (activity.subject?.type === 'address') {
} else if (activity.subject?.type === SubjectType.Address) {
const network = getLayer2NetworkFromAddress(activity.subject.address)
return { ...activity.subject, address: network ?? activity.subject.address }
} else {
Expand All @@ -27,9 +27,9 @@ export function getSubjectLocaleFromActivity(activity: Activity): string {
return localize('general.shimmerGenesis')
} else if (activity.type === ActivityType.Vesting) {
return localize('general.stardustGenesis')
} else if (subject?.type === 'account') {
} else if (subject?.type === SubjectType.Account) {
return truncateString(subject?.account?.name, 13, 0)
} else if (subject?.type === 'address') {
} else if (subject?.type === SubjectType.Address) {
const address = activity?.parsedLayer2Metadata?.ethereumAddress ?? subject?.address
const network = getLayer2NetworkFromAddress(address)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { findActiveAccountWithAddress } from '@core/profile'
import { Subject } from '../types'
import { SubjectType } from '../enums'

export function getSubjectFromAddress(address: string): Subject {
const account = findActiveAccountWithAddress(address)
if (account) {
return { type: 'account', account: account }
return { type: SubjectType.Account, account: account }
} else {
return { type: 'address', address }
return { type: SubjectType.Address, address }
}
}
3 changes: 2 additions & 1 deletion packages/shared/lib/core/wallet/utils/isSubjectInternal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SubjectType } from '../enums'
import { Subject } from '../types'

export function isSubjectInternal(subject: Subject | undefined): boolean {
return subject?.type === 'account'
return subject?.type === SubjectType.Account
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Subject } from '@core/wallet/types'
import { CommonOutput } from '@iota/sdk/out/types'
import { getSenderFromOutput } from '../outputs/getSenderFromOutput'
import { SubjectType } from '@core/wallet/enums'

export function getSenderFromTransaction(
isIncoming: boolean,
Expand All @@ -10,6 +11,6 @@ export function getSenderFromTransaction(
if (isIncoming) {
return getSenderFromOutput(output)
} else {
return { type: 'address', address: accountAddress }
return { type: SubjectType.Address, address: accountAddress }
}
}

0 comments on commit aec90b1

Please sign in to comment.