in packages/app/client/src/ui/helpers/activeBotHelper.ts [234:308]
static async confirmAndSwitchBots(bot: BotConfigWithPath | string): Promise<any> {
const currentActiveBot = getActiveBot();
const botPath = typeof bot === 'object' ? bot.path : bot;
if (currentActiveBot && currentActiveBot.path === botPath) {
// the bot is already open, so open a new live chat tab
try {
await this.commandService.call(SharedConstants.Commands.Emulator.NewLiveChat, currentActiveBot.services[0]);
} catch (e) {
throw new Error(`[confirmAndSwitchBots] Error while trying to open bot at ${botPath}: ${e}`);
}
return;
}
// TODO: We need to think about merging this with confirmAndCreateBot
// eslint-disable-next-line no-console
console.log(`Switching to bot ${botPath}`);
try {
// prompt the user to confirm the switch
const result = await this.confirmSwitchBot();
if (result) {
store.dispatch(closeNonGlobalTabs());
// if we only have the bot path, we first need to open the bot file
let newActiveBot: BotConfigWithPath;
if (typeof bot === 'string') {
try {
newActiveBot = await this.commandService.remoteCall<BotConfigWithPath>(
SharedConstants.Commands.Bot.Open,
bot
);
} catch (e) {
throw new Error(`[confirmAndSwitchBots] Error while trying to open bot at ${botPath}: ${e}`);
}
} else {
newActiveBot = bot;
}
// set the bot as active
await this.setActiveBot(newActiveBot);
// find a suitable endpoint configuration
let endpoint: IEndpointService;
const overridesArePresent = newActiveBot.overrides && newActiveBot.overrides.endpoint;
// if an endpoint id was specified, use that endpoint, otherwise use the first endpoint found
if (overridesArePresent && newActiveBot.overrides.endpoint.id) {
endpoint = newActiveBot.services.find(
service => service.type === ServiceTypes.Endpoint && service.id === newActiveBot.overrides.endpoint.id
) as IEndpointService;
} else {
endpoint = newActiveBot.services.find(service => service.type === ServiceTypes.Endpoint) as IEndpointService;
}
// apply endpoint overrides here
if (endpoint && overridesArePresent) {
endpoint = mergeEndpoints(endpoint, newActiveBot.overrides.endpoint);
}
// open a livechat with the configured endpoint
if (endpoint) {
await this.commandService.call(SharedConstants.Commands.Emulator.NewLiveChat, endpoint);
}
store.dispatch(selectNavBar(SharedConstants.NavBarItems.NAVBAR_BOT_EXPLORER));
store.dispatch(showExplorer(true));
}
} catch (e) {
const errMsg = `Error while trying to switch to bot: ${botPath}`;
const notification = newNotification(errMsg);
store.dispatch(beginAdd(notification));
throw new Error(errMsg);
}
}