/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ import * as React from 'react' import { StyledWrapper, StyledHeader, StyledTitle, StyledBalance, StyledBalanceTokens, StyledContent, StyledAction, StyledActionIcon, StyledCopy, StyledCopyImage, StyledIconAction, StyledBalanceConverted, StyledGrantWrapper, StyledGrant, StyledActionWrapper, StyledBalanceCurrency, StyledCurve, StyledAlertWrapper, StyledAlertClose, StyleGrantButton, StyledActionText, StyledBAT, StyledNotificationIcon, StyledNotificationCloseIcon, StyledTypeText, StyledMessageText, StyledDateText, StyledNotificationContent, StyledButtonWrapper, StyledButton, StyledNotificationMessage, StyledPipe } from './style' import { getLocale } from '../../../helpers' import { GrantCaptcha, GrantComplete, GrantError, GrantWrapper } from '../' import Alert, { Type as AlertType } from '../alert' import { Button } from '../../../components' import { CaratDownIcon, CaratUpIcon, CloseCircleOIcon, SettingsAdvancedIcon, UpholdColorIcon, UpholdSystemIcon } from '../../../components/icons' import giftIconUrl from './assets/gift.svg' import loveIconUrl from './assets/love.svg' import megaphoneIconUrl from './assets/megaphone.svg' type Grant = { tokens: string, expireDate: string } type GrantClaim = { promotionId?: string altcurrency?: string probi: string expiryTime: number captcha?: string hint?: string status?: 'wrongPosition' | 'grantGone' | 'generalError' | 'grantAlreadyClaimed' | number | null } export interface AlertWallet { node: React.ReactNode type: AlertType onAlertClose?: () => void } export interface ActionWallet { icon: React.ReactNode name: string action: () => void } export type NotificationType = 'ads' | 'ads-launch' | 'backupWallet' | 'contribute' | 'grant' | 'insufficientFunds' | 'tipsProcessed' | 'error' | '' export interface Notification { id: string date?: string type: NotificationType text: React.ReactNode onCloseNotification: (id: string) => void } export interface Props { balance: string converted: string | null actions: ActionWallet[] connectedWallet?: boolean compact?: boolean contentPadding?: boolean showCopy?: boolean children?: React.ReactNode showSecActions?: boolean onSettingsClick?: () => void onActivityClick?: () => void grants?: Grant[] grant?: GrantClaim alert?: AlertWallet | null id?: string gradientTop?: string isMobile?: boolean notification?: Notification onNotificationClick?: () => void onGrantHide?: () => void onFinish?: () => void onSolution?: (x: number, y: number) => void convertProbiToFixed?: (probi: string, place: number) => string } export type Step = '' | 'captcha' | 'complete' interface State { grantDetails: boolean } export default class WalletWrapper extends React.PureComponent { constructor (props: Props) { super(props) this.state = { grantDetails: false } } generateActions (actions: { icon: React.ReactNode, name: string, action: () => void }[], id?: string) { return actions && actions.map((action, i: number) => { return ( {action.icon} {action.name} ) }) } onNotificationClick = () => { if (this.props.onNotificationClick) { this.props.onNotificationClick() } } onGrantHide = () => { if (this.props.onGrantHide) { this.props.onGrantHide() } } onFinish = () => { if (this.props.onFinish) { this.props.onFinish() } } onSolution = (x: number, y: number) => { if (this.props.onSolution) { this.props.onSolution(x, y) } } grantCaptcha = () => { const { grant } = this.props const status = grant && grant.status if (!grant || !grant.promotionId) { return } if (status === 'grantGone') { return ( ) } if (status === 'grantAlreadyClaimed') { return ( ) } if (status === 'generalError') { return ( ) } if (!grant.captcha || !grant.hint) { return } return ( ) } getNotificationButton = (type: NotificationType, onClose: any) => { let buttonText = 'OK' let buttonAction = onClose switch (type) { case 'grant': buttonText = getLocale('claim') buttonAction = this.onNotificationClick break case 'ads-launch': buttonText = getLocale('turnOnAds') buttonAction = this.onNotificationClick break case 'backupWallet': buttonText = getLocale('backupNow') buttonAction = this.onNotificationClick break default: buttonText = getLocale('ok').toUpperCase() break } return ( ) } generateNotification = (notification: Notification | undefined) => { if (!notification) { return null } const onClose = notification.onCloseNotification.bind(this, notification.id) return ( <> {this.getNotificationIcon(notification)} {this.getNotificationMessage(notification)} {this.getNotificationButton(notification.type, onClose)} ) } toggleGrantDetails = () => { this.setState({ grantDetails: !this.state.grantDetails }) } hasGrants = (grants?: Grant[]) => { return grants && grants.length > 0 } getNotificationIcon = (notification: Notification) => { let icon switch (notification.type) { case 'ads': case 'ads-launch': case 'backupWallet': icon = megaphoneIconUrl break case 'contribute': case 'tipsProcessed': icon = loveIconUrl break case 'grant': icon = giftIconUrl break case 'insufficientFunds': icon = megaphoneIconUrl break default: icon = '' break } if (!icon) { return null } return ( ) } getNotificationMessage = (notification: Notification) => { let typeText switch (notification.type) { case 'ads': typeText = getLocale('braveAdsTitle') break case 'ads-launch': typeText = getLocale('braveAdsLaunchTitle') break case 'backupWallet': typeText = getLocale('backupWalletTitle') break case 'contribute': typeText = getLocale('braveContributeTitle') break case 'grant': typeText = getLocale('tokenGrant') break case 'insufficientFunds': typeText = getLocale('insufficientFunds') break case 'tipsProcessed': typeText = getLocale('contributionTips') break default: typeText = '' break } return ( {typeText} | {notification.text} { notification.date ? {notification.date} : null } ) } render () { const { id, children, balance, converted, actions, showCopy, connectedWallet, compact, contentPadding, showSecActions, grants, grant, onSettingsClick, alert, gradientTop, notification, isMobile, convertProbiToFixed } = this.props const hasGrants = this.hasGrants(grants) let tokens = '0.0' if (grant && grant.probi && convertProbiToFixed) { tokens = convertProbiToFixed(grant.probi, 1) } return ( <> { grant && !grant.expiryTime ? this.grantCaptcha() : null } { grant && grant.expiryTime ? : null } { !notification ? <> { alert && alert.node ? { alert.onAlertClose ? : null } {alert.node} : null } {getLocale('yourWallet')} { showSecActions ? : null } {balance} BAT { converted ? {converted} : null } { hasGrants ?