@umituz/react-native-subscription 2.27.145 → 2.27.146
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/package.json +1 -1
- package/src/domains/subscription/presentation/featureGateActions.ts +37 -0
- package/src/domains/subscription/presentation/featureGateHelpers.ts +31 -0
- package/src/domains/subscription/presentation/featureGateRefs.ts +27 -0
- package/src/domains/subscription/presentation/screens/components/SubscriptionHeader.constants.ts +1 -0
- package/src/domains/subscription/presentation/screens/components/SubscriptionHeader.styles.ts +41 -0
- package/src/domains/subscription/presentation/screens/components/SubscriptionHeader.tsx +18 -108
- package/src/domains/subscription/presentation/screens/components/SubscriptionHeader.types.ts +20 -0
- package/src/domains/subscription/presentation/screens/components/SubscriptionHeaderContent.tsx +58 -0
- package/src/domains/subscription/presentation/useFeatureGate.ts +46 -94
- package/src/domains/subscription/presentation/useFeatureGate.types.ts +18 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-subscription",
|
|
3
|
-
"version": "2.27.
|
|
3
|
+
"version": "2.27.146",
|
|
4
4
|
"description": "Complete subscription management with RevenueCat, paywall UI, and credits system for React Native apps",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { MutableRefObject } from "react";
|
|
2
|
+
|
|
3
|
+
export const executeFeatureAction = (
|
|
4
|
+
action: () => void | Promise<void>,
|
|
5
|
+
isAuthenticated: boolean,
|
|
6
|
+
onShowAuthModal: (callback: () => void | Promise<void>) => void,
|
|
7
|
+
hasSubscriptionRef: MutableRefObject<boolean>,
|
|
8
|
+
creditBalanceRef: MutableRefObject<number>,
|
|
9
|
+
requiredCreditsRef: MutableRefObject<number>,
|
|
10
|
+
onShowPaywallRef: MutableRefObject<(requiredCredits?: number) => void>,
|
|
11
|
+
pendingActionRef: MutableRefObject<(() => void | Promise<void>) | null>,
|
|
12
|
+
isWaitingForAuthCreditsRef: MutableRefObject<boolean>,
|
|
13
|
+
isWaitingForPurchaseRef: MutableRefObject<boolean>
|
|
14
|
+
): void => {
|
|
15
|
+
if (!isAuthenticated) {
|
|
16
|
+
const postAuthAction = () => {
|
|
17
|
+
pendingActionRef.current = action;
|
|
18
|
+
isWaitingForAuthCreditsRef.current = true;
|
|
19
|
+
};
|
|
20
|
+
onShowAuthModal(postAuthAction);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (hasSubscriptionRef.current) {
|
|
25
|
+
action();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (creditBalanceRef.current < requiredCreditsRef.current) {
|
|
30
|
+
pendingActionRef.current = action;
|
|
31
|
+
isWaitingForPurchaseRef.current = true;
|
|
32
|
+
onShowPaywallRef.current(requiredCreditsRef.current);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
action();
|
|
37
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const DEFAULT_REQUIRED_CREDITS = 1;
|
|
2
|
+
|
|
3
|
+
export const shouldExecuteAuthAction = (
|
|
4
|
+
isWaitingForAuthCredits: boolean,
|
|
5
|
+
isCreditsLoaded: boolean,
|
|
6
|
+
hasPendingAction: boolean,
|
|
7
|
+
hasSubscription: boolean,
|
|
8
|
+
creditBalance: number,
|
|
9
|
+
requiredCredits: number
|
|
10
|
+
): boolean => {
|
|
11
|
+
if (!isWaitingForAuthCredits || !isCreditsLoaded || !hasPendingAction) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return hasSubscription || creditBalance >= requiredCredits;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const shouldExecutePurchaseAction = (
|
|
18
|
+
isWaitingForPurchase: boolean,
|
|
19
|
+
creditBalance: number,
|
|
20
|
+
prevBalance: number,
|
|
21
|
+
hasSubscription: boolean,
|
|
22
|
+
prevHasSubscription: boolean,
|
|
23
|
+
hasPendingAction: boolean
|
|
24
|
+
): boolean => {
|
|
25
|
+
if (!isWaitingForPurchase || !hasPendingAction) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
const creditsIncreased = creditBalance > prevBalance;
|
|
29
|
+
const subscriptionAcquired = hasSubscription && !prevHasSubscription;
|
|
30
|
+
return creditsIncreased || subscriptionAcquired;
|
|
31
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useRef, useEffect, type MutableRefObject } from "react";
|
|
2
|
+
|
|
3
|
+
export interface FeatureGateRefs {
|
|
4
|
+
creditBalanceRef: MutableRefObject<number>;
|
|
5
|
+
hasSubscriptionRef: MutableRefObject<boolean>;
|
|
6
|
+
onShowPaywallRef: MutableRefObject<(requiredCredits?: number) => void>;
|
|
7
|
+
requiredCreditsRef: MutableRefObject<number>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const useSyncedRefs = (
|
|
11
|
+
creditBalance: number,
|
|
12
|
+
hasSubscription: boolean,
|
|
13
|
+
onShowPaywall: (requiredCredits?: number) => void,
|
|
14
|
+
requiredCredits: number
|
|
15
|
+
): FeatureGateRefs => {
|
|
16
|
+
const creditBalanceRef = useRef(creditBalance);
|
|
17
|
+
const hasSubscriptionRef = useRef(hasSubscription);
|
|
18
|
+
const onShowPaywallRef = useRef(onShowPaywall);
|
|
19
|
+
const requiredCreditsRef = useRef(requiredCredits);
|
|
20
|
+
|
|
21
|
+
useEffect(() => { creditBalanceRef.current = creditBalance; }, [creditBalance]);
|
|
22
|
+
useEffect(() => { hasSubscriptionRef.current = hasSubscription; }, [hasSubscription]);
|
|
23
|
+
useEffect(() => { onShowPaywallRef.current = onShowPaywall; }, [onShowPaywall]);
|
|
24
|
+
useEffect(() => { requiredCreditsRef.current = requiredCredits; }, [requiredCredits]);
|
|
25
|
+
|
|
26
|
+
return { creditBalanceRef, hasSubscriptionRef, onShowPaywallRef, requiredCreditsRef };
|
|
27
|
+
};
|
package/src/domains/subscription/presentation/screens/components/SubscriptionHeader.constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const EXPIRING_SOON_THRESHOLD_DAYS = 7;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { StyleSheet } from "react-native";
|
|
2
|
+
import type { AppDesignTokens } from "@umituz/react-native-design-system";
|
|
3
|
+
|
|
4
|
+
export const createSubscriptionHeaderStyles = (tokens: AppDesignTokens) =>
|
|
5
|
+
StyleSheet.create({
|
|
6
|
+
container: {
|
|
7
|
+
borderRadius: tokens.radius.lg,
|
|
8
|
+
padding: tokens.spacing.lg,
|
|
9
|
+
gap: tokens.spacing.lg,
|
|
10
|
+
backgroundColor: tokens.colors.surface,
|
|
11
|
+
},
|
|
12
|
+
header: {
|
|
13
|
+
flexDirection: "row",
|
|
14
|
+
justifyContent: "space-between",
|
|
15
|
+
alignItems: "center",
|
|
16
|
+
},
|
|
17
|
+
titleContainer: {
|
|
18
|
+
flex: 1,
|
|
19
|
+
marginRight: tokens.spacing.md,
|
|
20
|
+
},
|
|
21
|
+
title: {
|
|
22
|
+
fontWeight: "700",
|
|
23
|
+
},
|
|
24
|
+
details: {
|
|
25
|
+
gap: tokens.spacing.md,
|
|
26
|
+
paddingTop: tokens.spacing.md,
|
|
27
|
+
},
|
|
28
|
+
row: {
|
|
29
|
+
flexDirection: "row",
|
|
30
|
+
justifyContent: "space-between",
|
|
31
|
+
alignItems: "center",
|
|
32
|
+
gap: tokens.spacing.lg,
|
|
33
|
+
},
|
|
34
|
+
label: {
|
|
35
|
+
flex: 1,
|
|
36
|
+
},
|
|
37
|
+
value: {
|
|
38
|
+
fontWeight: "600",
|
|
39
|
+
textAlign: "right",
|
|
40
|
+
},
|
|
41
|
+
});
|
|
@@ -1,34 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Subscription Header Component
|
|
3
|
-
* Displays status badge and subscription details
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
1
|
import React, { useMemo } from "react";
|
|
7
|
-
import { View
|
|
2
|
+
import { View } from "react-native";
|
|
8
3
|
import { useAppDesignTokens, AtomicText } from "@umituz/react-native-design-system";
|
|
9
4
|
import { PremiumStatusBadge } from "../../components/details/PremiumStatusBadge";
|
|
10
|
-
import {
|
|
5
|
+
import type { SubscriptionHeaderProps } from "./SubscriptionHeader.types";
|
|
6
|
+
import { createSubscriptionHeaderStyles } from "./SubscriptionHeader.styles";
|
|
7
|
+
import { EXPIRING_SOON_THRESHOLD_DAYS } from "./SubscriptionHeader.constants";
|
|
8
|
+
import { SubscriptionHeaderContent } from "./SubscriptionHeaderContent";
|
|
11
9
|
|
|
12
|
-
export
|
|
13
|
-
statusType: "active" | "expired" | "none" | "canceled";
|
|
14
|
-
showExpirationDate: boolean;
|
|
15
|
-
isLifetime: boolean;
|
|
16
|
-
expirationDate?: string;
|
|
17
|
-
purchaseDate?: string;
|
|
18
|
-
daysRemaining?: number | null;
|
|
19
|
-
hideTitle?: boolean;
|
|
20
|
-
translations: {
|
|
21
|
-
title: string;
|
|
22
|
-
statusActive: string;
|
|
23
|
-
statusExpired: string;
|
|
24
|
-
statusFree: string;
|
|
25
|
-
statusCanceled: string;
|
|
26
|
-
statusLabel: string;
|
|
27
|
-
lifetimeLabel: string;
|
|
28
|
-
expiresLabel: string;
|
|
29
|
-
purchasedLabel: string;
|
|
30
|
-
};
|
|
31
|
-
}
|
|
10
|
+
export type { SubscriptionHeaderProps } from "./SubscriptionHeader.types";
|
|
32
11
|
|
|
33
12
|
export const SubscriptionHeader: React.FC<SubscriptionHeaderProps> = ({
|
|
34
13
|
statusType,
|
|
@@ -41,60 +20,15 @@ export const SubscriptionHeader: React.FC<SubscriptionHeaderProps> = ({
|
|
|
41
20
|
translations,
|
|
42
21
|
}) => {
|
|
43
22
|
const tokens = useAppDesignTokens();
|
|
44
|
-
const showExpiring =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const styles = useMemo(
|
|
48
|
-
() =>
|
|
49
|
-
StyleSheet.create({
|
|
50
|
-
container: {
|
|
51
|
-
borderRadius: tokens.radius.lg,
|
|
52
|
-
padding: tokens.spacing.lg,
|
|
53
|
-
gap: tokens.spacing.lg,
|
|
54
|
-
backgroundColor: tokens.colors.surface,
|
|
55
|
-
},
|
|
56
|
-
header: {
|
|
57
|
-
flexDirection: "row",
|
|
58
|
-
justifyContent: "space-between",
|
|
59
|
-
alignItems: "center",
|
|
60
|
-
},
|
|
61
|
-
titleContainer: {
|
|
62
|
-
flex: 1,
|
|
63
|
-
marginRight: tokens.spacing.md,
|
|
64
|
-
},
|
|
65
|
-
title: {
|
|
66
|
-
fontWeight: "700",
|
|
67
|
-
},
|
|
68
|
-
details: {
|
|
69
|
-
gap: tokens.spacing.md,
|
|
70
|
-
paddingTop: tokens.spacing.md,
|
|
71
|
-
},
|
|
72
|
-
row: {
|
|
73
|
-
flexDirection: "row",
|
|
74
|
-
justifyContent: "space-between",
|
|
75
|
-
alignItems: "center",
|
|
76
|
-
gap: tokens.spacing.lg,
|
|
77
|
-
},
|
|
78
|
-
label: {
|
|
79
|
-
flex: 1,
|
|
80
|
-
},
|
|
81
|
-
value: {
|
|
82
|
-
fontWeight: "600",
|
|
83
|
-
textAlign: "right",
|
|
84
|
-
},
|
|
85
|
-
}),
|
|
86
|
-
[tokens]
|
|
87
|
-
);
|
|
23
|
+
const showExpiring = daysRemaining !== null && daysRemaining !== undefined && daysRemaining <= EXPIRING_SOON_THRESHOLD_DAYS;
|
|
24
|
+
const styles = useMemo(() => createSubscriptionHeaderStyles(tokens), [tokens]);
|
|
88
25
|
|
|
89
26
|
return (
|
|
90
27
|
<View style={styles.container}>
|
|
91
28
|
<View style={styles.header}>
|
|
92
29
|
{!hideTitle && (
|
|
93
30
|
<View style={styles.titleContainer}>
|
|
94
|
-
<AtomicText
|
|
95
|
-
type="headlineSmall"
|
|
96
|
-
style={[styles.title, { color: tokens.colors.textPrimary }]}
|
|
97
|
-
>
|
|
31
|
+
<AtomicText type="headlineSmall" style={[styles.title, { color: tokens.colors.textPrimary }]}>
|
|
98
32
|
{translations.title}
|
|
99
33
|
</AtomicText>
|
|
100
34
|
</View>
|
|
@@ -108,39 +42,15 @@ export const SubscriptionHeader: React.FC<SubscriptionHeaderProps> = ({
|
|
|
108
42
|
/>
|
|
109
43
|
</View>
|
|
110
44
|
|
|
111
|
-
<
|
|
112
|
-
{isLifetime
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
) : (
|
|
121
|
-
<>
|
|
122
|
-
{showExpirationDate && expirationDate && (
|
|
123
|
-
<DetailRow
|
|
124
|
-
label={translations.expiresLabel}
|
|
125
|
-
value={expirationDate}
|
|
126
|
-
highlight={showExpiring}
|
|
127
|
-
style={styles.row}
|
|
128
|
-
labelStyle={styles.label}
|
|
129
|
-
valueStyle={styles.value}
|
|
130
|
-
/>
|
|
131
|
-
)}
|
|
132
|
-
{purchaseDate && (
|
|
133
|
-
<DetailRow
|
|
134
|
-
label={translations.purchasedLabel}
|
|
135
|
-
value={purchaseDate}
|
|
136
|
-
style={styles.row}
|
|
137
|
-
labelStyle={styles.label}
|
|
138
|
-
valueStyle={styles.value}
|
|
139
|
-
/>
|
|
140
|
-
)}
|
|
141
|
-
</>
|
|
142
|
-
)}
|
|
143
|
-
</View>
|
|
45
|
+
<SubscriptionHeaderContent
|
|
46
|
+
isLifetime={isLifetime}
|
|
47
|
+
showExpirationDate={showExpirationDate}
|
|
48
|
+
expirationDate={expirationDate}
|
|
49
|
+
purchaseDate={purchaseDate}
|
|
50
|
+
showExpiring={showExpiring}
|
|
51
|
+
translations={translations}
|
|
52
|
+
styles={styles}
|
|
53
|
+
/>
|
|
144
54
|
</View>
|
|
145
55
|
);
|
|
146
56
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface SubscriptionHeaderProps {
|
|
2
|
+
statusType: "active" | "expired" | "none" | "canceled";
|
|
3
|
+
showExpirationDate: boolean;
|
|
4
|
+
isLifetime: boolean;
|
|
5
|
+
expirationDate?: string;
|
|
6
|
+
purchaseDate?: string;
|
|
7
|
+
daysRemaining?: number | null;
|
|
8
|
+
hideTitle?: boolean;
|
|
9
|
+
translations: {
|
|
10
|
+
title: string;
|
|
11
|
+
statusActive: string;
|
|
12
|
+
statusExpired: string;
|
|
13
|
+
statusFree: string;
|
|
14
|
+
statusCanceled: string;
|
|
15
|
+
statusLabel: string;
|
|
16
|
+
lifetimeLabel: string;
|
|
17
|
+
expiresLabel: string;
|
|
18
|
+
purchasedLabel: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
package/src/domains/subscription/presentation/screens/components/SubscriptionHeaderContent.tsx
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { View } from "react-native";
|
|
3
|
+
import { DetailRow } from "../../components/details/DetailRow";
|
|
4
|
+
import type { SubscriptionHeaderProps } from "./SubscriptionHeader.types";
|
|
5
|
+
|
|
6
|
+
interface SubscriptionHeaderContentProps {
|
|
7
|
+
isLifetime: boolean;
|
|
8
|
+
showExpirationDate: boolean;
|
|
9
|
+
expirationDate?: string;
|
|
10
|
+
purchaseDate?: string;
|
|
11
|
+
showExpiring: boolean;
|
|
12
|
+
translations: SubscriptionHeaderProps["translations"];
|
|
13
|
+
styles: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const SubscriptionHeaderContent: React.FC<SubscriptionHeaderContentProps> = ({
|
|
17
|
+
isLifetime,
|
|
18
|
+
showExpirationDate,
|
|
19
|
+
expirationDate,
|
|
20
|
+
purchaseDate,
|
|
21
|
+
showExpiring,
|
|
22
|
+
translations,
|
|
23
|
+
styles,
|
|
24
|
+
}) => (
|
|
25
|
+
<View style={styles.details}>
|
|
26
|
+
{isLifetime ? (
|
|
27
|
+
<DetailRow
|
|
28
|
+
label={translations.statusLabel}
|
|
29
|
+
value={translations.lifetimeLabel}
|
|
30
|
+
style={styles.row}
|
|
31
|
+
labelStyle={styles.label}
|
|
32
|
+
valueStyle={styles.value}
|
|
33
|
+
/>
|
|
34
|
+
) : (
|
|
35
|
+
<>
|
|
36
|
+
{showExpirationDate && expirationDate && (
|
|
37
|
+
<DetailRow
|
|
38
|
+
label={translations.expiresLabel}
|
|
39
|
+
value={expirationDate}
|
|
40
|
+
highlight={showExpiring}
|
|
41
|
+
style={styles.row}
|
|
42
|
+
labelStyle={styles.label}
|
|
43
|
+
valueStyle={styles.value}
|
|
44
|
+
/>
|
|
45
|
+
)}
|
|
46
|
+
{purchaseDate && (
|
|
47
|
+
<DetailRow
|
|
48
|
+
label={translations.purchasedLabel}
|
|
49
|
+
value={purchaseDate}
|
|
50
|
+
style={styles.row}
|
|
51
|
+
labelStyle={styles.label}
|
|
52
|
+
valueStyle={styles.value}
|
|
53
|
+
/>
|
|
54
|
+
)}
|
|
55
|
+
</>
|
|
56
|
+
)}
|
|
57
|
+
</View>
|
|
58
|
+
);
|
|
@@ -1,30 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useFeatureGate Hook
|
|
3
|
-
* Unified feature gate: Auth → Subscription → Credits
|
|
4
|
-
* Uses ref pattern to avoid stale closure issues.
|
|
5
|
-
* Event-driven approach - no polling, no waiting.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
1
|
import { useCallback, useRef, useEffect } from "react";
|
|
2
|
+
import type { UseFeatureGateParams, UseFeatureGateResult } from "./useFeatureGate.types";
|
|
3
|
+
import { DEFAULT_REQUIRED_CREDITS, shouldExecuteAuthAction, shouldExecutePurchaseAction } from "./featureGateHelpers";
|
|
4
|
+
import { useSyncedRefs } from "./featureGateRefs";
|
|
5
|
+
import { executeFeatureAction } from "./featureGateActions";
|
|
9
6
|
|
|
10
|
-
export
|
|
11
|
-
readonly isAuthenticated: boolean;
|
|
12
|
-
readonly onShowAuthModal: (pendingCallback: () => void | Promise<void>) => void;
|
|
13
|
-
readonly hasSubscription?: boolean;
|
|
14
|
-
readonly creditBalance: number;
|
|
15
|
-
readonly requiredCredits?: number;
|
|
16
|
-
readonly onShowPaywall: (requiredCredits?: number) => void;
|
|
17
|
-
readonly isCreditsLoaded?: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface UseFeatureGateResult {
|
|
21
|
-
readonly requireFeature: (action: () => void | Promise<void>) => void;
|
|
22
|
-
readonly isAuthenticated: boolean;
|
|
23
|
-
readonly hasSubscription: boolean;
|
|
24
|
-
readonly hasCredits: boolean;
|
|
25
|
-
readonly creditBalance: number;
|
|
26
|
-
readonly canAccess: boolean;
|
|
27
|
-
}
|
|
7
|
+
export type { UseFeatureGateParams, UseFeatureGateResult } from "./useFeatureGate.types";
|
|
28
8
|
|
|
29
9
|
export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResult {
|
|
30
10
|
const {
|
|
@@ -32,7 +12,7 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
|
|
|
32
12
|
onShowAuthModal,
|
|
33
13
|
hasSubscription = false,
|
|
34
14
|
creditBalance,
|
|
35
|
-
requiredCredits =
|
|
15
|
+
requiredCredits = DEFAULT_REQUIRED_CREDITS,
|
|
36
16
|
onShowPaywall,
|
|
37
17
|
isCreditsLoaded = true,
|
|
38
18
|
} = params;
|
|
@@ -42,52 +22,41 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
|
|
|
42
22
|
const isWaitingForPurchaseRef = useRef(false);
|
|
43
23
|
const isWaitingForAuthCreditsRef = useRef(false);
|
|
44
24
|
|
|
45
|
-
const creditBalanceRef =
|
|
46
|
-
const hasSubscriptionRef = useRef(hasSubscription);
|
|
47
|
-
const onShowPaywallRef = useRef(onShowPaywall);
|
|
48
|
-
const requiredCreditsRef = useRef(requiredCredits);
|
|
49
|
-
|
|
50
|
-
useEffect(() => {
|
|
51
|
-
creditBalanceRef.current = creditBalance;
|
|
52
|
-
}, [creditBalance]);
|
|
53
|
-
|
|
54
|
-
useEffect(() => {
|
|
55
|
-
hasSubscriptionRef.current = hasSubscription;
|
|
56
|
-
}, [hasSubscription]);
|
|
57
|
-
|
|
58
|
-
useEffect(() => {
|
|
59
|
-
onShowPaywallRef.current = onShowPaywall;
|
|
60
|
-
}, [onShowPaywall]);
|
|
61
|
-
|
|
62
|
-
useEffect(() => {
|
|
63
|
-
requiredCreditsRef.current = requiredCredits;
|
|
64
|
-
}, [requiredCredits]);
|
|
25
|
+
const { creditBalanceRef, hasSubscriptionRef, onShowPaywallRef, requiredCreditsRef } = useSyncedRefs(creditBalance, hasSubscription, onShowPaywall, requiredCredits);
|
|
65
26
|
|
|
66
27
|
useEffect(() => {
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
28
|
+
if (shouldExecuteAuthAction(
|
|
29
|
+
isWaitingForAuthCreditsRef.current,
|
|
30
|
+
isCreditsLoaded,
|
|
31
|
+
!!pendingActionRef.current,
|
|
32
|
+
hasSubscription,
|
|
33
|
+
creditBalance,
|
|
34
|
+
requiredCredits
|
|
35
|
+
)) {
|
|
36
|
+
isWaitingForAuthCreditsRef.current = false;
|
|
37
|
+
const action = pendingActionRef.current!;
|
|
75
38
|
pendingActionRef.current = null;
|
|
76
39
|
action();
|
|
77
40
|
return;
|
|
78
41
|
}
|
|
79
42
|
|
|
80
|
-
|
|
81
|
-
|
|
43
|
+
if (isWaitingForAuthCreditsRef.current && isCreditsLoaded && pendingActionRef.current) {
|
|
44
|
+
isWaitingForAuthCreditsRef.current = false;
|
|
45
|
+
isWaitingForPurchaseRef.current = true;
|
|
46
|
+
onShowPaywall(requiredCredits);
|
|
47
|
+
}
|
|
82
48
|
}, [isCreditsLoaded, creditBalance, hasSubscription, requiredCredits, onShowPaywall]);
|
|
83
49
|
|
|
84
50
|
useEffect(() => {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
51
|
+
if (shouldExecutePurchaseAction(
|
|
52
|
+
isWaitingForPurchaseRef.current,
|
|
53
|
+
creditBalance,
|
|
54
|
+
prevCreditBalanceRef.current ?? 0,
|
|
55
|
+
hasSubscription,
|
|
56
|
+
hasSubscriptionRef.current,
|
|
57
|
+
!!pendingActionRef.current
|
|
58
|
+
)) {
|
|
59
|
+
const action = pendingActionRef.current!;
|
|
91
60
|
pendingActionRef.current = null;
|
|
92
61
|
isWaitingForPurchaseRef.current = false;
|
|
93
62
|
action();
|
|
@@ -99,45 +68,28 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
|
|
|
99
68
|
|
|
100
69
|
const requireFeature = useCallback(
|
|
101
70
|
(action: () => void | Promise<void>) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const currentRequiredCredits = requiredCreditsRef.current;
|
|
115
|
-
|
|
116
|
-
if (currentHasSubscription) {
|
|
117
|
-
action();
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (currentBalance < currentRequiredCredits) {
|
|
122
|
-
pendingActionRef.current = action;
|
|
123
|
-
isWaitingForPurchaseRef.current = true;
|
|
124
|
-
onShowPaywallRef.current(currentRequiredCredits);
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
action();
|
|
71
|
+
executeFeatureAction(
|
|
72
|
+
action,
|
|
73
|
+
isAuthenticated,
|
|
74
|
+
onShowAuthModal,
|
|
75
|
+
hasSubscriptionRef,
|
|
76
|
+
creditBalanceRef,
|
|
77
|
+
requiredCreditsRef,
|
|
78
|
+
onShowPaywallRef,
|
|
79
|
+
pendingActionRef,
|
|
80
|
+
isWaitingForAuthCreditsRef,
|
|
81
|
+
isWaitingForPurchaseRef
|
|
82
|
+
);
|
|
129
83
|
},
|
|
130
|
-
[isAuthenticated, onShowAuthModal]
|
|
84
|
+
[isAuthenticated, onShowAuthModal, hasSubscriptionRef, creditBalanceRef, requiredCreditsRef, onShowPaywallRef]
|
|
131
85
|
);
|
|
132
86
|
|
|
133
|
-
const hasCredits = creditBalance >= requiredCredits;
|
|
134
|
-
|
|
135
87
|
return {
|
|
136
88
|
requireFeature,
|
|
137
89
|
isAuthenticated,
|
|
138
90
|
hasSubscription,
|
|
139
|
-
hasCredits,
|
|
91
|
+
hasCredits: creditBalance >= requiredCredits,
|
|
140
92
|
creditBalance,
|
|
141
|
-
canAccess: isAuthenticated && (hasSubscription ||
|
|
93
|
+
canAccess: isAuthenticated && (hasSubscription || creditBalance >= requiredCredits),
|
|
142
94
|
};
|
|
143
95
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface UseFeatureGateParams {
|
|
2
|
+
readonly isAuthenticated: boolean;
|
|
3
|
+
readonly onShowAuthModal: (pendingCallback: () => void | Promise<void>) => void;
|
|
4
|
+
readonly hasSubscription?: boolean;
|
|
5
|
+
readonly creditBalance: number;
|
|
6
|
+
readonly requiredCredits?: number;
|
|
7
|
+
readonly onShowPaywall: (requiredCredits?: number) => void;
|
|
8
|
+
readonly isCreditsLoaded?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface UseFeatureGateResult {
|
|
12
|
+
readonly requireFeature: (action: () => void | Promise<void>) => void;
|
|
13
|
+
readonly isAuthenticated: boolean;
|
|
14
|
+
readonly hasSubscription: boolean;
|
|
15
|
+
readonly hasCredits: boolean;
|
|
16
|
+
readonly creditBalance: number;
|
|
17
|
+
readonly canAccess: boolean;
|
|
18
|
+
}
|