@@ -2,10 +2,14 @@ import { SyntheticEvent, useCallback, useMemo } from 'react'
2
2
import { DropdownProps , Field , InputOnChangeData , SelectField , TextAreaField , TextAreaProps } from 'decentraland-ui'
3
3
import { MappingType , MultipleMapping } from '@dcl/schemas'
4
4
import { t } from 'decentraland-dapps/dist/modules/translation'
5
+ import { LinkedContractProtocol } from 'modules/thirdParty/types'
6
+ import { shorten } from 'lib/address'
5
7
import allIcon from '../../icons/all.svg'
6
8
import multipleIcon from '../../icons/multiple.svg'
7
9
import singleIcon from '../../icons/single.svg'
8
10
import rangeIcon from '../../icons/range.svg'
11
+ import ethereumSvg from '../../icons/ethereum.svg'
12
+ import polygonSvg from '../../icons/polygon.svg'
9
13
import { Props } from './MappingEditor.types'
10
14
import styles from './MappingEditor.module.css'
11
15
@@ -15,9 +19,26 @@ const mappingTypeIcons = {
15
19
[ MappingType . SINGLE ] : singleIcon ,
16
20
[ MappingType . RANGE ] : rangeIcon
17
21
}
22
+ const imgSrcByNetwork = {
23
+ [ LinkedContractProtocol . MAINNET ] : ethereumSvg ,
24
+ [ LinkedContractProtocol . MATIC ] : polygonSvg ,
25
+ [ LinkedContractProtocol . SEPOLIA ] : ethereumSvg ,
26
+ [ LinkedContractProtocol . AMOY ] : polygonSvg
27
+ }
18
28
19
29
export const MappingEditor = ( props : Props ) => {
20
- const { mapping, error, disabled, onChange } = props
30
+ const { mapping, error, disabled, contract, contracts, onChange } = props
31
+ const linkedContractsOptions = useMemo (
32
+ ( ) =>
33
+ contracts . map ( ( contract , index ) => ( {
34
+ value : index ,
35
+ key : index ,
36
+ image : imgSrcByNetwork [ contract . network ] ,
37
+ text : shorten ( contract . address , 14 , 14 )
38
+ } ) ) ,
39
+ [ contracts , imgSrcByNetwork ]
40
+ )
41
+
21
42
const [ mappingType , mappingValue ] = useMemo ( ( ) => {
22
43
switch ( mapping . type ) {
23
44
case MappingType . MULTIPLE :
@@ -42,58 +63,87 @@ export const MappingEditor = (props: Props) => {
42
63
[ ]
43
64
)
44
65
45
- const handleMappingTypeChange = useCallback ( ( _ : SyntheticEvent < HTMLElement , Event > , { value } : DropdownProps ) => {
46
- const mappingType = value as MappingType
47
- switch ( mappingType ) {
48
- case MappingType . ANY :
49
- props . onChange ( { type : mappingType } )
50
- break
51
- case MappingType . MULTIPLE :
52
- props . onChange ( { type : mappingType , ids : [ ] } )
53
- break
54
- case MappingType . SINGLE :
55
- props . onChange ( { type : mappingType , id : '' } )
56
- break
57
- case MappingType . RANGE :
58
- props . onChange ( { type : mappingType , to : '' , from : '' } )
59
- break
60
- }
61
- } , [ ] )
66
+ const handleMappingTypeChange = useCallback (
67
+ ( _ : SyntheticEvent < HTMLElement , Event > , { value } : DropdownProps ) => {
68
+ const mappingType = value as MappingType
69
+ switch ( mappingType ) {
70
+ case MappingType . ANY :
71
+ onChange ( { type : mappingType } , contract )
72
+ break
73
+ case MappingType . MULTIPLE :
74
+ onChange ( { type : mappingType , ids : [ ] } , contract )
75
+ break
76
+ case MappingType . SINGLE :
77
+ onChange ( { type : mappingType , id : '' } , contract )
78
+ break
79
+ case MappingType . RANGE :
80
+ onChange ( { type : mappingType , to : '' , from : '' } , contract )
81
+ break
82
+ }
83
+ } ,
84
+ [ contract , onChange ]
85
+ )
62
86
63
- const handleSingleMappingValueChange = useCallback ( ( _ : React . ChangeEvent < HTMLInputElement > , data : InputOnChangeData ) => {
64
- onChange ( { type : MappingType . SINGLE , id : data . value } )
65
- } , [ ] )
87
+ const handleSingleMappingValueChange = useCallback (
88
+ ( _ : React . ChangeEvent < HTMLInputElement > , data : InputOnChangeData ) => {
89
+ onChange ( { type : MappingType . SINGLE , id : data . value } , contract )
90
+ } ,
91
+ [ contract ]
92
+ )
66
93
67
- const handleMultipleMappingValueChange = useCallback ( ( _ : React . ChangeEvent < HTMLTextAreaElement > , data : TextAreaProps ) => {
68
- const ids =
69
- data . value
70
- ?. toString ( )
71
- . replaceAll ( / [ ^ 0 - 9 , \s ] / g, '' )
72
- . split ( ',' )
73
- . map ( value => value . trim ( ) ) ?? [ ]
94
+ const handleMultipleMappingValueChange = useCallback (
95
+ ( _ : React . ChangeEvent < HTMLTextAreaElement > , data : TextAreaProps ) => {
96
+ const ids =
97
+ data . value
98
+ ?. toString ( )
99
+ . replaceAll ( / [ ^ 0 - 9 , \s ] / g, '' )
100
+ . split ( ',' )
101
+ . map ( value => value . trim ( ) ) ?? [ ]
74
102
75
- onChange ( {
76
- type : MappingType . MULTIPLE ,
77
- ids
78
- } )
79
- } , [ ] )
103
+ onChange (
104
+ {
105
+ type : MappingType . MULTIPLE ,
106
+ ids
107
+ } ,
108
+ contract
109
+ )
110
+ } ,
111
+ [ contract ]
112
+ )
80
113
81
114
const handleFromMappingValueChange = useCallback (
82
115
( _ : React . ChangeEvent < HTMLInputElement > , data : InputOnChangeData ) => {
83
- onChange ( { type : MappingType . RANGE , from : data . value , to : mappingValue . split ( ',' ) [ 1 ] } )
116
+ onChange ( { type : MappingType . RANGE , from : data . value , to : mappingValue . split ( ',' ) [ 1 ] } , contract )
84
117
} ,
85
- [ mappingValue ]
118
+ [ mappingValue , contract ]
86
119
)
87
120
88
121
const handleToMappingValueChange = useCallback (
89
122
( _ : React . ChangeEvent < HTMLInputElement > , data : InputOnChangeData ) => {
90
- onChange ( { type : MappingType . RANGE , from : mappingValue . split ( ',' ) [ 0 ] , to : data . value } )
123
+ onChange ( { type : MappingType . RANGE , from : mappingValue . split ( ',' ) [ 0 ] , to : data . value } , contract )
91
124
} ,
92
- [ mappingValue ]
125
+ [ mappingValue , contract ]
126
+ )
127
+
128
+ const handleLinkedContractChange = useCallback (
129
+ ( _ : SyntheticEvent < HTMLElement , Event > , { value } : DropdownProps ) => {
130
+ onChange ( mapping , contracts [ value as number ] )
131
+ } ,
132
+ [ mapping , contracts ]
93
133
)
94
134
95
135
return (
96
136
< div className = { styles . main } >
137
+ < SelectField
138
+ label = { t ( 'create_linked_wearable_collection_modal.linked_contract_field.label' ) }
139
+ className = { styles . linkedContractSelect }
140
+ disabled = { linkedContractsOptions . length === 0 }
141
+ value = { contract ? contracts . indexOf ( contract ) : undefined }
142
+ options = { linkedContractsOptions }
143
+ search = { false }
144
+ onChange = { handleLinkedContractChange }
145
+ message = { linkedContractsOptions . length === 0 ? t ( 'create_linked_wearable_collection_modal.linked_contract_field.message' ) : '' }
146
+ />
97
147
< SelectField
98
148
label = { t ( 'mapping_editor.mapping_type_label' ) }
99
149
onChange = { handleMappingTypeChange }
0 commit comments