perf: 复用翻页按钮

This commit is contained in:
凌莞 2022-02-20 16:27:24 +08:00
parent c6ba18123f
commit da8b70266f
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
1 changed files with 17 additions and 15 deletions

View File

@ -9,24 +9,26 @@ export default async function createPaginatedInlineSelector(chat: TelegramChat,
let currentPage = 0;
const totalPages = Math.ceil(choices.length / PAGE_SIZE);
let sentMessage: Api.Message;
const buttonPageUp = Button.inline('⏪ 上一页', chat.parent.registerCallback(() => {
currentPage = Math.max(0, currentPage - 1);
sentMessage.edit({
text: message + `\n\n第 ${currentPage + 1} 页,共 ${totalPages}`,
buttons: getButtons(),
});
}));
const buttonPageDown = Button.inline('下一页 ⏩', chat.parent.registerCallback(() => {
currentPage = Math.min(totalPages - 1, currentPage + 1);
console.log(currentPage);
sentMessage.edit({
text: message + `\n\n第 ${currentPage + 1} 页,共 ${totalPages}`,
buttons: getButtons(),
});
}));
const getButtons = () => {
const buttons = arrays.pagination(choices, PAGE_SIZE, currentPage);
const paginateButtons: ButtonLike[] = [];
currentPage > 0 && paginateButtons.push(Button.inline('⏪ 上一页', chat.parent.registerCallback(() => {
currentPage = Math.max(0, currentPage - 1);
sentMessage.edit({
text: message + `\n\n第 ${currentPage + 1} 页,共 ${totalPages}`,
buttons: getButtons(),
});
})));
currentPage !== totalPages - 1 && paginateButtons.push(Button.inline('下一页 ⏩', chat.parent.registerCallback(() => {
currentPage = Math.min(totalPages - 1, currentPage + 1);
console.log(currentPage);
sentMessage.edit({
text: message + `\n\n第 ${currentPage + 1} 页,共 ${totalPages}`,
buttons: getButtons(),
});
})));
currentPage > 0 && paginateButtons.push(buttonPageUp);
currentPage !== totalPages - 1 && paginateButtons.push(buttonPageDown);
paginateButtons.length && buttons.push(paginateButtons);
return buttons;
};