tweb/src/components/sidebarRight/tabs/chatType.ts

162 lines
5.7 KiB
TypeScript
Raw Normal View History

2021-04-08 15:52:31 +02:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2021-03-16 16:18:51 +01:00
import { copyTextToClipboard } from "../../../helpers/clipboard";
import { randomLong } from "../../../helpers/random";
import { Chat, ChatFull, ExportedChatInvite } from "../../../layer";
import appChatsManager from "../../../lib/appManagers/appChatsManager";
import appProfileManager from "../../../lib/appManagers/appProfileManager";
import Button from "../../button";
import { setButtonLoader } from "../../misc";
import RadioField from "../../radioField";
import Row, { RadioFormFromRows } from "../../row";
import { SettingSection } from "../../sidebarLeft";
import { toast } from "../../toast";
import { UsernameInputField } from "../../usernameInputField";
2021-03-16 17:50:25 +01:00
import { SliderSuperTabEventable } from "../../sliderTab";
2021-03-28 20:37:11 +02:00
import I18n from "../../../lib/langPack";
import PopupPeer from "../../popups/peer";
2021-05-03 21:17:50 +02:00
import ButtonCorner from "../../buttonCorner";
import { attachClickEvent } from "../../../helpers/dom/clickEvent";
import toggleDisability from "../../../helpers/dom/toggleDisability";
2021-03-16 16:18:51 +01:00
2021-05-05 12:50:32 +02:00
export default class AppChatTypeTab extends SliderSuperTabEventable {
public chatId: number;
2021-03-16 16:18:51 +01:00
public chatFull: ChatFull;
protected init() {
this.container.classList.add('edit-peer-container', 'group-type-container');
2021-05-05 12:50:32 +02:00
const isBroadcast = appChatsManager.isBroadcast(this.chatId);
this.setTitle(isBroadcast ? 'ChannelType' : 'GroupType');
2021-03-16 16:18:51 +01:00
const section = new SettingSection({
2021-05-05 12:50:32 +02:00
name: isBroadcast ? 'ChannelType' : 'GroupType'
2021-03-16 16:18:51 +01:00
});
const random = randomLong();
const privateRow = new Row({
radioField: new RadioField({
2021-05-05 12:50:32 +02:00
langKey: isBroadcast ? 'ChannelPrivate' : 'MegaPrivate',
2021-03-16 16:18:51 +01:00
name: random,
value: 'private'
}),
2021-05-05 12:50:32 +02:00
subtitleLangKey: isBroadcast ? 'ChannelPrivateInfo' : 'MegaPrivateInfo'
2021-03-16 16:18:51 +01:00
});
const publicRow = new Row({
radioField: new RadioField({
2021-05-05 12:50:32 +02:00
langKey: isBroadcast ? 'ChannelPublic' : 'MegaPublic',
2021-03-16 16:18:51 +01:00
name: random,
value: 'public'
}),
2021-05-05 12:50:32 +02:00
subtitleLangKey: isBroadcast ? 'ChannelPublicInfo' : 'MegaPublicInfo'
2021-03-16 16:18:51 +01:00
});
const form = RadioFormFromRows([privateRow, publicRow], (value) => {
const a = [privateSection, publicSection];
if(value === 'public') a.reverse();
a[0].container.classList.remove('hide');
a[1].container.classList.add('hide');
onChange();
});
2021-05-05 12:50:32 +02:00
const chat: Chat = appChatsManager.getChat(this.chatId);
2021-03-16 16:18:51 +01:00
section.content.append(form);
const privateSection = new SettingSection({});
//let revoked = false;
const linkRow = new Row({
title: (this.chatFull.exported_invite as ExportedChatInvite.chatInviteExported).link,
2021-05-05 12:50:32 +02:00
subtitleLangKey: isBroadcast ? 'ChannelPrivateLinkHelp' : 'MegaPrivateLinkHelp',
2021-03-16 16:18:51 +01:00
clickable: () => {
copyTextToClipboard((this.chatFull.exported_invite as ExportedChatInvite.chatInviteExported).link);
2021-03-28 20:37:11 +02:00
toast(I18n.format('LinkCopied', true));
2021-03-16 16:18:51 +01:00
}
});
2021-03-28 20:37:11 +02:00
const btnRevoke = Button('btn-primary btn-transparent danger', {icon: 'delete', text: 'RevokeLink'});
2021-03-16 16:18:51 +01:00
attachClickEvent(btnRevoke, () => {
new PopupPeer('revoke-link', {
buttons: [{
langKey: 'RevokeButton',
callback: () => {
const toggle = toggleDisability([btnRevoke], true);
2021-05-05 12:50:32 +02:00
appProfileManager.getChatInviteLink(this.chatId, true).then(link => {
toggle();
linkRow.title.innerHTML = link;
//revoked = true;
//onChange();
});
}
}],
titleLangKey: 'RevokeLink',
descriptionLangKey: 'RevokeAlert'
2021-03-16 16:18:51 +01:00
}).show();
}, {listenerSetter: this.listenerSetter});
privateSection.content.append(linkRow.container, btnRevoke);
const publicSection = new SettingSection({
2021-05-05 12:50:32 +02:00
caption: isBroadcast ? 'Channel.UsernameAboutChannel' : 'Channel.UsernameAboutGroup',
2021-03-16 16:18:51 +01:00
noDelimiter: true
});
const inputWrapper = document.createElement('div');
inputWrapper.classList.add('input-wrapper');
const placeholder = 't.me/';
const onChange = () => {
const changed = (privateRow.radioField.checked && (originalValue !== placeholder/* || revoked */))
|| (linkInputField.isValid() && linkInputField.input.classList.contains('valid'));
applyBtn.classList.toggle('is-visible', changed);
};
const linkInputField = new UsernameInputField({
2021-03-28 20:37:11 +02:00
label: 'SetUrlPlaceholder',
2021-03-16 16:18:51 +01:00
name: 'group-public-link',
plainText: true,
listenerSetter: this.listenerSetter,
2021-03-28 20:37:11 +02:00
availableText: 'Link.Available',
2021-04-04 17:39:17 +02:00
invalidText: 'Link.Invalid',
2021-03-28 20:37:11 +02:00
takenText: 'Link.Taken',
2021-03-16 16:18:51 +01:00
onChange: onChange,
2021-05-05 12:50:32 +02:00
peerId: -this.chatId,
2021-03-16 16:18:51 +01:00
head: placeholder
});
const originalValue = placeholder + ((chat as Chat.channel).username || '');
inputWrapper.append(linkInputField.container)
publicSection.content.append(inputWrapper);
2021-05-03 21:17:50 +02:00
const applyBtn = ButtonCorner({icon: 'check', className: 'is-visible'});
2021-03-16 16:18:51 +01:00
this.content.append(applyBtn);
attachClickEvent(applyBtn, () => {
2021-03-16 17:50:25 +01:00
/* const unsetLoader = */setButtonLoader(applyBtn);
2021-03-16 16:18:51 +01:00
const username = publicRow.radioField.checked ? linkInputField.getValue() : '';
2021-05-05 12:50:32 +02:00
appChatsManager.migrateChat(this.chatId).then(channelId => {
2021-03-16 16:18:51 +01:00
return appChatsManager.updateUsername(channelId, username);
}).then(() => {
2021-03-16 17:50:25 +01:00
//unsetLoader();
2021-03-16 16:18:51 +01:00
this.close();
});
}, {listenerSetter: this.listenerSetter});
(originalValue !== placeholder ? publicRow : privateRow).radioField.checked = true;
linkInputField.setOriginalValue(originalValue);
this.scrollable.append(section.container, privateSection.container, publicSection.container);
}
}