-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathAssetIcon.svelte
153 lines (143 loc) · 5.45 KB
/
AssetIcon.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<script lang="ts">
import { AnimationRenderer } from '@auxiliary/animation'
import { Icon as IconEnum, NETWORK_ICON_SVG } from '@auxiliary/icon'
import { getIconColorFromString } from '@core/account'
import { COIN_TYPE, NetworkId } from '@core/network'
import { isBright } from '@core/utils'
import { ANIMATED_TOKEN_IDS, IPersistedAsset, TokenStandard, getAssetInitials } from '@core/wallet'
import { Animation, AssetIconSize, Icon, VerificationBadge } from 'shared/components'
export let asset: IPersistedAsset
export let size: AssetIconSize = AssetIconSize.Medium
let icon: IconEnum | null
let assetIconColor: string = 'text-blue-500'
let assetIconBackgroundColor: string
let assetInitials: string
let assetIconWrapperWidth: number
let assetLogoUrl: string
const IOTA_NAMES = [NetworkId.Iota, 'tst']
$: isAnimation = asset.id in ANIMATED_TOKEN_IDS
$: asset, updateAssetIcon()
$: assetIconColor = isBright(assetIconBackgroundColor) ? 'text-gray-800' : 'text-white'
function updateAssetIcon(): void {
const assetName = asset?.metadata?.name?.toLowerCase() ?? ''
const assetId = asset?.id
if (
[
String(COIN_TYPE[NetworkId.Iota]),
String(COIN_TYPE[NetworkId.IotaTestnet]),
String(COIN_TYPE[NetworkId.IotaAlphanet]),
String(COIN_TYPE[NetworkId.Shimmer]),
String(COIN_TYPE[NetworkId.ShimmerTestnet]),
].includes(assetId)
) {
// if not a production network, use gray icon
if (
[
String(COIN_TYPE[NetworkId.IotaTestnet]),
String(COIN_TYPE[NetworkId.IotaAlphanet]),
String(COIN_TYPE[NetworkId.ShimmerTestnet]),
].includes(assetId)
) {
assetIconBackgroundColor = '#C4D1E8'
} else if (String(NetworkId.Iota) === assetName) {
assetIconBackgroundColor = '#000000'
} else if (String(NetworkId.Shimmer) === assetName) {
assetIconBackgroundColor = '#25DFCA'
}
if (IOTA_NAMES.includes(assetName)) {
icon = NETWORK_ICON_SVG[NetworkId.Iota]
} else {
icon = NETWORK_ICON_SVG[NetworkId.Shimmer]
}
} else {
assetInitials = getAssetInitials(asset)
assetIconBackgroundColor = getIconColorFromString(asset.metadata?.name, {
shades: ['500', '600', '700', '800'],
colorsToExclude: ['gray'],
})
assetLogoUrl = asset.metadata?.standard === TokenStandard.Irc30 ? asset.metadata?.logoUrl ?? '' : ''
icon = null
}
}
function handleOnError(): void {
assetLogoUrl = ''
}
</script>
<asset-icon-wrapper class:large={AssetIconSize.Large === size} class:small={AssetIconSize.Small === size}>
<asset-icon
class:isAnimation
class:large={AssetIconSize.Large === size}
class:small={AssetIconSize.Small === size}
class:icon-bg={assetIconBackgroundColor}
style={assetIconBackgroundColor ? `--icon-bg-color: ${assetIconBackgroundColor}` : ''}
bind:clientWidth={assetIconWrapperWidth}
>
{#if isAnimation}
<Animation
classes={AssetIconSize.Large === size
? 'w-12 h-12'
: AssetIconSize.Small === size
? 'w-6 h-6'
: 'w-8 h-8'}
animation={ANIMATED_TOKEN_IDS[asset.id]}
loop={true}
renderer={AnimationRenderer.Canvas}
/>
{:else if icon}
<Icon {icon} width="90%" height="90%" classes="{assetIconColor} text-center" />
{:else if assetLogoUrl}
<img src={assetLogoUrl} on:error={handleOnError} alt={assetLogoUrl} class="w-full h-full" />
{:else}
<p
style:--font-size={Math.floor(
Math.min(AssetIconSize.Large === size ? 20 : 12, assetIconWrapperWidth / assetInitials?.length)
) + 'px'}
class={assetIconColor}
>
{assetInitials?.toUpperCase() ?? '-'}
</p>
{/if}
</asset-icon>
<verification-badge-wrapper class="absolute flex justify-center items-center bottom-0 right-0">
<VerificationBadge status={asset.verification?.status} width={14} height={14} />
</verification-badge-wrapper>
</asset-icon-wrapper>
<style lang="scss">
asset-icon-wrapper {
@apply relative;
@apply flex;
@apply w-8 h-8;
&.large {
@apply w-12 h-12;
}
&.small {
@apply w-6 h-6;
}
asset-icon {
@apply p-1;
@apply rounded-full;
@apply flex justify-center items-center;
@apply transition-none;
@apply bg-blue-500;
@apply w-8 h-8;
&.isAnimation {
@apply p-0;
}
&.large {
@apply w-12 h-12;
}
&.small {
@apply w-6 h-6;
}
&.icon-bg {
background-color: var(--icon-bg-color);
}
p {
@apply transition-none;
@apply font-600;
@apply text-center;
font-size: var(--font-size);
}
}
}
</style>