|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { service } from '@ember/service'; |
| 3 | +import { action } from '@ember/object'; |
| 4 | +import type AuthenticatorService from 'codecrafters-frontend/services/authenticator'; |
| 5 | + |
| 6 | +const LOBBYSIDE_SCRIPT_ID = 'lobbyside-secondary-widget-script'; |
| 7 | + |
| 8 | +export default class LobbysideSecondaryWidgetComponent extends Component { |
| 9 | + @service declare authenticator: AuthenticatorService; |
| 10 | + |
| 11 | + get shouldShow(): boolean { |
| 12 | + return true; |
| 13 | + } |
| 14 | + |
| 15 | + @action |
| 16 | + insertScript(): void { |
| 17 | + if (document.getElementById(LOBBYSIDE_SCRIPT_ID)) { |
| 18 | + this.syncVisitorData(); |
| 19 | + |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + const script = document.createElement('script'); |
| 24 | + script.id = LOBBYSIDE_SCRIPT_ID; |
| 25 | + script.src = 'https://lobbyside.com/widget.js'; |
| 26 | + script.dataset['widgetId'] = 'b0d7150e-77e2-431c-a57c-765e080d9c72'; |
| 27 | + script.onload = () => this.syncVisitorData(); |
| 28 | + document.body.appendChild(script); |
| 29 | + } |
| 30 | + |
| 31 | + @action |
| 32 | + removeScript(): void { |
| 33 | + const script = document.getElementById(LOBBYSIDE_SCRIPT_ID); |
| 34 | + |
| 35 | + if (script) { |
| 36 | + script.remove(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private syncVisitorData(): void { |
| 41 | + const user = this.authenticator.currentUser; |
| 42 | + |
| 43 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 44 | + if (!user || !(window as any)['Lobbyside']) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 49 | + const lobbyside = (window as any).Lobbyside; |
| 50 | + |
| 51 | + lobbyside.setVisitor({ |
| 52 | + email: user.primaryEmailAddress || '', |
| 53 | + name: user.name || user.githubName || '', |
| 54 | + github: user.githubUsername || '', |
| 55 | + }); |
| 56 | + } |
| 57 | +} |
0 commit comments