Skip to content

Commit a841a1b

Browse files
committed
handle non-existing directories better
1 parent 3dfe1d5 commit a841a1b

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

source/lib/all-events.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,13 @@ export function find(
106106
events: directory.events ?? {},
107107
};
108108
}
109+
110+
export function directoryExists(path: string[]): boolean {
111+
try {
112+
resolvePath(path);
113+
114+
return true;
115+
} catch {
116+
return false;
117+
}
118+
}

source/menu/events/add.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
deleteMenuFromContext, getMenuOfPath, MenuTemplate, replyMenuToContext,
55
} from 'grammy-inline-menu';
66
import {html as format} from 'telegram-format';
7-
import {count as allEventsCount, find as allEventsFind, getEventName} from '../../lib/all-events.ts';
7+
import {
8+
count as allEventsCount, directoryExists, find as allEventsFind, getEventName,
9+
} from '../../lib/all-events.ts';
810
import {filterButtonText} from '../../lib/inline-menu-filter.ts';
911
import {BACK_BUTTON_TEXT} from '../../lib/inline-menu.ts';
1012
import type {EventDirectory, EventId, MyContext} from '../../lib/types.ts';
@@ -131,9 +133,22 @@ menu.choose('a', {
131133
}
132134

133135
if (key.startsWith('d')) {
134-
const chosenSubDirectory = key.slice(1);
135-
ctx.session.eventPath?.push(chosenSubDirectory);
136-
delete ctx.session.eventDirectorySubDirectoryItems;
136+
if (ctx.session.eventDirectorySubDirectoryItems !== undefined) {
137+
const chosenSubDirectory = ctx.session.eventDirectorySubDirectoryItems[Number(key.slice(1))];
138+
delete ctx.session.eventDirectorySubDirectoryItems;
139+
140+
if (chosenSubDirectory !== undefined) {
141+
ctx.session.eventPath ??= [];
142+
ctx.session.eventPath.push(chosenSubDirectory);
143+
144+
if (directoryExists(ctx.session.eventPath)) {
145+
return true;
146+
}
147+
}
148+
}
149+
150+
await ctx.answerCallbackQuery('Dieses Verzeichnis gibt es nicht mehr.');
151+
delete ctx.session.eventPath;
137152

138153
return true;
139154
}
@@ -165,6 +180,11 @@ menu.interact('back', {
165180
}
166181

167182
ctx.session.eventPath?.pop();
183+
if (ctx.session.eventPath !== undefined && !directoryExists(ctx.session.eventPath)) {
184+
delete ctx.session.eventPath;
185+
delete ctx.session.eventDirectorySubDirectoryItems;
186+
}
187+
168188
return true;
169189
},
170190
});

0 commit comments

Comments
 (0)