|
| 1 | +import { proxy, useSnapshot } from 'valtio' |
| 2 | +import { useEffect, useMemo, useState } from 'react' |
| 3 | +import PixelartIcon, { pixelartIcons } from '../react/PixelartIcon' |
| 4 | +import { useIsModalActive } from '../react/utilsApp' |
| 5 | +import { hideModal, showModal } from '../globalState' |
| 6 | +import { iframeState } from '../core/iframeChannels' |
| 7 | +import { lastConnectOptions } from '../appStatus' |
| 8 | +import { useAppScale } from '../scaleInterface' |
| 9 | +import { appStorage } from './appStorageProvider' |
| 10 | +import Button from './Button' |
| 11 | +import './IframeModal.css' |
| 12 | + |
| 13 | +const getDomainFromUrl = (url: string): string => { |
| 14 | + try { |
| 15 | + const urlObj = new URL(url) |
| 16 | + return urlObj.hostname |
| 17 | + } catch { |
| 18 | + // If URL parsing fails, try to extract domain manually |
| 19 | + const match = /^(?:https?:\/\/)?([^/]+)/.exec(url) |
| 20 | + return match ? match[1] : url |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +const getConsentKey = (serverIp: string, domain: string): string => { |
| 25 | + return `${serverIp}:${domain}` |
| 26 | +} |
| 27 | + |
| 28 | +const checkConsent = (serverIp: string, domain: string): boolean => { |
| 29 | + const consentKey = getConsentKey(serverIp, domain) |
| 30 | + return appStorage.iframeConsents?.includes(consentKey) ?? false |
| 31 | +} |
| 32 | + |
| 33 | +const addConsent = (serverIp: string, domain: string) => { |
| 34 | + const consentKey = getConsentKey(serverIp, domain) |
| 35 | + if (!appStorage.iframeConsents) { |
| 36 | + appStorage.iframeConsents = [] |
| 37 | + } |
| 38 | + if (!appStorage.iframeConsents.includes(consentKey)) { |
| 39 | + appStorage.iframeConsents.push(consentKey) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export default () => { |
| 44 | + const { url, title, id, metadata } = useSnapshot(iframeState) |
| 45 | + const isModalActive = useIsModalActive('iframe-modal') |
| 46 | + const serverIp = lastConnectOptions.value?.server ?? '' |
| 47 | + const domain = useMemo(() => (url ? getDomainFromUrl(url) : ''), [url]) |
| 48 | + const [showConsentScreen, setShowConsentScreen] = useState(true) |
| 49 | + const scale = useAppScale() |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + if (id && url && !isModalActive) { |
| 53 | + showModal({ reactType: 'iframe-modal' }) |
| 54 | + const consent = checkConsent(serverIp, domain) |
| 55 | + setShowConsentScreen(!consent) |
| 56 | + } |
| 57 | + if (!id && isModalActive) { |
| 58 | + hideModal() |
| 59 | + } |
| 60 | + }, [id, url]) |
| 61 | + |
| 62 | + |
| 63 | + const handleConsent = () => { |
| 64 | + if (!domain) return |
| 65 | + addConsent(serverIp ?? '', domain) |
| 66 | + setShowConsentScreen(false) |
| 67 | + } |
| 68 | + |
| 69 | + if (!isModalActive) return null |
| 70 | + |
| 71 | + return <div className="iframe-modal-container"> |
| 72 | + <div className="iframe-modal-close"> |
| 73 | + <PixelartIcon |
| 74 | + iconName={pixelartIcons.close} |
| 75 | + width={26} |
| 76 | + onClick={() => { |
| 77 | + hideModal() |
| 78 | + }} |
| 79 | + /> |
| 80 | + </div> |
| 81 | + {title && ( |
| 82 | + <div className="iframe-modal-title"> |
| 83 | + {title} |
| 84 | + </div> |
| 85 | + )} |
| 86 | + <div className="iframe-modal-wrapper"> |
| 87 | + {showConsentScreen ? ( |
| 88 | + <div className="iframe-consent-screen"> |
| 89 | + <div className="iframe-consent-content" style={{ transform: `scale(${scale})` }}> |
| 90 | + <div className="iframe-consent-message"> |
| 91 | + Allow <strong>{serverIp}</strong> to open for you <strong>{domain}</strong>? |
| 92 | + </div> |
| 93 | + <Button |
| 94 | + label="Open Page" |
| 95 | + onClick={handleConsent} |
| 96 | + className="iframe-consent-button" |
| 97 | + /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ) : ( |
| 101 | + <iframe |
| 102 | + src={url} |
| 103 | + className="iframe-modal-iframe" |
| 104 | + allow="*" |
| 105 | + allowFullScreen |
| 106 | + sandbox="allow-same-origin allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox allow-presentation allow-downloads allow-modals allow-orientation-lock allow-pointer-lock allow-top-navigation-by-user-activation allow-storage-access-by-user-activation" |
| 107 | + /> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + </div> |
| 111 | +} |
0 commit comments