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 { voteCallback } from "./voteCallback.ts";
export enum CallbackQueryEnum { export enum CallbackQueryEnum {
NOOP = "noop",
APPROVE = "approve", APPROVE = "approve",
REJECT = "reject", REJECT = "reject",
} }

View File

@ -1,23 +1,28 @@
import { Bot } from "grammy"; import { Bot } from "grammy";
import { BotContext } from "../mod.ts"; import { BotContext } from "../mod.ts";
import { IButtonCallbackData } from "../keyboards/mod.ts";
import { CallbackQueryEnum, voteCallback } from "./mod.ts"; import { CallbackQueryEnum, voteCallback } from "./mod.ts";
import { parseCallbackQueryData } from "./utils.ts";
export const setupCallbackQueries = (bot: Bot<BotContext>) => { export const setupCallbackQueries = (bot: Bot<BotContext>) => {
bot.on("callback_query:data", (ctx) => { bot.on("callback_query:data", (ctx) => {
const callbackQueryData = JSON.parse( const callbackQueryData = parseCallbackQueryData(ctx.callbackQuery.data);
ctx.callbackQuery.data,
) as IButtonCallbackData;
switch (callbackQueryData.cq) { switch (callbackQueryData.cq) {
case CallbackQueryEnum.APPROVE: case CallbackQueryEnum.APPROVE:
case CallbackQueryEnum.REJECT: case CallbackQueryEnum.REJECT:
voteCallback(ctx, bot, callbackQueryData); voteCallback(ctx, bot, callbackQueryData);
break; break;
case CallbackQueryEnum.NOOP:
break;
default: default:
throw new Error("Invalid callback query data"); throw new Error("Invalid callback query data");
} }
ctx.answerCallbackQuery();
}); });
console.log("Callback Queries setup complete"); 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 // edit the message to show the outcome of approval
await ctx.editMessageReplyMarkup({ await ctx.editMessageReplyMarkup({
reply_markup: { reply_markup: getPostOutcomeKeyboard(isApproved)
inline_keyboard: getPostOutcomeKeyboard(isApproved),
},
}); });
}; };

View File

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

View File

@ -1,11 +1,13 @@
export * from "./vote.ts"; export * from "./vote.ts";
export { getCallbackQueryData } from "./utils.ts";
import { CallbackQueryEnum } from "../callbackQueries/mod.ts"; import { CallbackQueryEnum } from "../callbackQueries/mod.ts";
export interface IButtonCallbackData { export interface IButtonCallbackData {
/** Callback Query */ /** Callback Query */
cq: CallbackQueryEnum.APPROVE | CallbackQueryEnum.REJECT; cq: CallbackQueryEnum;
/** Session ID */ /** 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 { 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) => const getVoteButtonCallbackData = (sid: number, isApproved: boolean) =>
JSON.stringify({ getCallbackQueryData({
cq: isApprove ? CallbackQueryEnum.APPROVE : CallbackQueryEnum.REJECT, cq: isApproved ? CallbackQueryEnum.APPROVE : CallbackQueryEnum.REJECT,
sid, sid,
} as IButtonCallbackData); });
export const getPostApprovalKeyboard = ( export const getPostApprovalKeyboard = (
sid: number, sid: number,
): InlineKeyboardButton[][] => [ ) => new InlineKeyboard()
[ .text("🟢", getVoteButtonCallbackData(sid, true))
{ .text("🔴", getVoteButtonCallbackData(sid, false))
text: "🟢",
callback_data: getVoteButtonCallbackData(sid, true),
},
{
text: "🔴",
callback_data: getVoteButtonCallbackData(sid, false),
},
],
];
export const getPostOutcomeKeyboard = ( export const getPostOutcomeKeyboard = (
isApproved: boolean, isApproved: boolean,
): InlineKeyboardButton[][] => [ ) => new InlineKeyboard()
[ .text(isApproved ? "✅ Approved" : "❌ Rejected",
{ getCallbackQueryData({ cq: CallbackQueryEnum.NOOP }));
text: isApproved ? "✅ Approved" : "❌ Rejected",
callback_data: "noop",
},
],
];