|
1 | 1 | <script lang="ts"> |
2 | 2 | import { WizardModal, type WizardStep, type WizardSteps } from '@dfinity/gix-components'; |
3 | | - import { assertNever, isNullish } from '@dfinity/utils'; |
4 | | - import { setContext } from 'svelte'; |
| 3 | + import { assertNever, isNullish, nonNullish } from '@dfinity/utils'; |
| 4 | + import { setContext, untrack } from 'svelte'; |
5 | 5 | import OpenCryptoPayWizard from '$lib/components/open-crypto-pay/OpenCryptoPayWizard.svelte'; |
6 | 6 | import ScannerCode from '$lib/components/scanner/ScannerCode.svelte'; |
| 7 | + import ScannerInfo from '$lib/components/scanner/ScannerInfo.svelte'; |
7 | 8 | import ScannerModalPayDataLoader from '$lib/components/scanner/ScannerModalPayDataLoader.svelte'; |
8 | 9 | import WalletConnectSessionWizard from '$lib/components/wallet-connect/WalletConnectSessionWizard.svelte'; |
9 | 10 | import { scannerWizardSteps } from '$lib/config/scanner.config'; |
| 11 | + import { modalUniversalScannerData } from '$lib/derived/modal.derived'; |
10 | 12 | import { PLAUSIBLE_EVENTS } from '$lib/enums/plausible'; |
11 | 13 | import { ProgressStepsPayment } from '$lib/enums/progress-steps'; |
12 | 14 | import { WizardStepsScanner } from '$lib/enums/wizard-steps'; |
13 | 15 | import { trackEvent } from '$lib/services/analytics.services'; |
| 16 | + import { connectListener } from '$lib/services/wallet-connect.services'; |
14 | 17 | import { i18n } from '$lib/stores/i18n.store'; |
15 | 18 | import { modalStore } from '$lib/stores/modal.store'; |
16 | 19 | import { |
|
63 | 66 | }); |
64 | 67 | }; |
65 | 68 |
|
66 | | - const onWalletConnectConnect = async () => { |
67 | | - // TODO: implement this function |
| 69 | + const goToScanStep = () => goToStep(WizardStepsScanner.SCAN); |
| 70 | +
|
| 71 | + const goToInfoStep = () => goToStep(WizardStepsScanner.OISY_SCANNER_INFO); |
| 72 | +
|
| 73 | + const startWalletConnect = async (uri: string) => { |
| 74 | + goToStep(WizardStepsScanner.WALLET_CONNECT_REVIEW); |
| 75 | +
|
| 76 | + const { result } = await connectListener({ uri, onSessionDeleteCallback: goToScanStep }); |
| 77 | +
|
| 78 | + if (result === 'error') { |
| 79 | + goToStep(WizardStepsScanner.SCAN); |
| 80 | + } |
| 81 | + }; |
| 82 | +
|
| 83 | + const onWalletConnectConnect = async (uri: string) => { |
| 84 | + await startWalletConnect(uri); |
68 | 85 | }; |
69 | 86 |
|
70 | | - const onNext = (results: ScannerResults) => { |
| 87 | + const onNext = async ({ results, code }: { results: ScannerResults; code?: string }) => { |
71 | 88 | if (results === ScannerResults.PAY) { |
72 | 89 | goToStep(WizardStepsScanner.PAY); |
73 | 90 |
|
74 | 91 | return; |
75 | 92 | } |
76 | 93 |
|
77 | 94 | if (results === ScannerResults.WALLET_CONNECT) { |
78 | | - // TODO: implement wallet connect flow |
| 95 | + if (isNullish(code)) { |
| 96 | + return; |
| 97 | + } |
| 98 | +
|
| 99 | + await startWalletConnect(code); |
79 | 100 |
|
80 | 101 | return; |
81 | 102 | } |
82 | 103 |
|
83 | 104 | assertNever(results, `Unhandled scanner result: ${results}`); |
84 | 105 | }; |
| 106 | +
|
| 107 | + // When WalletConnectSession opens the scanner with a WC URI (deep-link), navigate to WC review once the modal renders |
| 108 | + let walletConnectDeepLinkHandled = $state(false); |
| 109 | +
|
| 110 | + $effect(() => { |
| 111 | + if ( |
| 112 | + !walletConnectDeepLinkHandled && |
| 113 | + nonNullish($modalUniversalScannerData?.walletConnectUri) && |
| 114 | + nonNullish(modal) |
| 115 | + ) { |
| 116 | + walletConnectDeepLinkHandled = true; |
| 117 | +
|
| 118 | + const uri = $modalUniversalScannerData.walletConnectUri; |
| 119 | +
|
| 120 | + untrack(() => startWalletConnect(uri)); |
| 121 | + } |
| 122 | + }); |
85 | 123 | </script> |
86 | 124 |
|
87 | 125 | <ScannerModalPayDataLoader> |
88 | | - <WizardModal |
89 | | - bind:this={modal} |
90 | | - disablePointerEvents={currentStep?.name === WizardStepsScanner.TOKENS_LIST} |
91 | | - {onClose} |
92 | | - {steps} |
93 | | - bind:currentStep |
94 | | - > |
95 | | - {#snippet title()} |
96 | | - {currentStep?.title} |
97 | | - {/snippet} |
98 | | - |
99 | | - {#key currentStep?.name} |
100 | | - {#if currentStep?.name === WizardStepsScanner.SCAN} |
101 | | - <ScannerCode {onNext} /> |
102 | | - {:else if currentStep?.name === WizardStepsScanner.PAY || currentStep?.name === WizardStepsScanner.TOKENS_LIST || currentStep?.name === WizardStepsScanner.PAYING || currentStep?.name === WizardStepsScanner.PAYMENT_FAILED || currentStep?.name === WizardStepsScanner.PAYMENT_CONFIRMED} |
103 | | - <OpenCryptoPayWizard {currentStep} {modal} {steps} bind:payProgressStep /> |
104 | | - {:else if currentStep?.name === WizardStepsScanner.WALLET_CONNECT_CONNECT || currentStep?.name === WizardStepsScanner.WALLET_CONNECT_REVIEW} |
105 | | - <WalletConnectSessionWizard {currentStep} onConnect={onWalletConnectConnect} /> |
106 | | - {/if} |
107 | | - {/key} |
108 | | - </WizardModal> |
| 126 | + <div class="scanner-modal"> |
| 127 | + <WizardModal |
| 128 | + bind:this={modal} |
| 129 | + disablePointerEvents={currentStep?.name === WizardStepsScanner.TOKENS_LIST} |
| 130 | + {onClose} |
| 131 | + {steps} |
| 132 | + bind:currentStep |
| 133 | + > |
| 134 | + {#snippet title()} |
| 135 | + {currentStep?.title} |
| 136 | + {/snippet} |
| 137 | + |
| 138 | + {#key currentStep?.name} |
| 139 | + {#if currentStep?.name === WizardStepsScanner.OISY_SCANNER_INFO} |
| 140 | + <ScannerInfo onButtonClick={goToScanStep} /> |
| 141 | + {:else if currentStep?.name === WizardStepsScanner.SCAN} |
| 142 | + <ScannerCode {onNext} onOpenInfo={goToInfoStep} /> |
| 143 | + {:else if currentStep?.name === WizardStepsScanner.PAY || currentStep?.name === WizardStepsScanner.TOKENS_LIST || currentStep?.name === WizardStepsScanner.PAYING || currentStep?.name === WizardStepsScanner.PAYMENT_FAILED || currentStep?.name === WizardStepsScanner.PAYMENT_CONFIRMED} |
| 144 | + <OpenCryptoPayWizard {currentStep} {modal} {steps} bind:payProgressStep /> |
| 145 | + {:else if currentStep?.name === WizardStepsScanner.WALLET_CONNECT_CONNECT || currentStep?.name === WizardStepsScanner.WALLET_CONNECT_REVIEW} |
| 146 | + <WalletConnectSessionWizard {currentStep} onConnect={onWalletConnectConnect} /> |
| 147 | + {/if} |
| 148 | + {/key} |
| 149 | + </WizardModal> |
| 150 | + </div> |
109 | 151 | </ScannerModalPayDataLoader> |
| 152 | + |
| 153 | +<style lang="scss"> |
| 154 | + .scanner-modal :global(.content) { |
| 155 | + --dialog-padding-x: 0px; |
| 156 | + --dialog-padding-y: 0px; |
| 157 | + } |
| 158 | +</style> |
0 commit comments