Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/components/Common/QRCode.js
import React, { useState } from 'react'; | import React, { useState } from 'react'; | ||||
import styled from 'styled-components'; | import styled from 'styled-components'; | ||||
import RawQRCode from 'qrcode.react'; | import RawQRCode from 'qrcode.react'; | ||||
import { currency } from '@components/Common/Ticker.js'; | import { | ||||
currency, | |||||
isValidCashPrefix, | |||||
isValidTokenPrefix, | |||||
} from '@components/Common/Ticker.js'; | |||||
import { CopyToClipboard } from 'react-copy-to-clipboard'; | import { CopyToClipboard } from 'react-copy-to-clipboard'; | ||||
import { Event } from '@utils/GoogleAnalytics'; | import { Event } from '@utils/GoogleAnalytics'; | ||||
import { convertToEcashPrefix } from '@utils/cashMethods'; | |||||
export const StyledRawQRCode = styled(RawQRCode)` | export const StyledRawQRCode = styled(RawQRCode)` | ||||
cursor: pointer; | cursor: pointer; | ||||
border-radius: 23px; | border-radius: 23px; | ||||
background: ${props => props.theme.qr.background}; | background: ${props => props.theme.qr.background}; | ||||
box-shadow: ${props => props.theme.qr.shadow}; | box-shadow: ${props => props.theme.qr.shadow}; | ||||
margin-bottom: 10px; | margin-bottom: 10px; | ||||
border: 1px solid ${props => props.theme.wallet.borders.color}; | border: 1px solid ${props => props.theme.wallet.borders.color}; | ||||
Show All 14 Lines | |||||
const Copied = styled.div` | const Copied = styled.div` | ||||
font-size: 18px; | font-size: 18px; | ||||
font-weight: bold; | font-weight: bold; | ||||
width: 100%; | width: 100%; | ||||
text-align: center; | text-align: center; | ||||
background-color: ${({ bch = 0, ...props }) => | background-color: ${({ bch = 0, ...props }) => | ||||
bch === 1 ? props.theme.primary : props.theme.qr.token}; | bch === 1 ? props.theme.primary : props.theme.qr.token}; | ||||
border: 1px solid; | |||||
border-color: ${({ bch = 0, ...props }) => | |||||
bch === 1 | |||||
? props.theme.qr.copyBorderCash | |||||
: props.theme.qr.copyBorderToken}; | |||||
color: ${props => props.theme.contrast}; | color: ${props => props.theme.contrast}; | ||||
position: absolute; | position: absolute; | ||||
top: 65px; | top: 65px; | ||||
padding: 30px 0; | padding: 30px 0; | ||||
@media (max-width: 768px) { | @media (max-width: 768px) { | ||||
top: 52px; | top: 52px; | ||||
padding: 20px 0; | padding: 20px 0; | ||||
} | } | ||||
`; | `; | ||||
const CustomInput = styled.div` | const CustomInput = styled.div` | ||||
font-size: 15px; | font-size: 15px; | ||||
color: ${props => props.theme.wallet.text.secondary}; | color: ${props => props.theme.wallet.text.secondary}; | ||||
text-align: center; | text-align: center; | ||||
cursor: pointer; | cursor: pointer; | ||||
margin-bottom: 15px; | margin-bottom: 0px; | ||||
padding: 10px 0; | padding: 6px 0; | ||||
font-family: 'Roboto Mono', monospace; | font-family: 'Roboto Mono', monospace; | ||||
border-radius: 5px; | border-radius: 5px; | ||||
span { | span { | ||||
font-weight: bold; | font-weight: bold; | ||||
color: ${props => props.theme.wallet.text.primary}; | color: ${props => props.theme.wallet.text.primary}; | ||||
font-size: 16px; | font-size: 16px; | ||||
} | } | ||||
Show All 38 Lines | @media (max-width: 340px) { | ||||
font-size: 11px; | font-size: 11px; | ||||
margin-bottom: 10px; | margin-bottom: 10px; | ||||
} | } | ||||
} | } | ||||
`; | `; | ||||
export const QRCode = ({ | export const QRCode = ({ | ||||
address, | address, | ||||
legacy, | |||||
size = 210, | size = 210, | ||||
onClick = () => null, | onClick = () => null, | ||||
...otherProps | ...otherProps | ||||
}) => { | }) => { | ||||
const [visible, setVisible] = useState(false); | const [visible, setVisible] = useState(false); | ||||
const trimAmount = 6; | const trimAmount = 8; | ||||
// Set address format to legacy or not | |||||
if (!legacy) { | |||||
address = address ? convertToEcashPrefix(address) : ''; | |||||
} | |||||
// get the prefix | |||||
const addressSplit = address ? address.split(':') : ['']; | |||||
const addressPrefix = addressSplit[0]; | |||||
const prefixLength = addressPrefix.length + 1; | |||||
const isCash = isValidCashPrefix(address); | |||||
const address_trim = address ? address.length - trimAmount : ''; | const address_trim = address ? address.length - trimAmount : ''; | ||||
const txtRef = React.useRef(null); | const txtRef = React.useRef(null); | ||||
const handleOnClick = evt => { | const handleOnClick = evt => { | ||||
setVisible(true); | setVisible(true); | ||||
setTimeout(() => { | setTimeout(() => { | ||||
setVisible(false); | setVisible(false); | ||||
}, 1500); | }, 1500); | ||||
onClick(evt); | onClick(evt); | ||||
}; | }; | ||||
const handleOnCopy = () => { | const handleOnCopy = () => { | ||||
// Event.("Category", "Action", "Label") | // Event.("Category", "Action", "Label") | ||||
// BCH or slp? | // BCH or slp? | ||||
let eventLabel = currency.ticker; | let eventLabel = currency.ticker; | ||||
if (address) { | if (address) { | ||||
const isToken = address.includes(currency.tokenPrefix); | const isToken = isValidTokenPrefix(address); | ||||
if (isToken) { | if (isToken) { | ||||
eventLabel = currency.tokenTicker; | eventLabel = currency.tokenTicker; | ||||
} | } | ||||
// Event('Category', 'Action', 'Label') | // Event('Category', 'Action', 'Label') | ||||
Event('Wallet', 'Copy Address', eventLabel); | Event('Wallet', 'Copy Address', eventLabel); | ||||
} | } | ||||
setVisible(true); | setVisible(true); | ||||
Show All 9 Lines | return ( | ||||
width: '100%', | width: '100%', | ||||
position: 'relative', | position: 'relative', | ||||
}} | }} | ||||
text={address} | text={address} | ||||
onCopy={handleOnCopy} | onCopy={handleOnCopy} | ||||
> | > | ||||
<div style={{ position: 'relative' }} onClick={handleOnClick}> | <div style={{ position: 'relative' }} onClick={handleOnClick}> | ||||
<Copied | <Copied | ||||
bch={address && address.includes('bitcoin') ? 1 : 0} | bch={address && isCash ? 1 : 0} | ||||
style={{ display: visible ? null : 'none' }} | style={{ display: visible ? null : 'none' }} | ||||
> | > | ||||
Copied | Copied <br /> | ||||
<span style={{ fontSize: '12px' }}>{address}</span> | |||||
</Copied> | </Copied> | ||||
<StyledRawQRCode | <StyledRawQRCode | ||||
id="borderedQRCode" | id="borderedQRCode" | ||||
value={address || ''} | value={address || ''} | ||||
size={size} | size={size} | ||||
bch={address && address.includes('bitcoin') ? 1 : 0} | bch={address && isCash ? 1 : 0} | ||||
renderAs={'svg'} | renderAs={'svg'} | ||||
includeMargin | includeMargin | ||||
imageSettings={{ | imageSettings={{ | ||||
src: | src: | ||||
address && address.includes('bitcoin') | address && isCash | ||||
? currency.logo | ? currency.logo | ||||
: currency.tokenLogo, | : currency.tokenLogo, | ||||
x: null, | x: null, | ||||
y: null, | y: null, | ||||
height: 32, | height: 24, | ||||
width: 32, | width: 24, | ||||
excavate: true, | excavate: true, | ||||
}} | }} | ||||
/> | /> | ||||
{address && ( | {address && ( | ||||
<CustomInput> | <CustomInput> | ||||
<input | <input | ||||
ref={txtRef} | ref={txtRef} | ||||
readOnly | readOnly | ||||
value={address} | value={address} | ||||
spellCheck="false" | spellCheck="false" | ||||
type="text" | type="text" | ||||
/> | /> | ||||
<span> | <span> | ||||
{address.slice( | {address.slice( | ||||
address.includes('bitcoin') ? 12 : 13, | prefixLength, | ||||
address.includes('bitcoin') | prefixLength + trimAmount, | ||||
? 12 + trimAmount | |||||
: 13 + trimAmount, | |||||
)} | )} | ||||
</span> | </span> | ||||
{address.slice( | {address.slice(prefixLength + trimAmount, address_trim)} | ||||
address.includes('bitcoin') | |||||
? 12 + trimAmount | |||||
: 13 + trimAmount, | |||||
address.includes('bitcoin') | |||||
? address_trim | |||||
: address_trim, | |||||
)} | |||||
<span>{address.slice(-trimAmount)}</span> | <span>{address.slice(-trimAmount)}</span> | ||||
</CustomInput> | </CustomInput> | ||||
)} | )} | ||||
</div> | </div> | ||||
</CopyToClipboard> | </CopyToClipboard> | ||||
); | ); | ||||
}; | }; |