-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
56 lines (55 loc) · 1.64 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const createInlineKeyboard = (
list,
callbackDataPrefix,
eachRowCount,
addBackButton = false,
pagination = null
) => {
const keyboard = [];
let row = [];
if (!!pagination) {
const { pageSize, page } = pagination;
list.slice(pageSize * (page - 1), pageSize * (page - 1) + pageSize).forEach((x) => {
row.push({ text: x, callback_data: `${callbackDataPrefix}${x}` });
if (row.length % eachRowCount === 0) {
keyboard.push(row);
row = [];
}
});
} else {
list.forEach((x) => {
row.push({ text: x, callback_data: `${callbackDataPrefix}${x}` });
if (row.length % eachRowCount === 0) {
keyboard.push(row);
row = [];
}
});
}
if (row.length !== 0) {
keyboard.push(row);
}
if (!!pagination) {
const { page, pageSize } = pagination;
if (list.length > pageSize) {
if (page === 1) {
keyboard.push([
{ text: "Next page >>", callback_data: `${callbackDataPrefix}page-${page + 1}` },
]);
} else if (page * pageSize >= list.length) {
keyboard.push([
{ text: "<< Previous page", callback_data: `${callbackDataPrefix}page-${page - 1}` },
]);
} else if (page > 1 && page * pageSize < list.length) {
keyboard.push([
{ text: "<< Previous page", callback_data: `${callbackDataPrefix}page-${page - 1}` },
{ text: "Next page >>", callback_data: `${callbackDataPrefix}page-${page + 1}` },
]);
}
}
}
if (addBackButton) {
keyboard.push([{ text: "<< back", callback_data: `${callbackDataPrefix}back` }]);
}
return keyboard;
};
export { createInlineKeyboard };