@umituz/react-native-subscription 2.27.88 → 2.27.89
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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-subscription",
|
|
3
|
-
"version": "2.27.
|
|
3
|
+
"version": "2.27.89",
|
|
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",
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
getCreditsConfig,
|
|
16
16
|
isCreditsRepositoryConfigured,
|
|
17
17
|
} from "../infrastructure/CreditsRepositoryProvider";
|
|
18
|
+
import { calculateCreditPercentage, canAffordCost } from "../utils/creditCalculations";
|
|
18
19
|
|
|
19
20
|
export const creditsQueryKeys = {
|
|
20
21
|
all: ["credits"] as const,
|
|
@@ -49,14 +50,8 @@ export const useCredits = (): UseCreditsResult => {
|
|
|
49
50
|
const userId = useAuthStore(selectUserId);
|
|
50
51
|
const isConfigured = isCreditsRepositoryConfigured();
|
|
51
52
|
|
|
52
|
-
//
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
config = isConfigured ? getCreditsConfig() : { creditLimit: 200 }; // Default fallback
|
|
56
|
-
} catch (e) {
|
|
57
|
-
config = { creditLimit: 200 };
|
|
58
|
-
}
|
|
59
|
-
|
|
53
|
+
// Only access config if configured to avoid throwing errors
|
|
54
|
+
const config = isConfigured ? getCreditsConfig() : null;
|
|
60
55
|
const queryEnabled = !!userId && isConfigured;
|
|
61
56
|
|
|
62
57
|
const { data, status, error, refetch } = useQuery({
|
|
@@ -119,12 +114,13 @@ export const useCredits = (): UseCreditsResult => {
|
|
|
119
114
|
|
|
120
115
|
const derivedValues = useMemo(() => {
|
|
121
116
|
const has = (credits?.credits ?? 0) > 0;
|
|
122
|
-
const
|
|
117
|
+
const limit = config?.creditLimit ?? 0;
|
|
118
|
+
const percent = calculateCreditPercentage(credits?.credits, limit);
|
|
123
119
|
return { hasCredits: has, creditsPercent: percent };
|
|
124
|
-
}, [credits, config
|
|
120
|
+
}, [credits, config?.creditLimit]);
|
|
125
121
|
|
|
126
122
|
const canAfford = useCallback(
|
|
127
|
-
(cost: number): boolean => (credits?.credits
|
|
123
|
+
(cost: number): boolean => canAffordCost(credits?.credits, cost),
|
|
128
124
|
[credits]
|
|
129
125
|
);
|
|
130
126
|
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credit Calculation Utilities
|
|
3
|
+
* Centralized logic for credit mathematical operations
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const calculateCreditPercentage = (
|
|
7
|
+
currentCredits: number | null | undefined,
|
|
8
|
+
creditLimit: number
|
|
9
|
+
): number => {
|
|
10
|
+
if (currentCredits === null || currentCredits === undefined || creditLimit <= 0) {
|
|
11
|
+
return 0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const percent = Math.round((currentCredits / creditLimit) * 100);
|
|
15
|
+
return Math.min(Math.max(percent, 0), 100); // Clamp between 0-100
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const canAffordCost = (
|
|
19
|
+
currentCredits: number | null | undefined,
|
|
20
|
+
cost: number
|
|
21
|
+
): boolean => {
|
|
22
|
+
if (currentCredits === null || currentCredits === undefined) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return currentCredits >= cost;
|
|
26
|
+
};
|