BlueWallet/components/QRCodeComponent.tsx

137 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-08-26 21:31:36 -04:00
import React, { useRef } from 'react';
2021-10-08 12:58:48 -04:00
import { View, StyleSheet, Platform } from 'react-native';
2021-08-26 21:31:36 -04:00
import QRCode from 'react-native-qrcode-svg';
import ToolTipMenu from './TooltipMenu';
import Share from 'react-native-share';
import loc from '../loc';
2021-10-08 12:58:48 -04:00
import Clipboard from '@react-native-clipboard/clipboard';
import { useTheme } from '@react-navigation/native';
2021-08-26 21:31:36 -04:00
2023-03-23 22:42:56 -04:00
interface QRCodeComponentProps {
value: string;
isLogoRendered?: boolean;
isMenuAvailable?: boolean;
logoSize?: number;
size?: number;
ecl?: 'H' | 'Q' | 'M' | 'L';
onError?: () => void;
}
interface ActionIcons {
iconType: 'SYSTEM';
iconValue: string;
}
interface ActionType {
Share: 'share';
Copy: 'copy';
}
interface Action {
id: string;
text: string;
icon: ActionIcons;
}
const actionKeys: ActionType = {
Share: 'share',
Copy: 'copy',
};
interface ActionIcons {
iconType: 'SYSTEM';
iconValue: string;
}
const actionIcons: { [key: string]: ActionIcons } = {
Share: {
iconType: 'SYSTEM',
iconValue: 'square.and.arrow.up',
},
Copy: {
iconType: 'SYSTEM',
iconValue: 'doc.on.doc',
},
};
const QRCodeComponent: React.FC<QRCodeComponentProps> = ({
value = '',
2021-08-26 21:31:36 -04:00
isLogoRendered = true,
isMenuAvailable = true,
logoSize = 90,
2021-08-28 02:12:33 -04:00
size = 300,
2021-08-26 21:31:36 -04:00
ecl = 'H',
onError = () => {},
}) => {
2023-03-23 22:42:56 -04:00
const qrCode = useRef<any>();
2021-08-26 21:31:36 -04:00
const { colors } = useTheme();
const handleShareQRCode = () => {
2023-03-23 22:42:56 -04:00
qrCode.current.toDataURL((data: string) => {
2021-08-26 21:31:36 -04:00
const shareImageBase64 = {
url: `data:image/png;base64,${data}`,
};
2023-03-23 22:42:56 -04:00
Share.open(shareImageBase64).catch((error: any) => console.log(error));
2021-08-26 21:31:36 -04:00
});
};
2023-03-23 22:42:56 -04:00
const onPressMenuItem = (id: string) => {
if (id === actionKeys.Share) {
2021-10-08 12:58:48 -04:00
handleShareQRCode();
2023-03-23 22:42:56 -04:00
} else if (id === actionKeys.Copy) {
2021-10-08 12:58:48 -04:00
qrCode.current.toDataURL(Clipboard.setImage);
}
};
2023-03-23 22:42:56 -04:00
const menuActions = (): Action[] => {
const actions: Action[] = [];
2021-10-08 12:58:48 -04:00
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
actions.push({
2023-03-23 22:42:56 -04:00
id: actionKeys.Copy,
2021-10-08 12:58:48 -04:00
text: loc.transactions.details_copy,
2023-03-23 22:42:56 -04:00
icon: actionIcons.Copy,
2021-10-08 12:58:48 -04:00
});
}
actions.push({
2023-03-23 22:42:56 -04:00
id: actionKeys.Share,
2021-10-08 12:58:48 -04:00
text: loc.receive.details_share,
2023-03-23 22:42:56 -04:00
icon: actionIcons.Share,
2021-10-08 12:58:48 -04:00
});
return actions;
};
2021-08-26 21:31:36 -04:00
const renderQRCode = (
<QRCode
value={value}
{...(isLogoRendered ? { logo: require('../img/qr-code.png') } : {})}
size={size}
logoSize={logoSize}
color="#000000"
2023-03-23 22:42:56 -04:00
// @ts-ignore: logoBackgroundColor is not in the type definition
2021-08-26 21:31:36 -04:00
logoBackgroundColor={colors.brandingColor}
backgroundColor="#FFFFFF"
ecl={ecl}
2023-03-23 22:42:56 -04:00
getRef={(c: any) => (qrCode.current = c)}
2021-08-26 21:31:36 -04:00
onError={onError}
/>
);
return (
<View style={styles.qrCodeContainer} testID="BitcoinAddressQRCodeContainer">
{isMenuAvailable ? (
2021-10-08 12:58:48 -04:00
<ToolTipMenu actions={menuActions()} onPressMenuItem={onPressMenuItem}>
2021-08-26 21:31:36 -04:00
{renderQRCode}
</ToolTipMenu>
) : (
renderQRCode
)}
</View>
);
};
export default QRCodeComponent;
const styles = StyleSheet.create({
qrCodeContainer: { borderWidth: 6, borderRadius: 8, borderColor: '#FFFFFF' },
});