BlueWallet/screen/transactions/details.js

321 lines
9.3 KiB
JavaScript
Raw Normal View History

2020-06-10 18:24:47 -04:00
/* global alert */
2021-08-16 00:43:04 -04:00
import React, { useContext, useEffect, useState } from 'react';
import { View, ScrollView, TouchableOpacity, Text, TextInput, Linking, StatusBar, StyleSheet, Keyboard } from 'react-native';
2020-12-25 19:09:53 +03:00
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
import { BlueCard, BlueCopyToClipboardButton, BlueLoading, BlueSpacing20, BlueText, SafeBlueArea } from '../../BlueComponents';
import navigationStyle from '../../components/navigationStyle';
2021-01-22 11:34:47 -05:00
import HandoffComponent from '../../components/handoff';
2020-07-20 16:38:46 +03:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2021-02-28 00:13:37 -05:00
import Clipboard from '@react-native-clipboard/clipboard';
2021-02-10 00:18:40 -05:00
import ToolTipMenu from '../../components/TooltipMenu';
const dayjs = require('dayjs');
2018-09-11 19:17:24 +01:00
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function arrDiff(a1, a2) {
const ret = [];
for (const v of a2) {
2018-09-11 19:17:24 +01:00
if (a1.indexOf(v) === -1) {
ret.push(v);
}
}
return ret;
}
2020-11-29 22:59:20 -05:00
const TransactionsDetails = () => {
const { setOptions } = useNavigation();
const { hash } = useRoute().params;
const { saveToDisk, txMetadata, wallets, getTransactions } = useContext(BlueStorageContext);
const [from, setFrom] = useState();
const [to, setTo] = useState();
const [isLoading, setIsLoading] = useState(true);
const [tx, setTX] = useState();
const [memo, setMemo] = useState();
const { colors } = useTheme();
const stylesHooks = StyleSheet.create({
txLink: {
color: colors.alternativeTextColor2,
},
saveText: {
color: colors.alternativeTextColor2,
},
memoTextInput: {
borderColor: colors.formBorder,
borderBottomColor: colors.formBorder,
backgroundColor: colors.inputBackgroundColor,
},
greyButton: {
backgroundColor: colors.lightButton,
},
Link: {
color: colors.buttonTextColor,
},
2020-11-29 22:59:20 -05:00
});
useEffect(() => {
setOptions({
headerRight: () => (
<TouchableOpacity accessibilityRole="button" disabled={isLoading} style={styles.save} onPress={handleOnSaveButtonTapped}>
2020-11-29 22:59:20 -05:00
<Text style={stylesHooks.saveText}>{loc.wallets.details_save}</Text>
</TouchableOpacity>
),
headerStyle: {
borderBottomWidth: 0,
elevation: 0,
shadowOpacity: 0,
shadowOffset: { height: 0, width: 0 },
backgroundColor: colors.customHeader,
},
});
// eslint-disable-next-line react-hooks/exhaustive-deps
2020-11-29 23:03:55 -05:00
}, [colors, isLoading, memo]);
2020-11-29 22:59:20 -05:00
useEffect(() => {
2018-03-17 22:39:21 +02:00
let foundTx = {};
let from = [];
let to = [];
for (const tx of getTransactions(null, Infinity, true)) {
2018-01-30 22:42:38 +00:00
if (tx.hash === hash) {
foundTx = tx;
for (const input of foundTx.inputs) {
2018-03-17 22:39:21 +02:00
from = from.concat(input.addresses);
2018-01-30 22:42:38 +00:00
}
for (const output of foundTx.outputs) {
if (output.addresses) to = to.concat(output.addresses);
2019-06-11 00:18:40 +01:00
if (output.scriptPubKey && output.scriptPubKey.addresses) to = to.concat(output.scriptPubKey.addresses);
2018-01-30 22:42:38 +00:00
}
}
}
2020-11-29 22:59:20 -05:00
for (const w of wallets) {
for (const t of w.getTransactions()) {
2018-10-31 20:14:28 +00:00
if (t.hash === hash) {
console.log('tx', hash, 'belongs to', w.getLabel());
}
}
}
2020-11-29 22:59:20 -05:00
if (txMetadata[foundTx.hash]) {
if (txMetadata[foundTx.hash].memo) {
setMemo(txMetadata[foundTx.hash].memo);
2020-06-10 18:24:47 -04:00
}
}
2018-01-30 22:42:38 +00:00
2020-11-29 22:59:20 -05:00
setTX(foundTx);
setFrom(from);
setTo(to);
setIsLoading(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hash, wallets]);
2020-11-29 22:59:20 -05:00
const handleOnSaveButtonTapped = () => {
Keyboard.dismiss();
2020-11-29 22:59:20 -05:00
txMetadata[tx.hash] = { memo };
saveToDisk().then(_success => alert(loc.transactions.transaction_note_saved));
2020-06-10 18:24:47 -04:00
};
2020-11-29 23:03:55 -05:00
const handleOnOpenTransactionOnBlockExporerTapped = () => {
const url = `https://mempool.space/tx/${tx.hash}`;
2020-11-29 23:03:55 -05:00
Linking.canOpenURL(url).then(supported => {
if (supported) {
Linking.openURL(url);
}
});
};
2021-02-10 00:18:40 -05:00
const handleCopyPress = () => {
Clipboard.setString(`https://mempool.space/tx/${tx.hash}`);
2021-02-10 00:18:40 -05:00
};
2020-11-29 22:59:20 -05:00
if (isLoading || !tx) {
return <BlueLoading />;
}
2020-06-10 18:24:47 -04:00
2020-11-29 22:59:20 -05:00
return (
<SafeBlueArea>
2021-01-18 22:40:11 -05:00
<HandoffComponent
title={`Bitcoin Transaction ${tx.hash}`}
type="io.bluewallet.bluewallet"
url={`https://blockstream.info/tx/${tx.hash}`}
/>
2020-11-29 22:59:20 -05:00
<StatusBar barStyle="default" />
<ScrollView style={styles.scroll}>
<BlueCard>
<View>
<TextInput
placeholder={loc.send.details_note_placeholder}
value={memo}
placeholderTextColor="#81868e"
style={[styles.memoTextInput, stylesHooks.memoTextInput]}
onChangeText={setMemo}
/>
<BlueSpacing20 />
</View>
2018-01-30 22:42:38 +00:00
2020-11-29 22:59:20 -05:00
{from && (
<>
<View style={styles.rowHeader}>
<BlueText style={styles.rowCaption}>{loc.transactions.details_from}</BlueText>
2020-11-29 22:59:20 -05:00
<BlueCopyToClipboardButton stringToCopy={from.filter(onlyUnique).join(', ')} />
</View>
<BlueText style={styles.rowValue}>{from.filter(onlyUnique).join(', ')}</BlueText>
</>
)}
2018-01-30 22:42:38 +00:00
2020-11-29 22:59:20 -05:00
{to && (
<>
<View style={styles.rowHeader}>
<BlueText style={styles.rowCaption}>{loc.transactions.details_to}</BlueText>
2020-11-29 22:59:20 -05:00
<BlueCopyToClipboardButton stringToCopy={to.filter(onlyUnique).join(', ')} />
</View>
<BlueText style={styles.rowValue}>{arrDiff(from, to.filter(onlyUnique)).join(', ')}</BlueText>
</>
)}
2020-11-29 22:59:20 -05:00
{tx.fee && (
<>
<BlueText style={styles.rowCaption}>{loc.send.create_fee}</BlueText>
2020-11-29 22:59:20 -05:00
<BlueText style={styles.rowValue}>{tx.fee + ' sats'}</BlueText>
</>
)}
2020-11-29 22:59:20 -05:00
{tx.hash && (
<>
<View style={styles.rowHeader}>
2020-12-21 21:27:49 +03:00
<BlueText style={[styles.txId, stylesHooks.txId]}>{loc.transactions.txid}</BlueText>
2020-11-29 22:59:20 -05:00
<BlueCopyToClipboardButton stringToCopy={tx.hash} />
</View>
2021-04-09 17:07:33 +02:00
<BlueText style={styles.rowValue}>{tx.hash}</BlueText>
2020-11-29 22:59:20 -05:00
</>
)}
2018-12-18 20:46:06 -05:00
2020-11-29 22:59:20 -05:00
{tx.received && (
<>
<BlueText style={styles.rowCaption}>{loc.transactions.details_received}</BlueText>
<BlueText style={styles.rowValue}>{dayjs(tx.received).format('LLL')}</BlueText>
2020-11-29 22:59:20 -05:00
</>
)}
2018-12-18 20:46:06 -05:00
2020-11-29 22:59:20 -05:00
{tx.block_height > 0 && (
<>
<BlueText style={styles.rowCaption}>{loc.transactions.details_block}</BlueText>
2020-11-29 22:59:20 -05:00
<BlueText style={styles.rowValue}>{tx.block_height}</BlueText>
</>
)}
2020-11-29 22:59:20 -05:00
{tx.inputs && (
<>
<BlueText style={styles.rowCaption}>{loc.transactions.details_inputs}</BlueText>
2020-11-29 22:59:20 -05:00
<BlueText style={styles.rowValue}>{tx.inputs.length}</BlueText>
</>
)}
{tx.outputs?.length > 0 && (
2020-11-29 22:59:20 -05:00
<>
<BlueText style={styles.rowCaption}>{loc.transactions.details_outputs}</BlueText>
2020-11-29 22:59:20 -05:00
<BlueText style={styles.rowValue}>{tx.outputs.length}</BlueText>
</>
)}
2021-04-09 17:06:32 -04:00
<ToolTipMenu
2021-09-05 13:35:08 -04:00
isButton
2021-04-09 17:06:32 -04:00
actions={[
{
2021-08-18 00:35:59 -04:00
id: TransactionsDetails.actionKeys.CopyToClipboard,
2021-09-05 13:35:08 -04:00
text: loc.transactions.copy_link,
icon: TransactionsDetails.actionIcons.Clipboard,
2021-04-09 17:06:32 -04:00
},
]}
2021-08-16 00:43:04 -04:00
onPress={handleCopyPress}
>
2021-08-16 00:43:04 -04:00
<TouchableOpacity
accessibilityRole="button"
onPress={handleOnOpenTransactionOnBlockExporerTapped}
style={[styles.greyButton, stylesHooks.greyButton]}
>
<Text style={[styles.Link, stylesHooks.Link]}>{loc.transactions.details_show_in_block_explorer}</Text>
</TouchableOpacity>
</ToolTipMenu>
2020-11-29 22:59:20 -05:00
</BlueCard>
</ScrollView>
</SafeBlueArea>
);
};
TransactionsDetails.actionKeys = {
CopyToClipboard: 'copyToClipboard',
};
TransactionsDetails.actionIcons = {
Clipboard: {
iconType: 'SYSTEM',
2021-08-26 22:00:58 -04:00
iconValue: 'doc.on.doc',
},
};
2020-11-29 22:59:20 -05:00
const styles = StyleSheet.create({
scroll: {
flex: 1,
},
rowHeader: {
flex: 1,
flexDirection: 'row',
marginBottom: 4,
justifyContent: 'space-between',
},
rowCaption: {
fontSize: 16,
fontWeight: '500',
marginBottom: 4,
},
rowValue: {
marginBottom: 26,
color: 'grey',
},
txId: {
fontSize: 16,
fontWeight: '500',
},
txHash: {
marginBottom: 8,
color: 'grey',
},
Link: {
2021-04-09 17:06:32 -04:00
fontWeight: '600',
fontSize: 15,
2020-11-29 22:59:20 -05:00
},
save: {
marginHorizontal: 16,
justifyContent: 'center',
alignItems: 'center',
},
memoTextInput: {
flexDirection: 'row',
borderWidth: 1,
borderBottomWidth: 0.5,
minHeight: 44,
height: 44,
alignItems: 'center',
marginVertical: 8,
borderRadius: 4,
paddingHorizontal: 8,
color: '#81868e',
},
greyButton: {
borderRadius: 9,
minHeight: 49,
paddingHorizontal: 8,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
alignSelf: 'auto',
flexGrow: 1,
marginHorizontal: 4,
},
2020-11-29 22:59:20 -05:00
});
2018-03-18 02:48:23 +00:00
2020-11-29 22:59:20 -05:00
export default TransactionsDetails;
2020-07-15 13:32:59 -04:00
2021-02-15 11:03:54 +03:00
TransactionsDetails.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.transactions.details_title }));