Eduard Kuzmenko
3 years ago
14 changed files with 414 additions and 61 deletions
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
/* |
||||
* https://github.com/morethanwords/tweb
|
||||
* Copyright (C) 2019-2021 Eduard Kuzmenko |
||||
* https://github.com/morethanwords/tweb/blob/master/LICENSE
|
||||
*/ |
||||
|
||||
import { SettingSection } from ".."; |
||||
import appReactionsManager from "../../../lib/appManagers/appReactionsManager"; |
||||
import RadioField from "../../radioField"; |
||||
import Row, { RadioFormFromRows } from "../../row"; |
||||
import SliderSuperTab from "../../sliderTab"; |
||||
import { wrapStickerToRow } from "../../wrappers"; |
||||
|
||||
export default class AppQuickReactionTab extends SliderSuperTab { |
||||
protected init() { |
||||
this.header.classList.add('with-border'); |
||||
this.setTitle('DoubleTapSetting'); |
||||
this.container.classList.add('quick-reaction-container'); |
||||
|
||||
return Promise.all([ |
||||
appReactionsManager.getQuickReaction(), |
||||
appReactionsManager.getAvailableReactions() |
||||
]).then(([quickReaction, availableReactions]) => { |
||||
availableReactions = availableReactions.filter(reaction => !reaction.pFlags.inactive); |
||||
|
||||
const section = new SettingSection(); |
||||
|
||||
const name = 'quick-reaction'; |
||||
const rows = availableReactions.map((availableReaction) => { |
||||
const radioField = new RadioField({ |
||||
name, |
||||
text: availableReaction.title, |
||||
value: availableReaction.reaction, |
||||
alignRight: true |
||||
}); |
||||
|
||||
const row = new Row({ |
||||
radioField, |
||||
havePadding: true |
||||
}); |
||||
|
||||
radioField.main.classList.add('quick-reaction-title'); |
||||
|
||||
wrapStickerToRow({ |
||||
row, |
||||
doc: availableReaction.static_icon, |
||||
size: 'small' |
||||
}); |
||||
|
||||
if(availableReaction === quickReaction) { |
||||
radioField.setValueSilently(true); |
||||
} |
||||
|
||||
return row; |
||||
}); |
||||
|
||||
const form = RadioFormFromRows(rows, (value) => { |
||||
appReactionsManager.setDefaultReaction(value); |
||||
}); |
||||
|
||||
section.content.append(form); |
||||
this.scrollable.append(section.container); |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
/* |
||||
* https://github.com/morethanwords/tweb
|
||||
* Copyright (C) 2019-2021 Eduard Kuzmenko |
||||
* https://github.com/morethanwords/tweb/blob/master/LICENSE
|
||||
*/ |
||||
|
||||
import appChatsManager from "../../../lib/appManagers/appChatsManager"; |
||||
import appProfileManager from "../../../lib/appManagers/appProfileManager"; |
||||
import appReactionsManager from "../../../lib/appManagers/appReactionsManager"; |
||||
import CheckboxField from "../../checkboxField"; |
||||
import Row from "../../row"; |
||||
import { SettingSection } from "../../sidebarLeft"; |
||||
import { SliderSuperTabEventable } from "../../sliderTab"; |
||||
import { wrapStickerToRow } from "../../wrappers"; |
||||
|
||||
export default class AppChatReactionsTab extends SliderSuperTabEventable { |
||||
public chatId: ChatId; |
||||
|
||||
protected async init() { |
||||
this.setTitle('Reactions'); |
||||
|
||||
const availableReactions = await appReactionsManager.getActiveAvailableReactions(); |
||||
const chatFull = await appProfileManager.getChatFull(this.chatId); |
||||
const originalReactions = chatFull.available_reactions ?? []; |
||||
const enabledReactions = new Set(originalReactions); |
||||
|
||||
const toggleSection = new SettingSection({ |
||||
caption: appChatsManager.isBroadcast(this.chatId) ? 'EnableReactionsChannelInfo' : 'EnableReactionsGroupInfo' |
||||
}); |
||||
|
||||
const toggleCheckboxField = new CheckboxField({toggle: true, checked: !!enabledReactions.size}); |
||||
const toggleRow = new Row({ |
||||
checkboxField: toggleCheckboxField, |
||||
titleLangKey: 'EnableReactions' |
||||
}); |
||||
|
||||
toggleSection.content.append(toggleRow.container); |
||||
|
||||
const reactionsSection = new SettingSection({ |
||||
name: 'AvailableReactions' |
||||
}); |
||||
|
||||
const checkboxFields = availableReactions.map(availableReaction => { |
||||
const checkboxField = new CheckboxField({ |
||||
toggle: true, |
||||
checked: enabledReactions.has(availableReaction.reaction) |
||||
}); |
||||
|
||||
this.listenerSetter.add(checkboxField.input)('change', () => { |
||||
if(checkboxField.checked) { |
||||
enabledReactions.add(availableReaction.reaction); |
||||
|
||||
if(!toggleCheckboxField.checked) { |
||||
toggleCheckboxField.setValueSilently(true); |
||||
} |
||||
} else enabledReactions.delete(availableReaction.reaction); |
||||
}); |
||||
|
||||
const row = new Row({ |
||||
checkboxField, |
||||
title: availableReaction.title, |
||||
havePadding: true |
||||
}); |
||||
|
||||
wrapStickerToRow({ |
||||
row, |
||||
doc: availableReaction.static_icon, |
||||
size: 'small' |
||||
}); |
||||
|
||||
reactionsSection.content.append(row.container); |
||||
|
||||
return checkboxField; |
||||
}); |
||||
|
||||
this.listenerSetter.add(toggleRow.checkboxField.input)('change', () => { |
||||
if(!toggleCheckboxField.checked) { |
||||
checkboxFields.forEach(checkboxField => checkboxField.setValueSilently(false)); |
||||
} else if(checkboxFields.every(checkboxField => !checkboxField.checked)) { |
||||
checkboxFields.forEach(checkboxField => checkboxField.setValueSilently(true)); |
||||
} |
||||
}); |
||||
|
||||
this.eventListener.addEventListener('destroy', () => { |
||||
const newReactions = Array.from(enabledReactions); |
||||
if([...newReactions].sort().join() === [...originalReactions].sort().join()) { |
||||
return; |
||||
} |
||||
|
||||
const chatFull = appProfileManager.getCachedFullChat(this.chatId); |
||||
if(chatFull) { |
||||
chatFull.available_reactions = newReactions; |
||||
} |
||||
|
||||
appChatsManager.setChatAvailableReactions(this.chatId, newReactions); |
||||
}, {once: true}); |
||||
|
||||
this.scrollable.append(toggleSection.container, reactionsSection.container); |
||||
} |
||||
} |
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
import {Awaited} from '../types'; |
||||
|
||||
export default function callbackify<T extends Awaited<any>, R extends any>(smth: T, callback: (result: Awaited<T>) => R): PromiseLike<R> | R { |
||||
if(smth instanceof Promise) { |
||||
return smth.then(callback); |
||||
} else { |
||||
return callback(smth as any); |
||||
} |
||||
} |
Loading…
Reference in new issue