Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ctx.menu.at function #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const INJECT_METHODS = new Set([
* Context flavor for context objects in listeners that react to menus. Provides
* `ctx.menu`, a control pane for the respective menu.
*/
export interface MenuFlavor {
export interface MenuFlavor<C extends Context> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change.

match?: string;
/**
* Control panel for the currently active menu. `ctx.menu` is only available
Expand All @@ -57,7 +57,7 @@ export interface MenuFlavor {
* Otherwise, a dedicated API call will be performed after your middleware
* completes.
*/
menu: MenuControlPanel;
menu: MenuControlPanel<C>;
}

interface Immediate {
Expand All @@ -67,7 +67,7 @@ interface Immediate {
* Menu control panel. Can be used to update or close the menu, or to perform
* manual navigation between menus.
*/
export interface MenuControlPanel {
export interface MenuControlPanel<C extends Context> {
/**
* Call this method to update the menu. For instance, if you have a button
* that changes its text based on `ctx`, then you should call this method to
Expand Down Expand Up @@ -146,13 +146,20 @@ export interface MenuControlPanel {
*/
nav(to: string, config: { immediate: true }): Promise<void>;
nav(to: string, config?: { immediate?: false }): void;
/**
* Returns the menu instance for the given identifier.
*
* @param id Menu identifier
* @returns The identified menu
*/
at(id: string): Menu<C>
}

/**
* Middleware that has access to the `ctx.menu` control panel.
*/
type MenuMiddleware<C extends Context> = Middleware<
Filter<C, "callback_query:data"> & MenuFlavor
Filter<C, "callback_query:data"> & MenuFlavor<C>
>;

/** A value, or a promise of a value */
Expand Down Expand Up @@ -986,6 +993,7 @@ export class Menu<C extends Context = Context> extends MenuRange<C>
}
return nav(config, menu.at(parent));
},
at: (id) => menu.at(id)
};
// register ctx.menu
Object.assign(ctx, { menu: controlPanel });
Expand Down