Skip to content

Commit

Permalink
Merge branch 'comma-separate-amounts' into 'dev'
Browse files Browse the repository at this point in the history
Comma separate amounts

See merge request ergo/minotaur/minotaur-wallet!40
  • Loading branch information
vorujack committed Dec 1, 2024
2 parents 515b26a + bce6626 commit 461076d
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-teachers-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'minotaur-wallet': minor
---

Display all values comma separated
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { commaSeparate } from '@/utils/convert';
import { createEmptyArray } from '@/utils/functions';
import React from 'react';

Expand All @@ -6,13 +7,15 @@ interface TokenAmountDisplayPropsType {
decimal: number;
displayDecimal?: number;
}

const TokenAmountDisplay = (props: TokenAmountDisplayPropsType) => {
const amount_str =
createEmptyArray(props.decimal, '0').join('') + props.amount.toString();
const valuePart =
const valuePart = commaSeparate(
amount_str
.substring(0, amount_str.length - props.decimal)
.replace(/^0+/, '') || '0';
.replace(/^0+/, '') || '0',
);
const decimalPart = amount_str.substring(amount_str.length - props.decimal);
const decimalPartTrimmed =
props.displayDecimal === undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const QrCodeDetectedType = (props: QrCodeDetectedTypePropsType) => {
};
useEffect(() => {
if (props.scanned !== checked && !checking) {
console.log("start process detected type")
console.log('start process detected type');
setChecking(true);
const selectedTypes = QrCodeTypes.filter((item) =>
item.detect(props.scanned),
Expand All @@ -45,11 +45,11 @@ const QrCodeDetectedType = (props: QrCodeDetectedTypePropsType) => {
}
}, [checked, checking, props]);
const usedWallet = wallet === undefined ? firstWallet : wallet;
console.log(selectedType, usedWallet)
console.log(selectedType, usedWallet);
if (checking) {
return <LoadingPage/>
return <LoadingPage />;
}
if(usedWallet && selectedType !== undefined && selectedType.render){
if (usedWallet && selectedType !== undefined && selectedType.render) {
return (
<SelectableWalletContext.Provider
value={{ setWallet: storeWallet, wallet: usedWallet }}
Expand All @@ -62,7 +62,7 @@ const QrCodeDetectedType = (props: QrCodeDetectedTypePropsType) => {
{selectedType.render(props.scanned, props.close)}
</TxSignContextHandler>
</SelectableWalletContext.Provider>
)
);
}
};

Expand Down
20 changes: 12 additions & 8 deletions apps/wallet/src/components/qr-code-scanner/QrCodeReaderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ const QrCodeReaderView = (props: QrCodeReaderViewPropsType) => {
}, []);

const senDataToCallbacks = (data: string) => {
callbacks.forEach(item => item.resolve(data));
callbacks.forEach((item) => item.resolve(data));
setCallbacks([]);
setOpen(false);
setData('');
}
};

const stopScanning = () => {
callbacks.forEach((item) => item.reject('Canceled by user'));
Expand All @@ -47,11 +47,11 @@ const QrCodeReaderView = (props: QrCodeReaderViewPropsType) => {
setData('');
setScanning(false);
setOpen(false);
}
};
return (
<QrCodeContext.Provider
value={{
start: startScan
start: startScan,
}}
>
{open && scanning && (
Expand All @@ -62,12 +62,16 @@ const QrCodeReaderView = (props: QrCodeReaderViewPropsType) => {
/>
)}
<div style={{ display: open && !scanning ? 'block' : 'none' }}>
<QrCodeDetectedType scanned={data} open={open} scanning={scanning} close={close} callback={senDataToCallbacks}/>
<QrCodeDetectedType
scanned={data}
open={open}
scanning={scanning}
close={close}
callback={senDataToCallbacks}
/>
</div>

<div style={{ display: open ? 'none' : 'block' }}>
{props.children}
</div>
<div style={{ display: open ? 'none' : 'block' }}>{props.children}</div>
</QrCodeContext.Provider>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ const ColdSigningRequest = (props: ColdSigningRequestPropsType) => {
if (data.sender) {
const filtered = wallets.filter(
(item) =>
item.addresses.filter((address) => address.address === data.sender)
.length > 0,
item.addresses.filter(
(address) => address.address === data.sender,
).length > 0,
);
if (filtered.length >= 1) {
walletContext.setWallet(filtered[0]);
Expand All @@ -68,7 +69,8 @@ const ColdSigningRequest = (props: ColdSigningRequestPropsType) => {
return (
wallet.addresses
.map((address) => address.address)
.filter((address) => inputAddressStr.includes(address)).length > 0
.filter((address) => inputAddressStr.includes(address)).length >
0
);
});
if (filtered.length === 1) {
Expand All @@ -82,7 +84,7 @@ const ColdSigningRequest = (props: ColdSigningRequestPropsType) => {
txSignContext.setReducedTx(reduced);
txSignContext.setTx(reduced.unsigned_tx(), boxes);
setHasError(false);
}catch (exp){
} catch (exp) {
console.error(exp);
setError('Invalid Data Scanned');
setHasError(true);
Expand Down
23 changes: 12 additions & 11 deletions apps/wallet/src/components/qr-code-scanner/reader/QrCodeReader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { readClipBoard } from '@/utils/clipboard';
import { IconButton } from '@mui/material';
import { useCallback, useEffect, useState } from 'react';


interface QrCodeReaderPropsType {
success: (scanned: string) => unknown;
fail: () => unknown;
Expand All @@ -20,28 +19,28 @@ interface QrCodeReaderPropsType {

const QrCodeReader = (props: QrCodeReaderPropsType) => {
const [scanned, setScanned] = useState('');
const {chunks, error, completed} = useChunks(scanned);
const { chunks, error, completed } = useChunks(scanned);
const [multipleCamera, setMultipleCamera] = useState(false);
const readClipboard = () => {
readClipBoard().then((data) => {
newData(data);
});
};
const newData = useCallback((scanned: string) => {
setScanned(scanned)
}, [])
setScanned(scanned);
}, []);
useEffect(() => {
if(completed){
if (completed) {
props.success(completed);
}
}, [completed, props]);
return (
<AppFrame
title="Scan QrCode"
className='barcode-scanner-modal'
className="barcode-scanner-modal"
navigation={<BackButton onClick={props.closeQrCode} />}
actions={
<div style={{zIndex: 1}}>
<div style={{ zIndex: 1 }}>
<IconButton onClick={readClipboard}>
<ContentPasteRounded />
</IconButton>
Expand All @@ -52,11 +51,13 @@ const QrCodeReader = (props: QrCodeReaderPropsType) => {
) : null}
</div>
}
toolbar={
<ScannedChunkStatus error={error} chunks={chunks} />
}
toolbar={<ScannedChunkStatus error={error} chunks={chunks} />}
>
<QrCodeReaderSwitch {...props} setSupportMultipleCamera={setMultipleCamera} success={newData}/>
<QrCodeReaderSwitch
{...props}
setSupportMultipleCamera={setMultipleCamera}
success={newData}
/>
<CameraBox>
<ScanQrBox>
<div className="line" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const QrCodeReaderWeb = (props: QrCodeReaderWebPropsType) => {
left: 0,
objectFit: 'cover',
transform: 'scaleX(-1)',
zIndex: -1
zIndex: -1,
}}
/>
<video/>
<video />
{totalDevices > 1 ? (
<Fab onClick={() => selectNext}>
<CameraswitchOutlined />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import StatusBox from '@/components/qr-code-scanner/reader/StatusBox';
import { Box, Typography } from '@mui/material';


interface ScannedChunkStatusPropsType {
chunks?: Array<string>;
error?: string;
Expand All @@ -13,16 +12,18 @@ const ScannedChunkStatus = (props: ScannedChunkStatusPropsType) => {
<Typography color="warning.light" mb={3}>
{props.error}
</Typography>
<Box display="flex" style={{zIndex: 10000}} flexWrap="wrap" justifyContent="center">
<Box
display="flex"
style={{ zIndex: 10000 }}
flexWrap="wrap"
justifyContent="center"
>
{(props.chunks ?? []).map((item, index) => (
<StatusBox
value={item}
key={index}
/>
<StatusBox value={item} key={index} />
))}
</Box>
</Box>
)
}
);
};

export default ScannedChunkStatus;
11 changes: 7 additions & 4 deletions apps/wallet/src/components/qr-code-scanner/reader/StatusBox.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { CheckCircleOutline, RadioButtonUnchecked } from '@mui/icons-material';
import { Box } from '@mui/material';


interface StatusBoxPropsTypes {
value: string;
}

const StatusBox = (props: StatusBoxPropsTypes) => {
const scanned = props.value !== '';
return (
<Box p={0} display="flex" color={scanned ? "success.light" : undefined}>
{scanned ? <CheckCircleOutline fontSize="small"/> : <RadioButtonUnchecked fontSize="small"/>}
<Box p={0} display="flex" color={scanned ? 'success.light' : undefined}>
{scanned ? (
<CheckCircleOutline fontSize="small" />
) : (
<RadioButtonUnchecked fontSize="small" />
)}
</Box>
);
}
};

export default StatusBox;
52 changes: 31 additions & 21 deletions apps/wallet/src/hooks/useChunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,70 @@ import { useEffect, useState } from 'react';
const useChunks = (scanned: string) => {
const [chunks, setChunks] = useState<Array<string>>([]);
const [completed, setCompleted] = useState<string>('');
const [type, setType] = useState<string>('')
const [type, setType] = useState<string>('');
const [loaded, setLoaded] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
useEffect(() => {
if(!loading && loaded !== scanned) {
setLoading(true)
if (!loading && loaded !== scanned) {
setLoading(true);
try {
const scannedJSON = JSON.parse(scanned);
if (type !== '') {
if (!Object.prototype.hasOwnProperty.call(scannedJSON, type) || scannedJSON.n !== chunks.length) {
setError('It seems qrcode changed. Please close qrcode scanner and try again');
}else if(scannedJSON.p > scannedJSON.n){
if (
!Object.prototype.hasOwnProperty.call(scannedJSON, type) ||
scannedJSON.n !== chunks.length
) {
setError(
'It seems qrcode changed. Please close qrcode scanner and try again',
);
} else if (scannedJSON.p > scannedJSON.n) {
setError('Invalid QrCode Scanned');
} else {
setChunks(chunks => {
setChunks((chunks) => {
const newChunks = [...chunks];
newChunks[scannedJSON.p - 1] = scannedJSON[type]
newChunks[scannedJSON.p - 1] = scannedJSON[type];
return newChunks;
})
});
}
} else {
const newType = Object.keys(scannedJSON).filter(item => !['p', 'n'].includes(item))[0];
const newType = Object.keys(scannedJSON).filter(
(item) => !['p', 'n'].includes(item),
)[0];
setType(newType);
if(!scannedJSON.n || scannedJSON.n === 1){
setCompleted(JSON.stringify({[newType]: scannedJSON[newType]}));
}else{
if (!scannedJSON.n || scannedJSON.n === 1) {
setCompleted(JSON.stringify({ [newType]: scannedJSON[newType] }));
} else {
const newChunks = createEmptyArray(scannedJSON.n, '');
newChunks[scannedJSON.p - 1] = scannedJSON[newType];
setChunks(newChunks)
setChunks(newChunks);
}
}
setLoaded(scanned);
setLoading(false);
} catch (e) {
if (chunks.length === 0) {
setCompleted(scanned)
setCompleted(scanned);
} else {
setError('Invalid Data Scanned')
setError('Invalid Data Scanned');
}
}
}
}, [scanned, loaded, loading, chunks,type]);
}, [scanned, loaded, loading, chunks, type]);
useEffect(() => {
if(chunks.length > 0 && chunks.filter(item => item === '').length === 0){
setCompleted(JSON.stringify({[type]: chunks.join('')}));
if (
chunks.length > 0 &&
chunks.filter((item) => item === '').length === 0
) {
setCompleted(JSON.stringify({ [type]: chunks.join('') }));
}
}, [type, chunks]);
return {
chunks,
error,
loading,
completed
}
completed,
};
};

export default useChunks;
6 changes: 3 additions & 3 deletions apps/wallet/src/hooks/useQrReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const useQrReader = (
},
)
.then((controls) => {
console.log("initialized")
if(controller.current) controller.current.stop();
console.log('initialized');
if (controller.current) controller.current.stop();
controller.current = controls;
})
.catch((error) => {
console.log(`error ${error}`)
console.log(`error ${error}`);
errorCallback(error);
});
return () => {
Expand Down
3 changes: 0 additions & 3 deletions apps/wallet/src/pages/wallet-add/WalletAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import { RouteMap } from '@/router/routerMap';
import ItemCard from './components/item-card/ItemCard';
import BackButtonRouter from '@/components/back-button/BackButtonRouter';




const AddWallet = () => {
return (
<AppFrame title="Add Wallet" navigation={<BackButtonRouter />}>
Expand Down
Loading

0 comments on commit 461076d

Please sign in to comment.