|
| 1 | +import { logger } from 'src/utils/logger'; |
| 2 | +import { TsEmbed } from './ts-embed'; |
| 3 | +import { Action, HostEvent } from 'src/types'; |
| 4 | + |
| 5 | +class PuppetString { |
| 6 | + private embedInstance: TsEmbed; |
| 7 | + |
| 8 | + constructor(embedInstance: TsEmbed) { |
| 9 | + this.embedInstance = embedInstance; |
| 10 | + } |
| 11 | + |
| 12 | + public async clickPinButton() { |
| 13 | + const res = await this.embedInstance.trigger('EMBED_PUPPET_MOVE', { |
| 14 | + element: 'dual-intent-open-modal', |
| 15 | + action: 'click', |
| 16 | + }); |
| 17 | + |
| 18 | + if (!res.data.success) { |
| 19 | + throw Error('Failed to click pin button'); |
| 20 | + } |
| 21 | + |
| 22 | + return this; |
| 23 | + } |
| 24 | + |
| 25 | + public async filterPinboardInput(text: string) { |
| 26 | + const res = await this.embedInstance.trigger('EMBED_PUPPET_MOVE', { |
| 27 | + element: 'filter-pinboard', |
| 28 | + action: 'click', |
| 29 | + }); |
| 30 | + |
| 31 | + if (res.data.success) { |
| 32 | + const res = await this.embedInstance.trigger('EMBED_PUPPET_MOVE', { |
| 33 | + element: 'filter-pinboard', |
| 34 | + action: 'fill', |
| 35 | + text, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + throw Error('Failed to Filter pinboard'); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +class EmbedPuppeteer { |
| 44 | + private embedInstance: TsEmbed; |
| 45 | + |
| 46 | + private isPuppetEnabled = false; |
| 47 | + |
| 48 | + private puppet: PuppetString; |
| 49 | + |
| 50 | + constructor(embedInstance: TsEmbed) { |
| 51 | + this.puppet = new PuppetString(embedInstance); |
| 52 | + this.embedInstance = embedInstance; |
| 53 | + } |
| 54 | + |
| 55 | + protected async checkIfPuppetEnabled() { |
| 56 | + const res = await this.embedInstance.trigger(HostEvent.EMBED_PUPPET, { |
| 57 | + puppetCheck: true, |
| 58 | + }); |
| 59 | + if (res.data.puppetEnabled) { |
| 60 | + this.isPuppetEnabled = true; |
| 61 | + return; |
| 62 | + } |
| 63 | + this.isPuppetEnabled = false; |
| 64 | + } |
| 65 | + |
| 66 | + public async pinVizToLiveboard(liveboardName: string) { |
| 67 | + if (!this.isPuppetEnabled) { |
| 68 | + logger.warn('Embed puppet is not enabled. Please enable puppeteer to use this feature.'); |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + (await this.puppet.clickPinButton()).filterPinboardInput(liveboardName).catch((e) => { |
| 73 | + logger.error('Failed to pin viz to liveboard', e); |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
0 commit comments