@umituz/react-native-subscription 2.14.38 → 2.14.40

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.14.38",
3
+ "version": "2.14.40",
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",
@@ -6,6 +6,7 @@
6
6
  */
7
7
 
8
8
  import { useQuery } from "@tanstack/react-query";
9
+ import { useCallback, useMemo } from "react";
9
10
  import type { UserCredits, CreditType } from "../../domain/entities/Credits";
10
11
 
11
12
  declare const __DEV__: boolean;
@@ -76,44 +77,45 @@ export const useCredits = ({
76
77
  });
77
78
 
78
79
  const credits = data ?? null;
79
- const hasTextCredits = (credits?.textCredits ?? 0) > 0;
80
- const hasImageCredits = (credits?.imageCredits ?? 0) > 0;
81
-
82
- if (__DEV__) {
83
- console.log("[useCredits] State", {
84
- userId,
85
- enabled,
86
- isLoading,
87
- imageCredits: credits?.imageCredits ?? 0,
88
- textCredits: credits?.textCredits ?? 0,
89
- hasImageCredits,
90
- hasTextCredits,
91
- });
92
- }
93
-
94
- const textCreditsPercent = credits
95
- ? Math.round((credits.textCredits / config.textCreditLimit) * 100)
96
- : 0;
97
-
98
- const imageCreditsPercent = credits
99
- ? Math.round((credits.imageCredits / config.imageCreditLimit) * 100)
100
- : 0;
101
-
102
- const canAfford = (cost: number, type: CreditType = "text"): boolean => {
103
- if (!credits) return false;
104
- return type === "text"
105
- ? credits.textCredits >= cost
106
- : credits.imageCredits >= cost;
107
- };
80
+
81
+ // Memoize derived values to prevent unnecessary re-renders
82
+ const derivedValues = useMemo(() => {
83
+ const hasText = (credits?.textCredits ?? 0) > 0;
84
+ const hasImage = (credits?.imageCredits ?? 0) > 0;
85
+ const textPercent = credits
86
+ ? Math.round((credits.textCredits / config.textCreditLimit) * 100)
87
+ : 0;
88
+ const imagePercent = credits
89
+ ? Math.round((credits.imageCredits / config.imageCreditLimit) * 100)
90
+ : 0;
91
+
92
+ return {
93
+ hasTextCredits: hasText,
94
+ hasImageCredits: hasImage,
95
+ textCreditsPercent: textPercent,
96
+ imageCreditsPercent: imagePercent,
97
+ };
98
+ }, [credits, config.textCreditLimit, config.imageCreditLimit]);
99
+
100
+ // Memoize canAfford to prevent recreation on every render
101
+ const canAfford = useCallback(
102
+ (cost: number, type: CreditType = "text"): boolean => {
103
+ if (!credits) return false;
104
+ return type === "text"
105
+ ? credits.textCredits >= cost
106
+ : credits.imageCredits >= cost;
107
+ },
108
+ [credits]
109
+ );
108
110
 
109
111
  return {
110
112
  credits,
111
113
  isLoading,
112
114
  error: error as Error | null,
113
- hasTextCredits,
114
- hasImageCredits,
115
- textCreditsPercent,
116
- imageCreditsPercent,
115
+ hasTextCredits: derivedValues.hasTextCredits,
116
+ hasImageCredits: derivedValues.hasImageCredits,
117
+ textCreditsPercent: derivedValues.textCreditsPercent,
118
+ imageCreditsPercent: derivedValues.imageCreditsPercent,
117
119
  refetch,
118
120
  canAfford,
119
121
  };