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