@frak-labs/components 0.0.26-beta.06c52c98 → 0.0.26-beta.13642b4b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cdn/Banner.BfkMel9Q.js +1 -0
- package/cdn/ButtonShare.BM9Inhmf.js +1 -0
- package/cdn/ButtonWallet.B7UmJcbb.js +40 -0
- package/cdn/OpenInAppButton.B4mrsz6L.js +1 -0
- package/cdn/PostPurchase.BIc0C4Ma.js +1 -0
- package/cdn/components.js +1 -1
- package/cdn/formatReward.C8hlSKRj.js +1 -0
- package/cdn/jsxRuntime.module.5UNmmhNi.js +138 -0
- package/cdn/loader.js +26 -26
- package/cdn/useLightDomStyles.D895e4W1.js +1 -0
- package/cdn/useReward.DEU0AF3P.js +1 -0
- package/cdn/useShareModal.CN9_P7Sl.js +1 -0
- package/dist/banner.d.ts +103 -0
- package/dist/banner.js +187 -0
- package/dist/buttonShare.d.ts +1 -1
- package/dist/buttonShare.js +20 -54
- package/dist/buttonWallet.js +2 -2
- package/dist/formatReward-6JQldDEC.js +28 -0
- package/dist/openInApp.js +2 -2
- package/dist/postPurchase.d.ts +116 -0
- package/dist/postPurchase.js +146 -0
- package/dist/{useLightDomStyles-C029nIFk.js → useLightDomStyles-DukxuNnJ.js} +3 -3
- package/dist/{usePlacement-DD1NUBps.js → usePlacement-BbMuz8_A.js} +112 -65
- package/dist/useReward-CI2yRrCj.js +67 -0
- package/dist/useShareModal-DHlayNqk.js +55 -0
- package/package.json +13 -4
- package/cdn/ButtonShare.BG6DCGo6.js +0 -1
- package/cdn/ButtonWallet.BlV-eDcH.js +0 -40
- package/cdn/OpenInAppButton.Dq3J_3Ci.js +0 -1
- package/cdn/jsxRuntime.module.PHHpn0I_.js +0 -58
- package/dist/useReward-DAkT-7wT.js +0 -48
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { a as useClientReady, o as registerWebComponent, t as usePlacement } from "./usePlacement-BbMuz8_A.js";
|
|
2
|
+
import { t as useLightDomStyles } from "./useLightDomStyles-DukxuNnJ.js";
|
|
3
|
+
import { t as formatEstimatedReward } from "./formatReward-6JQldDEC.js";
|
|
4
|
+
import { t as useShareModal } from "./useShareModal-DHlayNqk.js";
|
|
5
|
+
import { getMerchantInformation, getUserReferralStatus, trackPurchaseStatus } from "@frak-labs/core-sdk/actions";
|
|
6
|
+
import { useEffect, useMemo, useState } from "preact/hooks";
|
|
7
|
+
import { jsx, jsxs } from "preact/jsx-runtime";
|
|
8
|
+
import { FrakRpcError, RpcErrorCodes } from "@frak-labs/frame-connector";
|
|
9
|
+
|
|
10
|
+
//#region src/components/PostPurchase/PostPurchase.tsx
|
|
11
|
+
/**
|
|
12
|
+
* Given referral status and merchant info, compute the display variant
|
|
13
|
+
* and pick the appropriate purchase reward.
|
|
14
|
+
*/
|
|
15
|
+
function resolvePostPurchaseContext(referralStatus, merchantInfo) {
|
|
16
|
+
const purchaseReward = merchantInfo.rewards.find((r) => r.interactionTypeKey === "purchase" && (r.referrer || r.referee));
|
|
17
|
+
if (!purchaseReward) return null;
|
|
18
|
+
const variant = referralStatus?.isReferred && purchaseReward.referee ? "referee" : "referrer";
|
|
19
|
+
return {
|
|
20
|
+
variant,
|
|
21
|
+
reward: variant === "referee" ? purchaseReward.referee : purchaseReward.referrer,
|
|
22
|
+
merchantDomain: merchantInfo.onChainMetadata.domain
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Post-purchase card component.
|
|
27
|
+
*
|
|
28
|
+
* Renders an inline card on the merchant's thank-you / order-status page
|
|
29
|
+
* that either congratulates a referee or invites a referrer to share.
|
|
30
|
+
*
|
|
31
|
+
* Fetches referral status and merchant information via two independent
|
|
32
|
+
* RPC calls, then computes the display variant locally.
|
|
33
|
+
*
|
|
34
|
+
* @group components
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* Minimal — just show the card:
|
|
38
|
+
* ```html
|
|
39
|
+
* <frak-post-purchase></frak-post-purchase>
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* With purchase tracking fallback and custom sharing URL:
|
|
44
|
+
* ```html
|
|
45
|
+
* <frak-post-purchase
|
|
46
|
+
* customer-id="cust_123"
|
|
47
|
+
* order-id="ord_456"
|
|
48
|
+
* token="checkout_abc"
|
|
49
|
+
* sharing-url="https://merchant.com/product/shoes"
|
|
50
|
+
* ></frak-post-purchase>
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function PostPurchase({ customerId, orderId, token, sharingUrl, merchantId, placement: placementId, classname = "", variant: forcedVariant, referrerText: propReferrerText, refereeText: propRefereeText, ctaText: propCtaText }) {
|
|
54
|
+
const { shouldRender, isHidden, isClientReady } = useClientReady();
|
|
55
|
+
const placement = usePlacement(placementId);
|
|
56
|
+
useLightDomStyles("frak-post-purchase", placementId, placement?.components?.postPurchase?.css);
|
|
57
|
+
const [context, setContext] = useState(null);
|
|
58
|
+
const [hasFetched, setHasFetched] = useState(false);
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (!isClientReady || !customerId || !orderId || !token) return;
|
|
61
|
+
trackPurchaseStatus({
|
|
62
|
+
customerId,
|
|
63
|
+
orderId,
|
|
64
|
+
token,
|
|
65
|
+
merchantId
|
|
66
|
+
}).catch(() => {});
|
|
67
|
+
}, [
|
|
68
|
+
isClientReady,
|
|
69
|
+
customerId,
|
|
70
|
+
orderId,
|
|
71
|
+
token,
|
|
72
|
+
merchantId
|
|
73
|
+
]);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (!isClientReady || hasFetched) return;
|
|
76
|
+
const client = window.FrakSetup?.client;
|
|
77
|
+
if (!client) return;
|
|
78
|
+
setHasFetched(true);
|
|
79
|
+
Promise.all([getUserReferralStatus(client), getMerchantInformation(client)]).then(([referralStatus, merchantInfo]) => {
|
|
80
|
+
setContext(resolvePostPurchaseContext(referralStatus, merchantInfo));
|
|
81
|
+
}).catch((e) => {
|
|
82
|
+
if (e instanceof FrakRpcError && e.code === RpcErrorCodes.configError) return;
|
|
83
|
+
console.warn("[Frak] Post-purchase context error", e);
|
|
84
|
+
});
|
|
85
|
+
}, [isClientReady, hasFetched]);
|
|
86
|
+
const resolvedVariant = forcedVariant ?? context?.variant;
|
|
87
|
+
const resolvedSharingUrl = sharingUrl ?? context?.merchantDomain;
|
|
88
|
+
const rewardText = useMemo(() => {
|
|
89
|
+
if (!context?.reward) return void 0;
|
|
90
|
+
const currency = window.FrakSetup?.client?.config?.metadata?.currency;
|
|
91
|
+
return formatEstimatedReward(context.reward, currency);
|
|
92
|
+
}, [context?.reward]);
|
|
93
|
+
const postPurchaseConfig = placement?.components?.postPurchase;
|
|
94
|
+
const texts = useMemo(() => {
|
|
95
|
+
const applyReward = (text) => rewardText ? text.replace("{REWARD}", rewardText) : text;
|
|
96
|
+
return {
|
|
97
|
+
message: resolvedVariant === "referee" ? rewardText ? applyReward(propRefereeText ?? postPurchaseConfig?.refereeText ?? "You just earned {REWARD}! Share with friends to earn even more.") : propRefereeText ?? postPurchaseConfig?.refereeNoRewardText ?? "You just earned a reward! Share with friends to earn even more." : rewardText ? applyReward(propReferrerText ?? postPurchaseConfig?.referrerText ?? "Earn {REWARD} by sharing this with your friends!") : propReferrerText ?? postPurchaseConfig?.referrerNoRewardText ?? "Share this with your friends and earn rewards!",
|
|
98
|
+
cta: rewardText ? applyReward(propCtaText ?? postPurchaseConfig?.ctaText ?? "Share & earn {REWARD}") : propCtaText ?? postPurchaseConfig?.ctaNoRewardText ?? "Share & earn"
|
|
99
|
+
};
|
|
100
|
+
}, [
|
|
101
|
+
resolvedVariant,
|
|
102
|
+
rewardText,
|
|
103
|
+
postPurchaseConfig,
|
|
104
|
+
propReferrerText,
|
|
105
|
+
propRefereeText,
|
|
106
|
+
propCtaText
|
|
107
|
+
]);
|
|
108
|
+
const { handleShare } = useShareModal(void 0, placementId, resolvedSharingUrl);
|
|
109
|
+
if (!shouldRender || isHidden) return null;
|
|
110
|
+
if (!context || !resolvedVariant) return null;
|
|
111
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
112
|
+
class: ["post-purchase", classname].filter(Boolean).join(" "),
|
|
113
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
114
|
+
class: "post-purchase__content",
|
|
115
|
+
children: /* @__PURE__ */ jsx("p", {
|
|
116
|
+
class: "post-purchase__message",
|
|
117
|
+
children: texts.message
|
|
118
|
+
})
|
|
119
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
120
|
+
type: "button",
|
|
121
|
+
class: "post-purchase__cta button",
|
|
122
|
+
disabled: !isClientReady,
|
|
123
|
+
onClick: handleShare,
|
|
124
|
+
children: texts.cta
|
|
125
|
+
})]
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/components/PostPurchase/index.ts
|
|
131
|
+
registerWebComponent(PostPurchase, "frak-post-purchase", [
|
|
132
|
+
"customerId",
|
|
133
|
+
"orderId",
|
|
134
|
+
"token",
|
|
135
|
+
"sharingUrl",
|
|
136
|
+
"merchantId",
|
|
137
|
+
"placement",
|
|
138
|
+
"classname",
|
|
139
|
+
"variant",
|
|
140
|
+
"referrerText",
|
|
141
|
+
"refereeText",
|
|
142
|
+
"ctaText"
|
|
143
|
+
], { shadow: false });
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
export { PostPurchase };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as lightDomBaseCss } from "./usePlacement-BbMuz8_A.js";
|
|
2
2
|
import { useEffect } from "preact/hooks";
|
|
3
3
|
|
|
4
4
|
//#region src/utils/styleManager.ts
|
|
@@ -26,9 +26,9 @@ const styleManager = {
|
|
|
26
26
|
|
|
27
27
|
//#endregion
|
|
28
28
|
//#region src/hooks/useLightDomStyles.ts
|
|
29
|
-
function useLightDomStyles(tag, placementId, placementCss) {
|
|
29
|
+
function useLightDomStyles(tag, placementId, placementCss, baseCss) {
|
|
30
30
|
useEffect(() => {
|
|
31
|
-
styleManager.injectBase(tag, lightDomBaseCss);
|
|
31
|
+
styleManager.injectBase(tag, baseCss ?? lightDomBaseCss);
|
|
32
32
|
}, [tag]);
|
|
33
33
|
useEffect(() => {
|
|
34
34
|
if (!placementId || !placementCss) return;
|
|
@@ -1,24 +1,10 @@
|
|
|
1
1
|
import register from "preact-custom-element";
|
|
2
2
|
import * as coreSdkIndex from "@frak-labs/core-sdk";
|
|
3
|
-
import { sdkConfigStore, setupClient } from "@frak-labs/core-sdk";
|
|
3
|
+
import { sdkConfigStore, setupClient, withCache } from "@frak-labs/core-sdk";
|
|
4
4
|
import * as coreSdkActions from "@frak-labs/core-sdk/actions";
|
|
5
|
-
import { displayEmbeddedWallet
|
|
5
|
+
import { displayEmbeddedWallet } from "@frak-labs/core-sdk/actions";
|
|
6
6
|
import { useEffect, useMemo, useState } from "preact/hooks";
|
|
7
7
|
|
|
8
|
-
//#region ../../packages/ui/utils/onDocumentReady.ts
|
|
9
|
-
/**
|
|
10
|
-
* When the document is ready, run the callback
|
|
11
|
-
* @param callback
|
|
12
|
-
*/
|
|
13
|
-
function onDocumentReady(callback) {
|
|
14
|
-
if (document.readyState === "complete" || document.readyState === "interactive") setTimeout(callback, 1);
|
|
15
|
-
else if (document.addEventListener) document.addEventListener("DOMContentLoaded", callback);
|
|
16
|
-
else document.attachEvent("onreadystatechange", () => {
|
|
17
|
-
if (document.readyState === "complete") callback();
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
//#endregion
|
|
22
8
|
//#region src/utils/embeddedWallet.ts
|
|
23
9
|
async function openEmbeddedWallet(targetInteraction, placement) {
|
|
24
10
|
if (!window.FrakSetup?.client) {
|
|
@@ -75,73 +61,42 @@ function onClientReady(action, callback) {
|
|
|
75
61
|
(action === "add" ? window.addEventListener : window.removeEventListener)(CUSTOM_EVENT_NAME, callback, false);
|
|
76
62
|
}
|
|
77
63
|
|
|
78
|
-
//#endregion
|
|
79
|
-
//#region src/utils/setup.ts
|
|
80
|
-
/**
|
|
81
|
-
* Setup the referral
|
|
82
|
-
* @param client
|
|
83
|
-
*/
|
|
84
|
-
async function setupReferral(client) {
|
|
85
|
-
const referral = await referralInteraction(client);
|
|
86
|
-
console.log("referral", referral);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
64
|
//#endregion
|
|
90
65
|
//#region src/utils/initFrakSdk.ts
|
|
91
66
|
/**
|
|
92
67
|
* Initializes the Frak SDK client and sets up necessary configurations.
|
|
93
|
-
*
|
|
68
|
+
* Uses withCache for inflight dedup — concurrent callers share the same promise.
|
|
69
|
+
* Failures are not cached, allowing retry on next call.
|
|
94
70
|
*
|
|
95
71
|
* @returns {Promise<void>}
|
|
96
72
|
*/
|
|
97
|
-
|
|
73
|
+
function initFrakSdk() {
|
|
98
74
|
window.FrakSetup.core = {
|
|
99
75
|
...coreSdkIndex,
|
|
100
76
|
...coreSdkActions
|
|
101
77
|
};
|
|
102
|
-
if (
|
|
78
|
+
if (window.FrakSetup?.client) return Promise.resolve();
|
|
79
|
+
return withCache(() => doInit(), {
|
|
80
|
+
cacheKey: "frak-sdk-init",
|
|
81
|
+
cacheTime: Number.POSITIVE_INFINITY
|
|
82
|
+
}).catch(() => {});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Performs the actual SDK initialization.
|
|
86
|
+
* Throws on failure so withCache doesn't cache failed attempts.
|
|
87
|
+
*/
|
|
88
|
+
async function doInit() {
|
|
89
|
+
if (!window.FrakSetup?.config) throw new Error("[Frak SDK] Configuration not found. Please ensure window.FrakSetup.config is set.");
|
|
103
90
|
console.log("[Frak SDK] Starting initialization");
|
|
104
|
-
if (!window.FrakSetup.config) {
|
|
105
|
-
console.error("[Frak SDK] Configuration not found");
|
|
106
|
-
window.frakSetupInProgress = false;
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
91
|
const client = await setupClient({ config: window.FrakSetup.config });
|
|
110
|
-
if (!client)
|
|
111
|
-
console.error("[Frak SDK] Failed to create client");
|
|
112
|
-
window.frakSetupInProgress = false;
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
92
|
+
if (!client) throw new Error("[Frak SDK] Failed to create client");
|
|
115
93
|
window.FrakSetup.client = client;
|
|
116
94
|
console.log("[Frak SDK] Client initialized successfully");
|
|
117
95
|
dispatchClientReadyEvent();
|
|
118
|
-
setupReferral(client);
|
|
119
|
-
window.frakSetupInProgress = false;
|
|
96
|
+
coreSdkActions.setupReferral(client);
|
|
120
97
|
handleActionQueryParam();
|
|
121
98
|
}
|
|
122
99
|
/**
|
|
123
|
-
* Pre-checks for the Frak SDK initialization
|
|
124
|
-
* Sets frakSetupInProgress flag atomically to prevent race conditions
|
|
125
|
-
*/
|
|
126
|
-
function preChecks() {
|
|
127
|
-
if (window.frakSetupInProgress) {
|
|
128
|
-
console.log("[Frak SDK] Initialization already in progress");
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
window.frakSetupInProgress = true;
|
|
132
|
-
if (window.FrakSetup?.client) {
|
|
133
|
-
console.log("[Frak SDK] Client already initialized");
|
|
134
|
-
window.frakSetupInProgress = false;
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
if (!window.FrakSetup?.config) {
|
|
138
|
-
console.error("[Frak SDK] Configuration not found. Please ensure window.FrakSetup.config is set.");
|
|
139
|
-
window.frakSetupInProgress = false;
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
100
|
* Check the query param contain params for an auto opening of the frak modal
|
|
146
101
|
*/
|
|
147
102
|
function handleActionQueryParam() {
|
|
@@ -153,6 +108,17 @@ function handleActionQueryParam() {
|
|
|
153
108
|
}
|
|
154
109
|
}
|
|
155
110
|
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/utils/onDocumentReady.ts
|
|
113
|
+
/**
|
|
114
|
+
* When the document is ready, run the callback
|
|
115
|
+
* @param callback
|
|
116
|
+
*/
|
|
117
|
+
function onDocumentReady(callback) {
|
|
118
|
+
if (document.readyState === "complete" || document.readyState === "interactive") setTimeout(callback, 1);
|
|
119
|
+
else document.addEventListener("DOMContentLoaded", callback);
|
|
120
|
+
}
|
|
121
|
+
|
|
156
122
|
//#endregion
|
|
157
123
|
//#region src/utils/registerWebComponent.ts
|
|
158
124
|
/**
|
|
@@ -270,6 +236,87 @@ const lightDomBaseCss = `
|
|
|
270
236
|
}
|
|
271
237
|
}
|
|
272
238
|
`;
|
|
239
|
+
const bannerBaseCss = `
|
|
240
|
+
:where(frak-banner) {
|
|
241
|
+
display: block;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
:where(frak-banner .banner) {
|
|
245
|
+
display: flex;
|
|
246
|
+
align-items: center;
|
|
247
|
+
gap: 12px;
|
|
248
|
+
padding: 12px 16px;
|
|
249
|
+
border-top: 2px solid #3b82f6;
|
|
250
|
+
border-bottom: 2px solid #3b82f6;
|
|
251
|
+
font-family: inherit;
|
|
252
|
+
line-height: 1.4;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
:where(frak-banner .banner__fadeIn) {
|
|
256
|
+
animation: frak-fadeIn 300ms ease-in;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
@keyframes frak-fadeIn {
|
|
260
|
+
from {
|
|
261
|
+
opacity: 0;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
to {
|
|
265
|
+
opacity: 1;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
:where(frak-banner .banner__icon) {
|
|
270
|
+
flex-shrink: 0;
|
|
271
|
+
display: flex;
|
|
272
|
+
align-items: center;
|
|
273
|
+
justify-content: center;
|
|
274
|
+
width: 32px;
|
|
275
|
+
height: 32px;
|
|
276
|
+
color: #3b82f6;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
:where(frak-banner .banner__icon svg) {
|
|
280
|
+
width: 100%;
|
|
281
|
+
height: 100%;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
:where(frak-banner .banner__content) {
|
|
285
|
+
flex: 1;
|
|
286
|
+
min-width: 0;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
:where(frak-banner .banner__title) {
|
|
290
|
+
font-weight: 700;
|
|
291
|
+
font-size: 0.875rem;
|
|
292
|
+
margin: 0 0 2px;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
:where(frak-banner .banner__description) {
|
|
296
|
+
font-size: 0.75rem;
|
|
297
|
+
margin: 0;
|
|
298
|
+
opacity: 0.7;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
:where(frak-banner .banner__cta) {
|
|
302
|
+
flex-shrink: 0;
|
|
303
|
+
padding: 8px 16px;
|
|
304
|
+
font-weight: 700;
|
|
305
|
+
font-size: 0.75rem;
|
|
306
|
+
text-transform: uppercase;
|
|
307
|
+
letter-spacing: 0.05em;
|
|
308
|
+
border: 2px solid #eab308;
|
|
309
|
+
border-radius: 0;
|
|
310
|
+
background: #eab308;
|
|
311
|
+
color: #1e293b;
|
|
312
|
+
cursor: pointer;
|
|
313
|
+
white-space: nowrap;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
:where(frak-banner .banner__cta:hover) {
|
|
317
|
+
opacity: 0.9;
|
|
318
|
+
}
|
|
319
|
+
`;
|
|
273
320
|
|
|
274
321
|
//#endregion
|
|
275
322
|
//#region src/hooks/usePlacement.ts
|
|
@@ -290,4 +337,4 @@ function usePlacement(placementId) {
|
|
|
290
337
|
}
|
|
291
338
|
|
|
292
339
|
//#endregion
|
|
293
|
-
export {
|
|
340
|
+
export { useClientReady as a, openEmbeddedWallet as c, lightDomBaseCss as i, bannerBaseCss as n, registerWebComponent as o, buildStyleContent as r, openWalletModal as s, usePlacement as t };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { t as formatEstimatedReward } from "./formatReward-6JQldDEC.js";
|
|
2
|
+
import { getCurrencyAmountKey, getSupportedCurrency } from "@frak-labs/core-sdk";
|
|
3
|
+
import { getMerchantInformation } from "@frak-labs/core-sdk/actions";
|
|
4
|
+
import { useEffect, useState } from "preact/hooks";
|
|
5
|
+
|
|
6
|
+
//#region src/hooks/useReward.ts
|
|
7
|
+
/**
|
|
8
|
+
* Get the comparable fiat value of a reward for ranking purposes.
|
|
9
|
+
*/
|
|
10
|
+
function getRewardValue(reward, key) {
|
|
11
|
+
switch (reward.payoutType) {
|
|
12
|
+
case "fixed": return reward.amount[key];
|
|
13
|
+
case "tiered": return reward.tiers.reduce((acc, tier) => Math.max(acc, tier.amount[key]), 0);
|
|
14
|
+
case "percentage": return 0;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Pick the best referrer reward from merchant info and format it.
|
|
19
|
+
* Returns `undefined` when no displayable reward is found.
|
|
20
|
+
*/
|
|
21
|
+
function resolveBestReward({ rewards }, currency, targetInteraction) {
|
|
22
|
+
const referrerRewards = (targetInteraction ? rewards.filter((r) => r.interactionTypeKey === targetInteraction) : rewards).map((r) => r.referrer).filter((r) => r !== void 0);
|
|
23
|
+
if (referrerRewards.length === 0) return void 0;
|
|
24
|
+
const key = getCurrencyAmountKey(getSupportedCurrency(currency));
|
|
25
|
+
let bestReward = referrerRewards[0];
|
|
26
|
+
let bestValue = getRewardValue(bestReward, key);
|
|
27
|
+
for (let i = 1; i < referrerRewards.length; i++) {
|
|
28
|
+
const value = getRewardValue(referrerRewards[i], key);
|
|
29
|
+
if (value > bestValue) {
|
|
30
|
+
bestReward = referrerRewards[i];
|
|
31
|
+
bestValue = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (bestValue <= 0) {
|
|
35
|
+
const percentageReward = referrerRewards.find((r) => r.payoutType === "percentage");
|
|
36
|
+
if (!percentageReward) return void 0;
|
|
37
|
+
bestReward = percentageReward;
|
|
38
|
+
}
|
|
39
|
+
return formatEstimatedReward(bestReward, currency);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Hook to fetch and format the best referrer reward for a given interaction type.
|
|
43
|
+
*
|
|
44
|
+
* Calls `getMerchantInformation`, picks the highest-value referrer reward
|
|
45
|
+
* across all matching campaigns, and returns it as a formatted string.
|
|
46
|
+
*
|
|
47
|
+
* @param shouldUseReward - Whether to fetch the reward at all
|
|
48
|
+
* @param targetInteraction - Optional filter by interaction type (e.g. "purchase")
|
|
49
|
+
* @returns Object containing the formatted reward string, or undefined if unavailable
|
|
50
|
+
*/
|
|
51
|
+
function useReward(shouldUseReward, targetInteraction) {
|
|
52
|
+
const [reward, setReward] = useState(void 0);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!shouldUseReward) return;
|
|
55
|
+
const client = window.FrakSetup?.client;
|
|
56
|
+
if (!client) return;
|
|
57
|
+
getMerchantInformation(client).then((merchantInfo) => {
|
|
58
|
+
const currency = client.config.metadata?.currency;
|
|
59
|
+
const formatted = resolveBestReward(merchantInfo, currency, targetInteraction);
|
|
60
|
+
if (formatted) setReward(formatted);
|
|
61
|
+
}).catch(() => {});
|
|
62
|
+
}, [shouldUseReward, targetInteraction]);
|
|
63
|
+
return { reward };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
export { useReward as t };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DebugInfoGatherer, trackEvent } from "@frak-labs/core-sdk";
|
|
2
|
+
import { modalBuilder } from "@frak-labs/core-sdk/actions";
|
|
3
|
+
import { useCallback, useState } from "preact/hooks";
|
|
4
|
+
import { FrakRpcError, RpcErrorCodes } from "@frak-labs/frame-connector";
|
|
5
|
+
|
|
6
|
+
//#region src/components/ButtonShare/hooks/useShareModal.ts
|
|
7
|
+
/**
|
|
8
|
+
* Open the share modal
|
|
9
|
+
*
|
|
10
|
+
* @description
|
|
11
|
+
* This function will open the share modal, lazily creating a modal builder on demand.
|
|
12
|
+
*/
|
|
13
|
+
function useShareModal(targetInteraction, placement, sharingLink) {
|
|
14
|
+
const [debugInfo, setDebugInfo] = useState(void 0);
|
|
15
|
+
const [isError, setIsError] = useState(false);
|
|
16
|
+
return {
|
|
17
|
+
handleShare: useCallback(async () => {
|
|
18
|
+
if (!window.FrakSetup?.client) {
|
|
19
|
+
console.error("Frak client not found");
|
|
20
|
+
setDebugInfo(DebugInfoGatherer.empty().formatDebugInfo("Frak client not found"));
|
|
21
|
+
setIsError(true);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const builder = modalBuilder(window.FrakSetup.client, {});
|
|
25
|
+
try {
|
|
26
|
+
await builder.sharing(sharingLink ? { link: sharingLink } : {}).display((metadata) => ({
|
|
27
|
+
...metadata,
|
|
28
|
+
targetInteraction
|
|
29
|
+
}), placement);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (e instanceof FrakRpcError && e.code === RpcErrorCodes.clientAborted) {
|
|
32
|
+
console.debug("User aborted the modal");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const debugInfo = window.FrakSetup.client.debugInfo.formatDebugInfo(e);
|
|
36
|
+
trackEvent(window.FrakSetup.client, "share_modal_error", {
|
|
37
|
+
error: e instanceof Object && "message" in e ? e.message : "Unknown error",
|
|
38
|
+
debugInfo
|
|
39
|
+
});
|
|
40
|
+
setDebugInfo(debugInfo);
|
|
41
|
+
setIsError(true);
|
|
42
|
+
console.error("Error while opening the modal", e);
|
|
43
|
+
}
|
|
44
|
+
}, [
|
|
45
|
+
targetInteraction,
|
|
46
|
+
placement,
|
|
47
|
+
sharingLink
|
|
48
|
+
]),
|
|
49
|
+
isError,
|
|
50
|
+
debugInfo
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
export { useShareModal as t };
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"url": "https://twitter.com/QNivelais"
|
|
12
12
|
}
|
|
13
13
|
],
|
|
14
|
-
"version": "0.0.26-beta.
|
|
14
|
+
"version": "0.0.26-beta.13642b4b",
|
|
15
15
|
"description": "Frak Wallet components, helping any person to interact with the Frak wallet.",
|
|
16
16
|
"repository": {
|
|
17
17
|
"url": "https://github.com/frak-id/wallet",
|
|
@@ -56,6 +56,16 @@
|
|
|
56
56
|
"import": "./dist/openInApp.js",
|
|
57
57
|
"types": "./dist/openInApp.d.ts"
|
|
58
58
|
},
|
|
59
|
+
"./postPurchase": {
|
|
60
|
+
"development": "./src/components/PostPurchase/index.ts",
|
|
61
|
+
"import": "./dist/postPurchase.js",
|
|
62
|
+
"types": "./dist/postPurchase.d.ts"
|
|
63
|
+
},
|
|
64
|
+
"./banner": {
|
|
65
|
+
"development": "./src/components/Banner/index.ts",
|
|
66
|
+
"import": "./dist/banner.js",
|
|
67
|
+
"types": "./dist/banner.d.ts"
|
|
68
|
+
},
|
|
59
69
|
"./cdn": {
|
|
60
70
|
"import": "./cdn/components.js"
|
|
61
71
|
},
|
|
@@ -76,15 +86,14 @@
|
|
|
76
86
|
"publish": "echo 'Publishing components...'"
|
|
77
87
|
},
|
|
78
88
|
"dependencies": {
|
|
79
|
-
"@frak-labs/core-sdk": "0.2.1-beta.
|
|
80
|
-
"@frak-labs/frame-connector": "0.2.0-beta.
|
|
89
|
+
"@frak-labs/core-sdk": "0.2.1-beta.13642b4b",
|
|
90
|
+
"@frak-labs/frame-connector": "0.2.0-beta.13642b4b",
|
|
81
91
|
"preact": "^10.29.0",
|
|
82
92
|
"preact-custom-element": "^4.6.0"
|
|
83
93
|
},
|
|
84
94
|
"devDependencies": {
|
|
85
95
|
"@frak-labs/dev-tooling": "0.0.0",
|
|
86
96
|
"@frak-labs/test-foundation": "0.1.0",
|
|
87
|
-
"@frak-labs/ui": "0.0.0",
|
|
88
97
|
"@preact/preset-vite": "^2.10.4",
|
|
89
98
|
"@rolldown/plugin-node-polyfills": "^1.0.3",
|
|
90
99
|
"@testing-library/jest-dom": "^6.9.1",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as e,c as t,f as n,g as r,h as i,l as a,m as o,o as s,s as c}from"./loader.js";import{a as l,c as u,d,l as f,n as p,o as m,s as h,t as g,u as _}from"./jsxRuntime.module.PHHpn0I_.js";import{t as v}from"./ButtonWallet.BlV-eDcH.js";function y(e,t){return!e||e.payoutType!==`fixed`?0:e.amount[t]}function b(e,t){return e.reduce((e,n)=>Math.max(e,y(n.referrer,t)),0)}async function x({targetInteraction:e}){let t=window.FrakSetup?.client;if(!t){console.warn(`Frak client not ready yet`);return}let{rewards:r}=await s(t),i=a(t.config.metadata?.currency),o=b(e?r.filter(t=>t.interactionTypeKey===e):r,i);if(!(o<=0))return n(Math.round(o),t.config.metadata?.currency)}function S(e,t){let[n,r]=h(void 0);return f(()=>{e&&x({targetInteraction:t}).then(e=>{e&&r(e)})},[e,t]),{reward:n}}function C(e={}){let{successDuration:t=2e3}=e,[n,r]=h(!1);return{copy:u(async e=>{try{if(navigator.clipboard&&window.isSecureContext)await navigator.clipboard.writeText(e),r(!0);else{let t=document.createElement(`textarea`);t.value=e,t.style.position=`fixed`,t.style.opacity=`0`,document.body.appendChild(t),t.focus(),t.select();try{document.execCommand(`copy`),r(!0)}catch(e){return console.error(`Failed to copy text:`,e),!1}finally{t.remove()}}return setTimeout(()=>{r(!1)},t),!0}catch(e){return console.error(`Failed to copy text:`,e),!1}},[t]),copied:n}}const w={errorContainer:{marginTop:`16px`,padding:`16px`,backgroundColor:`#FEE2E2`,border:`1px solid #FCA5A5`,borderRadius:`4px`,color:`#991B1B`},header:{display:`flex`,alignItems:`center`,gap:`8px`,marginBottom:`12px`},title:{margin:0,fontSize:`16px`,fontWeight:500},message:{fontSize:`14px`,lineHeight:`1.5`,margin:`0 0 12px 0`},link:{color:`#991B1B`,textDecoration:`underline`,textUnderlineOffset:`2px`},copyButton:{display:`inline-flex`,alignItems:`center`,gap:`8px`,marginBottom:`10px`,padding:`8px 12px`,backgroundColor:`white`,border:`1px solid #D1D5DB`,borderRadius:`4px`,color:`black`,fontSize:`14px`,fontWeight:500}};function T({debugInfo:e}){let[t,n]=h(!1);return g(`div`,{children:[g(`button`,{type:`button`,style:w.copyButton,onClick:()=>n(!t),children:`Ouvrir les informations`}),t&&g(`textarea`,{style:{display:`block`,width:`100%`,height:`200px`,fontSize:`12px`},children:e})]})}function E({debugInfo:e}){let{copied:t,copy:n}=C();return g(`div`,{style:w.errorContainer,children:[g(`div`,{style:w.header,children:g(`h3`,{style:w.title,children:`Oups ! Nous avons rencontré un petit problème`})}),g(`p`,{style:w.message,children:[`Impossible d'ouvrir le menu de partage pour le moment. Si le problème persiste, copiez les informations ci-dessous et collez-les dans votre mail à`,` `,g(`a`,{href:`mailto:help@frak-labs.com?subject=Debug`,style:w.link,children:`help@frak-labs.com`}),` `,g(`br`,{}),`Merci pour votre retour, nous traitons votre demande dans les plus brefs délais.`]}),g(`button`,{type:`button`,onClick:()=>n(e??``),style:w.copyButton,children:t?`Informations copiées !`:`Copier les informations de débogage`}),g(T,{debugInfo:e})]})}function D(e,n){let[a,s]=h(void 0),[l,d]=h(!1);return{handleShare:u(async()=>{if(!window.FrakSetup?.client){console.error(`Frak client not found`),s(t.empty().formatDebugInfo(`Frak client not found`)),d(!0);return}let a=c(window.FrakSetup.client,{});try{await a.sharing({}).display(t=>({...t,targetInteraction:e}),n)}catch(e){if(e instanceof r&&e.code===i.clientAborted){console.debug(`User aborted the modal`);return}let t=window.FrakSetup.client.debugInfo.formatDebugInfo(e);o(window.FrakSetup.client,`share_modal_error`,{error:e instanceof Object&&`message`in e?e.message:`Unknown error`,debugInfo:t}),s(t),d(!0),console.error(`Error while opening the modal`,e)}},[e,n]),isError:l,debugInfo:a}}function O({placement:t,text:n=`Share and earn!`,classname:r=``,useReward:i,noRewardText:a,targetInteraction:s,clickAction:c}){let f=p(t),h=f?.components?.buttonShare;v(`frak-button-share`,t,h?.css);let _=m(()=>f?.targetInteraction===void 0?s:f.targetInteraction,[f?.targetInteraction,s]),y=h?.text??n,b=h?.noRewardText??a,x=m(()=>h?.useReward??i===!0,[h?.useReward,i]),C=m(()=>h?.clickAction??c??`embedded-wallet`,[h?.clickAction,c]),{shouldRender:w,isHidden:T,isClientReady:O}=l(),{reward:k}=S(x&&O,_),{handleShare:A,isError:j,debugInfo:M}=D(_,t),N=m(()=>x?k?y.includes(`{REWARD}`)?y.replace(`{REWARD}`,k):`${y} ${k}`:b??y.replace(`{REWARD}`,``):y,[x,y,b,k]),P=u(async()=>{o(window.FrakSetup.client,`share_button_clicked`),C===`share-modal`?await A():e(_,t)},[C,A,_,t]);if(!w||T)return null;let F=[`button`,`button__fadeIn`,r].filter(Boolean).join(` `);return g(d,{children:[g(`button`,{type:`button`,disabled:!O,class:F,onClick:P,children:N}),j&&g(E,{debugInfo:M})]})}_(O,`frak-button-share`,[`text`,`placement`,`classname`,`clickAction`,`useReward`,`noRewardText`,`targetInteraction`],{shadow:!1});export{O as ButtonShare,S as t};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import{i as e,m as t,t as n}from"./loader.js";import{a as r,d as i,i as a,l as o,n as s,o as c,r as l,s as u,t as d,u as f}from"./jsxRuntime.module.PHHpn0I_.js";import{t as p}from"./ButtonShare.BG6DCGo6.js";function m(e,t,r){o(()=>{n.injectBase(e,a)},[e]),o(()=>{!t||!r||n.injectPlacement(e,t,r)},[e,t,r])}function h(e){return d(`svg`,{fill:`none`,height:`1em`,viewBox:`0 0 28 28`,width:`1em`,xmlns:`http://www.w3.org/2000/svg`,...e,children:[d(`title`,{children:`Gift icon`}),d(`path`,{d:`m23.1427 13.9999v11.4285h-18.2857v-11.4285m9.1429 11.4285v-17.14282m0 0h-5.1429c-.75776 0-1.48448-.30102-2.0203-.83684s-.83684-1.26255-.83684-2.02031.30102-1.48448.83684-2.0203 1.26254-.83684 2.0203-.83684c4 0 5.1429 5.71429 5.1429 5.71429zm0 0h5.1428c.7578 0 1.4845-.30102 2.0203-.83684s.8369-1.26255.8369-2.02031-.3011-1.48448-.8369-2.0203-1.2625-.83684-2.0203-.83684c-4 0-5.1428 5.71429-5.1428 5.71429zm-11.42861 0h22.85711v5.71432h-22.85711z`,stroke:`#fff`,"stroke-linecap":`round`,"stroke-linejoin":`round`})]})}function g({placement:n,classname:a=``,useReward:f,targetInteraction:m}){let g=s(n),_=c(()=>g?.targetInteraction===void 0?m:g.targetInteraction,[g?.targetInteraction,m]),v=c(()=>f===!0,[f]),{shouldRender:y,isHidden:b,isClientReady:x}=r(),{reward:S}=p(v&&x,_),[C,w]=u(`right`);if(o(()=>{let e=g?.components?.buttonWallet?.position,t=window.FrakSetup?.modalWalletConfig?.metadata?.position;w(e??t??`right`)},[g?.components?.buttonWallet?.position]),!y||b)return null;let T=[`button`,`button__fadeIn`,C===`left`?`button__left`:`button__right`,a].filter(Boolean).join(` `);return d(i,{children:[d(`style`,{children:l(`
|
|
2
|
-
.button {
|
|
3
|
-
all: unset;
|
|
4
|
-
position: fixed;
|
|
5
|
-
bottom: 20px;
|
|
6
|
-
z-index: 2000000;
|
|
7
|
-
display: flex;
|
|
8
|
-
justify-content: center;
|
|
9
|
-
align-items: center;
|
|
10
|
-
background-color: #3e557e;
|
|
11
|
-
width: 45px;
|
|
12
|
-
height: 45px;
|
|
13
|
-
border-radius: 50%;
|
|
14
|
-
cursor: pointer;
|
|
15
|
-
text-align: center;
|
|
16
|
-
font-size: 24px;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
.button__left {
|
|
20
|
-
left: 20px;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
.button__right {
|
|
24
|
-
right: 20px;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
.reward {
|
|
28
|
-
position: absolute;
|
|
29
|
-
top: -4px;
|
|
30
|
-
right: 27px;
|
|
31
|
-
padding: 2px 3px;
|
|
32
|
-
border-radius: 5px;
|
|
33
|
-
background: #ff3f3f;
|
|
34
|
-
font-size: 9px;
|
|
35
|
-
color: #fff;
|
|
36
|
-
font-weight: 600;
|
|
37
|
-
white-space: nowrap;
|
|
38
|
-
line-height: 9px;
|
|
39
|
-
}
|
|
40
|
-
`,g?.components?.buttonWallet?.css)}),d(`button`,{type:`button`,"aria-label":`Open wallet`,part:`button`,disabled:!x,class:T,onClick:()=>{t(window.FrakSetup.client,`wallet_button_clicked`),e(_,n)},children:[d(h,{}),S&&d(`span`,{class:`reward`,children:S})]})]})}f(g,`frak-button-wallet`,[`placement`,`classname`,`useReward`,`targetInteraction`],{shadow:!0});export{g as ButtonWallet,m as t};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{d as e,m as t,u as n}from"./loader.js";import{a as r,n as i,o as a,t as o,u as s}from"./jsxRuntime.module.PHHpn0I_.js";import{t as c}from"./ButtonWallet.BlV-eDcH.js";function l(){return typeof navigator>`u`?!1:!!(/iPhone|iPad|iPod|Android|webOS|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)||/Macintosh/i.test(navigator.userAgent)&&navigator.maxTouchPoints>1)}function u(){return{isMobile:a(()=>l(),[])}}function d(r=`wallet`){let i=window.FrakSetup?.client;i&&t(i,`open_in_app_clicked`),e(`${n}${r}`,{onFallback:()=>{i&&t(i,`app_not_installed`)}})}function f({placement:e,text:t=`Open in App`,classname:n=``}){let a=i(e),{shouldRender:s,isHidden:l,isClientReady:f}=r(),{isMobile:p}=u();c(`frak-open-in-app`,e,a?.components?.openInApp?.css);let m=a?.components?.openInApp?.text??t;if(!p||!s||l)return null;let h=()=>{d()},g=[`button`,`button__fadeIn`,n].filter(Boolean).join(` `);return o(`button`,{type:`button`,"aria-label":`Open in Frak Wallet app`,disabled:!f,class:g,onClick:h,children:m})}s(f,`frak-open-in-app`,[`text`,`placement`,`classname`],{shadow:!1});export{f as OpenInAppButton};
|