Skip to content

Commit

Permalink
feat!: move long arguments to options object
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Feb 6, 2024
1 parent 8583bfe commit fde671d
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 259 deletions.
54 changes: 32 additions & 22 deletions examples/main-javascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import {

const menu = new MenuTemplate(() => 'Main Menu\n' + new Date().toISOString());

menu.url('EdJoPaTo.de', 'https://edjopato.de');
menu.url({text: 'EdJoPaTo.de', url: 'https://edjopato.de'});

let mainMenuToggle = false;
menu.toggle('toggle me', 'toggle me', {
menu.toggle('toggle me', {
text: 'toggle me',
set(_, newState) {
mainMenuToggle = newState;
// Update the menu afterwards
Expand All @@ -26,7 +27,8 @@ menu.toggle('toggle me', 'toggle me', {
isSet: () => mainMenuToggle,
});

menu.interact('interaction', 'interact', {
menu.interact('interact', {
text: 'interaction',
hide: () => mainMenuToggle,
async do(ctx) {
await ctx.answerCallbackQuery({text: 'you clicked me!'});
Expand All @@ -35,8 +37,9 @@ menu.interact('interaction', 'interact', {
},
});

menu.interact('update after action', 'update afterwards', {
menu.interact('update afterwards', {
joinLastRow: true,
text: 'update after action',
hide: () => mainMenuToggle,
async do(ctx) {
await ctx.answerCallbackQuery({text: 'I will update the menu now…'});
Expand All @@ -50,7 +53,8 @@ menu.interact('update after action', 'update afterwards', {
});

let selectedKey = 'b';
menu.select('select', ['A', 'B', 'C'], {
menu.select('select', {
choices: ['A', 'B', 'C'],
async set(ctx, key) {
selectedKey = key;
await ctx.answerCallbackQuery({text: `you selected ${key}`});
Expand All @@ -66,15 +70,6 @@ const foodMenu = new MenuTemplate(
const people = {Mark: {}, Paul: {}};
const FOOD = ['bread', 'cake', 'bananas'];

function personButtonText(_, key) {
const entry = people[key];
if (entry?.food) {
return `${key} (${entry.food})`;
}

return key;
}

function foodSelectText(ctx) {
const person = ctx.match[1];
const hisChoice = people[person].food;
Expand All @@ -86,7 +81,8 @@ function foodSelectText(ctx) {
}

const foodSelectSubmenu = new MenuTemplate(foodSelectText);
foodSelectSubmenu.toggle('Prefer tea', 'tea', {
foodSelectSubmenu.toggle('tea', {
text: 'Prefer tea',
set(ctx, choice) {
const person = ctx.match[1];
people[person].tee = choice;
Expand All @@ -97,7 +93,8 @@ foodSelectSubmenu.toggle('Prefer tea', 'tea', {
return people[person].tee === true;
},
});
foodSelectSubmenu.select('food', FOOD, {
foodSelectSubmenu.select('food', {
choices: FOOD,
set(ctx, key) {
const person = ctx.match[1];
people[person].food = key;
Expand All @@ -110,13 +107,22 @@ foodSelectSubmenu.select('food', FOOD, {
});
foodSelectSubmenu.manualRow(createBackMainMenuButtons());

foodMenu.chooseIntoSubmenu('person', () => Object.keys(people), foodSelectSubmenu, {
buttonText: personButtonText,
foodMenu.chooseIntoSubmenu('person', foodSelectSubmenu, {
columns: 2,
choices: () => Object.keys(people),
buttonText(_ctx, key) {
const entry = people[key];
if (entry?.food) {
return `${key} (${entry.food})`;
}

return key;
},
});
foodMenu.manualRow(createBackMainMenuButtons());

menu.submenu('Food menu', 'food', foodMenu, {
menu.submenu('food', foodMenu, {
text: 'Food menu',
hide: () => mainMenuToggle,
});

Expand Down Expand Up @@ -202,14 +208,16 @@ const mediaMenu = new MenuTemplate(() => {
media: 'https://telegram.org/img/SiteiOs.jpg',
};
});
mediaMenu.interact('Just a button', 'randomButton', {
mediaMenu.interact('randomButton', {
text: 'Just a button',
async do(ctx) {
await ctx.answerCallbackQuery({text: 'Just a callback query answer'});
return false;
},
});
mediaMenu.select('type', MEDIA_OPTIONS, {
mediaMenu.select('type', {
columns: 2,
choices: MEDIA_OPTIONS,
isSet: (_, key) => mediaOption === key,
set(_, key) {
mediaOption = key;
Expand All @@ -218,7 +226,9 @@ mediaMenu.select('type', MEDIA_OPTIONS, {
});
mediaMenu.manualRow(createBackMainMenuButtons());

menu.submenu('Media Menu', 'media', mediaMenu);
menu.submenu('media', mediaMenu, {
text: 'Media Menu',
});

const menuMiddleware = new MenuMiddleware('/', menu);
console.log(menuMiddleware.tree());
Expand Down
56 changes: 33 additions & 23 deletions examples/main-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ const menu = new MenuTemplate<MyContext>(() =>
'Main Menu\n' + new Date().toISOString(),
);

menu.url('EdJoPaTo.de', 'https://edjopato.de');
menu.url({text: 'EdJoPaTo.de', url: 'https://edjopato.de'});

let mainMenuToggle = false;
menu.toggle('toggle me', 'toggle me', {
menu.toggle('toggle me', {
text: 'toggle me',
set(_, newState) {
mainMenuToggle = newState;
// Update the menu afterwards
Expand All @@ -25,7 +26,8 @@ menu.toggle('toggle me', 'toggle me', {
isSet: () => mainMenuToggle,
});

menu.interact('interaction', 'interact', {
menu.interact('interact', {
text: 'interaction',
hide: () => mainMenuToggle,
async do(ctx) {
await ctx.answerCallbackQuery({text: 'you clicked me!'});
Expand All @@ -34,8 +36,9 @@ menu.interact('interaction', 'interact', {
},
});

menu.interact('update after action', 'update afterwards', {
menu.interact('update afterwards', {
joinLastRow: true,
text: 'update after action',
hide: () => mainMenuToggle,
async do(ctx) {
await ctx.answerCallbackQuery({text: 'I will update the menu now…'});
Expand All @@ -49,7 +52,8 @@ menu.interact('update after action', 'update afterwards', {
});

let selectedKey = 'b';
menu.select('select', ['A', 'B', 'C'], {
menu.select('select', {
choices: ['A', 'B', 'C'],
async set(ctx, key) {
selectedKey = key;
await ctx.answerCallbackQuery({text: `you selected ${key}`});
Expand All @@ -68,16 +72,7 @@ type FoodChoices = {
};

const people: Record<string, FoodChoices> = {Mark: {}, Paul: {}};
const FOOD = ['bread', 'cake', 'bananas'] as const;

function personButtonText(_: MyContext, key: string): string {
const entry = people[key];
if (entry?.food) {
return `${key} (${entry.food})`;
}

return key;
}
const FOOD = ['bread', 'cake', 'bananas'];

function foodSelectText(ctx: MyContext): string {
const person = ctx.match![1]!;
Expand All @@ -90,7 +85,8 @@ function foodSelectText(ctx: MyContext): string {
}

const foodSelectSubmenu = new MenuTemplate<MyContext>(foodSelectText);
foodSelectSubmenu.toggle('Prefer tea', 'tea', {
foodSelectSubmenu.toggle('tea', {
text: 'Prefer tea',
set(ctx, choice) {
const person = ctx.match![1]!;
people[person]!.tee = choice;
Expand All @@ -101,7 +97,8 @@ foodSelectSubmenu.toggle('Prefer tea', 'tea', {
return people[person]!.tee === true;
},
});
foodSelectSubmenu.select('food', FOOD, {
foodSelectSubmenu.select('food', {
choices: FOOD,
set(ctx, key) {
const person = ctx.match![1]!;
people[person]!.food = key;
Expand All @@ -114,13 +111,22 @@ foodSelectSubmenu.select('food', FOOD, {
});
foodSelectSubmenu.manualRow(createBackMainMenuButtons());

foodMenu.chooseIntoSubmenu('person', () => Object.keys(people), foodSelectSubmenu, {
buttonText: personButtonText,
foodMenu.chooseIntoSubmenu('person', foodSelectSubmenu, {
columns: 2,
choices: () => Object.keys(people),
buttonText(_ctx, key) {
const entry = people[key];
if (entry?.food) {
return `${key} (${entry.food})`;
}

return key;
},
});
foodMenu.manualRow(createBackMainMenuButtons());

menu.submenu('Food menu', 'food', foodMenu, {
menu.submenu('food', foodMenu, {
text: 'Food menu',
hide: () => mainMenuToggle,
});

Expand Down Expand Up @@ -206,14 +212,16 @@ const mediaMenu = new MenuTemplate<MyContext>(() => {
media: 'https://telegram.org/img/SiteiOs.jpg',
};
});
mediaMenu.interact('Just a button', 'randomButton', {
mediaMenu.interact('randomButton', {
text: 'Just a button',
async do(ctx) {
await ctx.answerCallbackQuery({text: 'Just a callback query answer'});
return false;
},
});
mediaMenu.select('type', MEDIA_OPTIONS, {
mediaMenu.select('type', {
columns: 2,
choices: MEDIA_OPTIONS,
isSet: (_, key) => mediaOption === key,
set(_, key) {
mediaOption = key;
Expand All @@ -222,7 +230,9 @@ mediaMenu.select('type', MEDIA_OPTIONS, {
});
mediaMenu.manualRow(createBackMainMenuButtons());

menu.submenu('Media Menu', 'media', mediaMenu);
menu.submenu('media', mediaMenu, {
text: 'Media Menu',
});

const menuMiddleware = new MenuMiddleware<MyContext>('/', menu);
console.log(menuMiddleware.tree());
Expand Down
29 changes: 27 additions & 2 deletions source/buttons/basic.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
import type {ActionFunc} from '../action-hive.js';
import type {ContextPathFunc} from '../generic-types.js';
import type {
ConstOrContextPathFunc,
ContextPathFunc,
} from '../generic-types.js';

export interface BasicOptions<Context> {
/** Return true when the button(s) should be hidden and not to be called */
readonly hide?: ContextPathFunc<Context, boolean>;
}

export interface SingleButtonOptions<Context> extends BasicOptions<Context> {
interface JoinLastRowOption {
/** Decide whether the button should be in the last added row or in a new row. Own row per default, last row when true. */
readonly joinLastRow?: boolean;
}

export interface SingleButtonOptions<Context>
extends BasicOptions<Context>, JoinLastRowOption {
/** Label text on the button */
readonly text: ConstOrContextPathFunc<Context, string>;
}

export type ManualButtonOptions<Context> =
& BasicOptions<Context>
& JoinLastRowOption;

export interface UrlButtonOptions<Context>
extends SingleButtonOptions<Context> {
/** Url where this button should be heading */
readonly url: ConstOrContextPathFunc<Context, string>;
}

export interface SwitchToChatOptions<Context>
extends SingleButtonOptions<Context> {
/** Query that is shown next to the bot username. Can be empty ('') */
readonly query: ConstOrContextPathFunc<Context, string>;
}

export interface InteractionOptions<Context>
extends SingleButtonOptions<Context> {
/** Function which is called when the button is pressed */
Expand Down
Loading

0 comments on commit fde671d

Please sign in to comment.