|
| 1 | +import React, { useEffect, useRef } from 'react' |
| 2 | +import styles from './index.module.scss' |
| 3 | +import { ModalAnimation } from '@hyperplay/ui' |
| 4 | +import { WebviewTag } from 'electron' |
| 5 | +import { observer } from 'mobx-react-lite' |
| 6 | +import { DEV_PORTAL_URL } from 'common/constants' |
| 7 | +import emailSubscriptionState from '../../../state/EmailSubscriptionState' |
| 8 | +import { useFlags } from 'launchdarkly-react-client-sdk' |
| 9 | + |
| 10 | +const url = `${DEV_PORTAL_URL}/signin?isLauncher=true&promoMode=true` |
| 11 | + |
| 12 | +const EmailSubscriptionModal = () => { |
| 13 | + const flags = useFlags() |
| 14 | + const webviewRef = useRef<WebviewTag>(null) |
| 15 | + const isEnabled = flags.emailSubscriptionModal |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + const webview = webviewRef.current |
| 19 | + if (!webview) return |
| 20 | + |
| 21 | + const handleIpcMessage = async (event: Electron.IpcMessageEvent) => { |
| 22 | + switch (event.channel) { |
| 23 | + case 'closeAuthModal': |
| 24 | + emailSubscriptionState.closeEmailModal() |
| 25 | + break |
| 26 | + default: |
| 27 | + break |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + const handleDomReady = () => { |
| 32 | + webview.addEventListener('ipc-message', handleIpcMessage) |
| 33 | + } |
| 34 | + |
| 35 | + webview.addEventListener('dom-ready', handleDomReady) |
| 36 | + |
| 37 | + return () => { |
| 38 | + webview.removeEventListener('dom-ready', handleDomReady) |
| 39 | + webview.removeEventListener('ipc-message', handleIpcMessage) |
| 40 | + } |
| 41 | + }, []) |
| 42 | + |
| 43 | + return ( |
| 44 | + <ModalAnimation |
| 45 | + isOpen={isEnabled && emailSubscriptionState.isEmailModalOpen} |
| 46 | + onClose={() => emailSubscriptionState.closeEmailModal()} |
| 47 | + > |
| 48 | + <webview |
| 49 | + ref={webviewRef} |
| 50 | + src={url} |
| 51 | + className={styles.webview} |
| 52 | + partition="persist:emailModal" |
| 53 | + allowpopups={'true' as unknown as boolean | undefined} |
| 54 | + useragent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15" |
| 55 | + /> |
| 56 | + </ModalAnimation> |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +export default observer(EmailSubscriptionModal) |
0 commit comments