Skip to content

Commit

Permalink
refactor: rename action(prefix) to uniqueIdentifier or trigger
Browse files Browse the repository at this point in the history
its way easier to understand what it needs to be and what it is. It identifies the element uniquely.

For submenus the action was also not that helpful. Its the trigger on getting into the menu.
  • Loading branch information
EdJoPaTo committed Mar 12, 2024
1 parent 02d3bc8 commit f3e9792
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 87 deletions.
4 changes: 2 additions & 2 deletions source/action-hive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export class ActionHive<Context> {
.includes(trigger.source);
if (alreadyExisting) {
throw new Error(
`The action "${
`The unique identifier "${
trigger.source.slice(0, -1)
}" you wanna add was already added. When you hit the button only the first one will be used and not both. This one can not be accessed then. Change the action code to something different.`,
}" you wanna add was already added. When you hit the button only the first one will be used and not both. This one can not be accessed then. Change the unique identifier code to something different.`,
);
}

Expand Down
8 changes: 4 additions & 4 deletions source/buttons/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface SelectOptions<Context> extends ManyChoicesOptions<Context> {
}

export function generateSelectButtons<Context>(
actionPrefix: string,
uniqueIdentifierPrefix: string,
options: SelectOptions<Context>,
): (context: Context, path: string) => Promise<CallbackButtonTemplate[][]> {
return async (context, path) => {
Expand All @@ -61,7 +61,7 @@ export function generateSelectButtons<Context>(
? await options.choices(context)
: options.choices;
const choiceKeys = getChoiceKeysFromChoices(choicesConstant);
ensureCorrectChoiceKeys(actionPrefix, path, choiceKeys);
ensureCorrectChoiceKeys(uniqueIdentifierPrefix, path, choiceKeys);
const textFunction = createChoiceTextFunction(
choicesConstant,
options.buttonText,
Expand All @@ -85,14 +85,14 @@ export function generateSelectButtons<Context>(
const text = await formatFunction(context, textResult, state, key);

const dropinLetter = state ? 'F' : 'T';
const relativePath = actionPrefix + dropinLetter + ':' + key;
const relativePath = uniqueIdentifierPrefix + dropinLetter + ':' + key;
return {text, relativePath};
}));
const rows = getButtonsAsRows(buttonsOfPage, options.columns);

if (options.setPage) {
rows.push(generateChoicesPaginationButtons(
actionPrefix,
uniqueIdentifierPrefix,
choiceKeys.length,
currentPage,
options,
Expand Down
4 changes: 2 additions & 2 deletions source/buttons/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface ToggleOptions<Context> extends SingleButtonOptions<Context> {
}

export function generateToggleButton<Context>(
actionPrefix: string,
uniqueIdentifierPrefix: string,
options: ToggleOptions<Context>,
): ContextPathFunc<Context, CallbackButtonTemplate | undefined> {
const formatFunction: FormatStateFunction<Context> = options.formatState
Expand All @@ -42,7 +42,7 @@ export function generateToggleButton<Context>(
const state = await options.isSet(context, path);
return {
text: await formatFunction(context, textResult, state, path),
relativePath: actionPrefix + ':' + (state ? 'false' : 'true'),
relativePath: uniqueIdentifierPrefix + ':' + (state ? 'false' : 'true'),
};
};
}
5 changes: 3 additions & 2 deletions source/choices/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {Choices} from './types.js';
import {getChoiceKeysFromChoices} from './understand-choices.js';

export function combineHideAndChoices<Context>(
actionPrefix: string,
uniqueIdentifierPrefix: string,
choices: ConstOrContextFunc<Context, Choices>,
hide: undefined | ContextPathFunc<Context, boolean>,
): ContextPathFunc<Context, boolean> {
Expand All @@ -16,7 +16,8 @@ export function combineHideAndChoices<Context>(
return true;
}

const match = new RegExp('/' + actionPrefix + ':([^/]+)/?$').exec(path);
const match = new RegExp('/' + uniqueIdentifierPrefix + ':([^/]+)/?$')
.exec(path);
const toBeFound = match?.[1];
if (!toBeFound) {
throw new TypeError('could not read choice from path');
Expand Down
13 changes: 7 additions & 6 deletions source/choices/buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from './understand-choices.js';

export function generateChoicesButtons<Context>(
actionPrefix: string,
uniqueIdentifierPrefix: string,
isSubmenu: boolean,
options: ManyChoicesOptions<Context>,
): (context: Context, path: string) => Promise<CallbackButtonTemplate[][]> {
Expand All @@ -26,7 +26,7 @@ export function generateChoicesButtons<Context>(
? await options.choices(context)
: options.choices;
const choiceKeys = getChoiceKeysFromChoices(choicesConstant);
ensureCorrectChoiceKeys(actionPrefix, path, choiceKeys);
ensureCorrectChoiceKeys(uniqueIdentifierPrefix, path, choiceKeys);
const textFunction = createChoiceTextFunction(
choicesConstant,
options.buttonText,
Expand All @@ -40,14 +40,15 @@ export function generateChoicesButtons<Context>(
);
const buttonsOfPage = await Promise.all(keysOfPage.map(async key => {
const text = await textFunction(context, key);
const relativePath = actionPrefix + ':' + key + (isSubmenu ? '/' : '');
const relativePath = uniqueIdentifierPrefix + ':' + key
+ (isSubmenu ? '/' : '');
return {text, relativePath};
}));
const rows = getButtonsAsRows(buttonsOfPage, options.columns);

if (options.setPage) {
rows.push(generateChoicesPaginationButtons(
actionPrefix,
uniqueIdentifierPrefix,
choiceKeys.length,
currentPage,
options,
Expand All @@ -59,7 +60,7 @@ export function generateChoicesButtons<Context>(
}

export function generateChoicesPaginationButtons<Context>(
actionPrefix: string,
uniqueIdentifierPrefix: string,
choiceKeys: number,
currentPage: number | undefined,
options: ManyChoicesOptions<Context>,
Expand All @@ -73,7 +74,7 @@ export function generateChoicesPaginationButtons<Context>(
const pageKeys = Object.keys(pageRecord).map(Number);
const pageButtons = pageKeys
.map((page): CallbackButtonTemplate => ({
relativePath: `${actionPrefix}P:${page}`,
relativePath: `${uniqueIdentifierPrefix}P:${page}`,
text: pageRecord[page]!,
}));

Expand Down
4 changes: 2 additions & 2 deletions source/choices/understand-choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ export function getChoiceTextByKey(choices: Choices, key: string): string {
}

export function ensureCorrectChoiceKeys(
actionPrefix: string,
uniqueIdentifierPrefix: string,
path: string,
choiceKeys: readonly string[],
): void {
const containSlashExample = choiceKeys.find(o => o.includes('/'));
if (containSlashExample) {
throw new Error(
`Choices can not contain '/'. Found '${containSlashExample}' in action '${actionPrefix}' at path '${path}'.`,
`Choices can not contain '/'. Found '${containSlashExample}' in unique identifier '${uniqueIdentifierPrefix}' at path '${path}'.`,
);
}
}
3 changes: 2 additions & 1 deletion source/menu-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export type MenuLike<Context> = {
};

export type Submenu<Context> = {
readonly action: RegExpLike;
/** Unique within the current menu depth */
readonly trigger: RegExpLike;
readonly hide: undefined | ContextPathFunc<Context, boolean>;
readonly menu: MenuLike<Context>;
};
2 changes: 1 addition & 1 deletion source/menu-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ function createResponder<Context extends BaseContext>(

const submenuResponders = [...menu.listSubmenus()]
.map((submenu): MenuResponder<Context> => {
const submenuTrigger = combineTrigger(menuTrigger, submenu.action);
const submenuTrigger = combineTrigger(menuTrigger, submenu.trigger);

const canEnterSubmenu: ContextPathFunc<Context, boolean> = async (
context,
Expand Down
Loading

0 comments on commit f3e9792

Please sign in to comment.