@umituz/react-native-ai-generation-content 1.17.93 → 1.17.95

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-ai-generation-content",
3
- "version": "1.17.93",
3
+ "version": "1.17.95",
4
4
  "description": "Provider-agnostic AI generation orchestration for React Native",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
package/src/index.ts CHANGED
@@ -253,6 +253,7 @@ export {
253
253
  usePhotoGeneration,
254
254
  useGenerationFlow,
255
255
  useGenerationCallbacksBuilder,
256
+ useAIFeatureCallbacks,
256
257
  } from "./presentation/hooks";
257
258
 
258
259
  export type {
@@ -277,6 +278,9 @@ export type {
277
278
  GenerationCallbacksConfig,
278
279
  GenerationCallbacks,
279
280
  UseGenerationCallbacksBuilderOptions,
281
+ AIFeatureCallbacksConfig,
282
+ AIFeatureCallbacks,
283
+ AIFeatureGenerationResult,
280
284
  } from "./presentation/hooks";
281
285
 
282
286
  // =============================================================================
@@ -52,3 +52,10 @@ export type {
52
52
  GenerationCallbacks,
53
53
  UseGenerationCallbacksBuilderOptions,
54
54
  } from "./generation-callbacks.types";
55
+
56
+ export { useAIFeatureCallbacks } from "./useAIFeatureCallbacks";
57
+ export type {
58
+ AIFeatureCallbacksConfig,
59
+ AIFeatureCallbacks,
60
+ AIFeatureGenerationResult,
61
+ } from "./useAIFeatureCallbacks";
@@ -0,0 +1,169 @@
1
+ /**
2
+ * AI Feature Callbacks Adapter Hook
3
+ * Universal adapter for all AI generation features
4
+ * Works with: TextToImage, TextToVideo, ImageToVideo, etc.
5
+ */
6
+
7
+ import { useCallback, useMemo } from "react";
8
+
9
+ export interface AIFeatureCallbacksConfig<TRequest = unknown, TResult = unknown> {
10
+ // App provides reactive state
11
+ userId: string | null;
12
+ isAuthenticated: boolean;
13
+ creditBalance: number;
14
+
15
+ // Cost config
16
+ creditCostPerUnit: number;
17
+
18
+ // Executor - the actual generation function
19
+ executor: (request: TRequest) => Promise<{
20
+ success: boolean;
21
+ data?: TResult;
22
+ error?: string;
23
+ imageUrl?: string;
24
+ imageUrls?: string[];
25
+ }>;
26
+
27
+ // Actions from app
28
+ showAuthModal: () => void;
29
+ openPaywall: () => void;
30
+ deductCredits?: (amount: number) => Promise<void>;
31
+
32
+ // Optional callbacks
33
+ onSuccess?: (result: TResult) => void;
34
+ onError?: (error: string) => void;
35
+ }
36
+
37
+ /**
38
+ * Discriminated union result type for generation
39
+ */
40
+ export type AIFeatureGenerationResult =
41
+ | { success: true; imageUrls: string[] }
42
+ | { success: false; error: string };
43
+
44
+ /**
45
+ * Universal callbacks interface that maps to all feature-specific ones
46
+ */
47
+ export interface AIFeatureCallbacks<TRequest = unknown, TResult = unknown> {
48
+ // TextToImageCallbacks compatible
49
+ executeGeneration: (request: TRequest) => Promise<AIFeatureGenerationResult>;
50
+ calculateCost: (multiplier?: number, _model?: string | null) => number;
51
+ canAfford: (cost: number) => boolean;
52
+ isAuthenticated: () => boolean;
53
+ onAuthRequired: () => void;
54
+ onCreditsRequired: (cost?: number) => void;
55
+ onSuccess?: (result: TResult) => void;
56
+ onError?: (error: string) => void;
57
+
58
+ // ImageToVideoCallbacks compatible
59
+ onCreditCheck: (cost: number) => boolean;
60
+ onShowPaywall: (cost: number) => void;
61
+
62
+ // TextToVideoCallbacks compatible
63
+ onAuthCheck: () => boolean;
64
+ }
65
+
66
+ export function useAIFeatureCallbacks<TRequest = unknown, TResult = unknown>(
67
+ config: AIFeatureCallbacksConfig<TRequest, TResult>,
68
+ ): AIFeatureCallbacks<TRequest, TResult> {
69
+ const {
70
+ userId,
71
+ isAuthenticated: isAuth,
72
+ creditBalance,
73
+ creditCostPerUnit,
74
+ executor,
75
+ showAuthModal,
76
+ openPaywall,
77
+ deductCredits,
78
+ onSuccess,
79
+ onError,
80
+ } = config;
81
+
82
+ const canAfford = useCallback(
83
+ (cost: number): boolean => creditBalance >= cost,
84
+ [creditBalance],
85
+ );
86
+
87
+ const isAuthenticated = useCallback(
88
+ (): boolean => isAuth && !!userId,
89
+ [isAuth, userId],
90
+ );
91
+
92
+ const calculateCost = useCallback(
93
+ (multiplier = 1, _model?: string | null): number => creditCostPerUnit * multiplier,
94
+ [creditCostPerUnit],
95
+ );
96
+
97
+ const onAuthRequired = useCallback(() => {
98
+ showAuthModal();
99
+ }, [showAuthModal]);
100
+
101
+ const onCreditsRequired = useCallback(
102
+ (_cost?: number) => {
103
+ openPaywall();
104
+ },
105
+ [openPaywall],
106
+ );
107
+
108
+ const executeGeneration = useCallback(
109
+ async (request: TRequest): Promise<AIFeatureGenerationResult> => {
110
+ try {
111
+ const result = await executor(request);
112
+
113
+ if (result.success && deductCredits) {
114
+ await deductCredits(creditCostPerUnit);
115
+ }
116
+
117
+ if (result.success && result.data) {
118
+ onSuccess?.(result.data);
119
+ } else if (!result.success && result.error) {
120
+ onError?.(result.error);
121
+ }
122
+
123
+ if (result.success) {
124
+ return { success: true, imageUrls: result.imageUrls ?? [] };
125
+ }
126
+ return { success: false, error: result.error ?? "Unknown error" };
127
+ } catch (error) {
128
+ const message = error instanceof Error ? error.message : String(error);
129
+ onError?.(message);
130
+ return { success: false, error: message };
131
+ }
132
+ },
133
+ [executor, deductCredits, creditCostPerUnit, onSuccess, onError],
134
+ );
135
+
136
+ // Aliases for different callback interfaces
137
+ const onCreditCheck = canAfford;
138
+ const onAuthCheck = isAuthenticated;
139
+ const onShowPaywall = onCreditsRequired;
140
+
141
+ return useMemo(
142
+ () => ({
143
+ executeGeneration,
144
+ calculateCost,
145
+ canAfford,
146
+ isAuthenticated,
147
+ onAuthRequired,
148
+ onCreditsRequired,
149
+ onSuccess,
150
+ onError,
151
+ onCreditCheck,
152
+ onAuthCheck,
153
+ onShowPaywall,
154
+ }),
155
+ [
156
+ executeGeneration,
157
+ calculateCost,
158
+ canAfford,
159
+ isAuthenticated,
160
+ onAuthRequired,
161
+ onCreditsRequired,
162
+ onSuccess,
163
+ onError,
164
+ onCreditCheck,
165
+ onAuthCheck,
166
+ onShowPaywall,
167
+ ],
168
+ );
169
+ }