Skip to content

Commit 572ba3c

Browse files
committed
refactoring: share basic scanner
1 parent a3fbe79 commit 572ba3c

File tree

13 files changed

+228
-179
lines changed

13 files changed

+228
-179
lines changed

_locales/_untranslated_en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
"message": "Are you sure you want to add this transport?"
100100
},
101101
"confirm_remove_transport": {
102-
"message": "Are you sure you want to remove this?"
102+
"message": "Are you sure you want to remove this transport? %1$s"
103+
},
104+
"invalid_transport_qr": {
105+
"message": "The scanned QR code does not contain a valid transport"
103106
}
104107
}

packages/frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@breezystack/lamejs": "^1.2.7",
2626
"@deltachat-desktop/runtime-interface": "link:../runtime",
2727
"@deltachat-desktop/shared": "link:../shared",
28-
"@deltachat/jsonrpc-client": "link:../../../core/deltachat-jsonrpc/typescript",
28+
"@deltachat/jsonrpc-client": "catalog:",
2929
"@emoji-mart/data": "^1.2.1",
3030
"@emoji-mart/react": "^1.1.1",
3131
"@jcoreio/async-throttle": "^1.6.1",

packages/frontend/src/components/dialogs/TransportScanner.tsx renamed to packages/frontend/src/components/dialogs/BasicScanner.tsx

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ import Dialog, { DialogBody, DialogFooter, FooterActions } from '../Dialog'
44
import FooterActionButton from '../Dialog/FooterActionButton'
55
import { QrReader, QrCodeScanRef } from '../QrReader'
66
import useTranslationFunction from '../../hooks/useTranslationFunction'
7-
import { processQr } from '../../backend/qr'
87
import useAlertDialog from '../../hooks/dialog/useAlertDialog'
9-
import { selectedAccountId } from '../../ScreenController'
108

119
import type { DialogProps } from '../../contexts/DialogContext'
1210

1311
/**
14-
* QR code scanner for transport configuration
15-
* Processes scanned QR code and calls onSuccess if it is a valid transport QR code
16-
* copied from dialogs/QrCode.tsx
12+
* Basic QR code scanner
13+
* Just returns the scanned QR code as string
1714
*/
18-
export default function TransportQrScanner({
15+
export default function BasicQrScanner({
1916
onSuccess,
2017
onClose,
2118
}: DialogProps & { onSuccess: (result: string) => void }) {
2219
const tx = useTranslationFunction()
23-
const accountId = selectedAccountId()
2420
const openAlertDialog = useAlertDialog()
2521
const qrReaderRef = useRef<QrCodeScanRef | null>(null)
2622

@@ -29,25 +25,18 @@ export default function TransportQrScanner({
2925
const errorMessage = error?.message || error.toString()
3026
openAlertDialog({
3127
message: `${tx('qrscan_failed')} ${errorMessage}`,
32-
dataTestid: 'transport-scan-failed',
28+
dataTestid: 'scan-failed',
3329
})
3430
},
3531
[openAlertDialog, tx]
3632
)
3733

3834
const handleScan = useCallback(
3935
async (data: string) => {
40-
if (data) {
41-
const { qr } = await processQr(accountId, data)
42-
if (qr.kind === 'account') {
43-
onSuccess(data)
44-
onClose()
45-
} else {
46-
handleError(new Error('Invalid transport QR code'))
47-
}
48-
}
36+
onSuccess(data)
37+
onClose()
4938
},
50-
[onSuccess, onClose, accountId, handleError]
39+
[onSuccess, onClose]
5140
)
5241

5342
const pasteClipboard = useCallback(async () => {

packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { getLogger } from '@deltachat-desktop/shared/logger'
1919
import { unknownErrorToString } from '../../helpers/unknownErrorToString'
2020

2121
import ProxyItemRow from './ProxyItemRow'
22-
import ProxyQrScanner from '../ProxyQrScanner'
22+
import { processQr } from '../../../backend/qr'
23+
import BasicQrScanner from '../BasicScanner'
2324

2425
const log = getLogger('proxy-configuration')
2526

@@ -210,12 +211,21 @@ export default function ProxyConfiguration(
210211
)
211212

212213
const openQrScanner = useCallback(() => {
213-
openDialog(ProxyQrScanner, {
214-
onSuccess: (result: string) => {
215-
addProxy(result)
214+
openDialog(BasicQrScanner, {
215+
onSuccess: async (result: string) => {
216+
if (result) {
217+
const { qr } = await processQr(accountId, result)
218+
if (qr.kind === 'proxy') {
219+
addProxy(result)
220+
} else {
221+
openAlertDialog({
222+
message: tx('proxy_invalid'),
223+
})
224+
}
225+
}
216226
},
217227
})
218-
}, [openDialog, addProxy])
228+
}, [openDialog, accountId, addProxy, openAlertDialog, tx])
219229

220230
const changeActiveProxy = useCallback(
221231
(proxyUrl: string) => {

packages/frontend/src/components/dialogs/ProxyQrScanner.tsx

Lines changed: 0 additions & 80 deletions
This file was deleted.

packages/frontend/src/components/dialogs/Transports/index.tsx

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { BackendRemote } from '../../../backend-com'
55

66
import useTranslationFunction from '../../../hooks/useTranslationFunction'
77
import useConfirmationDialog from '../../../hooks/dialog/useConfirmationDialog'
8-
import TransportQrScanner from '../TransportScanner'
8+
import useAlertDialog from '../../../hooks/dialog/useAlertDialog'
9+
import BasicQrScanner from '../BasicScanner'
910
import EditAccountAndPasswordDialog from '../EditAccountAndPasswordDialog'
1011
import Button from '../../Button'
1112

@@ -14,10 +15,10 @@ import styles from './styles.module.scss'
1415
import { EnteredLoginParam } from '@deltachat/jsonrpc-client/dist/generated/types'
1516
import classNames from 'classnames'
1617
import useDialog from '../../../hooks/dialog/useDialog'
18+
import { processQr } from '../../../backend/qr'
1719

1820
/**
1921
* Dialog for transports configuration
20-
2122
*/
2223
export default function TransportsDialog(
2324
props: DialogProps & {
@@ -27,7 +28,7 @@ export default function TransportsDialog(
2728
}
2829
) {
2930
const tx = useTranslationFunction()
30-
31+
const openAlertDialog = useAlertDialog()
3132
const { accountId, onClose } = props
3233

3334
// used in new transport form
@@ -50,63 +51,67 @@ export default function TransportsDialog(
5051
fetchTransports()
5152
}, [accountId])
5253

53-
// const addTransport = useCallback(async (transport: EnteredLoginParam) => {
54-
// await BackendRemote.rpc
55-
// .addTransport(accountId, transport)
56-
// .then(() => {
57-
// // refresh transport list
58-
// setTransports((prev) => [
59-
// ...prev,
60-
// transport,
61-
// ])
62-
// })
63-
// }, [accountId])
64-
6554
const { openDialog } = useDialog()
6655
const openConfirmationDialog = useConfirmationDialog()
6756

68-
const changeDefaultTransport = (transport: EnteredLoginParam) => {
69-
const setDefaultConfig = async (addr: string) => {
70-
await BackendRemote.rpc.setConfig(accountId, 'configured_addr', addr)
57+
const changeDefaultTransport = useCallback(
58+
async (transport: EnteredLoginParam) => {
59+
await BackendRemote.rpc.setConfig(
60+
accountId,
61+
'configured_addr',
62+
transport.addr
63+
)
7164
await BackendRemote.rpc.stopIo(accountId)
7265
await BackendRemote.rpc.startIo(accountId)
7366
setTransports(prev =>
7467
prev.map(t => ({ ...t, isDefault: t.addr === transport.addr }))
7568
)
76-
}
77-
setDefaultConfig(transport.addr)
78-
}
69+
},
70+
[accountId]
71+
)
7972

8073
const openQrScanner = useCallback(() => {
81-
openDialog(TransportQrScanner, {
82-
onSuccess: (result: string) => {
83-
BackendRemote.rpc.addTransportFromQr(accountId, result).then(() => {
74+
openDialog(BasicQrScanner, {
75+
onSuccess: async (result: string) => {
76+
const { qr } = await processQr(accountId, result)
77+
if (qr.kind === 'account') {
78+
await BackendRemote.rpc.addTransportFromQr(accountId, result)
8479
// refresh transport list
8580
getTransports()
86-
})
81+
} else {
82+
openAlertDialog({
83+
message: tx('invalid_transport_qr'),
84+
})
85+
}
8786
},
8887
})
89-
}, [openDialog, accountId, getTransports])
88+
}, [openDialog, accountId, getTransports, openAlertDialog, tx])
9089

9190
useEffect(() => {
9291
getTransports()
9392
}, [getTransports])
9493

95-
const deleteTransport = async (transport: EnteredLoginParam) => {
96-
const confirmed = await openConfirmationDialog({
97-
message: tx('confirm_remove_transport', transport.addr),
98-
})
99-
if (confirmed) {
100-
await BackendRemote.rpc.deleteTransport(accountId, transport.addr)
101-
setTransports(prev => prev.filter(t => t.addr !== transport.addr))
102-
}
103-
}
94+
const deleteTransport = useCallback(
95+
async (transport: EnteredLoginParam) => {
96+
const confirmed = await openConfirmationDialog({
97+
message: tx('confirm_remove_transport', transport.addr),
98+
})
99+
if (confirmed) {
100+
await BackendRemote.rpc.deleteTransport(accountId, transport.addr)
101+
setTransports(prev => prev.filter(t => t.addr !== transport.addr))
102+
}
103+
},
104+
[openConfirmationDialog, tx, accountId]
105+
)
104106

105-
const editTransport = (transport: EnteredLoginParam) => {
106-
openDialog(EditAccountAndPasswordDialog, {
107-
addr: transport.addr,
108-
})
109-
}
107+
const editTransport = useCallback(
108+
(transport: EnteredLoginParam) => {
109+
openDialog(EditAccountAndPasswordDialog, {
110+
addr: transport.addr,
111+
})
112+
},
113+
[openDialog]
114+
)
110115

111116
return (
112117
<Dialog

packages/frontend/src/components/dialogs/Transports/styles.module.scss

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
display: flex;
3030
flex-grow: 1;
3131
padding-inline-end: 10px;
32-
&:hover {
33-
background-color: var(--globalHoverBg);
34-
}
3532
}
3633
.transportItem {
3734
cursor: pointer;
@@ -69,13 +66,6 @@ $iconSize: 1.3em;
6966
margin-bottom: 20px;
7067
}
7168

72-
.buttons {
73-
display: flex;
74-
button {
75-
padding: 6px;
76-
}
77-
}
78-
7969
.addTransportButton {
8070
padding: 8px 10px 7px 10px;
8171
}

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"dependencies": {
1010
"@deltachat-desktop/shared": "link:../shared",
11-
"@deltachat/jsonrpc-client": "link:../../../core/deltachat-jsonrpc/typescript",
11+
"@deltachat/jsonrpc-client": "catalog:",
1212
"@types/node": "catalog:"
1313
}
1414
}

packages/target-browser/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"dependencies": {
1818
"@deltachat-desktop/runtime-interface": "link:../runtime",
1919
"@deltachat-desktop/shared": "link:../shared",
20-
"@deltachat/jsonrpc-client": "link:../../../core/deltachat-jsonrpc/typescript",
21-
"@deltachat/stdio-rpc-server": "link:../../../core/deltachat-rpc-server/npm-package",
20+
"@deltachat/jsonrpc-client": "catalog:",
21+
"@deltachat/stdio-rpc-server": "catalog:",
2222
"@types/express-session": "^1.18.0",
2323
"@types/node-localstorage": "^1.3.3",
2424
"express": "^4.20.0",

0 commit comments

Comments
 (0)