@baukit/purchases-core 0.12.2 → 0.14.1
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/README.md +5 -6
- package/dist/access.d.ts +22 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +24 -0
- package/dist/access.js.map +1 -0
- package/dist/account-plan.d.ts +22 -0
- package/dist/account-plan.d.ts.map +1 -0
- package/dist/account-plan.js +96 -0
- package/dist/account-plan.js.map +1 -0
- package/dist/entitlement.d.ts +27 -4
- package/dist/entitlement.d.ts.map +1 -1
- package/dist/entitlement.js +4 -1
- package/dist/entitlement.js.map +1 -1
- package/dist/flow.d.ts +7 -0
- package/dist/flow.d.ts.map +1 -1
- package/dist/flow.js +21 -3
- package/dist/flow.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/paywall.d.ts +28 -0
- package/dist/paywall.d.ts.map +1 -0
- package/dist/paywall.js +41 -0
- package/dist/paywall.js.map +1 -0
- package/dist/plans-link.d.ts +9 -0
- package/dist/plans-link.d.ts.map +1 -0
- package/dist/plans-link.js +17 -0
- package/dist/plans-link.js.map +1 -0
- package/dist/react.d.ts +19 -4
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +28 -10
- package/dist/react.js.map +1 -1
- package/package.json +1 -1
- package/src/access.ts +45 -0
- package/src/account-plan.test.ts +96 -0
- package/src/account-plan.ts +124 -0
- package/src/entitlement.ts +31 -4
- package/src/flow.test.ts +84 -13
- package/src/flow.ts +28 -3
- package/src/index.ts +4 -0
- package/src/paywall.test.ts +82 -0
- package/src/paywall.ts +60 -0
- package/src/plans-link.ts +21 -0
- package/src/react.test.ts +113 -12
- package/src/react.ts +54 -11
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { paywallFor, plansLink, type BillingClaim, type VerifiedEntitlement } from './index.js';
|
|
4
|
+
|
|
5
|
+
const PLANS = 'https://accounts.example.test/plans';
|
|
6
|
+
|
|
7
|
+
function plan(
|
|
8
|
+
billing: BillingClaim | null,
|
|
9
|
+
mode: VerifiedEntitlement['mode'] = 'hub',
|
|
10
|
+
): VerifiedEntitlement {
|
|
11
|
+
return {
|
|
12
|
+
mode,
|
|
13
|
+
suiteSubject: 'suite|a',
|
|
14
|
+
tier: 'premium',
|
|
15
|
+
features: [],
|
|
16
|
+
revision: 1,
|
|
17
|
+
plansUrl: mode === 'hub' ? PLANS : null,
|
|
18
|
+
billing,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const context = { productId: 'eigenruhe', feature: 'eigenruhe.studio' };
|
|
22
|
+
const link = `${PLANS}?from=eigenruhe&feature=eigenruhe.studio`;
|
|
23
|
+
|
|
24
|
+
describe('plansLink', () => {
|
|
25
|
+
it('adds the product and feature to the Schlüsselbund C17 plans URL', () => {
|
|
26
|
+
expect(plansLink(PLANS, { from: 'eigenruhe', feature: 'eigenruhe.studio' })).toBe(link);
|
|
27
|
+
expect(plansLink(PLANS, { from: 'hebkit' })).toBe(`${PLANS}?from=hebkit`);
|
|
28
|
+
expect(plansLink(PLANS)).toBe(PLANS);
|
|
29
|
+
expect(plansLink(`${PLANS}?lang=de`, { from: 'hebkit', feature: 'a b&c' })).toBe(
|
|
30
|
+
`${PLANS}?lang=de&from=hebkit&feature=a%20b%26c`,
|
|
31
|
+
);
|
|
32
|
+
expect(plansLink(PLANS, { feature: 'eigenruhe.studio' })).toBe(PLANS);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('refuses a URL that is not HTTP(S)', () => {
|
|
36
|
+
for (const url of ['/plans', 'javascript:alert(1)', 'https://user@host/plans'])
|
|
37
|
+
expect(() => plansLink(url)).toThrow(TypeError);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('paywallFor', () => {
|
|
42
|
+
it('sells to free and promo accounts through the plans link', () => {
|
|
43
|
+
expect(paywallFor(plan(null), context)).toEqual({ kind: 'purchase', plansUrl: link });
|
|
44
|
+
expect(paywallFor(plan({ channel: 'promo' }), context)).toEqual({
|
|
45
|
+
kind: 'purchase',
|
|
46
|
+
plansUrl: link,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('shows where a web or store plan is managed and never sells a second plan', () => {
|
|
51
|
+
expect(paywallFor(plan({ channel: 'web', manageUrl: PLANS }), context)).toEqual({
|
|
52
|
+
kind: 'manage',
|
|
53
|
+
channel: 'web',
|
|
54
|
+
manageUrl: link,
|
|
55
|
+
soldBy: null,
|
|
56
|
+
});
|
|
57
|
+
expect(paywallFor(plan({ channel: 'app_store', soldBy: 'hebkit' }), context)).toEqual({
|
|
58
|
+
kind: 'manage',
|
|
59
|
+
channel: 'app_store',
|
|
60
|
+
manageUrl: null,
|
|
61
|
+
soldBy: 'hebkit',
|
|
62
|
+
});
|
|
63
|
+
expect(paywallFor(plan({ channel: 'play_store' }), context)).toEqual({
|
|
64
|
+
kind: 'manage',
|
|
65
|
+
channel: 'play_store',
|
|
66
|
+
manageUrl: null,
|
|
67
|
+
soldBy: null,
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('offers no purchase under family or organization coverage or in standalone mode', () => {
|
|
72
|
+
expect(paywallFor(plan({ channel: 'family' }), context)).toEqual({
|
|
73
|
+
kind: 'covered',
|
|
74
|
+
channel: 'family',
|
|
75
|
+
});
|
|
76
|
+
expect(paywallFor(plan({ channel: 'organization' }), context)).toEqual({
|
|
77
|
+
kind: 'covered',
|
|
78
|
+
channel: 'organization',
|
|
79
|
+
});
|
|
80
|
+
expect(paywallFor(plan(null, 'standalone'), context)).toEqual({ kind: 'none' });
|
|
81
|
+
});
|
|
82
|
+
});
|
package/src/paywall.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { BillingChannel, VerifiedEntitlement } from './entitlement.js';
|
|
2
|
+
import { plansLink } from './plans-link.js';
|
|
3
|
+
|
|
4
|
+
/** What a locked feature may offer. Only `purchase` sells anything. */
|
|
5
|
+
export type Paywall =
|
|
6
|
+
| { readonly kind: 'purchase'; readonly plansUrl: string | null }
|
|
7
|
+
| {
|
|
8
|
+
readonly kind: 'manage';
|
|
9
|
+
readonly channel: 'web' | 'app_store' | 'play_store';
|
|
10
|
+
readonly manageUrl: string | null;
|
|
11
|
+
readonly soldBy: string | null;
|
|
12
|
+
}
|
|
13
|
+
| { readonly kind: 'covered'; readonly channel: 'family' | 'organization' }
|
|
14
|
+
| { readonly kind: 'none' };
|
|
15
|
+
|
|
16
|
+
export interface PaywallContext {
|
|
17
|
+
/** The calling product, sent as `from` on the Plans link. */
|
|
18
|
+
readonly productId: string;
|
|
19
|
+
readonly feature?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The C18 rules: a plan managed on the web or in a store shows where to manage it and
|
|
24
|
+
* never sells a second plan, family and organization coverage offer no purchase, and
|
|
25
|
+
* standalone mode sells nothing. Promo grants and free accounts may buy.
|
|
26
|
+
*/
|
|
27
|
+
export function paywallFor(entitlement: VerifiedEntitlement, context: PaywallContext): Paywall {
|
|
28
|
+
if (entitlement.mode === 'standalone') return { kind: 'none' };
|
|
29
|
+
const link =
|
|
30
|
+
entitlement.plansUrl === null
|
|
31
|
+
? null
|
|
32
|
+
: plansLink(entitlement.plansUrl, {
|
|
33
|
+
from: context.productId,
|
|
34
|
+
...(context.feature === undefined ? {} : { feature: context.feature }),
|
|
35
|
+
});
|
|
36
|
+
const channel: BillingChannel | undefined = entitlement.billing?.channel;
|
|
37
|
+
switch (channel) {
|
|
38
|
+
case 'web':
|
|
39
|
+
return {
|
|
40
|
+
kind: 'manage',
|
|
41
|
+
channel,
|
|
42
|
+
manageUrl: link ?? entitlement.billing?.manageUrl ?? null,
|
|
43
|
+
soldBy: null,
|
|
44
|
+
};
|
|
45
|
+
case 'app_store':
|
|
46
|
+
case 'play_store':
|
|
47
|
+
return {
|
|
48
|
+
kind: 'manage',
|
|
49
|
+
channel,
|
|
50
|
+
manageUrl: null,
|
|
51
|
+
soldBy: entitlement.billing?.soldBy ?? null,
|
|
52
|
+
};
|
|
53
|
+
case 'family':
|
|
54
|
+
case 'organization':
|
|
55
|
+
return { kind: 'covered', channel };
|
|
56
|
+
case 'promo':
|
|
57
|
+
case undefined:
|
|
58
|
+
return { kind: 'purchase', plansUrl: link };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface PlansLinkContext {
|
|
2
|
+
/** The product the visitor comes from, for example `eigenruhe`. */
|
|
3
|
+
readonly from?: string;
|
|
4
|
+
/** The locked feature key, for example `eigenruhe.studio`. Dropped without `from`. */
|
|
5
|
+
readonly feature?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// React Native's URL polyfill cannot edit search parameters, so this builds the string.
|
|
9
|
+
const httpUrl = /^https?:\/\/[^\s/?#@]+(?:[/?]\S*)?$/u;
|
|
10
|
+
|
|
11
|
+
/** The hub Plans link `<plansUrl>?from=<product>&feature=<key>`. Both are optional, and the hub ignores `feature` without `from`, so it is sent only with `from`. */
|
|
12
|
+
export function plansLink(plansUrl: string, context: PlansLinkContext = {}): string {
|
|
13
|
+
if (!httpUrl.test(plansUrl)) throw new TypeError('The plans URL must be an HTTP(S) URL.');
|
|
14
|
+
const parameters: string[] = [];
|
|
15
|
+
if (context.from) parameters.push(`from=${encodeURIComponent(context.from)}`);
|
|
16
|
+
if (context.from && context.feature)
|
|
17
|
+
parameters.push(`feature=${encodeURIComponent(context.feature)}`);
|
|
18
|
+
const query = parameters.join('&');
|
|
19
|
+
if (query === '') return plansUrl;
|
|
20
|
+
return `${plansUrl}${plansUrl.includes('?') ? '&' : '?'}${query}`;
|
|
21
|
+
}
|
package/src/react.test.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
|
-
import { act, cleanup, renderHook } from '@testing-library/react';
|
|
2
|
+
import { act, cleanup, render, renderHook } from '@testing-library/react';
|
|
3
3
|
import { createElement, type ReactNode } from 'react';
|
|
4
4
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
5
5
|
|
|
6
6
|
import { createNoopPurchasesPort, PurchaseFlow, type VerifiedEntitlement } from './index.js';
|
|
7
7
|
import {
|
|
8
|
+
LockedFeature,
|
|
8
9
|
PurchasesProvider,
|
|
9
10
|
useEntitlement,
|
|
10
11
|
useFeature,
|
|
@@ -13,6 +14,7 @@ import {
|
|
|
13
14
|
} from './react.js';
|
|
14
15
|
|
|
15
16
|
const SUBJECT = 'suite|person-1';
|
|
17
|
+
const PLANS = 'https://accounts.example.test/plans';
|
|
16
18
|
|
|
17
19
|
afterEach(() => {
|
|
18
20
|
cleanup();
|
|
@@ -20,16 +22,35 @@ afterEach(() => {
|
|
|
20
22
|
|
|
21
23
|
function setup() {
|
|
22
24
|
let current: VerifiedEntitlement | null = null;
|
|
25
|
+
let fail = false;
|
|
23
26
|
const flow = new PurchaseFlow({
|
|
24
27
|
port: createNoopPurchasesPort(),
|
|
25
|
-
backend: {
|
|
28
|
+
backend: {
|
|
29
|
+
refreshEntitlement: () =>
|
|
30
|
+
fail ? Promise.reject(new Error('offline')) : Promise.resolve(current),
|
|
31
|
+
},
|
|
26
32
|
});
|
|
27
33
|
const wrapper = ({ children }: { children: ReactNode }) =>
|
|
28
|
-
createElement(PurchasesProvider, { flow }, children);
|
|
29
|
-
const grant = (
|
|
30
|
-
|
|
34
|
+
createElement(PurchasesProvider, { flow, productId: 'hebkit' }, children);
|
|
35
|
+
const grant = (
|
|
36
|
+
features: string[],
|
|
37
|
+
revision: number,
|
|
38
|
+
billing: VerifiedEntitlement['billing'] = null,
|
|
39
|
+
) => {
|
|
40
|
+
current = {
|
|
41
|
+
mode: 'hub',
|
|
42
|
+
suiteSubject: SUBJECT,
|
|
43
|
+
tier: 'premium',
|
|
44
|
+
features,
|
|
45
|
+
revision,
|
|
46
|
+
plansUrl: PLANS,
|
|
47
|
+
billing,
|
|
48
|
+
};
|
|
31
49
|
};
|
|
32
|
-
|
|
50
|
+
const failNext = () => {
|
|
51
|
+
fail = true;
|
|
52
|
+
};
|
|
53
|
+
return { flow, wrapper, grant, failNext };
|
|
33
54
|
}
|
|
34
55
|
|
|
35
56
|
describe('purchase hooks', () => {
|
|
@@ -37,24 +58,50 @@ describe('purchase hooks', () => {
|
|
|
37
58
|
const { flow, wrapper, grant } = setup();
|
|
38
59
|
const view = renderHook(
|
|
39
60
|
() => ({ exports: useFeature('export'), entitlement: useEntitlement() }),
|
|
40
|
-
{
|
|
41
|
-
wrapper,
|
|
42
|
-
},
|
|
61
|
+
{ wrapper },
|
|
43
62
|
);
|
|
44
|
-
expect(view.result.current.exports).
|
|
63
|
+
expect(view.result.current.exports).toEqual({
|
|
64
|
+
status: 'locked',
|
|
65
|
+
entitlement: null,
|
|
66
|
+
paywall: { kind: 'none' },
|
|
67
|
+
});
|
|
45
68
|
|
|
46
69
|
await act(async () => {
|
|
47
70
|
await flow.identify(SUBJECT);
|
|
71
|
+
});
|
|
72
|
+
expect(view.result.current.exports).toEqual({ status: 'loading' });
|
|
73
|
+
|
|
74
|
+
await act(async () => {
|
|
75
|
+
grant(['sync'], 1);
|
|
76
|
+
await flow.refreshEntitlement();
|
|
77
|
+
});
|
|
78
|
+
expect(view.result.current.exports).toMatchObject({
|
|
79
|
+
status: 'locked',
|
|
80
|
+
paywall: { kind: 'purchase', plansUrl: `${PLANS}?from=hebkit&feature=export` },
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
await act(async () => {
|
|
48
84
|
grant(['export'], 2);
|
|
49
85
|
await flow.refreshEntitlement();
|
|
50
86
|
});
|
|
51
|
-
expect(view.result.current.exports).toBe(
|
|
87
|
+
expect(view.result.current.exports.status).toBe('granted');
|
|
52
88
|
expect(view.result.current.entitlement?.revision).toBe(2);
|
|
53
89
|
|
|
54
90
|
await act(async () => {
|
|
55
91
|
await flow.signOut();
|
|
56
92
|
});
|
|
57
|
-
expect(view.result.current.exports).toBe(
|
|
93
|
+
expect(view.result.current.exports.status).toBe('locked');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('reports an unreadable plan as unavailable, never as granted or for sale', async () => {
|
|
97
|
+
const { flow, wrapper, failNext } = setup();
|
|
98
|
+
const view = renderHook(() => useFeature('export'), { wrapper });
|
|
99
|
+
await act(async () => {
|
|
100
|
+
await flow.identify(SUBJECT);
|
|
101
|
+
failNext();
|
|
102
|
+
await flow.refreshEntitlement().catch(() => undefined);
|
|
103
|
+
});
|
|
104
|
+
expect(view.result.current).toEqual({ status: 'unavailable' });
|
|
58
105
|
});
|
|
59
106
|
|
|
60
107
|
it('exposes the flow and its state', async () => {
|
|
@@ -76,3 +123,57 @@ describe('purchase hooks', () => {
|
|
|
76
123
|
);
|
|
77
124
|
});
|
|
78
125
|
});
|
|
126
|
+
|
|
127
|
+
describe('LockedFeature', () => {
|
|
128
|
+
function view(wrapper: (props: { children: ReactNode }) => ReactNode) {
|
|
129
|
+
return render(
|
|
130
|
+
createElement(LockedFeature, {
|
|
131
|
+
feature: 'export',
|
|
132
|
+
locked: (paywall) => `locked:${paywall.kind}`,
|
|
133
|
+
loading: 'loading',
|
|
134
|
+
unavailable: 'retry',
|
|
135
|
+
children: 'export',
|
|
136
|
+
}),
|
|
137
|
+
{ wrapper },
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
it('renders each state', async () => {
|
|
142
|
+
const { flow, wrapper, grant, failNext } = setup();
|
|
143
|
+
const screen = view(wrapper);
|
|
144
|
+
expect(screen.container.textContent).toBe('locked:none');
|
|
145
|
+
await act(async () => {
|
|
146
|
+
await flow.identify(SUBJECT);
|
|
147
|
+
});
|
|
148
|
+
expect(screen.container.textContent).toBe('loading');
|
|
149
|
+
await act(async () => {
|
|
150
|
+
grant([], 1, { channel: 'play_store', soldBy: 'eigenruhe' });
|
|
151
|
+
await flow.refreshEntitlement();
|
|
152
|
+
});
|
|
153
|
+
expect(screen.container.textContent).toBe('locked:manage');
|
|
154
|
+
await act(async () => {
|
|
155
|
+
grant(['export'], 2);
|
|
156
|
+
await flow.refreshEntitlement();
|
|
157
|
+
});
|
|
158
|
+
expect(screen.container.textContent).toBe('export');
|
|
159
|
+
await act(async () => {
|
|
160
|
+
await flow.signOut();
|
|
161
|
+
await flow.identify(SUBJECT);
|
|
162
|
+
failNext();
|
|
163
|
+
await flow.refreshEntitlement().catch(() => undefined);
|
|
164
|
+
});
|
|
165
|
+
expect(screen.container.textContent).toBe('retry');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('renders nothing for loading and unavailable by default', async () => {
|
|
169
|
+
const { flow, wrapper } = setup();
|
|
170
|
+
const screen = render(
|
|
171
|
+
createElement(LockedFeature, { feature: 'export', locked: () => 'locked', children: 'x' }),
|
|
172
|
+
{ wrapper },
|
|
173
|
+
);
|
|
174
|
+
await act(async () => {
|
|
175
|
+
await flow.identify(SUBJECT);
|
|
176
|
+
});
|
|
177
|
+
expect(screen.container.textContent).toBe('');
|
|
178
|
+
});
|
|
179
|
+
});
|
package/src/react.ts
CHANGED
|
@@ -1,24 +1,41 @@
|
|
|
1
|
-
import { createContext, createElement, useContext, useSyncExternalStore } from 'react';
|
|
1
|
+
import { createContext, createElement, Fragment, useContext, useSyncExternalStore } from 'react';
|
|
2
2
|
import type { ReactNode } from 'react';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { featureAccess, type FeatureAccess } from './access.js';
|
|
5
|
+
import type { VerifiedEntitlement } from './entitlement.js';
|
|
5
6
|
import type { PurchaseFlow, PurchaseState } from './flow.js';
|
|
7
|
+
import type { Paywall } from './paywall.js';
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
interface PurchasesContext {
|
|
10
|
+
readonly flow: PurchaseFlow;
|
|
11
|
+
readonly productId: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const PurchaseFlowContext = createContext<PurchasesContext | null>(null);
|
|
8
15
|
|
|
9
16
|
export interface PurchasesProviderProps {
|
|
10
17
|
readonly flow: PurchaseFlow;
|
|
18
|
+
/** The calling product, sent as `from` on Plans links. */
|
|
19
|
+
readonly productId: string;
|
|
11
20
|
readonly children?: ReactNode;
|
|
12
21
|
}
|
|
13
22
|
|
|
14
|
-
export function PurchasesProvider({
|
|
15
|
-
|
|
23
|
+
export function PurchasesProvider({
|
|
24
|
+
flow,
|
|
25
|
+
productId,
|
|
26
|
+
children,
|
|
27
|
+
}: PurchasesProviderProps): ReactNode {
|
|
28
|
+
return createElement(PurchaseFlowContext.Provider, { value: { flow, productId } }, children);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function usePurchasesContext(): PurchasesContext {
|
|
32
|
+
const context = useContext(PurchaseFlowContext);
|
|
33
|
+
if (context === null) throw new Error('usePurchaseFlow requires a PurchasesProvider');
|
|
34
|
+
return context;
|
|
16
35
|
}
|
|
17
36
|
|
|
18
37
|
export function usePurchaseFlow(): PurchaseFlow {
|
|
19
|
-
|
|
20
|
-
if (flow === null) throw new Error('usePurchaseFlow requires a PurchasesProvider');
|
|
21
|
-
return flow;
|
|
38
|
+
return usePurchasesContext().flow;
|
|
22
39
|
}
|
|
23
40
|
|
|
24
41
|
export function usePurchaseState(): PurchaseState {
|
|
@@ -30,7 +47,33 @@ export function useEntitlement(): VerifiedEntitlement | null {
|
|
|
30
47
|
return usePurchaseState().entitlement;
|
|
31
48
|
}
|
|
32
49
|
|
|
33
|
-
/**
|
|
34
|
-
export function useFeature(key: string):
|
|
35
|
-
|
|
50
|
+
/** Access to a feature key from the backend-verified entitlement. See `featureAccess`. */
|
|
51
|
+
export function useFeature(key: string): FeatureAccess {
|
|
52
|
+
const { productId } = usePurchasesContext();
|
|
53
|
+
return featureAccess(usePurchaseState(), key, { productId });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface LockedFeatureProps {
|
|
57
|
+
readonly feature: string;
|
|
58
|
+
readonly children?: ReactNode;
|
|
59
|
+
/** Rendered when the plan lacks the feature, with the C18 paywall decision. */
|
|
60
|
+
readonly locked: (paywall: Paywall) => ReactNode;
|
|
61
|
+
readonly loading?: ReactNode;
|
|
62
|
+
/** Rendered when the plan cannot be read. Offer a retry, never a purchase. */
|
|
63
|
+
readonly unavailable?: ReactNode;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Renders `children` only while the verified entitlement grants `feature`. */
|
|
67
|
+
export function LockedFeature(props: LockedFeatureProps): ReactNode {
|
|
68
|
+
const access = useFeature(props.feature);
|
|
69
|
+
switch (access.status) {
|
|
70
|
+
case 'granted':
|
|
71
|
+
return createElement(Fragment, null, props.children);
|
|
72
|
+
case 'locked':
|
|
73
|
+
return props.locked(access.paywall);
|
|
74
|
+
case 'loading':
|
|
75
|
+
return props.loading ?? null;
|
|
76
|
+
case 'unavailable':
|
|
77
|
+
return props.unavailable ?? null;
|
|
78
|
+
}
|
|
36
79
|
}
|