2022-09-09 17:18:04 +00:00
import React , { useEffect , useState } from 'react' ;
import { useTranslation } from 'react-i18next' ;
2022-09-15 15:42:33 +00:00
import { useTheme } from '@mui/material/styles' ;
2022-09-09 17:18:04 +00:00
import { Link as LinkRouter } from 'react-router-dom' ;
2022-05-18 09:17:25 +00:00
import {
Avatar ,
Badge ,
Button ,
CircularProgress ,
Dialog ,
DialogContent ,
Divider ,
FormControlLabel ,
Grid ,
IconButton ,
List ,
ListItemAvatar ,
ListItemButton ,
ListItemText ,
ListItem ,
ListItemIcon ,
Switch ,
TextField ,
Tooltip ,
Typography ,
2022-09-09 17:18:04 +00:00
} from '@mui/material' ;
2022-05-18 09:17:25 +00:00
2022-09-15 15:42:33 +00:00
import { EnableTelegramDialog } from '.' ;
2022-09-09 17:18:04 +00:00
import BoltIcon from '@mui/icons-material/Bolt' ;
2022-09-15 15:42:33 +00:00
import SendIcon from '@mui/icons-material/Send' ;
2022-09-09 17:18:04 +00:00
import NumbersIcon from '@mui/icons-material/Numbers' ;
import PasswordIcon from '@mui/icons-material/Password' ;
import ContentCopy from '@mui/icons-material/ContentCopy' ;
import PersonAddAltIcon from '@mui/icons-material/PersonAddAlt' ;
import EmojiEventsIcon from '@mui/icons-material/EmojiEvents' ;
import { UserNinjaIcon , BitcoinIcon } from '../Icons' ;
2022-05-18 09:17:25 +00:00
2022-09-09 17:18:04 +00:00
import { getCookie } from '../../utils/cookies' ;
import { copyToClipboard } from '../../utils/clipboard' ;
import { getWebln } from '../../utils/webln' ;
2022-05-18 09:17:25 +00:00
2022-09-09 17:33:29 +00:00
interface Props {
2022-05-18 09:17:25 +00:00
isOpen : boolean ;
handleClickCloseProfile : ( ) = > void ;
nickname : string ;
activeOrderId : string | number ;
lastOrderId : string | number ;
referralCode : string ;
2022-09-15 15:42:33 +00:00
tgEnabled : boolean ;
tgBotName : string ;
tgToken : string ;
2022-09-09 17:18:04 +00:00
handleSubmitInvoiceClicked : ( e : any , invoice : string ) = > void ;
2022-05-18 09:17:25 +00:00
host : string ;
showRewardsSpinner : boolean ;
withdrawn : boolean ;
badInvoice : boolean | string ;
earnedRewards : number ;
2022-08-12 17:41:06 +00:00
stealthInvoices : boolean ;
handleSetStealthInvoice : ( stealth : boolean ) = > void ;
2022-05-18 09:17:25 +00:00
setAppState : ( state : any ) = > void ; // TODO: move to a ContextProvider
2022-09-09 17:33:29 +00:00
}
2022-05-18 09:17:25 +00:00
const ProfileDialog = ( {
isOpen ,
handleClickCloseProfile ,
nickname ,
activeOrderId ,
lastOrderId ,
referralCode ,
2022-09-15 15:42:33 +00:00
tgEnabled ,
tgBotName ,
tgToken ,
2022-05-18 09:17:25 +00:00
handleSubmitInvoiceClicked ,
host ,
showRewardsSpinner ,
withdrawn ,
badInvoice ,
earnedRewards ,
setAppState ,
2022-08-12 17:41:06 +00:00
stealthInvoices ,
handleSetStealthInvoice ,
2022-05-18 09:17:25 +00:00
} : Props ) : JSX . Element = > {
const { t } = useTranslation ( ) ;
2022-09-15 15:42:33 +00:00
const theme = useTheme ( ) ;
2022-05-18 09:17:25 +00:00
2022-09-09 17:18:04 +00:00
const [ rewardInvoice , setRewardInvoice ] = useState < string > ( '' ) ;
2022-05-18 09:17:25 +00:00
const [ showRewards , setShowRewards ] = useState < boolean > ( false ) ;
const [ openClaimRewards , setOpenClaimRewards ] = useState < boolean > ( false ) ;
2022-09-09 17:18:04 +00:00
const [ weblnEnabled , setWeblnEnabled ] = useState < boolean > ( false ) ;
2022-09-15 15:42:33 +00:00
const [ openEnableTelegram , setOpenEnableTelegram ] = useState < boolean > ( false ) ;
2022-08-25 08:50:48 +00:00
useEffect ( ( ) = > {
2022-09-09 17:18:04 +00:00
getWebln ( ) . then ( ( webln ) = > {
setWeblnEnabled ( webln !== undefined ) ;
} ) ;
} , [ showRewards ] ) ;
2022-05-18 09:17:25 +00:00
const copyTokenHandler = ( ) = > {
2022-09-09 17:18:04 +00:00
const robotToken = getCookie ( 'robot_token' ) ;
2022-05-18 09:17:25 +00:00
if ( robotToken ) {
2022-07-04 18:42:04 +00:00
copyToClipboard ( robotToken ) ;
2022-09-09 17:18:04 +00:00
setAppState ( { copiedToken : true } ) ;
2022-05-18 09:17:25 +00:00
}
} ;
const copyReferralCodeHandler = ( ) = > {
2022-07-04 18:42:04 +00:00
copyToClipboard ( ` http:// ${ host } /ref/ ${ referralCode } ` ) ;
2022-05-18 09:17:25 +00:00
} ;
2022-09-09 17:18:04 +00:00
const handleWeblnInvoiceClicked = async ( e : any ) = > {
2022-08-25 08:50:48 +00:00
e . preventDefault ( ) ;
if ( earnedRewards ) {
const webln = await getWebln ( ) ;
const invoice = webln . makeInvoice ( earnedRewards ) . then ( ( ) = > {
if ( invoice ) {
2022-09-09 17:18:04 +00:00
handleSubmitInvoiceClicked ( e , invoice . paymentRequest ) ;
2022-08-25 08:50:48 +00:00
}
2022-09-09 17:18:04 +00:00
} ) ;
2022-08-25 08:50:48 +00:00
}
2022-09-09 17:18:04 +00:00
} ;
2022-08-25 08:50:48 +00:00
2022-09-15 15:42:33 +00:00
const handleClickEnableTelegram = ( ) = > {
window . open ( 'https://t.me/' + tgBotName + '?start=' + tgToken , '_blank' ) . focus ( ) ;
setOpenEnableTelegram ( false ) ;
} ;
2022-05-18 09:17:25 +00:00
return (
< Dialog
open = { isOpen }
onClose = { handleClickCloseProfile }
2022-09-09 17:18:04 +00:00
aria - labelledby = 'profile-title'
aria - describedby = 'profile-description'
2022-05-18 09:17:25 +00:00
>
< DialogContent >
2022-09-09 17:18:04 +00:00
< Typography component = 'h5' variant = 'h5' >
{ t ( 'Your Profile' ) }
< / Typography >
2022-05-18 09:17:25 +00:00
< List >
2022-09-09 17:18:04 +00:00
< Divider / >
2022-05-18 09:17:25 +00:00
2022-09-09 17:18:04 +00:00
< ListItem className = 'profileNickname' >
< ListItemText secondary = { t ( 'Your robot' ) } >
< Typography component = 'h6' variant = 'h6' >
2022-05-18 09:17:25 +00:00
{ nickname ? (
2022-09-09 17:18:04 +00:00
< div style = { { position : 'relative' , left : '-7px' } } >
< div
style = { {
display : 'flex' ,
alignItems : 'center' ,
justifyContent : 'left' ,
flexWrap : 'wrap' ,
width : 300 ,
} }
>
< BoltIcon sx = { { color : '#fcba03' , height : '28px' , width : '24px' } } / >
2022-05-18 09:17:25 +00:00
< a > { nickname } < / a >
2022-09-09 17:18:04 +00:00
< BoltIcon sx = { { color : '#fcba03' , height : '28px' , width : '24px' } } / >
2022-05-18 09:17:25 +00:00
< / div >
< / div >
2022-09-09 17:18:04 +00:00
) : null }
2022-05-18 09:17:25 +00:00
< / Typography >
< / ListItemText >
< ListItemAvatar >
2022-09-09 17:18:04 +00:00
< Avatar
className = 'profileAvatar'
sx = { { width : 65 , height : 65 } }
2022-05-18 09:17:25 +00:00
alt = { nickname }
2022-09-09 17:18:04 +00:00
src = {
nickname ? ` ${ window . location . origin } /static/assets/avatars/ ${ nickname } .png ` : ''
}
2022-05-18 09:17:25 +00:00
/ >
< / ListItemAvatar >
< / ListItem >
2022-09-09 17:18:04 +00:00
< Divider / >
2022-05-18 09:17:25 +00:00
{ activeOrderId ? (
< ListItemButton
onClick = { handleClickCloseProfile }
to = { ` /order/ ${ activeOrderId } ` }
component = { LinkRouter }
>
< ListItemIcon >
2022-09-09 17:18:04 +00:00
< Badge badgeContent = '' color = 'primary' >
< NumbersIcon color = 'primary' / >
2022-05-18 09:17:25 +00:00
< / Badge >
< / ListItemIcon >
< ListItemText
2022-09-09 17:18:04 +00:00
primary = { t ( 'One active order #{{orderID}}' , { orderID : activeOrderId } ) }
secondary = { t ( 'Your current order' ) }
2022-05-18 09:17:25 +00:00
/ >
< / ListItemButton >
2022-09-09 17:18:04 +00:00
) : lastOrderId ? (
< ListItemButton
onClick = { handleClickCloseProfile }
to = { ` /order/ ${ lastOrderId } ` }
component = { LinkRouter }
>
< ListItemIcon >
< NumbersIcon color = 'primary' / >
< / ListItemIcon >
< ListItemText
primary = { t ( 'Your last order #{{orderID}}' , { orderID : lastOrderId } ) }
secondary = { t ( 'Inactive order' ) }
/ >
< / ListItemButton >
) : (
< ListItem >
< ListItemIcon >
< NumbersIcon / >
< / ListItemIcon >
< ListItemText
primary = { t ( 'No active orders' ) }
secondary = { t ( 'You do not have previous orders' ) }
/ >
< / ListItem >
) }
2022-05-18 09:17:25 +00:00
< ListItem >
< ListItemIcon >
< PasswordIcon / >
< / ListItemIcon >
2022-09-09 17:18:04 +00:00
< ListItemText secondary = { t ( 'Your token (will not remain here)' ) } >
{ getCookie ( 'robot_token' ) ? (
2022-05-18 09:17:25 +00:00
< TextField
disabled
2022-09-09 17:18:04 +00:00
sx = { { width : '100%' , maxWidth : '450px' } }
label = { t ( 'Back it up!' ) }
value = { getCookie ( 'robot_token' ) }
variant = 'filled'
size = 'small'
2022-05-18 09:17:25 +00:00
InputProps = { {
2022-09-09 17:18:04 +00:00
endAdornment : (
< Tooltip disableHoverListener enterTouchDelay = { 0 } title = { t ( 'Copied!' ) || '' } >
< IconButton onClick = { copyTokenHandler } >
< ContentCopy color = 'inherit' / >
< / IconButton >
< / Tooltip >
) ,
} }
2022-05-18 09:17:25 +00:00
/ >
2022-09-09 17:18:04 +00:00
) : (
t ( 'Cannot remember' )
) }
2022-05-18 09:17:25 +00:00
< / ListItemText >
< / ListItem >
2022-09-09 17:18:04 +00:00
< Divider / >
2022-05-18 09:17:25 +00:00
2022-09-15 15:42:33 +00:00
< EnableTelegramDialog
open = { openEnableTelegram }
onClose = { ( ) = > setOpenEnableTelegram ( false ) }
tgBotName = { tgBotName }
tgToken = { tgToken }
onClickEnable = { handleClickEnableTelegram }
/ >
< ListItem >
< ListItemIcon >
< SendIcon / >
< / ListItemIcon >
< ListItemText >
{ tgEnabled ? (
< Typography color = { theme . palette . success . main } >
< b > { t ( 'Telegram enabled' ) } < / b >
< / Typography >
) : (
< Button color = 'primary' onClick = { ( ) = > setOpenEnableTelegram ( true ) } >
{ t ( 'Enable Telegram Notifications' ) }
< / Button >
) }
< / ListItemText >
< / ListItem >
2022-08-13 12:55:04 +00:00
< ListItem >
< ListItemIcon >
2022-09-09 17:18:04 +00:00
< UserNinjaIcon / >
2022-08-13 12:55:04 +00:00
< / ListItemIcon >
< ListItemText >
2022-09-09 17:18:04 +00:00
< Tooltip
2022-09-15 15:42:33 +00:00
placement = 'bottom'
2022-09-09 17:18:04 +00:00
enterTouchDelay = { 0 }
title = { t (
"Stealth lightning invoices do not contain details about the trade except an order reference. Enable this setting if you don't want to disclose details to a custodial lightning wallet." ,
) }
>
2022-08-13 12:55:04 +00:00
< Grid item >
< FormControlLabel
2022-09-09 17:18:04 +00:00
labelPlacement = 'end'
label = { t ( 'Use stealth invoices' ) }
2022-08-13 12:55:04 +00:00
control = {
< Switch
checked = { stealthInvoices }
2022-09-09 17:18:04 +00:00
onChange = { ( ) = > handleSetStealthInvoice ( ! stealthInvoices ) }
2022-08-13 12:55:04 +00:00
/ >
}
/ >
< / Grid >
< / Tooltip >
< / ListItemText >
< / ListItem >
2022-08-12 17:41:06 +00:00
2022-08-13 12:55:04 +00:00
< ListItem >
< ListItemIcon >
2022-09-09 17:18:04 +00:00
< BitcoinIcon / >
2022-08-13 12:55:04 +00:00
< / ListItemIcon >
< ListItemText >
2022-05-18 09:17:25 +00:00
< FormControlLabel
2022-09-09 17:18:04 +00:00
labelPlacement = 'end'
2022-08-13 12:55:04 +00:00
label = {
2022-09-09 17:18:04 +00:00
< div style = { { display : 'flex' , alignItems : 'center' } } >
{ t ( 'Rewards and compensations' ) }
< / div >
}
2022-05-18 09:17:25 +00:00
control = {
2022-09-09 17:18:04 +00:00
< Switch checked = { showRewards } onChange = { ( ) = > setShowRewards ( ! showRewards ) } / >
2022-05-18 09:17:25 +00:00
}
/ >
2022-08-13 12:55:04 +00:00
< / ListItemText >
< / ListItem >
2022-05-18 09:17:25 +00:00
{ showRewards && (
< >
< ListItem >
< ListItemIcon >
< PersonAddAltIcon / >
< / ListItemIcon >
2022-09-09 17:18:04 +00:00
< ListItemText secondary = { t ( 'Share to earn 100 Sats per trade' ) } >
2022-05-18 09:17:25 +00:00
< TextField
2022-09-09 17:18:04 +00:00
label = { t ( 'Your referral link' ) }
value = { host + '/ref/' + referralCode }
size = 'small'
2022-05-18 09:17:25 +00:00
InputProps = { {
2022-09-09 17:18:04 +00:00
endAdornment : (
< Tooltip
disableHoverListener
enterTouchDelay = { 0 }
title = { t ( 'Copied!' ) || '' }
>
< IconButton onClick = { copyReferralCodeHandler } >
< ContentCopy / >
< / IconButton >
< / Tooltip >
) ,
} }
/ >
2022-05-18 09:17:25 +00:00
< / ListItemText >
< / ListItem >
< ListItem >
< ListItemIcon >
< EmojiEventsIcon / >
< / ListItemIcon >
{ ! openClaimRewards ? (
2022-09-09 17:18:04 +00:00
< ListItemText secondary = { t ( 'Your earned rewards' ) } >
2022-05-18 09:17:25 +00:00
< Grid container >
< Grid item xs = { 9 } >
< Typography > { ` ${ earnedRewards } Sats ` } < / Typography >
< / Grid >
< Grid item xs = { 3 } >
< Button
2022-09-09 17:33:29 +00:00
disabled = { earnedRewards === 0 }
2022-05-18 09:17:25 +00:00
onClick = { ( ) = > setOpenClaimRewards ( true ) }
2022-09-09 17:18:04 +00:00
variant = 'contained'
size = 'small'
2022-05-18 09:17:25 +00:00
>
2022-09-09 17:18:04 +00:00
{ t ( 'Claim' ) }
2022-05-18 09:17:25 +00:00
< / Button >
< / Grid >
< / Grid >
< / ListItemText >
) : (
2022-09-09 17:18:04 +00:00
< form noValidate style = { { maxWidth : 270 } } >
< Grid container style = { { display : 'flex' , alignItems : 'stretch' } } >
< Grid item style = { { display : 'flex' , maxWidth : 160 } } >
2022-05-18 09:17:25 +00:00
< TextField
2022-09-09 17:33:29 +00:00
error = { ! ! badInvoice }
helperText = { badInvoice || '' }
2022-09-09 17:18:04 +00:00
label = { t ( 'Invoice for {{amountSats}} Sats' , {
amountSats : earnedRewards ,
} ) }
size = 'small'
2022-05-18 09:17:25 +00:00
value = { rewardInvoice }
2022-09-09 17:18:04 +00:00
onChange = { ( e ) = > {
2022-05-18 09:17:25 +00:00
setRewardInvoice ( e . target . value ) ;
} }
/ >
< / Grid >
2022-09-09 17:18:04 +00:00
< Grid item alignItems = 'stretch' style = { { display : 'flex' , maxWidth : 80 } } >
2022-05-18 09:17:25 +00:00
< Button
2022-09-09 17:18:04 +00:00
sx = { { maxHeight : 38 } }
2022-05-22 20:56:07 +00:00
onClick = { ( e ) = > handleSubmitInvoiceClicked ( e , rewardInvoice ) }
2022-09-09 17:18:04 +00:00
variant = 'contained'
color = 'primary'
size = 'small'
type = 'submit'
2022-05-18 09:17:25 +00:00
>
2022-09-09 17:18:04 +00:00
{ t ( 'Submit' ) }
2022-05-18 09:17:25 +00:00
< / Button >
< / Grid >
< / Grid >
2022-08-25 08:50:48 +00:00
{ weblnEnabled && (
2022-09-09 17:18:04 +00:00
< Grid container style = { { display : 'flex' , alignItems : 'stretch' } } >
< Grid item alignItems = 'stretch' style = { { display : 'flex' , maxWidth : 240 } } >
2022-08-25 08:50:48 +00:00
< Button
2022-09-09 17:18:04 +00:00
sx = { { maxHeight : 38 , minWidth : 230 } }
2022-09-09 17:33:29 +00:00
onClick = { async ( e ) = > await handleWeblnInvoiceClicked ( e ) }
2022-09-09 17:18:04 +00:00
variant = 'contained'
color = 'primary'
size = 'small'
type = 'submit'
2022-08-25 08:50:48 +00:00
>
2022-09-09 17:18:04 +00:00
{ t ( 'Generate with Webln' ) }
2022-08-25 08:50:48 +00:00
< / Button >
< / Grid >
< / Grid >
) }
2022-05-18 09:17:25 +00:00
< / form >
) }
< / ListItem >
{ showRewardsSpinner && (
2022-09-09 17:18:04 +00:00
< div style = { { display : 'flex' , justifyContent : 'center' } } >
2022-05-18 09:17:25 +00:00
< CircularProgress / >
< / div >
) }
{ withdrawn && (
2022-09-09 17:18:04 +00:00
< div style = { { display : 'flex' , justifyContent : 'center' } } >
< Typography color = 'primary' variant = 'body2' >
< b > { t ( 'There it goes, thank you!🥇' ) } < / b >
2022-05-18 09:17:25 +00:00
< / Typography >
< / div >
) }
< / >
) }
< / List >
< / DialogContent >
< / Dialog >
) ;
} ;
export default ProfileDialog ;