Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.2 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import ListenerSetter from "../helpers/listenerSetter";
import debounce from "../helpers/schedulers/debounce";
import { LangPackKey } from "../lib/langPack";
import InputField, { InputFieldOptions, InputState } from "./inputField";
3 years ago
import isUsernameValid from "../lib/richTextProcessor/isUsernameValid";
import { AppManagers } from "../lib/appManagers/managers";
3 years ago
export class UsernameInputField extends InputField {
private checkUsernamePromise: Promise<any>;
private checkUsernameDebounced: (username: string) => void;
public options: InputFieldOptions & {
3 years ago
peerId?: PeerId,
3 years ago
listenerSetter: ListenerSetter,
onChange?: () => void,
invalidText: LangPackKey,
takenText: LangPackKey,
availableText: LangPackKey,
head?: string
};
3 years ago
constructor(
options: UsernameInputField['options'],
private managers: AppManagers
3 years ago
) {
3 years ago
super(options);
this.checkUsernameDebounced = debounce(this.checkUsername.bind(this), 150, false, true);
options.listenerSetter.add(this.input)('input', () => {
const value = this.getValue();
//console.log('userNameInput:', value);
if(value === this.originalValue || !value.length) {
this.setState(InputState.Neutral, this.options.label);
this.options.onChange && this.options.onChange();
return;
3 years ago
} else if(!isUsernameValid(value)) { // does not check the last underscore
3 years ago
this.setError(this.options.invalidText);
} else {
this.setState(InputState.Neutral);
}
if(this.input.classList.contains('error')) {
this.options.onChange && this.options.onChange();
return;
}
this.checkUsernameDebounced(value);
});
}
public getValue() {
let value = this.value;
if(this.options.head) {
value = value.slice(this.options.head.length);
this.setValueSilently(this.options.head + value);
}
return value;
}
private checkUsername(username: string) {
if(this.checkUsernamePromise) return;
if(this.options.peerId) {
this.checkUsernamePromise = this.managers.appChatsManager.checkUsername(this.options.peerId.toChatId(), username);
3 years ago
} else {
this.checkUsernamePromise = this.managers.appUsersManager.checkUsername(username);
3 years ago
}
this.checkUsernamePromise.then((available) => {
3 years ago
if(this.getValue() !== username) return;
if(available) {
this.setState(InputState.Valid, this.options.availableText);
} else {
this.setError(this.options.takenText);
}
}, (err) => {
if(this.getValue() !== username) return;
switch(err.type) {
case 'USERNAME_INVALID': {
this.setError(this.options.invalidText);
break;
}
}
}).then(() => {
this.checkUsernamePromise = undefined;
this.options.onChange && this.options.onChange();
const value = this.getValue();
3 years ago
if(value !== username && this.isValidToChange() && isUsernameValid(value)) {
3 years ago
this.checkUsername(value);
}
});
};
}