@umituz/react-native-subscription 2.14.39 → 2.14.41
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.
|
|
3
|
+
"version": "2.14.41",
|
|
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;
|
|
@@ -20,17 +21,22 @@ export const creditsQueryKeys = {
|
|
|
20
21
|
user: (userId: string) => ["credits", userId] as const,
|
|
21
22
|
};
|
|
22
23
|
|
|
24
|
+
/** Default stale time: 30 seconds - prevents infinite re-render loops */
|
|
25
|
+
const DEFAULT_STALE_TIME = 30 * 1000;
|
|
26
|
+
/** Default gc time: 5 minutes */
|
|
27
|
+
const DEFAULT_GC_TIME = 5 * 60 * 1000;
|
|
28
|
+
|
|
23
29
|
export interface CreditsCacheConfig {
|
|
24
|
-
/** Time in ms before data is considered stale. Default:
|
|
30
|
+
/** Time in ms before data is considered stale. Default: 30 seconds */
|
|
25
31
|
staleTime?: number;
|
|
26
|
-
/** Time in ms before inactive data is garbage collected. Default:
|
|
32
|
+
/** Time in ms before inactive data is garbage collected. Default: 5 minutes */
|
|
27
33
|
gcTime?: number;
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
export interface UseCreditsParams {
|
|
31
37
|
userId: string | undefined;
|
|
32
38
|
enabled?: boolean;
|
|
33
|
-
/** Cache configuration. Default:
|
|
39
|
+
/** Cache configuration. Default: 30 second staleTime, 5 minute gcTime */
|
|
34
40
|
cache?: CreditsCacheConfig;
|
|
35
41
|
}
|
|
36
42
|
|
|
@@ -55,9 +61,9 @@ export const useCredits = ({
|
|
|
55
61
|
const isConfigured = isCreditsRepositoryConfigured();
|
|
56
62
|
const config = getCreditsConfig();
|
|
57
63
|
|
|
58
|
-
// Default:
|
|
59
|
-
const staleTime = cache?.staleTime ??
|
|
60
|
-
const gcTime = cache?.gcTime ??
|
|
64
|
+
// Default: 30 second stale time to prevent infinite re-render loops
|
|
65
|
+
const staleTime = cache?.staleTime ?? DEFAULT_STALE_TIME;
|
|
66
|
+
const gcTime = cache?.gcTime ?? DEFAULT_GC_TIME;
|
|
61
67
|
|
|
62
68
|
const { data, isLoading, error, refetch } = useQuery({
|
|
63
69
|
queryKey: creditsQueryKeys.user(userId ?? ""),
|
|
@@ -76,44 +82,45 @@ export const useCredits = ({
|
|
|
76
82
|
});
|
|
77
83
|
|
|
78
84
|
const credits = data ?? null;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
85
|
+
|
|
86
|
+
// Memoize derived values to prevent unnecessary re-renders
|
|
87
|
+
const derivedValues = useMemo(() => {
|
|
88
|
+
const hasText = (credits?.textCredits ?? 0) > 0;
|
|
89
|
+
const hasImage = (credits?.imageCredits ?? 0) > 0;
|
|
90
|
+
const textPercent = credits
|
|
91
|
+
? Math.round((credits.textCredits / config.textCreditLimit) * 100)
|
|
92
|
+
: 0;
|
|
93
|
+
const imagePercent = credits
|
|
94
|
+
? Math.round((credits.imageCredits / config.imageCreditLimit) * 100)
|
|
95
|
+
: 0;
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
hasTextCredits: hasText,
|
|
99
|
+
hasImageCredits: hasImage,
|
|
100
|
+
textCreditsPercent: textPercent,
|
|
101
|
+
imageCreditsPercent: imagePercent,
|
|
102
|
+
};
|
|
103
|
+
}, [credits, config.textCreditLimit, config.imageCreditLimit]);
|
|
104
|
+
|
|
105
|
+
// Memoize canAfford to prevent recreation on every render
|
|
106
|
+
const canAfford = useCallback(
|
|
107
|
+
(cost: number, type: CreditType = "text"): boolean => {
|
|
108
|
+
if (!credits) return false;
|
|
109
|
+
return type === "text"
|
|
110
|
+
? credits.textCredits >= cost
|
|
111
|
+
: credits.imageCredits >= cost;
|
|
112
|
+
},
|
|
113
|
+
[credits]
|
|
114
|
+
);
|
|
108
115
|
|
|
109
116
|
return {
|
|
110
117
|
credits,
|
|
111
118
|
isLoading,
|
|
112
119
|
error: error as Error | null,
|
|
113
|
-
hasTextCredits,
|
|
114
|
-
hasImageCredits,
|
|
115
|
-
textCreditsPercent,
|
|
116
|
-
imageCreditsPercent,
|
|
120
|
+
hasTextCredits: derivedValues.hasTextCredits,
|
|
121
|
+
hasImageCredits: derivedValues.hasImageCredits,
|
|
122
|
+
textCreditsPercent: derivedValues.textCreditsPercent,
|
|
123
|
+
imageCreditsPercent: derivedValues.imageCreditsPercent,
|
|
117
124
|
refetch,
|
|
118
125
|
canAfford,
|
|
119
126
|
};
|
|
@@ -95,6 +95,6 @@ export const useSubscriptionPackages = (userId: string | undefined) => {
|
|
|
95
95
|
staleTime: STALE_TIME,
|
|
96
96
|
gcTime: GC_TIME,
|
|
97
97
|
enabled: isConfigured, // Only enabled when SubscriptionManager is configured
|
|
98
|
-
refetchOnMount:
|
|
98
|
+
refetchOnMount: true, // Respects staleTime - refetches only if data is stale
|
|
99
99
|
});
|
|
100
100
|
};
|