|
1 |
| -import { ethers } from 'ethers'; |
2 |
| -import { |
3 |
| - AccountsType, |
4 |
| - Actions, |
5 |
| - CacheType, |
6 |
| - GlobalState, |
7 |
| - LeftViewSelected, |
8 |
| - ModalStateType, |
9 |
| - RightViewSelected, |
10 |
| - UiViewStateType, |
11 |
| - UserDbType, |
12 |
| -} from '../../utils/enum-type-utils'; |
13 |
| -import { |
14 |
| - formatAddress, |
15 |
| - getAccountDisplayName, |
16 |
| - normalizeEnsName, |
17 |
| -} from '@dm3-org/dm3-lib-profile'; |
18 |
| -import { closeLoader, startLoader } from '../Loader/Loader'; |
19 |
| -import { ContactPreview, NewContact } from '../../interfaces/utils'; |
20 |
| -import humanIcon from '../../assets/images/human.svg'; |
21 |
| - |
22 | 1 | // class for input field
|
23 | 2 | export const INPUT_FIELD_CLASS =
|
24 | 3 | 'conversation-name font-weight-400 font-size-14 border-radius-6 w-100 line-height-24';
|
@@ -47,164 +26,3 @@ export const closeConversationModal = (
|
47 | 26 | resetName();
|
48 | 27 | resetInputFieldClass();
|
49 | 28 | };
|
50 |
| - |
51 |
| -// method to add new conversation in contacts |
52 |
| -export const addContact = async ( |
53 |
| - state: GlobalState, |
54 |
| - name: string, |
55 |
| - ethAddress: string, |
56 |
| - dispatch: React.Dispatch<Actions>, |
57 |
| - resetName: Function, |
58 |
| - showErrorMessage: Function, |
59 |
| - resetInputFieldClass: Function, |
60 |
| -) => { |
61 |
| - // start loader |
62 |
| - dispatch({ |
63 |
| - type: ModalStateType.LoaderContent, |
64 |
| - payload: 'Adding contact...', |
65 |
| - }); |
66 |
| - |
67 |
| - startLoader(); |
68 |
| - |
69 |
| - // check if its a valid address or a valid name |
70 |
| - const { normalizedAccountName, check } = await isEnsNameValid(name, state); |
71 |
| - |
72 |
| - if ( |
73 |
| - ethAddress && |
74 |
| - name.split('.')[0] && |
75 |
| - ethAddress.toLowerCase() === name.split('.')[0].toLowerCase() |
76 |
| - ) { |
77 |
| - showErrorMessage(true, 'Invalid ENS name'); |
78 |
| - closeLoader(); |
79 |
| - return; |
80 |
| - } |
81 |
| - |
82 |
| - if (!check) { |
83 |
| - showErrorMessage(true, 'Invalid ENS name'); |
84 |
| - closeLoader(); |
85 |
| - return; |
86 |
| - } |
87 |
| - |
88 |
| - { |
89 |
| - // update states |
90 |
| - updateStates( |
91 |
| - state, |
92 |
| - dispatch, |
93 |
| - resetName, |
94 |
| - showErrorMessage, |
95 |
| - resetInputFieldClass, |
96 |
| - normalizedAccountName, |
97 |
| - ); |
98 |
| - |
99 |
| - // create empty conversation |
100 |
| - dispatch({ |
101 |
| - type: UserDbType.createEmptyConversation, |
102 |
| - payload: normalizedAccountName, |
103 |
| - }); |
104 |
| - |
105 |
| - closeLoader(); |
106 |
| - } |
107 |
| -}; |
108 |
| - |
109 |
| -// updates states and closes modal |
110 |
| -const updateStates = ( |
111 |
| - state: GlobalState, |
112 |
| - dispatch: React.Dispatch<Actions>, |
113 |
| - resetName: Function, |
114 |
| - showErrorMessage: Function, |
115 |
| - resetInputFieldClass: Function, |
116 |
| - normalizedAccountName: string, |
117 |
| -) => { |
118 |
| - // add new contact item in the list |
119 |
| - const newContact: ContactPreview = { |
120 |
| - name: getAccountDisplayName(normalizedAccountName, 25), |
121 |
| - message: null, |
122 |
| - image: humanIcon, |
123 |
| - unreadMsgCount: 0, |
124 |
| - contactDetails: { |
125 |
| - account: { |
126 |
| - ensName: normalizedAccountName, |
127 |
| - profile: undefined, |
128 |
| - profileSignature: undefined, |
129 |
| - }, |
130 |
| - deliveryServiceProfile: undefined, |
131 |
| - }, |
132 |
| - isHidden: false, |
133 |
| - }; |
134 |
| - |
135 |
| - // close the modal |
136 |
| - closeConversationModal(resetName, showErrorMessage, resetInputFieldClass); |
137 |
| - |
138 |
| - const addConversationData: NewContact = { |
139 |
| - active: true, |
140 |
| - ensName: normalizedAccountName, |
141 |
| - processed: false, |
142 |
| - }; |
143 |
| - |
144 |
| - // set new contact data |
145 |
| - dispatch({ |
146 |
| - type: ModalStateType.AddConversationData, |
147 |
| - payload: addConversationData, |
148 |
| - }); |
149 |
| - |
150 |
| - // set left view to contacts |
151 |
| - dispatch({ |
152 |
| - type: UiViewStateType.SetSelectedLeftView, |
153 |
| - payload: LeftViewSelected.Contacts, |
154 |
| - }); |
155 |
| - |
156 |
| - // set right view to chat |
157 |
| - dispatch({ |
158 |
| - type: UiViewStateType.SetSelectedRightView, |
159 |
| - payload: RightViewSelected.Chat, |
160 |
| - }); |
161 |
| -}; |
162 |
| - |
163 |
| -// updates hidden contact states |
164 |
| -const updateHiddenContact = ( |
165 |
| - state: GlobalState, |
166 |
| - dispatch: React.Dispatch<Actions>, |
167 |
| - normalizedAccountName: string, |
168 |
| - aka: string | undefined, |
169 |
| -) => { |
170 |
| - if (!aka) { |
171 |
| - // unhide the contact |
172 |
| - dispatch({ |
173 |
| - type: UserDbType.unhideContact, |
174 |
| - payload: normalizedAccountName, |
175 |
| - }); |
176 |
| - } else { |
177 |
| - // set contact selected |
178 |
| - dispatch({ |
179 |
| - type: AccountsType.SetSelectedContact, |
180 |
| - payload: state.accounts.contacts?.find( |
181 |
| - (contact) => contact.account.ensName === aka, |
182 |
| - ), |
183 |
| - }); |
184 |
| - } |
185 |
| -}; |
186 |
| - |
187 |
| -// check ENS name is valid or not |
188 |
| -const isEnsNameValid = async (name: string, state: GlobalState) => { |
189 |
| - let normalizedAccountName: string; |
190 |
| - |
191 |
| - // check if it is valid address |
192 |
| - if ( |
193 |
| - ethers.utils.isAddress(name) && |
194 |
| - process.env.REACT_APP_ADDR_ENS_SUBDOMAIN |
195 |
| - ) { |
196 |
| - normalizedAccountName = normalizeEnsName( |
197 |
| - formatAddress(name) + process.env.REACT_APP_ADDR_ENS_SUBDOMAIN, |
198 |
| - ); |
199 |
| - return { |
200 |
| - normalizedAccountName: normalizedAccountName, |
201 |
| - check: true, |
202 |
| - }; |
203 |
| - } else { |
204 |
| - normalizedAccountName = normalizeEnsName(name); |
205 |
| - return { |
206 |
| - normalizedAccountName: normalizedAccountName, |
207 |
| - check: true, |
208 |
| - }; |
209 |
| - } |
210 |
| -}; |
0 commit comments