Skip to content

Commit

Permalink
refactor(menu): inline menu body function
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Mar 11, 2024
1 parent 00b2ca3 commit 71f3052
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
8 changes: 3 additions & 5 deletions source/connected.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {type Body, MenuTemplate} from 'grammy-inline-menu';
import {MenuTemplate} from 'grammy-inline-menu';
import {html as format} from 'telegram-format';
import * as connectedLogic from './lib/connected-logic.js';
import {timespan} from './lib/format.js';
import {getAll} from './lib/mqtt-history.js';

export const menu = new MenuTemplate(menuBody);

function menuBody(): Body {
export const menu = new MenuTemplate(() => {
const lines = getAll()
.filter(([topic]) => connectedLogic.isRelevantTopic(topic))
.map(([topic, data]) => {
Expand All @@ -27,6 +25,6 @@ function menuBody(): Body {

const text = lines.join('\n');
return {text, parse_mode: format.parse_mode};
}
});

menu.navigate('Update', '.');
13 changes: 3 additions & 10 deletions source/notify.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {StatelessQuestion} from '@grammyjs/stateless-question';
import {Composer} from 'grammy';
import {
type Body,
MenuTemplate,
replyMenuToContext,
} from 'grammy-inline-menu';
import {MenuTemplate, replyMenuToContext} from 'grammy-inline-menu';
import {html as format} from 'telegram-format';
import type {MyContext} from './context.js';
import {toggleKeyInArray} from './lib/array-helper.js';
Expand Down Expand Up @@ -33,10 +29,9 @@ function myRuleList(ctx: MyContext) {
return text;
}

function notifyOverviewText(ctx: MyContext): Body {
export const menu = new MenuTemplate<MyContext>(ctx => {
let text = format.bold('Benachrichtigungen');
text += '\n';

text += 'Du kannst benachrichtigt werden, wenn Geräte bestimmte Bedinungen erfüllen.';

const ruleList = myRuleList(ctx);
Expand All @@ -46,9 +41,7 @@ function notifyOverviewText(ctx: MyContext): Body {
}

return {text, parse_mode: format.parse_mode};
}

export const menu = new MenuTemplate(notifyOverviewText);
});

const addMenu = new MenuTemplate<MyContext>('Spezifiziere die Regel…');

Expand Down
8 changes: 3 additions & 5 deletions source/status.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type Body, MenuTemplate} from 'grammy-inline-menu';
import {MenuTemplate} from 'grammy-inline-menu';
import {html as format} from 'telegram-format';
import type {MyContext} from './context.js';
import {getRelatedConnectionStatus} from './lib/connected-logic.js';
Expand All @@ -9,8 +9,6 @@ import {addFilterButtons} from './topic-filter.js';
const MIN_AGE_MILLISECONDS = 1000 * 60 * 60 * 48;
const PER_PAGE = 50;

export const menu = new MenuTemplate(getStatusText);

function getAllContent(ctx: MyContext) {
const filter = new RegExp(ctx.session.topicFilter ?? '.+', 'i');
const relevantData = getAll()
Expand All @@ -28,7 +26,7 @@ function getAllContent(ctx: MyContext) {
return relevantData;
}

function getStatusText(ctx: MyContext): Body {
export const menu = new MenuTemplate<MyContext>(async ctx => {
const all = getAllContent(ctx);
if (all.length === 0) {
return 'no topics match your filter 😔';
Expand Down Expand Up @@ -58,7 +56,7 @@ function getStatusText(ctx: MyContext): Body {

const text = lines.join('\n');
return {text, parse_mode: format.parse_mode};
}
});

menu.navigate('Update', '.');

Expand Down

0 comments on commit 71f3052

Please sign in to comment.