@flare-engine/ui-shop 0.2.1 → 0.3.0
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/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +44 -30
- package/dist/index.d.mts +65 -54
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +65 -54
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +245 -274
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +238 -241
- package/dist/index.mjs.map +1 -1
- package/package.json +20 -12
package/dist/index.mjs
CHANGED
|
@@ -1,249 +1,246 @@
|
|
|
1
|
-
// src/ShopPanel.tsx
|
|
2
1
|
import { useIAP } from "@flare-engine/iap";
|
|
3
|
-
import { Button, Label, Panel } from "@flare-engine/ui";
|
|
2
|
+
import { Button, Label, Modal, Panel } from "@flare-engine/ui";
|
|
4
3
|
import { useEffect, useRef, useState } from "react";
|
|
5
4
|
import { View } from "react-native";
|
|
6
5
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
owned.map((p) => {
|
|
72
|
-
const itemCopy = copy[p.id];
|
|
73
|
-
return /* @__PURE__ */ jsxs(View, { children: [
|
|
74
|
-
/* @__PURE__ */ jsx(Label, { variant: "md", children: itemCopy ? localize(itemCopy.titleKey) : p.title }),
|
|
75
|
-
itemCopy ? /* @__PURE__ */ jsx(Label, { variant: "sm", tone: "muted", children: localize(itemCopy.descriptionKey) }) : null,
|
|
76
|
-
/* @__PURE__ */ jsx(
|
|
77
|
-
Button,
|
|
78
|
-
{
|
|
79
|
-
label: itemCopy ? localize(itemCopy.ownedLabelKey) : localize(strings.ownedSectionKey),
|
|
80
|
-
variant: "ghost",
|
|
81
|
-
disabled: true
|
|
82
|
-
}
|
|
83
|
-
)
|
|
84
|
-
] }, p.id);
|
|
85
|
-
})
|
|
86
|
-
] }) : null,
|
|
87
|
-
available.length > 0 ? /* @__PURE__ */ jsxs(View, { children: [
|
|
88
|
-
/* @__PURE__ */ jsx(Label, { variant: "sm", tone: "muted", children: localize(strings.availableSectionKey) }),
|
|
89
|
-
available.map((p) => {
|
|
90
|
-
const itemCopy = copy[p.id];
|
|
91
|
-
const purchasing = inFlight === p.id;
|
|
92
|
-
return /* @__PURE__ */ jsxs(View, { children: [
|
|
93
|
-
/* @__PURE__ */ jsx(Label, { variant: "md", children: itemCopy ? localize(itemCopy.titleKey) : p.title }),
|
|
94
|
-
itemCopy ? /* @__PURE__ */ jsx(Label, { variant: "sm", tone: "muted", children: localize(itemCopy.descriptionKey) }) : null,
|
|
95
|
-
/* @__PURE__ */ jsx(
|
|
96
|
-
Button,
|
|
97
|
-
{
|
|
98
|
-
label: purchasing ? localize(strings.loadingKey) : itemCopy ? localize(itemCopy.ctaKey) : p.priceDisplay,
|
|
99
|
-
variant: "primary",
|
|
100
|
-
disabled: purchasing,
|
|
101
|
-
onPress: () => {
|
|
102
|
-
void handlePurchase(p.id);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
)
|
|
106
|
-
] }, p.id);
|
|
107
|
-
})
|
|
108
|
-
] }) : null
|
|
109
|
-
] });
|
|
6
|
+
//#region src/PaywallModal.tsx
|
|
7
|
+
/**
|
|
8
|
+
* Parent owns visibility — the paywall never hides itself except on successful
|
|
9
|
+
* purchase. This keeps routing logic in the caller and avoids double-close races.
|
|
10
|
+
*/
|
|
11
|
+
function PaywallModal({ visible, productId, entitlementKey, entitlementStore, strings, localize, onDismiss, onPurchased, onError }) {
|
|
12
|
+
const provider = useIAP();
|
|
13
|
+
const [purchasing, setPurchasing] = useState(false);
|
|
14
|
+
const [purchaseError, setPurchaseError] = useState(null);
|
|
15
|
+
if (!visible) return null;
|
|
16
|
+
async function handlePurchase() {
|
|
17
|
+
setPurchasing(true);
|
|
18
|
+
setPurchaseError(null);
|
|
19
|
+
try {
|
|
20
|
+
const result = await provider.purchase(productId);
|
|
21
|
+
if (result.status === "purchased") {
|
|
22
|
+
entitlementStore.grant(entitlementKey);
|
|
23
|
+
onPurchased?.(result);
|
|
24
|
+
onDismiss();
|
|
25
|
+
} else if (result.status === "failed") {
|
|
26
|
+
const e = result.error ?? /* @__PURE__ */ new Error("Purchase failed");
|
|
27
|
+
setPurchaseError(e);
|
|
28
|
+
onError?.(e);
|
|
29
|
+
}
|
|
30
|
+
} catch (err) {
|
|
31
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
32
|
+
setPurchaseError(e);
|
|
33
|
+
onError?.(e);
|
|
34
|
+
} finally {
|
|
35
|
+
setPurchasing(false);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return /* @__PURE__ */ jsxs(Modal, {
|
|
39
|
+
visible,
|
|
40
|
+
onBackdropPress: purchasing ? void 0 : onDismiss,
|
|
41
|
+
children: [
|
|
42
|
+
/* @__PURE__ */ jsx(Label, {
|
|
43
|
+
variant: "lg",
|
|
44
|
+
children: localize(strings.headingKey)
|
|
45
|
+
}),
|
|
46
|
+
/* @__PURE__ */ jsx(Label, {
|
|
47
|
+
variant: "sm",
|
|
48
|
+
tone: "muted",
|
|
49
|
+
children: localize(strings.bodyKey)
|
|
50
|
+
}),
|
|
51
|
+
purchaseError !== null ? /* @__PURE__ */ jsx(Label, {
|
|
52
|
+
tone: "danger",
|
|
53
|
+
children: localize(strings.errorKey)
|
|
54
|
+
}) : null,
|
|
55
|
+
/* @__PURE__ */ jsxs(View, { children: [/* @__PURE__ */ jsx(Button, {
|
|
56
|
+
label: purchasing ? localize(strings.loadingKey) : localize(strings.ctaKey),
|
|
57
|
+
variant: "primary",
|
|
58
|
+
disabled: purchasing,
|
|
59
|
+
onPress: () => {
|
|
60
|
+
handlePurchase();
|
|
61
|
+
}
|
|
62
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
63
|
+
label: localize(strings.dismissKey),
|
|
64
|
+
variant: "ghost",
|
|
65
|
+
disabled: purchasing,
|
|
66
|
+
onPress: onDismiss
|
|
67
|
+
})] })
|
|
68
|
+
]
|
|
69
|
+
});
|
|
110
70
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
function
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
/* @__PURE__ */ jsx2(
|
|
161
|
-
Button2,
|
|
162
|
-
{
|
|
163
|
-
label: purchasing ? localize(strings.loadingKey) : localize(strings.ctaKey),
|
|
164
|
-
variant: "primary",
|
|
165
|
-
disabled: purchasing,
|
|
166
|
-
onPress: () => {
|
|
167
|
-
void handlePurchase();
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
),
|
|
171
|
-
/* @__PURE__ */ jsx2(
|
|
172
|
-
Button2,
|
|
173
|
-
{
|
|
174
|
-
label: localize(strings.dismissKey),
|
|
175
|
-
variant: "ghost",
|
|
176
|
-
disabled: purchasing,
|
|
177
|
-
onPress: onDismiss
|
|
178
|
-
}
|
|
179
|
-
)
|
|
180
|
-
] })
|
|
181
|
-
] });
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/RestorePurchasesButton.tsx
|
|
73
|
+
function RestorePurchasesButton({ entitlementStore, strings, localize, onComplete, onError }) {
|
|
74
|
+
const provider = useIAP();
|
|
75
|
+
const [restoring, setRestoring] = useState(false);
|
|
76
|
+
const [banner, setBanner] = useState(null);
|
|
77
|
+
const [bannerError, setBannerError] = useState(null);
|
|
78
|
+
async function handleRestore() {
|
|
79
|
+
setRestoring(true);
|
|
80
|
+
setBanner(null);
|
|
81
|
+
setBannerError(null);
|
|
82
|
+
try {
|
|
83
|
+
const results = await provider.restore();
|
|
84
|
+
let granted = 0;
|
|
85
|
+
for (let i = 0; i < results.length; i++) {
|
|
86
|
+
const r = results[i];
|
|
87
|
+
if (r && r.status === "purchased" && r.transaction) {
|
|
88
|
+
entitlementStore.grantProduct(r.transaction.productId);
|
|
89
|
+
granted++;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
setBanner(granted > 0 ? "success" : "empty");
|
|
93
|
+
onComplete?.(results);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
96
|
+
setBanner("error");
|
|
97
|
+
setBannerError(e);
|
|
98
|
+
onError?.(e);
|
|
99
|
+
} finally {
|
|
100
|
+
setRestoring(false);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return /* @__PURE__ */ jsxs(View, { children: [/* @__PURE__ */ jsx(Button, {
|
|
104
|
+
label: restoring ? localize(strings.loadingKey) : localize(strings.ctaKey),
|
|
105
|
+
variant: "secondary",
|
|
106
|
+
disabled: restoring,
|
|
107
|
+
onPress: () => {
|
|
108
|
+
handleRestore();
|
|
109
|
+
}
|
|
110
|
+
}), banner === "success" ? /* @__PURE__ */ jsx(Label, {
|
|
111
|
+
tone: "success",
|
|
112
|
+
children: localize(strings.successKey)
|
|
113
|
+
}) : banner === "empty" ? /* @__PURE__ */ jsx(Label, {
|
|
114
|
+
tone: "muted",
|
|
115
|
+
children: localize(strings.emptyKey)
|
|
116
|
+
}) : banner === "error" ? /* @__PURE__ */ jsxs(Label, {
|
|
117
|
+
tone: "danger",
|
|
118
|
+
children: [localize(strings.errorKey), bannerError !== null ? `: ${bannerError.message}` : ""]
|
|
119
|
+
}) : null] });
|
|
182
120
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/ShopScreen.tsx
|
|
123
|
+
function ShopScreen({ catalog, entitlementStore, copy, strings, localize, onPurchase, onError }) {
|
|
124
|
+
const provider = useIAP();
|
|
125
|
+
const [products, setProducts] = useState(null);
|
|
126
|
+
const [loadError, setLoadError] = useState(null);
|
|
127
|
+
const [inFlight, setInFlight] = useState(null);
|
|
128
|
+
const [entitlementTick, setEntitlementTick] = useState(0);
|
|
129
|
+
const onErrorRef = useRef(onError);
|
|
130
|
+
onErrorRef.current = onError;
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
let cancelled = false;
|
|
133
|
+
setProducts(null);
|
|
134
|
+
setLoadError(null);
|
|
135
|
+
const ids = catalog.entries.map((e) => e.productId);
|
|
136
|
+
provider.getProducts(ids).then((p) => {
|
|
137
|
+
if (!cancelled) setProducts(p);
|
|
138
|
+
}).catch((err) => {
|
|
139
|
+
if (cancelled) return;
|
|
140
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
141
|
+
setLoadError(e);
|
|
142
|
+
onErrorRef.current?.(e);
|
|
143
|
+
});
|
|
144
|
+
return () => {
|
|
145
|
+
cancelled = true;
|
|
146
|
+
};
|
|
147
|
+
}, [catalog, provider]);
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
return entitlementStore.onChange.add(() => {
|
|
150
|
+
setEntitlementTick((n) => n + 1);
|
|
151
|
+
});
|
|
152
|
+
}, [entitlementStore]);
|
|
153
|
+
async function handlePurchase(id) {
|
|
154
|
+
setInFlight(id);
|
|
155
|
+
try {
|
|
156
|
+
const result = await provider.purchase(id);
|
|
157
|
+
if (result.status === "purchased") entitlementStore.grantProduct(id);
|
|
158
|
+
onPurchase?.(id, result);
|
|
159
|
+
} catch (err) {
|
|
160
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
161
|
+
onError?.(e);
|
|
162
|
+
} finally {
|
|
163
|
+
setInFlight(null);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (loadError !== null) return /* @__PURE__ */ jsx(Panel, { children: /* @__PURE__ */ jsx(Label, {
|
|
167
|
+
tone: "danger",
|
|
168
|
+
children: localize(strings.errorKey)
|
|
169
|
+
}) });
|
|
170
|
+
if (products === null) return /* @__PURE__ */ jsx(Panel, { children: /* @__PURE__ */ jsx(Label, {
|
|
171
|
+
tone: "muted",
|
|
172
|
+
children: localize(strings.loadingKey)
|
|
173
|
+
}) });
|
|
174
|
+
if (products.length === 0) return /* @__PURE__ */ jsx(Panel, { children: /* @__PURE__ */ jsx(Label, {
|
|
175
|
+
tone: "muted",
|
|
176
|
+
children: localize(strings.emptyKey)
|
|
177
|
+
}) });
|
|
178
|
+
const owned = products.filter((p) => {
|
|
179
|
+
return catalog.entries.find((e) => e.productId === p.id)?.grants.some((k) => entitlementStore.isEntitled(k)) ?? false;
|
|
180
|
+
});
|
|
181
|
+
const available = products.filter((p) => !owned.includes(p));
|
|
182
|
+
return /* @__PURE__ */ jsxs(Panel, {
|
|
183
|
+
variant: "elevated",
|
|
184
|
+
padding: "md",
|
|
185
|
+
children: [
|
|
186
|
+
/* @__PURE__ */ jsx(Label, {
|
|
187
|
+
variant: "lg",
|
|
188
|
+
children: localize(strings.headingKey)
|
|
189
|
+
}),
|
|
190
|
+
owned.length > 0 ? /* @__PURE__ */ jsxs(View, { children: [/* @__PURE__ */ jsx(Label, {
|
|
191
|
+
variant: "sm",
|
|
192
|
+
tone: "muted",
|
|
193
|
+
children: localize(strings.ownedSectionKey)
|
|
194
|
+
}), owned.map((p) => {
|
|
195
|
+
const itemCopy = copy[p.id];
|
|
196
|
+
return /* @__PURE__ */ jsxs(View, { children: [
|
|
197
|
+
/* @__PURE__ */ jsx(Label, {
|
|
198
|
+
variant: "md",
|
|
199
|
+
children: itemCopy ? localize(itemCopy.titleKey) : p.title
|
|
200
|
+
}),
|
|
201
|
+
itemCopy ? /* @__PURE__ */ jsx(Label, {
|
|
202
|
+
variant: "sm",
|
|
203
|
+
tone: "muted",
|
|
204
|
+
children: localize(itemCopy.descriptionKey)
|
|
205
|
+
}) : null,
|
|
206
|
+
/* @__PURE__ */ jsx(Button, {
|
|
207
|
+
label: itemCopy ? localize(itemCopy.ownedLabelKey) : localize(strings.ownedSectionKey),
|
|
208
|
+
variant: "ghost",
|
|
209
|
+
disabled: true
|
|
210
|
+
})
|
|
211
|
+
] }, p.id);
|
|
212
|
+
})] }) : null,
|
|
213
|
+
available.length > 0 ? /* @__PURE__ */ jsxs(View, { children: [/* @__PURE__ */ jsx(Label, {
|
|
214
|
+
variant: "sm",
|
|
215
|
+
tone: "muted",
|
|
216
|
+
children: localize(strings.availableSectionKey)
|
|
217
|
+
}), available.map((p) => {
|
|
218
|
+
const itemCopy = copy[p.id];
|
|
219
|
+
const purchasing = inFlight === p.id;
|
|
220
|
+
return /* @__PURE__ */ jsxs(View, { children: [
|
|
221
|
+
/* @__PURE__ */ jsx(Label, {
|
|
222
|
+
variant: "md",
|
|
223
|
+
children: itemCopy ? localize(itemCopy.titleKey) : p.title
|
|
224
|
+
}),
|
|
225
|
+
itemCopy ? /* @__PURE__ */ jsx(Label, {
|
|
226
|
+
variant: "sm",
|
|
227
|
+
tone: "muted",
|
|
228
|
+
children: localize(itemCopy.descriptionKey)
|
|
229
|
+
}) : null,
|
|
230
|
+
/* @__PURE__ */ jsx(Button, {
|
|
231
|
+
label: purchasing ? localize(strings.loadingKey) : itemCopy ? localize(itemCopy.ctaKey) : p.priceDisplay,
|
|
232
|
+
variant: "primary",
|
|
233
|
+
disabled: purchasing,
|
|
234
|
+
onPress: () => {
|
|
235
|
+
handlePurchase(p.id);
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
] }, p.id);
|
|
239
|
+
})] }) : null
|
|
240
|
+
]
|
|
241
|
+
});
|
|
243
242
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
ShopPanel
|
|
248
|
-
};
|
|
243
|
+
//#endregion
|
|
244
|
+
export { PaywallModal, RestorePurchasesButton, ShopScreen };
|
|
245
|
+
|
|
249
246
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ShopPanel.tsx","../src/PaywallModal.tsx","../src/RestorePurchasesButton.tsx"],"sourcesContent":["import type {\n EntitlementStore,\n Product,\n ProductCatalog,\n ProductId,\n PurchaseResult,\n} from '@flare-engine/iap';\nimport { useIAP } from '@flare-engine/iap';\nimport { Button, Label, Panel } from '@flare-engine/ui';\nimport { useEffect, useRef, useState } from 'react';\nimport { View } from 'react-native';\nimport type { LocalizeFn, ShopItemCopy, ShopPanelStrings } from './types.js';\n\nexport interface ShopPanelProps {\n readonly catalog: ProductCatalog;\n readonly entitlementStore: EntitlementStore;\n readonly copy: Record<ProductId, ShopItemCopy>;\n readonly strings: ShopPanelStrings;\n readonly localize: LocalizeFn;\n readonly onPurchase?: (productId: ProductId, result: PurchaseResult) => void;\n readonly onError?: (error: Error) => void;\n}\n\nexport function ShopPanel({\n catalog,\n entitlementStore,\n copy,\n strings,\n localize,\n onPurchase,\n onError,\n}: ShopPanelProps): React.ReactNode {\n const provider = useIAP();\n const [products, setProducts] = useState<readonly Product[] | null>(null);\n const [loadError, setLoadError] = useState<Error | null>(null);\n const [inFlight, setInFlight] = useState<ProductId | null>(null);\n // Bumped on every EntitlementStore change to trigger re-render.\n const [entitlementTick, setEntitlementTick] = useState(0);\n const fetched = useRef(false);\n\n useEffect(() => {\n if (fetched.current) return;\n fetched.current = true;\n const ids = catalog.entries.map((e) => e.productId);\n provider\n .getProducts(ids)\n .then(setProducts)\n .catch((err: unknown) => {\n const e = err instanceof Error ? err : new Error(String(err));\n setLoadError(e);\n onError?.(e);\n });\n }, [catalog, provider, onError]);\n\n useEffect(() => {\n return entitlementStore.onChange.add(() => {\n setEntitlementTick((n) => n + 1);\n });\n }, [entitlementStore]);\n\n // entitlementTick read in render to keep the lint rule happy.\n void entitlementTick;\n\n async function handlePurchase(id: ProductId): Promise<void> {\n setInFlight(id);\n try {\n const result = await provider.purchase(id);\n if (result.status === 'purchased') {\n entitlementStore.grantProduct(id);\n }\n onPurchase?.(id, result);\n } catch (err: unknown) {\n const e = err instanceof Error ? err : new Error(String(err));\n onError?.(e);\n } finally {\n setInFlight(null);\n }\n }\n\n if (loadError !== null) {\n return (\n <Panel>\n <Label tone=\"danger\">{localize(strings.errorKey)}</Label>\n </Panel>\n );\n }\n\n if (products === null) {\n return (\n <Panel>\n <Label tone=\"muted\">{localize(strings.loadingKey)}</Label>\n </Panel>\n );\n }\n\n if (products.length === 0) {\n return (\n <Panel>\n <Label tone=\"muted\">{localize(strings.emptyKey)}</Label>\n </Panel>\n );\n }\n\n const owned: readonly Product[] = products.filter((p) => {\n const entry = catalog.entries.find((e) => e.productId === p.id);\n return entry?.grants.some((k) => entitlementStore.isEntitled(k)) ?? false;\n });\n const available: readonly Product[] = products.filter((p) => !owned.includes(p));\n\n return (\n <Panel variant=\"elevated\" padding=\"md\">\n <Label variant=\"lg\">{localize(strings.headingKey)}</Label>\n {owned.length > 0 ? (\n <View>\n <Label variant=\"sm\" tone=\"muted\">\n {localize(strings.ownedSectionKey)}\n </Label>\n {owned.map((p) => {\n const itemCopy = copy[p.id];\n return (\n <View key={p.id}>\n <Label variant=\"md\">{itemCopy ? localize(itemCopy.titleKey) : p.title}</Label>\n {itemCopy ? (\n <Label variant=\"sm\" tone=\"muted\">\n {localize(itemCopy.descriptionKey)}\n </Label>\n ) : null}\n <Button\n label={\n itemCopy ? localize(itemCopy.ownedLabelKey) : localize(strings.ownedSectionKey)\n }\n variant=\"ghost\"\n disabled\n />\n </View>\n );\n })}\n </View>\n ) : null}\n {available.length > 0 ? (\n <View>\n <Label variant=\"sm\" tone=\"muted\">\n {localize(strings.availableSectionKey)}\n </Label>\n {available.map((p) => {\n const itemCopy = copy[p.id];\n const purchasing = inFlight === p.id;\n return (\n <View key={p.id}>\n <Label variant=\"md\">{itemCopy ? localize(itemCopy.titleKey) : p.title}</Label>\n {itemCopy ? (\n <Label variant=\"sm\" tone=\"muted\">\n {localize(itemCopy.descriptionKey)}\n </Label>\n ) : null}\n <Button\n label={\n purchasing\n ? localize(strings.loadingKey)\n : itemCopy\n ? localize(itemCopy.ctaKey)\n : p.priceDisplay\n }\n variant=\"primary\"\n disabled={purchasing}\n onPress={() => {\n void handlePurchase(p.id);\n }}\n />\n </View>\n );\n })}\n </View>\n ) : null}\n </Panel>\n );\n}\n","import type {\n EntitlementKey,\n EntitlementStore,\n ProductId,\n PurchaseResult,\n} from '@flare-engine/iap';\nimport { useIAP } from '@flare-engine/iap';\nimport { Button, Label, Modal } from '@flare-engine/ui';\nimport { useState } from 'react';\nimport { View } from 'react-native';\nimport type { LocalizeFn, PaywallStrings } from './types.js';\n\nexport interface PaywallModalProps {\n readonly visible: boolean;\n readonly productId: ProductId;\n readonly entitlementKey: EntitlementKey;\n readonly entitlementStore: EntitlementStore;\n readonly strings: PaywallStrings;\n readonly localize: LocalizeFn;\n readonly onDismiss: () => void;\n readonly onPurchased?: (result: PurchaseResult) => void;\n readonly onError?: (error: Error) => void;\n}\n\n/**\n * Parent owns visibility — the paywall never hides itself except on successful\n * purchase. This keeps routing logic in the caller and avoids double-close races.\n */\nexport function PaywallModal({\n visible,\n productId,\n entitlementKey,\n entitlementStore,\n strings,\n localize,\n onDismiss,\n onPurchased,\n onError,\n}: PaywallModalProps): React.ReactNode {\n const provider = useIAP();\n const [purchasing, setPurchasing] = useState(false);\n const [purchaseError, setPurchaseError] = useState<Error | null>(null);\n\n if (!visible) return null;\n\n async function handlePurchase(): Promise<void> {\n setPurchasing(true);\n setPurchaseError(null);\n try {\n const result = await provider.purchase(productId);\n if (result.status === 'purchased') {\n entitlementStore.grant(entitlementKey);\n onPurchased?.(result);\n onDismiss();\n } else if (result.status === 'failed') {\n const e = result.error ?? new Error('Purchase failed');\n setPurchaseError(e);\n onError?.(e);\n }\n // 'cancelled' — stay open, no error shown\n } catch (err: unknown) {\n const e = err instanceof Error ? err : new Error(String(err));\n setPurchaseError(e);\n onError?.(e);\n } finally {\n setPurchasing(false);\n }\n }\n\n return (\n <Modal visible={visible} onBackdropPress={onDismiss}>\n <Label variant=\"lg\">{localize(strings.headingKey)}</Label>\n <Label variant=\"sm\" tone=\"muted\">\n {localize(strings.bodyKey)}\n </Label>\n {purchaseError !== null ? <Label tone=\"danger\">{localize(strings.errorKey)}</Label> : null}\n <View>\n <Button\n label={purchasing ? localize(strings.loadingKey) : localize(strings.ctaKey)}\n variant=\"primary\"\n disabled={purchasing}\n onPress={() => {\n void handlePurchase();\n }}\n />\n <Button\n label={localize(strings.dismissKey)}\n variant=\"ghost\"\n disabled={purchasing}\n onPress={onDismiss}\n />\n </View>\n </Modal>\n );\n}\n","import type { EntitlementStore, PurchaseResult } from '@flare-engine/iap';\nimport { useIAP } from '@flare-engine/iap';\nimport { Button, Label } from '@flare-engine/ui';\nimport { useState } from 'react';\nimport { View } from 'react-native';\nimport type { LocalizeFn, RestoreStrings } from './types.js';\n\ntype BannerKind = 'success' | 'empty' | 'error';\n\nexport interface RestorePurchasesButtonProps {\n readonly entitlementStore: EntitlementStore;\n readonly strings: RestoreStrings;\n readonly localize: LocalizeFn;\n readonly onComplete?: (results: readonly PurchaseResult[]) => void;\n readonly onError?: (error: Error) => void;\n}\n\nexport function RestorePurchasesButton({\n entitlementStore,\n strings,\n localize,\n onComplete,\n onError,\n}: RestorePurchasesButtonProps): React.ReactNode {\n const provider = useIAP();\n const [restoring, setRestoring] = useState(false);\n const [banner, setBanner] = useState<BannerKind | null>(null);\n const [bannerError, setBannerError] = useState<Error | null>(null);\n\n async function handleRestore(): Promise<void> {\n setRestoring(true);\n setBanner(null);\n setBannerError(null);\n try {\n const results = await provider.restore();\n let granted = 0;\n for (let i = 0; i < results.length; i++) {\n const r = results[i];\n if (r && r.status === 'purchased' && r.transaction) {\n entitlementStore.grantProduct(r.transaction.productId);\n granted++;\n }\n }\n setBanner(granted > 0 ? 'success' : 'empty');\n onComplete?.(results);\n } catch (err: unknown) {\n const e = err instanceof Error ? err : new Error(String(err));\n setBanner('error');\n setBannerError(e);\n onError?.(e);\n } finally {\n setRestoring(false);\n }\n }\n\n return (\n <View>\n <Button\n label={restoring ? localize(strings.loadingKey) : localize(strings.ctaKey)}\n variant=\"secondary\"\n disabled={restoring}\n onPress={() => {\n void handleRestore();\n }}\n />\n {banner === 'success' ? (\n <Label tone=\"success\">{localize(strings.successKey)}</Label>\n ) : banner === 'empty' ? (\n <Label tone=\"muted\">{localize(strings.emptyKey)}</Label>\n ) : banner === 'error' ? (\n <Label tone=\"danger\">\n {localize(strings.errorKey)}\n {bannerError !== null ? `: ${bannerError.message}` : ''}\n </Label>\n ) : null}\n </View>\n );\n}\n"],"mappings":";AAOA,SAAS,cAAc;AACvB,SAAS,QAAQ,OAAO,aAAa;AACrC,SAAS,WAAW,QAAQ,gBAAgB;AAC5C,SAAS,YAAY;AAwEb,cAsCM,YAtCN;AA3DD,SAAS,UAAU;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAoC;AAClC,QAAM,WAAW,OAAO;AACxB,QAAM,CAAC,UAAU,WAAW,IAAI,SAAoC,IAAI;AACxE,QAAM,CAAC,WAAW,YAAY,IAAI,SAAuB,IAAI;AAC7D,QAAM,CAAC,UAAU,WAAW,IAAI,SAA2B,IAAI;AAE/D,QAAM,CAAC,iBAAiB,kBAAkB,IAAI,SAAS,CAAC;AAC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,MAACJ,QAAA,EAAM,MAAK,UACT;AAAA,eAAS,QAAQ,QAAQ;AAAA,MACzB,gBAAgB,OAAO,KAAK,YAAY,OAAO,KAAK;AAAA,OACvD,IACE;AAAA,KACN;AAEJ;","names":["useIAP","Button","Label","useState","View","jsx","jsxs","useIAP","Button","Label","useState","View","jsx","jsxs"]}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/PaywallModal.tsx","../src/RestorePurchasesButton.tsx","../src/ShopScreen.tsx"],"sourcesContent":["import type {\n EntitlementKey,\n EntitlementStore,\n ProductId,\n PurchaseResult,\n} from '@flare-engine/iap';\nimport { useIAP } from '@flare-engine/iap';\nimport { Button, Label, Modal } from '@flare-engine/ui';\nimport { useState } from 'react';\nimport { View } from 'react-native';\nimport type { LocalizeFn, PaywallStrings } from './types.js';\n\nexport interface PaywallModalProps {\n readonly visible: boolean;\n readonly productId: ProductId;\n readonly entitlementKey: EntitlementKey;\n readonly entitlementStore: EntitlementStore;\n readonly strings: PaywallStrings;\n readonly localize: LocalizeFn;\n readonly onDismiss: () => void;\n readonly onPurchased?: (result: PurchaseResult) => void;\n readonly onError?: (error: Error) => void;\n}\n\n/**\n * Parent owns visibility — the paywall never hides itself except on successful\n * purchase. This keeps routing logic in the caller and avoids double-close races.\n */\nexport function PaywallModal({\n visible,\n productId,\n entitlementKey,\n entitlementStore,\n strings,\n localize,\n onDismiss,\n onPurchased,\n onError,\n}: PaywallModalProps): React.ReactNode {\n const provider = useIAP();\n const [purchasing, setPurchasing] = useState(false);\n const [purchaseError, setPurchaseError] = useState<Error | null>(null);\n\n if (!visible) return null;\n\n async function handlePurchase(): Promise<void> {\n setPurchasing(true);\n setPurchaseError(null);\n try {\n const result = await provider.purchase(productId);\n if (result.status === 'purchased') {\n entitlementStore.grant(entitlementKey);\n onPurchased?.(result);\n onDismiss();\n } else if (result.status === 'failed') {\n const e = result.error ?? new Error('Purchase failed');\n setPurchaseError(e);\n onError?.(e);\n }\n // 'cancelled' — stay open, no error shown\n } catch (err: unknown) {\n const e = err instanceof Error ? err : new Error(String(err));\n setPurchaseError(e);\n onError?.(e);\n } finally {\n setPurchasing(false);\n }\n }\n\n return (\n <Modal visible={visible} onBackdropPress={purchasing ? undefined : onDismiss}>\n <Label variant=\"lg\">{localize(strings.headingKey)}</Label>\n <Label variant=\"sm\" tone=\"muted\">\n {localize(strings.bodyKey)}\n </Label>\n {purchaseError !== null ? <Label tone=\"danger\">{localize(strings.errorKey)}</Label> : null}\n <View>\n <Button\n label={purchasing ? localize(strings.loadingKey) : localize(strings.ctaKey)}\n variant=\"primary\"\n disabled={purchasing}\n onPress={() => {\n void handlePurchase();\n }}\n />\n <Button\n label={localize(strings.dismissKey)}\n variant=\"ghost\"\n disabled={purchasing}\n onPress={onDismiss}\n />\n </View>\n </Modal>\n );\n}\n","import type { EntitlementStore, PurchaseResult } from '@flare-engine/iap';\nimport { useIAP } from '@flare-engine/iap';\nimport { Button, Label } from '@flare-engine/ui';\nimport { useState } from 'react';\nimport { View } from 'react-native';\nimport type { LocalizeFn, RestoreStrings } from './types.js';\n\ntype BannerKind = 'success' | 'empty' | 'error';\n\nexport interface RestorePurchasesButtonProps {\n readonly entitlementStore: EntitlementStore;\n readonly strings: RestoreStrings;\n readonly localize: LocalizeFn;\n readonly onComplete?: (results: readonly PurchaseResult[]) => void;\n readonly onError?: (error: Error) => void;\n}\n\nexport function RestorePurchasesButton({\n entitlementStore,\n strings,\n localize,\n onComplete,\n onError,\n}: RestorePurchasesButtonProps): React.ReactNode {\n const provider = useIAP();\n const [restoring, setRestoring] = useState(false);\n const [banner, setBanner] = useState<BannerKind | null>(null);\n const [bannerError, setBannerError] = useState<Error | null>(null);\n\n async function handleRestore(): Promise<void> {\n setRestoring(true);\n setBanner(null);\n setBannerError(null);\n try {\n const results = await provider.restore();\n let granted = 0;\n for (let i = 0; i < results.length; i++) {\n const r = results[i];\n if (r && r.status === 'purchased' && r.transaction) {\n entitlementStore.grantProduct(r.transaction.productId);\n granted++;\n }\n }\n setBanner(granted > 0 ? 'success' : 'empty');\n onComplete?.(results);\n } catch (err: unknown) {\n const e = err instanceof Error ? err : new Error(String(err));\n setBanner('error');\n setBannerError(e);\n onError?.(e);\n } finally {\n setRestoring(false);\n }\n }\n\n return (\n <View>\n <Button\n label={restoring ? localize(strings.loadingKey) : localize(strings.ctaKey)}\n variant=\"secondary\"\n disabled={restoring}\n onPress={() => {\n void handleRestore();\n }}\n />\n {banner === 'success' ? (\n <Label tone=\"success\">{localize(strings.successKey)}</Label>\n ) : banner === 'empty' ? (\n <Label tone=\"muted\">{localize(strings.emptyKey)}</Label>\n ) : banner === 'error' ? (\n <Label tone=\"danger\">\n {localize(strings.errorKey)}\n {bannerError !== null ? `: ${bannerError.message}` : ''}\n </Label>\n ) : null}\n </View>\n );\n}\n","import type {\n EntitlementStore,\n Product,\n ProductCatalog,\n ProductId,\n PurchaseResult,\n} from '@flare-engine/iap';\nimport { useIAP } from '@flare-engine/iap';\nimport { Button, Label, Panel } from '@flare-engine/ui';\nimport { useEffect, useRef, useState } from 'react';\nimport { View } from 'react-native';\nimport type { LocalizeFn, ShopItemCopy, ShopPanelStrings } from './types.js';\n\nexport interface ShopScreenProps {\n /**\n * Product catalog. Its identity (and the ambient IAP provider's) keys the\n * product fetch: a change refetches and resets to the loading state — that is\n * how a Noop→concrete provider swap picks up real products. Pass a stable\n * reference (a memoized/singleton catalog), not a value rebuilt every render,\n * or the fetch will re-run on each parent render.\n */\n readonly catalog: ProductCatalog;\n readonly entitlementStore: EntitlementStore;\n readonly copy: Record<ProductId, ShopItemCopy>;\n readonly strings: ShopPanelStrings;\n readonly localize: LocalizeFn;\n readonly onPurchase?: (productId: ProductId, result: PurchaseResult) => void;\n readonly onError?: (error: Error) => void;\n}\n\nexport function ShopScreen({\n catalog,\n entitlementStore,\n copy,\n strings,\n localize,\n onPurchase,\n onError,\n}: ShopScreenProps): React.ReactNode {\n const provider = useIAP();\n const [products, setProducts] = useState<readonly Product[] | null>(null);\n const [loadError, setLoadError] = useState<Error | null>(null);\n const [inFlight, setInFlight] = useState<ProductId | null>(null);\n // Bumped on every EntitlementStore change to trigger re-render.\n const [entitlementTick, setEntitlementTick] = useState(0);\n // Latest-ref for onError so a fresh inline callback identity cannot re-trigger\n // the fetch effect (which is keyed to catalog/provider identity only).\n const onErrorRef = useRef(onError);\n onErrorRef.current = onError;\n\n // Refetch whenever the catalog or the IAP provider identity changes (e.g. a\n // Noop→concrete provider swap), resetting to the loading state; a cancelled\n // flag drops a stale in-flight response from the previous provider.\n useEffect(() => {\n let cancelled = false;\n setProducts(null);\n setLoadError(null);\n const ids = catalog.entries.map((e) => e.productId);\n provider\n .getProducts(ids)\n .then((p) => {\n if (!cancelled) setProducts(p);\n })\n .catch((err: unknown) => {\n if (cancelled) return;\n const e = err instanceof Error ? err : new Error(String(err));\n setLoadError(e);\n onErrorRef.current?.(e);\n });\n return () => {\n cancelled = true;\n };\n }, [catalog, provider]);\n\n useEffect(() => {\n return entitlementStore.onChange.add(() => {\n setEntitlementTick((n) => n + 1);\n });\n }, [entitlementStore]);\n\n // entitlementTick read in render to keep the lint rule happy.\n void entitlementTick;\n\n async function handlePurchase(id: ProductId): Promise<void> {\n setInFlight(id);\n try {\n const result = await provider.purchase(id);\n if (result.status === 'purchased') {\n entitlementStore.grantProduct(id);\n }\n onPurchase?.(id, result);\n } catch (err: unknown) {\n const e = err instanceof Error ? err : new Error(String(err));\n onError?.(e);\n } finally {\n setInFlight(null);\n }\n }\n\n if (loadError !== null) {\n return (\n <Panel>\n <Label tone=\"danger\">{localize(strings.errorKey)}</Label>\n </Panel>\n );\n }\n\n if (products === null) {\n return (\n <Panel>\n <Label tone=\"muted\">{localize(strings.loadingKey)}</Label>\n </Panel>\n );\n }\n\n if (products.length === 0) {\n return (\n <Panel>\n <Label tone=\"muted\">{localize(strings.emptyKey)}</Label>\n </Panel>\n );\n }\n\n const owned: readonly Product[] = products.filter((p) => {\n const entry = catalog.entries.find((e) => e.productId === p.id);\n return entry?.grants.some((k) => entitlementStore.isEntitled(k)) ?? false;\n });\n const available: readonly Product[] = products.filter((p) => !owned.includes(p));\n\n return (\n <Panel variant=\"elevated\" padding=\"md\">\n <Label variant=\"lg\">{localize(strings.headingKey)}</Label>\n {owned.length > 0 ? (\n <View>\n <Label variant=\"sm\" tone=\"muted\">\n {localize(strings.ownedSectionKey)}\n </Label>\n {owned.map((p) => {\n const itemCopy = copy[p.id];\n return (\n <View key={p.id}>\n <Label variant=\"md\">{itemCopy ? localize(itemCopy.titleKey) : p.title}</Label>\n {itemCopy ? (\n <Label variant=\"sm\" tone=\"muted\">\n {localize(itemCopy.descriptionKey)}\n </Label>\n ) : null}\n <Button\n label={\n itemCopy ? localize(itemCopy.ownedLabelKey) : localize(strings.ownedSectionKey)\n }\n variant=\"ghost\"\n disabled\n />\n </View>\n );\n })}\n </View>\n ) : null}\n {available.length > 0 ? (\n <View>\n <Label variant=\"sm\" tone=\"muted\">\n {localize(strings.availableSectionKey)}\n </Label>\n {available.map((p) => {\n const itemCopy = copy[p.id];\n const purchasing = inFlight === p.id;\n return (\n <View key={p.id}>\n <Label variant=\"md\">{itemCopy ? localize(itemCopy.titleKey) : p.title}</Label>\n {itemCopy ? (\n <Label variant=\"sm\" tone=\"muted\">\n {localize(itemCopy.descriptionKey)}\n </Label>\n ) : null}\n <Button\n label={\n purchasing\n ? localize(strings.loadingKey)\n : itemCopy\n ? localize(itemCopy.ctaKey)\n : p.priceDisplay\n }\n variant=\"primary\"\n disabled={purchasing}\n onPress={() => {\n void handlePurchase(p.id);\n }}\n />\n </View>\n );\n })}\n </View>\n ) : null}\n </Panel>\n );\n}\n"],"mappings":";;;;;;;;;;AA4BA,SAAgB,aAAa,EAC3B,SACA,WACA,gBACA,kBACA,SACA,UACA,WACA,aACA,WACqC;CACrC,MAAM,WAAW,OAAO;CACxB,MAAM,CAAC,YAAY,iBAAiB,SAAS,KAAK;CAClD,MAAM,CAAC,eAAe,oBAAoB,SAAuB,IAAI;CA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A,IACJ;GACH,UAAU,SAAS,IAClB,qBAAC,MAAD,EAAA,UAAA,CACE,oBAAC,OAAD;IAAO,SAAQ;IAAK,MAAK;cACtB,SAAS,QAAQ,mBAAmB;GAChC,CAAA,GACN,UAAU,KAAK,MAAM;IACpB,MAAM,WAAW,KAAK,EAAE;IACxB,MAAM,aAAa,aAAa,EAAE;IAClC,OACE,qBAAC,MAAD,EAAA,UAAA;KACE,oBAAC,OAAD;MAAO,SAAQ;gBAAM,WAAW,SAAS,SAAS,QAAQ,IAAI,EAAE;KAAa,CAAA;KAC5E,WACC,oBAAC,OAAD;MAAO,SAAQ;MAAK,MAAK;gBACtB,SAAS,SAAS,cAAc;KAC5B,CAAA,IACL;KACJ,oBAAC,QAAD;MACE,OACE,aACI,SAAS,QAAQ,UAAU,IAC3B,WACE,SAAS,SAAS,MAAM,IACxB,EAAE;MAEV,SAAQ;MACR,UAAU;MACV,eAAe;OACb,eAAoB,EAAE,EAAE;MAC1B;KACD,CAAA;IACG,EAAA,GArBK,EAAE,EAqBP;GAEV,CAAC,CACG,EAAA,CAAA,IACJ;EACC;;AAEX"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/ui-shop",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Reusable ShopPanel, PaywallModal, and RestorePurchasesButton consuming IAPContext + EntitlementStore",
|
|
5
5
|
"keywords": ["flare-engine", "ui", "shop", "iap", "paywall", "react-native"],
|
|
6
|
-
"license": "
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
7
|
"author": "Grzegorz Otto",
|
|
8
8
|
"homepage": "https://github.com/grzott/flare-engine#readme",
|
|
9
9
|
"bugs": {
|
|
@@ -19,28 +19,36 @@
|
|
|
19
19
|
"types": "dist/index.d.ts",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
|
-
"files": ["dist", "README.md", "LICENSE"],
|
|
32
|
+
"files": ["dist", "README.md", "LICENSE", "NOTICE"],
|
|
28
33
|
"scripts": {
|
|
29
|
-
"build": "
|
|
34
|
+
"build": "tsdown",
|
|
30
35
|
"test": "bun test",
|
|
31
36
|
"lint": "biome check src/",
|
|
32
37
|
"typecheck": "tsc --noEmit"
|
|
33
38
|
},
|
|
34
39
|
"dependencies": {
|
|
35
|
-
"@flare-engine/iap": "^0.
|
|
36
|
-
"@flare-engine/ui": "^0.
|
|
40
|
+
"@flare-engine/iap": "^0.3.0",
|
|
41
|
+
"@flare-engine/ui": "^0.3.0"
|
|
37
42
|
},
|
|
38
43
|
"peerDependencies": {
|
|
39
|
-
"react": ">=
|
|
40
|
-
"react-native": ">=0.
|
|
44
|
+
"react": ">=19.0.0",
|
|
45
|
+
"react-native": ">=0.79.0"
|
|
41
46
|
},
|
|
42
47
|
"devDependencies": {
|
|
43
|
-
"@types/react": "^
|
|
48
|
+
"@types/react": "^19.0.0"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=20"
|
|
44
52
|
},
|
|
45
53
|
"publishConfig": {
|
|
46
54
|
"access": "restricted",
|