@@ -15,12 +15,12 @@ import { uploadOurAvatar } from '../../interactions/conversationInteractions';
15
15
import { SessionButton , SessionButtonType } from '../basic/SessionButton' ;
16
16
import { SessionSpinner } from '../basic/SessionSpinner' ;
17
17
import { SessionIconButton } from '../icon' ;
18
- import { MAX_USERNAME_LENGTH } from '../registration/RegistrationStages' ;
19
18
import { SessionWrapperModal } from '../SessionWrapperModal' ;
20
19
import { pickFileForAvatar } from '../../types/attachments/VisualAttachment' ;
21
20
import { sanitizeSessionUsername } from '../../session/utils/String' ;
22
21
import { setLastProfileUpdateTimestamp } from '../../util/storage' ;
23
22
import { ConversationTypeEnum } from '../../models/conversationAttributes' ;
23
+ import { MAX_USERNAME_BYTES } from '../../session/constants' ;
24
24
25
25
interface State {
26
26
profileName : string ;
@@ -211,7 +211,7 @@ export class EditProfileDialog extends React.Component<{}, State> {
211
211
value = { this . state . profileName }
212
212
placeholder = { placeholderText }
213
213
onChange = { this . onNameEdited }
214
- maxLength = { MAX_USERNAME_LENGTH }
214
+ maxLength = { MAX_USERNAME_BYTES }
215
215
tabIndex = { 0 }
216
216
required = { true }
217
217
aria-required = { true }
@@ -237,10 +237,18 @@ export class EditProfileDialog extends React.Component<{}, State> {
237
237
}
238
238
239
239
private onNameEdited ( event : ChangeEvent < HTMLInputElement > ) {
240
- const newName = sanitizeSessionUsername ( event . target . value ) ;
241
- this . setState ( {
242
- profileName : newName ,
243
- } ) ;
240
+ const displayName = event . target . value ;
241
+ try {
242
+ const newName = sanitizeSessionUsername ( displayName ) ;
243
+ this . setState ( {
244
+ profileName : newName ,
245
+ } ) ;
246
+ } catch ( e ) {
247
+ this . setState ( {
248
+ profileName : displayName ,
249
+ } ) ;
250
+ ToastUtils . pushToastError ( 'nameTooLong' , window . i18n ( 'displayNameTooLong' ) ) ;
251
+ }
244
252
}
245
253
246
254
private onKeyUp ( event : any ) {
@@ -263,26 +271,37 @@ export class EditProfileDialog extends React.Component<{}, State> {
263
271
*/
264
272
private onClickOK ( ) {
265
273
const { newAvatarObjectUrl, profileName } = this . state ;
266
- const newName = profileName ? profileName . trim ( ) : '' ;
274
+ try {
275
+ const newName = profileName ? profileName . trim ( ) : '' ;
276
+
277
+ if ( newName . length === 0 || newName . length > MAX_USERNAME_BYTES ) {
278
+ return ;
279
+ }
280
+
281
+ // this throw if the length in bytes is too long
282
+ const sanitizedName = sanitizeSessionUsername ( newName ) ;
283
+ const trimName = sanitizedName . trim ( ) ;
284
+
285
+ this . setState (
286
+ {
287
+ profileName : trimName ,
288
+ loading : true ,
289
+ } ,
290
+ async ( ) => {
291
+ await commitProfileEdits ( newName , newAvatarObjectUrl ) ;
292
+ this . setState ( {
293
+ loading : false ,
294
+
295
+ mode : 'default' ,
296
+ updatedProfileName : this . state . profileName ,
297
+ } ) ;
298
+ }
299
+ ) ;
300
+ } catch ( e ) {
301
+ ToastUtils . pushToastError ( 'nameTooLong' , window . i18n ( 'displayNameTooLong' ) ) ;
267
302
268
- if ( newName . length === 0 || newName . length > MAX_USERNAME_LENGTH ) {
269
303
return ;
270
304
}
271
-
272
- this . setState (
273
- {
274
- loading : true ,
275
- } ,
276
- async ( ) => {
277
- await commitProfileEdits ( newName , newAvatarObjectUrl ) ;
278
- this . setState ( {
279
- loading : false ,
280
-
281
- mode : 'default' ,
282
- updatedProfileName : this . state . profileName ,
283
- } ) ;
284
- }
285
- ) ;
286
305
}
287
306
288
307
private closeDialog ( ) {
0 commit comments