import './showAlert.scss'; import classNames from 'classnames'; import html2canvas from 'html2canvas'; import React, { useContext, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { toast } from 'react-toastify'; import { MiniAppContext } from '../../context/miniAppContext'; import { MiniAppErrorType } from '../../model/miniAppError'; import msgHandler from '../../utils/iframe/messageHandler'; import Button from '../Button/button'; import ImageIcon from '../ImageIcon'; import Input from '../Input/input'; import Modal from '../Modal/modal'; import * as log from 'loglevel'; interface Props { alertParams: any; setAlertParams: any; toggleMiniAppSettings?: (v: boolean) => void; appDetail: any; } const ShowAlert: React.FC = (props) => { const { t } = useTranslation(); const { alertParams, setAlertParams, toggleMiniAppSettings, appDetail, } = props; const [codeValue, setCode] = useState(''); const { showAlert, title, text, buttonTitle, alertBoxName, successCB, failCB, completeCB, originId, imageUrl, screenshot, includeAppIconWhenShare, } = alertParams; const subMenuStyle = classNames('custom-btn', { 'custom-btn-submenu': alertBoxName === 'showMenusheet', }); const [screenshoturl, setScreenshotUrl] = useState(''); useEffect(() => { try { if (alertBoxName === 'share') { if (screenshot) { let frame = document .getElementsByClassName('screen')[0] .getElementsByTagName('iframe')[0]; html2canvas( frame?.contentDocument?.documentElement as HTMLElement ).then((canvas) => { setScreenshotUrl(canvas.toDataURL()); }); } } } catch (e) { log.error(`${JSON.stringify(e)}`); } }, [alertBoxName, includeAppIconWhenShare, screenshot]); const { state } = useContext(MiniAppContext); async function okClicked() { if (alertBoxName === 'scanCode' && codeValue.length) { msgHandler(originId, successCB, { code: codeValue }); setCode(''); } else if (alertBoxName === 'openApp') { msgHandler(originId, successCB, {}); toast.success('Opened the app'); } else if (alertBoxName === 'getContactList') { msgHandler(originId, successCB, { contacts: [{ name: 'PayPay', telNumbers: ['080123456789'] }], }); } finishTasks(); } function cancelClicked() { msgHandler(originId, failCB, { errorCode: alertBoxName === 'getContactList' ? MiniAppErrorType.hostAppContactsDenied : MiniAppErrorType.userCanceled, }); finishTasks(); } function finishTasks() { if (alertBoxName !== 'share') { msgHandler(originId, completeCB); } setAlertParams({ ...alertParams, showAlert: false, }); } function onCodeChange(event: any) { setCode(event?.target.value); } const renderTopIcon = () => { return alertBoxName === 'showErrorSheet' ? ( ) : alertBoxName === 'share' ? ( includeAppIconWhenShare && appDetail.appLogo ? ( ) : null ) : null; }; const renderTitle = () => { return alertBoxName === 'showMenusheet' ? (
{state.appName}
) : alertBoxName === 'openApp' ? (
{t('openApp.title')}
) : alertBoxName === 'getContactList' ? (
{t('openContact.title')}
) : (

{title}

); }; const renderMenuItems = () => { return ( <> {appDetail.channelId ? (
ブランド
の詳細
) : null} {appDetail.menuItems?.settings == null || appDetail.menuItems?.settings ? (
{ finishTasks(); toggleMiniAppSettings && toggleMiniAppSettings(true); }} role="button" tabIndex={0} >
{t('menuSheet.settings')}
) : null} {appDetail.menuItems?.share == null || appDetail.menuItems?.share ? (
{t('menuSheet.share')}
) : null} {appDetail.menuItems?.guidance == null || appDetail.menuItems?.guidance ? (
使い方
ガイド
) : null} {appDetail.menuItems?.help == null || appDetail.menuItems?.help ? (
{t('menuSheet.help')}
) : null} ); }; const renderBody = () => { return alertBoxName === 'showMenusheet' ? (
{renderMenuItems()}
) : alertBoxName === 'openApp' ? (
{t('openApp.openExternalApp')}
{t('openApp.areYouSure')}
) : alertBoxName === 'getContactList' ? (
{t('openContact.text')}
) : alertBoxName === 'share' ? (

{text}

imageurl
) : ( <>

{text}

{renderScanCodeInput()} ); }; const renderScanCodeInput = () => { return alertBoxName === 'scanCode' ? ( ) : null; }; const renderFooter = () => { return ( <> {['scanCode', 'openApp', 'getContactList'].indexOf(alertBoxName) > -1 ? ( ) : null} ); }; return (
{renderTopIcon()}
{renderTitle()}
{renderBody()}
{renderFooter()}
); }; export default ShowAlert;