Skip to content

Commit a9d0d33

Browse files
committed
this is getting painful, need a build target generator script
1 parent 480595f commit a9d0d33

File tree

236 files changed

+2276
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

236 files changed

+2276
-169
lines changed

packages/elements/example/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<span id="button-tip1"></span>
2222
</div>
2323

24+
<span id="page-tip3"></span>
25+
2426
<script type="module">
2527
import code from '../src/index';
2628

@@ -90,6 +92,26 @@
9092
button.mount(id);
9193
}
9294

95+
/*
96+
function mount_page(id) {
97+
const options = {
98+
mode: 'tip',
99+
platform: {
100+
name: 'twitter',
101+
username: 'getcode'
102+
}
103+
}
104+
105+
const { page } = code.elements.create('page', options);
106+
107+
page.on('error', (e) => { console.log('sdk-error', e); });
108+
page.on('cancel', (e) => { console.log('sdk-cancel', e); });
109+
110+
page.mount(id);
111+
}
112+
mount_page('#page-tip3');
113+
*/
114+
93115
mount_payment('#button-pay1', 'light');
94116
mount_payment('#button-pay2', 'dark');
95117

@@ -99,6 +121,7 @@
99121
mount_tip('#button-tip1', 'light');
100122
mount_tip('#button-tip2', 'dark');
101123

124+
102125
</script>
103126
</body>
104127

packages/elements/src/components/elements/IntentRequestModalDesktop.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
77
const config = useConfig();
88
const options = inject<ElementOptions>('options');
9-
const mode = options?.mode ?? 'payment';
9+
10+
const props = defineProps<{
11+
asPage?: boolean,
12+
}>();
1013
1114
const emit = defineEmits([
1215
'codeScanned',
@@ -20,7 +23,9 @@
2023
]);
2124
2225
const channel = new EventChannel<InternalEvents>();
23-
const url = `${config.codeSdk()}/${mode}-request-modal-desktop/#/${channel.id}/p=${encode(options)}`;
26+
const mode = options?.mode ?? 'payment';
27+
const kind = props.asPage ? 'page' : 'modal';
28+
const url = `${config.codeSdk()}/${mode}-request-${kind}-desktop/#/${channel.id}/p=${encode(options)}`;
2429
const el = ref<HTMLIFrameElement | null>(null);
2530
2631
console.log('url', url);
@@ -79,9 +84,10 @@
7984
8085
function getStyle() : { [key:string]: string } {
8186
const _ = (v:string) => v + ' !important';
87+
const inset = { inset: _('0'), top: _('0'), left: _('0'), right: _('0'), bottom: _('0'), }
8288
return {
89+
...inset,
8390
position: _('fixed'),
84-
inset: _('0'),
8591
zIndex: _('2147483647'),
8692
overflow: _('hidden'),
8793
width: _('100dvw'),

packages/elements/src/components/elements/IntentRequestModalMobile.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
const options = inject<ElementOptions>('options');
99
const mode = options?.mode ?? 'payment';
1010
11+
const props = defineProps<{
12+
asPage?: boolean,
13+
}>();
14+
1115
const emit = defineEmits([
1216
'codeScanned',
1317
'clientRejectedPayment',
@@ -20,7 +24,8 @@
2024
]);
2125
2226
const channel = new EventChannel<InternalEvents>();
23-
const url = `${config.codeSdk()}/${mode}-request-modal-mobile/#/${channel.id}/p=${encode(options)}`;
27+
const kind = props.asPage ? 'page' : 'modal';
28+
const url = `${config.codeSdk()}/${mode}-request-${kind}-mobile/#/${channel.id}/p=${encode(options)}`;
2429
const el = ref<HTMLIFrameElement | null>(null);
2530
2631
channel.on('codeScanned' , () => { emit('codeScanned'); });
@@ -82,9 +87,10 @@
8287
8388
function getStyle() : { [key:string]: string } {
8489
const _ = (v:string) => v + ' !important';
90+
const inset = { inset: _('0'), top: _('0'), left: _('0'), right: _('0'), bottom: _('0'), }
8591
return {
92+
...inset,
8693
position: _('fixed'),
87-
inset: _('0'),
8894
zIndex: _('2147483647'),
8995
overflow: _('hidden'),
9096
width: _('100dvw'),

packages/elements/src/components/flows/ButtonFlow.vue

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<script setup lang="ts">
2-
import { Ref, inject, nextTick, ref } from 'vue';
2+
import { Ref, inject, nextTick, reactive, ref } from 'vue';
33
import {
44
ElementEventEmitter,
55
ElementOptions,
66
ExternalPlatformOptions,
77
LoginRequestIntent,
88
LoginRequestOptions,
99
PaymentRequestIntent,
10-
PaymentRequestOptions,
11-
TipRequestIntent
10+
PaymentRequestOptions,
11+
TipRequestIntent
1212
} from '@code-wallet/intents';
1313
import { EventChannel, InternalEvents } from '@code-wallet/events';
1414
import * as code from "@code-wallet/client";
@@ -26,15 +26,19 @@ import {
2626
2727
import { isMobileDevice } from '../../utils/user-agent';
2828
import { getURLParam } from '../../utils/url';
29+
import { sleep } from '../../utils/delay';
2930
3031
const fadeDuration = 500; // Animation duration in milliseconds (we use this to delay emitting events)
31-
const sleep = (ms:number) => new Promise(resolve => setTimeout(resolve, ms));
32+
3233
const options = inject<ElementOptions>('options');
3334
const sdkEmit = inject<ElementEventEmitter>('emit', async () => false);
3435
const channel : Ref<EventChannel<InternalEvents> | null> = ref(null);
35-
const intent = ref<string | null>(null);
36-
const open = ref(false);
37-
const mobile = isMobileDevice();
36+
37+
const state = reactive({
38+
intent: null as string | null,
39+
open: false,
40+
mobile: isMobileDevice(),
41+
});
3842
3943
// We're going to try to guess these values later, so we have to keep track of
4044
// whether or not the user has provided them before we do that.
@@ -63,7 +67,7 @@ function onChannelCreated(val: EventChannel<InternalEvents>) {
6367
}
6468
6569
async function onInvoke() {
66-
open.value = true;
70+
state.open = true;
6771
6872
// Wait for the modal to be mounted, this is necessary because we need to wait
6973
// for the iframe to be mounted before we can get the channel for it.
@@ -78,27 +82,27 @@ async function onInvoke() {
7882
// Invoke user code, if the user returns true, we'll cancel the invocation
7983
const preventAction = await sdkEmit('invoke');
8084
if (preventAction) {
81-
open.value = false;
85+
state.open = false;
8286
return;
8387
}
8488
8589
// Get the intent id from the receiving app or iframe
8690
switch (options.mode) {
8791
case 'login':
88-
intent.value = new LoginRequestIntent(options as ElementOptions & LoginRequestOptions).getIntentId();
92+
state.intent = new LoginRequestIntent(options as ElementOptions & LoginRequestOptions).getIntentId();
8993
break;
9094
case 'payment':
91-
intent.value = new PaymentRequestIntent(options as ElementOptions & PaymentRequestOptions).getIntentId();
95+
state.intent = new PaymentRequestIntent(options as ElementOptions & PaymentRequestOptions).getIntentId();
9296
break;
9397
case 'tip':
94-
intent.value = new TipRequestIntent(options as ElementOptions & ExternalPlatformOptions).getIntentId();
98+
state.intent = new TipRequestIntent(options as ElementOptions & ExternalPlatformOptions).getIntentId();
9599
break;
96100
default:
97101
throw new Error('Invalid mode');
98102
}
99103
100104
const variables = {
101-
INTENT_ID: intent.value,
105+
INTENT_ID: state.intent,
102106
// ...
103107
}
104108
@@ -125,13 +129,13 @@ async function onInvoke() {
125129
}
126130
127131
async function onSuccess() {
128-
open.value = false;
132+
state.open = false;
129133
130134
// Ensure that the modal is fully closed before emitting events
131135
await sleep(fadeDuration);
132136
133137
const url = options?.confirmParams?.success?.url;
134-
const preventAction = await sdkEmit('success', { url, options, intent: intent.value });
138+
const preventAction = await sdkEmit('success', { url, options, intent: state.intent });
135139
if (preventAction) {
136140
return;
137141
}
@@ -142,13 +146,13 @@ async function onSuccess() {
142146
}
143147
144148
async function onCancel() {
145-
open.value = false;
149+
state.open = false;
146150
147151
// Ensure that the modal is fully closed before emitting events
148152
await sleep(fadeDuration);
149153
150154
const url = options?.confirmParams?.cancel?.url;
151-
const preventAction = await sdkEmit('cancel', { url, options, intent: intent.value });
155+
const preventAction = await sdkEmit('cancel', { url, options, intent: state.intent });
152156
if (preventAction) {
153157
return;
154158
}
@@ -164,23 +168,23 @@ async function onError(err: any) {
164168
}
165169
166170
async function onStreamTimeout() {
167-
open.value = false;
171+
state.open = false;
168172
await onError('stream_timed_out');
169173
}
170174
171175
async function onStreamClosed() {
172-
open.value = false;
176+
state.open = false;
173177
await onError('stream_closed');
174178
}
175179
176180
// Add an event listener for the 'visibilitychange' event
177181
document.addEventListener("visibilitychange", async () => {
178182
if (document.visibilityState === "visible") {
179-
if (!intent.value) { return; }
183+
if (!state.intent) { return; }
180184
181185
// Background tabs that might miss the 'intent-submitted' event, so we need
182186
// to check if the intent has been confirmed when the tab becomes visible.
183-
const { status } = await code.paymentIntents.getStatus({ intent: intent.value });
187+
const { status } = await code.paymentIntents.getStatus({ intent: state.intent });
184188
185189
if (status === 'confirmed') {
186190
onSuccess();
@@ -195,9 +199,9 @@ document.addEventListener("visibilitychange", async () => {
195199
@invoke="onInvoke" />
196200

197201
<Teleport to="body">
198-
<div v-if="open">
202+
<div v-if="state.open">
199203
<IntentRequestModalMobile
200-
v-if="mobile"
204+
v-if="state.mobile"
201205
@channel-created="onChannelCreated"
202206
@intent-submitted="onSuccess"
203207
@client-rejected-payment="onCancel"
@@ -219,7 +223,7 @@ document.addEventListener("visibilitychange", async () => {
219223
/>
220224
</div>
221225
<div v-else>
222-
<PreloadIntentRequestModalMobile v-if="mobile" />
226+
<PreloadIntentRequestModalMobile v-if="state.mobile" />
223227
<PreloadIntentRequestModalDesktop v-else />
224228
</div>
225229
</Teleport>

0 commit comments

Comments
 (0)