refactor: ♻️ minor refactor on inline keyboards

main
drendog 2024-01-23 17:57:36 +01:00
parent e4af51d08e
commit af0c5c3eee
8 changed files with 36 additions and 38 deletions

View File

@ -2,6 +2,7 @@ export { setupCallbackQueries } from "./setupCallbackQueries.ts";
export { voteCallback } from "./voteCallback.ts";
export enum CallbackQueryEnum {
NOOP = "noop",
APPROVE = "approve",
REJECT = "reject",
}

View File

@ -1,23 +1,28 @@
import { Bot } from "grammy";
import { BotContext } from "../mod.ts";
import { IButtonCallbackData } from "../keyboards/mod.ts";
import { CallbackQueryEnum, voteCallback } from "./mod.ts";
import { parseCallbackQueryData } from "./utils.ts";
export const setupCallbackQueries = (bot: Bot<BotContext>) => {
bot.on("callback_query:data", (ctx) => {
const callbackQueryData = JSON.parse(
ctx.callbackQuery.data,
) as IButtonCallbackData;
const callbackQueryData = parseCallbackQueryData(ctx.callbackQuery.data);
switch (callbackQueryData.cq) {
case CallbackQueryEnum.APPROVE:
case CallbackQueryEnum.REJECT:
voteCallback(ctx, bot, callbackQueryData);
break;
case CallbackQueryEnum.NOOP:
break;
default:
throw new Error("Invalid callback query data");
}
ctx.answerCallbackQuery();
});
console.log("Callback Queries setup complete");
};

View File

@ -0,0 +1,4 @@
import { IButtonCallbackData } from "../keyboards/mod.ts";
export const parseCallbackQueryData = (data: string) =>
JSON.parse(data) as IButtonCallbackData

View File

@ -32,8 +32,6 @@ export const voteCallback = async (
// edit the message to show the outcome of approval
await ctx.editMessageReplyMarkup({
reply_markup: {
inline_keyboard: getPostOutcomeKeyboard(isApproved),
},
reply_markup: getPostOutcomeKeyboard(isApproved)
});
};

View File

@ -14,8 +14,6 @@ export const post = async (
// Wait for a message response
const postContext = await conversation.wait();
await postContext.copyMessage(GROUP_ID, {
reply_markup: {
inline_keyboard: getPostApprovalKeyboard(ctx.session.sessionId),
},
reply_markup: getPostApprovalKeyboard(ctx.session.sessionId),
});
};

View File

@ -1,11 +1,13 @@
export * from "./vote.ts";
export { getCallbackQueryData } from "./utils.ts";
import { CallbackQueryEnum } from "../callbackQueries/mod.ts";
export interface IButtonCallbackData {
/** Callback Query */
cq: CallbackQueryEnum.APPROVE | CallbackQueryEnum.REJECT;
cq: CallbackQueryEnum;
/** Session ID */
sid: number;
sid?: number;
}

4
src/keyboards/utils.ts Normal file
View File

@ -0,0 +1,4 @@
import { IButtonCallbackData } from './mod.ts';
export const getCallbackQueryData = (buttonCallbackData: IButtonCallbackData) =>
JSON.stringify(buttonCallbackData);

View File

@ -1,35 +1,21 @@
import { InlineKeyboardButton } from "https://deno.land/x/grammy_types@v3.4.6/mod.ts";
import { CallbackQueryEnum } from "../callbackQueries/mod.ts";
import { IButtonCallbackData } from "./mod.ts";
import { getCallbackQueryData } from "./mod.ts";
import { InlineKeyboard } from "grammy";
const getVoteButtonCallbackData = (sid: number, isApprove: boolean) =>
JSON.stringify({
cq: isApprove ? CallbackQueryEnum.APPROVE : CallbackQueryEnum.REJECT,
const getVoteButtonCallbackData = (sid: number, isApproved: boolean) =>
getCallbackQueryData({
cq: isApproved ? CallbackQueryEnum.APPROVE : CallbackQueryEnum.REJECT,
sid,
} as IButtonCallbackData);
});
export const getPostApprovalKeyboard = (
sid: number,
): InlineKeyboardButton[][] => [
[
{
text: "🟢",
callback_data: getVoteButtonCallbackData(sid, true),
},
{
text: "🔴",
callback_data: getVoteButtonCallbackData(sid, false),
},
],
];
) => new InlineKeyboard()
.text("🟢", getVoteButtonCallbackData(sid, true))
.text("🔴", getVoteButtonCallbackData(sid, false))
export const getPostOutcomeKeyboard = (
isApproved: boolean,
): InlineKeyboardButton[][] => [
[
{
text: isApproved ? "✅ Approved" : "❌ Rejected",
callback_data: "noop",
},
],
];
) => new InlineKeyboard()
.text(isApproved ? "✅ Approved" : "❌ Rejected",
getCallbackQueryData({ cq: CallbackQueryEnum.NOOP }));