@umituz/react-native-subscription 2.27.87 → 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.87",
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,
@@ -48,7 +49,9 @@ function deriveLoadStatus(
48
49
  export const useCredits = (): UseCreditsResult => {
49
50
  const userId = useAuthStore(selectUserId);
50
51
  const isConfigured = isCreditsRepositoryConfigured();
51
- const config = getCreditsConfig();
52
+
53
+ // Only access config if configured to avoid throwing errors
54
+ const config = isConfigured ? getCreditsConfig() : null;
52
55
  const queryEnabled = !!userId && isConfigured;
53
56
 
54
57
  const { data, status, error, refetch } = useQuery({
@@ -111,12 +114,13 @@ export const useCredits = (): UseCreditsResult => {
111
114
 
112
115
  const derivedValues = useMemo(() => {
113
116
  const has = (credits?.credits ?? 0) > 0;
114
- const percent = credits ? Math.round((credits.credits / config.creditLimit) * 100) : 0;
117
+ const limit = config?.creditLimit ?? 0;
118
+ const percent = calculateCreditPercentage(credits?.credits, limit);
115
119
  return { hasCredits: has, creditsPercent: percent };
116
- }, [credits, config.creditLimit]);
120
+ }, [credits, config?.creditLimit]);
117
121
 
118
122
  const canAfford = useCallback(
119
- (cost: number): boolean => (credits?.credits ?? 0) >= cost,
123
+ (cost: number): boolean => canAffordCost(credits?.credits, cost),
120
124
  [credits]
121
125
  );
122
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
+ };