@umituz/react-native-subscription 2.27.91 → 2.27.93

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.
Files changed (75) hide show
  1. package/package.json +1 -1
  2. package/src/domains/credits/application/CreditsInitializer.ts +91 -38
  3. package/src/domains/credits/core/UserCreditsDocument.ts +33 -33
  4. package/src/domains/credits/infrastructure/CreditsRepository.ts +44 -58
  5. package/src/domains/paywall/components/PaywallModal.tsx +1 -1
  6. package/src/domains/subscription/application/SubscriptionInitializer.ts +59 -18
  7. package/src/domains/subscription/core/RevenueCatTypes.ts +2 -1
  8. package/src/domains/subscription/core/SubscriptionConstants.ts +12 -0
  9. package/src/domains/subscription/infrastructure/handlers/PackageHandler.ts +46 -27
  10. package/src/domains/subscription/infrastructure/managers/SubscriptionManager.ts +106 -42
  11. package/src/domains/subscription/infrastructure/services/RestoreHandler.ts +4 -2
  12. package/src/domains/subscription/infrastructure/services/RevenueCatInitializer.ts +1 -2
  13. package/src/domains/subscription/infrastructure/utils/RenewalDetector.ts +1 -1
  14. package/src/{presentation → domains/subscription/presentation}/components/details/PremiumStatusBadge.tsx +6 -4
  15. package/src/{presentation → domains/subscription/presentation}/components/feedback/PaywallFeedbackModal.tsx +1 -1
  16. package/src/{presentation → domains/subscription/presentation}/types/SubscriptionDetailTypes.ts +4 -2
  17. package/src/{presentation → domains/subscription/presentation}/types/SubscriptionSettingsTypes.ts +1 -1
  18. package/src/domains/subscription/presentation/usePremiumGate.ts +1 -1
  19. package/src/domains/subscription/presentation/useSavedPurchaseAutoExecution.ts +1 -1
  20. package/src/domains/subscription/presentation/useSubscriptionSettingsConfig.ts +4 -3
  21. package/src/domains/subscription/presentation/useSubscriptionSettingsConfig.utils.ts +1 -1
  22. package/src/domains/trial/application/TrialEligibilityService.ts +1 -1
  23. package/src/domains/trial/infrastructure/DeviceTrialRepository.ts +2 -2
  24. package/src/index.ts +5 -5
  25. package/src/shared/application/ports/IRevenueCatService.ts +2 -0
  26. package/src/shared/infrastructure/SubscriptionEventBus.ts +5 -2
  27. package/src/utils/tierUtils.ts +3 -3
  28. package/src/utils/types.ts +4 -1
  29. package/src/utils/validation.ts +2 -2
  30. package/src/domains/config/README.md +0 -100
  31. package/src/presentation/README.md +0 -125
  32. package/src/presentation/hooks/README.md +0 -156
  33. package/src/presentation/hooks/useAuthSubscriptionSync.md +0 -94
  34. package/src/presentation/hooks/useCredits.md +0 -103
  35. package/src/presentation/hooks/useDeductCredit.md +0 -100
  36. package/src/presentation/hooks/useFeatureGate.md +0 -112
  37. package/src/presentation/hooks/usePaywall.md +0 -89
  38. package/src/presentation/hooks/usePaywallOperations.md +0 -92
  39. package/src/presentation/hooks/usePaywallVisibility.md +0 -95
  40. package/src/presentation/hooks/usePremium.md +0 -88
  41. package/src/presentation/hooks/useSubscriptionSettingsConfig.md +0 -94
  42. package/src/utils/README.md +0 -42
  43. /package/src/{presentation → domains/subscription/presentation}/components/README.md +0 -0
  44. /package/src/{presentation → domains/subscription/presentation}/components/details/CreditRow.md +0 -0
  45. /package/src/{presentation → domains/subscription/presentation}/components/details/CreditRow.tsx +0 -0
  46. /package/src/{presentation → domains/subscription/presentation}/components/details/DetailRow.md +0 -0
  47. /package/src/{presentation → domains/subscription/presentation}/components/details/DetailRow.tsx +0 -0
  48. /package/src/{presentation → domains/subscription/presentation}/components/details/PremiumDetailsCard.md +0 -0
  49. /package/src/{presentation → domains/subscription/presentation}/components/details/PremiumDetailsCard.styles.ts +0 -0
  50. /package/src/{presentation → domains/subscription/presentation}/components/details/PremiumDetailsCard.tsx +0 -0
  51. /package/src/{presentation → domains/subscription/presentation}/components/details/PremiumDetailsCardTypes.ts +0 -0
  52. /package/src/{presentation → domains/subscription/presentation}/components/details/PremiumStatusBadge.md +0 -0
  53. /package/src/{presentation → domains/subscription/presentation}/components/details/README.md +0 -0
  54. /package/src/{presentation → domains/subscription/presentation}/components/feedback/PaywallFeedbackModal.md +0 -0
  55. /package/src/{presentation → domains/subscription/presentation}/components/feedback/README.md +0 -0
  56. /package/src/{presentation → domains/subscription/presentation}/components/feedback/paywallFeedbackStyles.ts +0 -0
  57. /package/src/{presentation → domains/subscription/presentation}/components/overlay/PurchaseLoadingOverlay.tsx +0 -0
  58. /package/src/{presentation → domains/subscription/presentation}/components/overlay/index.ts +0 -0
  59. /package/src/{presentation → domains/subscription/presentation}/components/paywall/PaywallModal.md +0 -0
  60. /package/src/{presentation → domains/subscription/presentation}/components/paywall/README.md +0 -0
  61. /package/src/{presentation → domains/subscription/presentation}/components/sections/README.md +0 -0
  62. /package/src/{presentation → domains/subscription/presentation}/components/sections/SubscriptionSection.md +0 -0
  63. /package/src/{presentation → domains/subscription/presentation}/components/sections/SubscriptionSection.tsx +0 -0
  64. /package/src/{presentation → domains/subscription/presentation}/screens/README.md +0 -0
  65. /package/src/{presentation → domains/subscription/presentation}/screens/SubscriptionDetailScreen.tsx +0 -0
  66. /package/src/{presentation → domains/subscription/presentation}/screens/components/CreditsList.tsx +0 -0
  67. /package/src/{presentation → domains/subscription/presentation}/screens/components/DevTestSection.tsx +0 -0
  68. /package/src/{presentation → domains/subscription/presentation}/screens/components/SubscriptionActions.tsx +0 -0
  69. /package/src/{presentation → domains/subscription/presentation}/screens/components/SubscriptionHeader.tsx +0 -0
  70. /package/src/{presentation → domains/subscription/presentation}/screens/components/UpgradePrompt.tsx +0 -0
  71. /package/src/{presentation → domains/subscription/presentation}/stores/index.ts +0 -0
  72. /package/src/{presentation → domains/subscription/presentation}/stores/purchaseLoadingStore.ts +0 -0
  73. /package/src/{presentation → domains/subscription/presentation}/types/README.md +0 -0
  74. /package/src/{presentation → domains/subscription/presentation}/utils/README.md +0 -0
  75. /package/src/{presentation → domains/subscription/presentation}/utils/subscriptionDateUtils.ts +0 -0
@@ -1,125 +0,0 @@
1
- # Presentation Layer
2
-
3
- UI/UX layer for the subscription system - React hooks, components, and screens.
4
-
5
- ## Location
6
-
7
- **Directory**: `src/presentation/`
8
-
9
- **Type**: Layer
10
-
11
- ## Strategy
12
-
13
- ### Layer Responsibilities
14
-
15
- The Presentation Layer is responsible for:
16
-
17
- 1. **State Management**
18
- - React hooks for data fetching and mutations
19
- - TanStack Query for server state management
20
- - Local state management for UI state
21
-
22
- 2. **UI Components**
23
- - Reusable subscription components
24
- - Feature gating UI elements
25
- - Credit display components
26
- - Paywall components
27
-
28
- 3. **User Interaction**
29
- - Handle user actions
30
- - Display appropriate feedback
31
- - Guide users through purchase flows
32
- - Show upgrade prompts at right time
33
-
34
- ### Architecture Pattern
35
-
36
- The presentation layer follows a layered architecture where:
37
- - Hooks manage state and data fetching at the top level
38
- - Components consume hooks and render UI
39
- - Screens compose multiple components together
40
- - All layers communicate with the domain layer for business logic
41
-
42
- ### Integration Points
43
-
44
- - **Domain Layer**: Business logic and data access
45
- - **TanStack Query**: Server state management
46
- - **RevenueCat**: Purchase operations
47
- - **Navigation**: Screen routing
48
-
49
- ## Restrictions
50
-
51
- ### REQUIRED
52
-
53
- - **Type Safety**: All components MUST be typed with TypeScript
54
- - **Error Boundaries**: MUST implement error boundaries for all screens
55
- - **Loading States**: MUST show loading indicators during async operations
56
- - **User Feedback**: MUST provide feedback for all user actions
57
-
58
- ### PROHIBITED
59
-
60
- - **NEVER** include business logic in components (use hooks instead)
61
- - **NEVER** make direct API calls from components (use hooks)
62
- - **DO NOT** store sensitive data in component state
63
- - **NEVER** hardcode strings (use localization)
64
-
65
- ### CRITICAL SAFETY
66
-
67
- - **ALWAYS** validate props before rendering
68
- - **ALWAYS** handle loading and error states
69
- - **NEVER** trust client-side state for security decisions
70
- - **MUST** implement proper error boundaries
71
- - **ALWAYS** sanitize user inputs before display
72
-
73
- ## AI Agent Guidelines
74
-
75
- ### When Building Presentation Layer
76
-
77
- 1. **Always** use hooks for data fetching and state management
78
- 2. **Always** handle loading and error states
79
- 3. **Always** provide user feedback for actions
80
- 4. **Always** implement error boundaries
81
- 5. **Never** include business logic in components
82
-
83
- ### Integration Checklist
84
-
85
- - [ ] Use appropriate hooks for data access
86
- - [ ] Handle loading states
87
- - [ ] Handle error states
88
- - [ ] Implement error boundaries
89
- - [ ] Provide user feedback
90
- - [ ] Test with various data states
91
- - [ ] Test error scenarios
92
- - [ ] Ensure type safety
93
- - [ ] Use localization for all strings
94
- - [ ] Test accessibility
95
-
96
- ### Common Patterns
97
-
98
- 1. **Compound Components**: Build complex UIs from simple components
99
- 2. **Render Props**: Share stateful logic between components
100
- 3. **Custom Hooks**: Extract reusable stateful logic
101
- 4. **Error Boundaries**: Prevent crashes from propagating
102
- 5. **Loading Skeletons**: Show placeholder during loading
103
- 6. **Optimistic Updates**: Update UI immediately, rollback on failure
104
- 7. **Graceful Degradation**: Show limited version on error
105
- 8. **Responsive Design**: Support different screen sizes
106
-
107
- ## Related Documentation
108
-
109
- - **Hooks**: `hooks/README.md`
110
- - **Components**: `components/README.md`
111
- - **Screens**: `screens/README.md`
112
- - **Wallet Domain**: `../../domains/wallet/README.md`
113
- - **Paywall Domain**: `../../domains/paywall/README.md`
114
- - **RevenueCat**: `../../revenuecat/README.md`
115
-
116
- ## Directory Structure
117
-
118
- The presentation layer contains:
119
- - **hooks/** - React hooks for state management (usePremium, useSubscription, useCredits, useDeductCredit, useFeatureGate)
120
- - **components/** - UI components organized by functionality
121
- - **details/** - Detail cards, badges
122
- - **feedback/** - Modals, feedback components
123
- - **sections/** - Section components
124
- - **paywall/** - Paywall components
125
- - **screens/** - Full-screen components (SubscriptionDetailScreen)
@@ -1,156 +0,0 @@
1
- # Subscription Hooks
2
-
3
- Collection of React hooks for subscription, premium, and credits management.
4
-
5
- ## Location
6
-
7
- **Import Path**: `@umituz/react-native-subscription`
8
-
9
- **Directory**: `src/presentation/hooks/`
10
-
11
- **Type**: Hooks Collection
12
-
13
- ## Strategy
14
-
15
- ### Hook Organization
16
-
17
- Hooks are organized by functionality:
18
-
19
- 1. **Subscription Hooks**: Manage subscription status and details
20
- 2. **Premium Hooks**: Check and manage premium access
21
- 3. **Credits Hooks**: Handle credit balance and operations
22
- 4. **Gate Hooks**: Control feature access based on user state
23
- 5. **Auth Hooks**: Authentication-aware purchase flows
24
- 6. **Paywall Hooks**: Paywall display and management
25
-
26
- ### Integration Pattern
27
-
28
- All hooks follow consistent patterns:
29
- - Return loading, error, and data states
30
- - Provide refetch/refresh functions
31
- - Use TanStack Query for caching
32
- - Handle optimistic updates
33
- - Support real-time updates
34
-
35
- ## Hook Categories
36
-
37
- ### Subscription Hooks
38
-
39
- - **useSubscription**: Core subscription status management
40
- - **useSubscriptionStatus**: Detailed subscription status
41
- - **useSubscriptionDetails**: Subscription package and feature details
42
- - **useSubscriptionSettingsConfig**: Settings and configuration
43
-
44
- ### Premium Hooks
45
-
46
- - **usePremium**: Premium status checker
47
- - **usePremiumGate**: Premium feature gating
48
- - **usePremiumWithCredits**: Hybrid premium/credits access
49
-
50
- ### Credits Hooks
51
-
52
- - **useCredits**: Credit balance and transactions
53
- - **useCreditChecker**: Simple credit validation
54
- - **useDeductCredit**: Credit deduction with optimistic updates
55
- - **useInitializeCredits**: Initialize credits after purchase
56
-
57
- ### Gate Hooks
58
-
59
- - **useFeatureGate**: Unified auth, subscription, and credits gating
60
- - **useSubscriptionGate**: Subscription-only gating
61
- - **useCreditsGate**: Credits-only gating
62
- - **usePremiumGate**: Premium-only gating
63
- - **useAuthGate**: Authentication gating
64
-
65
- ### Auth Hooks
66
-
67
- - **useAuthAwarePurchase**: Authentication-aware purchase flow
68
- - **useAuthSubscriptionSync**: Auth and subscription synchronization
69
-
70
- ### Paywall Hooks
71
-
72
- - **usePaywall**: Paywall display management
73
- - **usePaywallOperations**: Paywall show/hide operations
74
- - **usePaywallVisibility**: Paywall visibility rules
75
-
76
- ### User Tier Hooks
77
-
78
- - **useUserTier**: User tier management
79
- - **useUserTierWithRepository**: Repository-based tier management
80
-
81
- ### Development Hooks
82
-
83
- - **useDevTestCallbacks**: Development and testing utilities
84
-
85
- ## Restrictions
86
-
87
- ### REQUIRED
88
-
89
- - **Import Path**: MUST import from `@umituz/react-native-subscription`
90
- - **Loading States**: MUST handle loading state in UI
91
- - **Error Handling**: MUST handle error state
92
- - **User Authentication**: Most hooks require authenticated user
93
-
94
- ### PROHIBITED
95
-
96
- - **NEVER** use hooks outside React components
97
- - **NEVER** ignore loading states
98
- - **NEVER** skip error handling
99
- - **DO NOT** use hook returns for security decisions (server-side validation required)
100
-
101
- ### CRITICAL SAFETY
102
-
103
- - **ALWAYS** handle loading and error states
104
- - **NEVER** trust client-side state for security
105
- - **MUST** implement error boundaries
106
- - **ALWAYS** test with various user states (anonymous, free, premium)
107
-
108
- ## AI Agent Guidelines
109
-
110
- ### When Using Subscription Hooks
111
-
112
- 1. **Always** handle loading and error states
113
- 2. **Always** use appropriate gate hooks for feature access
114
- 3. **Always** provide user feedback for all operations
115
- 4. **Never** trust client-side state for security
116
- 5. **Always** test with different user tiers
117
-
118
- ### Integration Checklist
119
-
120
- - [ ] Import from correct path: `@umituz/react-native-subscription`
121
- - [ ] Choose appropriate hook for use case
122
- - [ ] Handle loading state
123
- - [ ] Handle error state
124
- - [ ] Implement error boundaries
125
- - [ ] Use gate hooks for feature access
126
- - [ ] Test with all user tiers
127
- - [ ] Test offline scenarios
128
- - [ ] Test error scenarios
129
-
130
- ### Common Patterns
131
-
132
- 1. **Feature Gating**: Use `useFeatureGate` for unified access control
133
- 2. **Credit Operations**: Check → Deduct → Execute pattern
134
- 3. **Subscription Display**: Show status, expiration, renewal info
135
- 4. **Premium Features**: Gate content, provide upgrade path
136
- 5. **Paywall Integration**: Automatic display on access denial
137
- 6. **Real-time Updates**: Refresh on focus, background sync
138
-
139
- ## Related Documentation
140
-
141
- - **Subscription Domain**: `src/domains/wallet/README.md`
142
- - **Paywall Domain**: `src/domains/paywall/README.md`
143
- - **Config Domain**: `src/domains/config/README.md`
144
- - **RevenueCat Integration**: `src/revenuecat/README.md`
145
- - **Presentation Components**: `src/presentation/components/README.md`
146
-
147
- ## Individual Hook Documentation
148
-
149
- - [useDeductCredit](./useDeductCredit.md) - Credit deduction with optimistic updates
150
- - [useCredits](./useCredits.md) - Credit balance access
151
- - [useCreditChecker](./useCreditChecker.md) - Simple credit validation
152
- - [useFeatureGate](./useFeatureGate.md) - Unified feature gating
153
- - [useSubscription](./useSubscription.md) - Subscription status management
154
- - [usePremium](./usePremium.md) - Premium status checker
155
- - [useCreditsGate](./useCreditsGate.md) - Credits-based feature gating
156
- - [useInitializeCredits](./useInitializeCredits.md) - Initialize credits after purchase
@@ -1,94 +0,0 @@
1
- # useAuthSubscriptionSync Hook
2
-
3
- Automatically synchronizes subscription state with authentication changes.
4
-
5
- ## Location
6
-
7
- **Import Path**: `@umituz/react-native-subscription`
8
-
9
- **File**: `src/presentation/hooks/useAuthSubscriptionSync.ts`
10
-
11
- **Type**: Hook
12
-
13
- ## Strategy
14
-
15
- ### Auth-Subscription Synchronization
16
-
17
- 1. **Auth Listener Setup**: Subscribe to auth state changes via provided callback
18
- 2. **User Change Detection**: Detect when userId changes (including sign out)
19
- 3. **Subscription Initialization**: Initialize subscription when user signs in
20
- 4. **One-Time Init**: Only initialize once per user session (skips duplicates)
21
- 5. **User Switching**: Re-initialize when switching between accounts
22
- 6. **Cleanup**: Unsubscribe from auth listener on unmount
23
-
24
- ### Integration Points
25
-
26
- - **Auth Provider**: Any auth system (Firebase, Auth0, custom)
27
- - **RevenueCat**: For subscription initialization with logIn
28
- - **Subscription Service**: Backend subscription sync
29
- - **Credits System**: Optional credits initialization
30
- - **App Root**: Should be placed in root App component
31
-
32
- ## Restrictions
33
-
34
- ### REQUIRED
35
-
36
- - **Auth State Change Callback**: MUST provide onAuthStateChanged function
37
- - **Subscription Init**: MUST provide initializeSubscription function
38
- - **App Root Setup**: SHOULD be placed in root App component
39
- - **Unsubscribe Handling**: Callback MUST return unsubscribe function
40
-
41
- ### PROHIBITED
42
-
43
- - **NEVER** place in individual screen components (use app root)
44
- - **NEVER** call without proper auth callback
45
- - **DO NOT** manually call initializeSubscription (hook handles it)
46
- - **DO NOT** configure multiple times (one-time setup)
47
-
48
- ### CRITICAL SAFETY
49
-
50
- - **ALWAYS** configure at app root level
51
- - **MUST** handle user switching correctly
52
- - **ALWAYS** return unsubscribe function from callback
53
- - **NEVER** rely on manual subscription initialization
54
-
55
- ## AI Agent Guidelines
56
-
57
- ### When Implementing Auth-Subscription Sync
58
-
59
- 1. **Always** place in root App component
60
- 2. **Always** provide onAuthStateChanged that returns unsubscribe
61
- 3. **Always** provide initializeSubscription for user ID
62
- 4. **Always** test user switching scenarios
63
- 5. **Never** place in child components
64
-
65
- ### Integration Checklist
66
-
67
- - [ ] Import from correct path: `@umituz/react-native-subscription`
68
- - [ ] Place in root App component
69
- - [ ] Provide onAuthStateChanged callback
70
- - [ ] Ensure callback returns unsubscribe function
71
- - [ ] Provide initializeSubscription function
72
- - [ ] Test with initial user sign-in
73
- - [ ] Test with user sign-out
74
- - [ ] Test with account switching
75
- - [ ] Verify subscription initializes only once per user
76
- - [ ] Test cleanup on unmount
77
-
78
- ### Common Patterns
79
-
80
- 1. **Firebase + RevenueCat**: Sync Firebase auth with RevenueCat
81
- 2. **Custom Auth + Backend**: Use custom auth with backend sync
82
- 3. **Multi-Service Setup**: Initialize multiple services (RevenueCat, credits, backend)
83
- 4. **Error Handling**: Handle initialization failures gracefully
84
- 5. **Loading State**: Show loading during sync
85
- 6. **Analytics Tracking**: Track auth and subscription events
86
-
87
- ## Related Documentation
88
-
89
- - **useAuth**: For authentication state
90
- - **usePremium**: For subscription status
91
- - **useAuthAwarePurchase**: For auth-gated purchases
92
- - **useUserTier**: For tier determination
93
- - **Auth Integration Guide**: `src/docs/AUTH_INTEGRATION.md`
94
- - **RevenueCat Setup**: `src/docs/REVENUECAT_SETUP.md`
@@ -1,103 +0,0 @@
1
- # useCredits Hook
2
-
3
- Hook for accessing and managing credits balance with real-time updates.
4
-
5
- ## Location
6
-
7
- **Import Path**: `@umituz/react-native-subscription`
8
-
9
- **File**: `src/presentation/hooks/useCredits.ts`
10
-
11
- **Type**: Hook
12
-
13
- ## Strategy
14
-
15
- ### Data Fetching Flow
16
-
17
- 1. **Initial Load**
18
- - Fetch credits from repository on mount
19
- - Cache results in TanStack Query
20
- - Handle loading/error states
21
-
22
- 2. **Real-time Updates**
23
- - Subscribe to credit changes via repository listener
24
- - Auto-update on balance changes
25
- - Invalidate query on external modifications
26
-
27
- 3. **Cache Management**
28
- - Use TanStack Query for caching
29
- - Background refetch on window focus
30
- - Manual refetch capability
31
-
32
- ### Integration Points
33
-
34
- - **Credits Repository**: `src/domains/wallet/infrastructure/repositories/CreditsRepository.ts`
35
- - **Credits Entity**: `src/domains/wallet/domain/entities/UserCredits.ts`
36
- - **TanStack Query**: For cache management and background updates
37
- - **useFocusEffect**: For refresh on screen focus
38
-
39
- ## Restrictions
40
-
41
- ### REQUIRED
42
-
43
- - **User Authentication**: User MUST be authenticated to access credits
44
- - **Error Handling**: MUST handle error state in UI
45
- - **Loading State**: MUST show loading indicator while fetching
46
-
47
- ### PROHIBITED
48
-
49
- - **NEVER** assume credits are available without checking loading state
50
- - **NEVER** use this hook for unauthenticated users
51
- - **DO NOT** mutate credits directly - use `useDeductCredit` instead
52
- - **DO NOT** call refetch excessively (causes unnecessary network calls)
53
-
54
- ### CRITICAL SAFETY
55
-
56
- - **ALWAYS** check `isLoading` before displaying credits
57
- - **ALWAYS** handle `error` state to prevent crashes
58
- - **NEVER** use `credits` value for security decisions (validate on backend)
59
- - **MUST** implement error boundaries when displaying balance
60
-
61
- ## AI Agent Guidelines
62
-
63
- ### When Implementing Credit Displays
64
-
65
- 1. **Always** handle loading state explicitly
66
- 2. **Always** handle error state gracefully
67
- 3. **Always** provide manual refresh option
68
- 4. **Always** format balance safely (handle undefined/null)
69
- 5. **Never** use credits for security decisions (server-side validation required)
70
-
71
- ### Integration Checklist
72
-
73
- - [ ] Import from correct path: `@umituz/react-native-subscription`
74
- - [ ] Handle loading state in UI
75
- - [ ] Handle error state in UI
76
- - [ ] Provide refresh mechanism
77
- - [ ] Format balance safely with default values
78
- - [ ] Implement error boundaries
79
- - [ ] Test with no credits (zero balance)
80
- - [ ] Test with loading states
81
- - [ ] Test with error states
82
- - [ ] Test refresh functionality
83
-
84
- ### Common Patterns to Implement
85
-
86
- 1. **Balance Card**: Display credits with currency conversion
87
- 2. **Transaction List**: Show recent credit activity
88
- 3. **Low Credits Warning**: Alert when balance is low
89
- 4. **Refresh Control**: Pull-to-refresh or button
90
- 5. **Real-time Updates**: Use focus effect for auto-refresh
91
- 6. **Purchase Prompt**: Link to credit packages when low
92
- 7. **Skeletal Loading**: Show skeleton during initial load
93
- 8. **Error Recovery**: Retry mechanism for failed fetches
94
-
95
- ## Related Documentation
96
-
97
- - **useDeductCredit**: Deduct credits from balance
98
- - **useCreditChecker**: Check credit availability before operations
99
- - **useInitializeCredits**: Initialize credits after purchase
100
- - **useCreditsGate**: Gate features behind credit requirements
101
- - **useFeatureGate**: Unified feature gating with credits
102
- - **Credits Repository**: `src/domains/wallet/infrastructure/repositories/README.md`
103
- - **Wallet Domain**: `src/domains/wallet/README.md`
@@ -1,100 +0,0 @@
1
- # useDeductCredit Hook
2
-
3
- Hook for deducting credits from user balance with optimistic updates.
4
-
5
- ## Location
6
-
7
- **Import Path**: `@umituz/react-native-subscription`
8
-
9
- **File**: `src/presentation/hooks/useDeductCredit.ts`
10
-
11
- ## Strategy
12
-
13
- ### Credit Deduction Flow
14
-
15
- 1. **Pre-deduction Validation**
16
- - Verify user is authenticated (userId must be defined)
17
- - Check if sufficient credits exist
18
- - Validate deduction amount is positive
19
-
20
- 2. **Optimistic Update**
21
- - Immediately update UI with new balance
22
- - Store previous state for potential rollback
23
- - Update TanStack Query cache
24
-
25
- 3. **Server Synchronization**
26
- - Send deduction request to backend
27
- - Handle success/failure responses
28
- - Rollback on failure
29
-
30
- 4. **Post-deduction Handling**
31
- - Trigger `onCreditsExhausted` callback if balance reaches zero
32
- - Return success/failure boolean
33
- - Reset loading state
34
-
35
- ### Integration Points
36
-
37
- - **Credits Repository**: `src/domains/wallet/infrastructure/repositories/CreditsRepository.ts`
38
- - **Credits Entity**: `src/domains/wallet/domain/entities/UserCredits.ts`
39
- - **TanStack Query**: For cache management and optimistic updates
40
-
41
- ## Restrictions
42
-
43
- ### REQUIRED
44
-
45
- - **User Authentication**: `userId` parameter MUST be provided and cannot be undefined
46
- - **Positive Amount**: Credit cost MUST be greater than zero
47
- - **Callback Implementation**: `onCreditsExhausted` callback SHOULD be implemented to handle zero balance scenarios
48
-
49
- ### PROHIBITED
50
-
51
- - **NEVER** call `deductCredit` or `deductCredits` without checking `isDeducting` state first
52
- - **NEVER** allow multiple simultaneous deduction calls for the same user
53
- - **NEVER** deduce credits when balance is insufficient (should check with `useCreditChecker` first)
54
- - **DO NOT** use this hook for guest users (userId undefined)
55
-
56
- ### CRITICAL SAFETY
57
-
58
- - **ALWAYS** check return value before proceeding with feature execution
59
- - **ALWAYS** handle the case where deduction returns `false`
60
- - **NEVER** assume deduction succeeded without checking return value
61
- - **MUST** implement error boundaries when using this hook
62
-
63
- ## AI Agent Guidelines
64
-
65
- ### When Implementing Features
66
-
67
- 1. **Always** check if user has sufficient credits BEFORE allowing action
68
- 2. **Always** show the credit cost to user before deducting
69
- 3. **Always** disable buttons while `isDeducting` is true
70
- 4. **Always** handle the case where deduction returns false
71
- 5. **Never** allow zero or negative credit costs
72
-
73
- ### Integration Checklist
74
-
75
- - [ ] Import from correct path: `@umituz/react-native-subscription`
76
- - [ ] Provide valid `userId` from authentication context
77
- - [ ] Implement `onCreditsExhausted` callback
78
- - [ ] Check `isDeducting` before calling deduct functions
79
- - [ ] Validate return value before proceeding
80
- - [ ] Show credit cost to user before action
81
- - [ ] Handle error cases gracefully
82
- - [ ] Test with insufficient credits
83
- - [ ] Test with zero balance
84
-
85
- ### Common Patterns to Implement
86
-
87
- 1. **Pre-check**: Use `useCreditChecker` before showing feature button
88
- 2. **Confirmation Dialog**: Ask user before expensive operations (>5 credits)
89
- 3. **Success Feedback**: Show success message after deduction
90
- 4. **Failure Handling**: Show appropriate error message on failure
91
- 5. **Purchase Flow**: Navigate to purchase screen on exhaustion
92
-
93
- ## Related Documentation
94
-
95
- - **useCredits**: Access current credit balance
96
- - **useCreditChecker**: Check credit availability before operations
97
- - **useInitializeCredits**: Initialize credits after purchase
98
- - **useFeatureGate**: Unified feature gating with credits
99
- - **Credits Repository**: `src/domains/wallet/infrastructure/repositories/README.md`
100
- - **Wallet Domain**: `src/domains/wallet/README.md`
@@ -1,112 +0,0 @@
1
- # useFeatureGate Hook
2
-
3
- Unified feature gate combining authentication, subscription, and credits checks.
4
-
5
- ## Location
6
-
7
- **Import Path**: `@umituz/react-native-subscription`
8
-
9
- **File**: `src/presentation/hooks/useFeatureGate.ts`
10
-
11
- **Type**: Hook
12
-
13
- ## Strategy
14
-
15
- ### Progressive Access Control
16
-
17
- 1. **Authentication Check**
18
- - Verify user is authenticated
19
- - Show auth modal if not authenticated
20
- - Queue pending action for post-auth execution
21
-
22
- 2. **Subscription Check**
23
- - Verify user has required subscription tier
24
- - Show paywall if subscription insufficient
25
- - Bypass credit check for premium users
26
-
27
- 3. **Credit Check**
28
- - Verify sufficient credit balance
29
- - Show purchase prompt if insufficient
30
- - Deduct credits only after all checks pass
31
-
32
- 4. **Action Execution**
33
- - Execute gated action only if all checks pass
34
- - Handle failures gracefully
35
- - Update UI state appropriately
36
-
37
- ### Integration Points
38
-
39
- - **useAuth**: For authentication state
40
- - **usePremium**: For subscription status
41
- - **useCredits**: For credit balance
42
- - **useDeductCredit**: For credit deduction
43
- - **Auth Modals**: Custom authentication UI
44
- - **Paywall Components**: Upgrade prompts
45
-
46
- ## Restrictions
47
-
48
- ### REQUIRED
49
-
50
- - **Authentication Check**: MUST implement `onShowAuthModal` callback
51
- - **Paywall Handler**: MUST implement `onShowPaywall` callback
52
- - **Credit Validation**: MUST check `hasCredits` before allowing actions
53
- - **State Management**: MUST respect `canAccess` boolean
54
-
55
- ### PROHIBITED
56
-
57
- - **NEVER** bypass authentication check for sensitive features
58
- - **NEVER** show feature gate if user has access (confusing UX)
59
- - **NEVER** execute action without checking all gates first
60
- - **DO NOT** hardcode gate logic (use hook parameters)
61
- - **NEVER** deduct credits without checking subscription status first
62
-
63
- ### CRITICAL SAFETY
64
-
65
- - **ALWAYS** check gates in order: Auth → Subscription → Credits
66
- - **ALWAYS** provide clear messaging about why access is denied
67
- - **ALWAYS** preserve user intent through auth/purchase flows
68
- - **NEVER** trust client-side gate for security (server-side validation required)
69
- - **MUST** implement proper error boundaries around gated actions
70
-
71
- ## AI Agent Guidelines
72
-
73
- ### When Implementing Feature Gates
74
-
75
- 1. **Always** check gates in order: Auth → Subscription → Credits
76
- 2. **Always** provide clear, specific messaging for each gate
77
- 3. **Always** preserve user intent through auth/purchase flows
78
- 4. **Always** bypass credit check for premium users (if applicable)
79
- 5. **Never** trust client-side gates for security (server validation required)
80
-
81
- ### Integration Checklist
82
-
83
- - [ ] Import from correct path: `@umituz/react-native-subscription`
84
- - [ ] Implement `onShowAuthModal` with pending action preservation
85
- - [ ] Implement `onShowPaywall` with context-aware messaging
86
- - [ ] Check `canAccess` before showing feature
87
- - [ ] Provide appropriate messaging for each gate state
88
- - [ ] Respect premium status (bypass credit check if needed)
89
- - [ ] Test all gate combinations (auth, subscription, credits)
90
- - [ ] Test pending action execution after auth/purchase
91
- - [ ] Implement error boundaries around gated actions
92
-
93
- ### Common Patterns to Implement
94
-
95
- 1. **Progressive Unlocking**: Show upgrade path for each gate
96
- 2. **Smart Paywalls**: Show subscription OR credits based on user state
97
- 3. **Intent Preservation**: Queue actions through auth/purchase flows
98
- 4. **Clear Messaging**: Tell users exactly what's required
99
- 5. **Premium Bypass**: Skip credit check for premium users
100
- 6. **Access Indicators**: Show lock/unlock status in UI
101
- 7. **Graceful Degradation**: Show limited version for free users
102
- 8. **Analytics Tracking**: Monitor gate triggers and conversions
103
-
104
- ## Related Documentation
105
-
106
- - **useAuthGate**: Authentication gating only
107
- - **useSubscriptionGate**: Subscription gating only
108
- - **useCreditsGate**: Credits gating only
109
- - **usePremiumGate**: Premium feature gating
110
- - **useDeductCredit**: Credit deduction after gate passes
111
- - **Feature Gating**: `../../../docs/FEATURE_GATING.md`
112
- - **Access Control**: `../../../docs/ACCESS_PATTERNS.md`