@umituz/react-native-ai-generation-content 1.17.93 → 1.17.94
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
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,8 @@ export type {
|
|
|
277
278
|
GenerationCallbacksConfig,
|
|
278
279
|
GenerationCallbacks,
|
|
279
280
|
UseGenerationCallbacksBuilderOptions,
|
|
281
|
+
AIFeatureCallbacksConfig,
|
|
282
|
+
AIFeatureCallbacks,
|
|
280
283
|
} from "./presentation/hooks";
|
|
281
284
|
|
|
282
285
|
// =============================================================================
|
|
@@ -52,3 +52,9 @@ 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
|
+
} from "./useAIFeatureCallbacks";
|
|
@@ -0,0 +1,167 @@
|
|
|
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
|
+
* Universal callbacks interface that maps to all feature-specific ones
|
|
39
|
+
*/
|
|
40
|
+
export interface AIFeatureCallbacks<TRequest = unknown, TResult = unknown> {
|
|
41
|
+
// TextToImageCallbacks compatible
|
|
42
|
+
executeGeneration: (request: TRequest) => Promise<{
|
|
43
|
+
success: boolean;
|
|
44
|
+
imageUrls?: string[];
|
|
45
|
+
error?: string;
|
|
46
|
+
}>;
|
|
47
|
+
calculateCost: (multiplier?: number, _model?: string | null) => number;
|
|
48
|
+
canAfford: (cost: number) => boolean;
|
|
49
|
+
isAuthenticated: () => boolean;
|
|
50
|
+
onAuthRequired: () => void;
|
|
51
|
+
onCreditsRequired: (cost?: number) => void;
|
|
52
|
+
onSuccess?: (result: TResult) => void;
|
|
53
|
+
onError?: (error: string) => void;
|
|
54
|
+
|
|
55
|
+
// ImageToVideoCallbacks compatible
|
|
56
|
+
onCreditCheck: (cost: number) => boolean;
|
|
57
|
+
onShowPaywall: (cost: number) => void;
|
|
58
|
+
|
|
59
|
+
// TextToVideoCallbacks compatible
|
|
60
|
+
onAuthCheck: () => boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function useAIFeatureCallbacks<TRequest = unknown, TResult = unknown>(
|
|
64
|
+
config: AIFeatureCallbacksConfig<TRequest, TResult>,
|
|
65
|
+
): AIFeatureCallbacks<TRequest, TResult> {
|
|
66
|
+
const {
|
|
67
|
+
userId,
|
|
68
|
+
isAuthenticated: isAuth,
|
|
69
|
+
creditBalance,
|
|
70
|
+
creditCostPerUnit,
|
|
71
|
+
executor,
|
|
72
|
+
showAuthModal,
|
|
73
|
+
openPaywall,
|
|
74
|
+
deductCredits,
|
|
75
|
+
onSuccess,
|
|
76
|
+
onError,
|
|
77
|
+
} = config;
|
|
78
|
+
|
|
79
|
+
const canAfford = useCallback(
|
|
80
|
+
(cost: number): boolean => creditBalance >= cost,
|
|
81
|
+
[creditBalance],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const isAuthenticated = useCallback(
|
|
85
|
+
(): boolean => isAuth && !!userId,
|
|
86
|
+
[isAuth, userId],
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const calculateCost = useCallback(
|
|
90
|
+
(multiplier = 1, _model?: string | null): number => creditCostPerUnit * multiplier,
|
|
91
|
+
[creditCostPerUnit],
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const onAuthRequired = useCallback(() => {
|
|
95
|
+
showAuthModal();
|
|
96
|
+
}, [showAuthModal]);
|
|
97
|
+
|
|
98
|
+
const onCreditsRequired = useCallback(
|
|
99
|
+
(_cost?: number) => {
|
|
100
|
+
openPaywall();
|
|
101
|
+
},
|
|
102
|
+
[openPaywall],
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const executeGeneration = useCallback(
|
|
106
|
+
async (request: TRequest) => {
|
|
107
|
+
try {
|
|
108
|
+
const result = await executor(request);
|
|
109
|
+
|
|
110
|
+
if (result.success && deductCredits) {
|
|
111
|
+
await deductCredits(creditCostPerUnit);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (result.success && result.data) {
|
|
115
|
+
onSuccess?.(result.data);
|
|
116
|
+
} else if (!result.success && result.error) {
|
|
117
|
+
onError?.(result.error);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
success: result.success,
|
|
122
|
+
imageUrls: result.imageUrls,
|
|
123
|
+
error: result.error,
|
|
124
|
+
};
|
|
125
|
+
} catch (error) {
|
|
126
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
127
|
+
onError?.(message);
|
|
128
|
+
return { success: false, error: message };
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
[executor, deductCredits, creditCostPerUnit, onSuccess, onError],
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
// Aliases for different callback interfaces
|
|
135
|
+
const onCreditCheck = canAfford;
|
|
136
|
+
const onAuthCheck = isAuthenticated;
|
|
137
|
+
const onShowPaywall = onCreditsRequired;
|
|
138
|
+
|
|
139
|
+
return useMemo(
|
|
140
|
+
() => ({
|
|
141
|
+
executeGeneration,
|
|
142
|
+
calculateCost,
|
|
143
|
+
canAfford,
|
|
144
|
+
isAuthenticated,
|
|
145
|
+
onAuthRequired,
|
|
146
|
+
onCreditsRequired,
|
|
147
|
+
onSuccess,
|
|
148
|
+
onError,
|
|
149
|
+
onCreditCheck,
|
|
150
|
+
onAuthCheck,
|
|
151
|
+
onShowPaywall,
|
|
152
|
+
}),
|
|
153
|
+
[
|
|
154
|
+
executeGeneration,
|
|
155
|
+
calculateCost,
|
|
156
|
+
canAfford,
|
|
157
|
+
isAuthenticated,
|
|
158
|
+
onAuthRequired,
|
|
159
|
+
onCreditsRequired,
|
|
160
|
+
onSuccess,
|
|
161
|
+
onError,
|
|
162
|
+
onCreditCheck,
|
|
163
|
+
onAuthCheck,
|
|
164
|
+
onShowPaywall,
|
|
165
|
+
],
|
|
166
|
+
);
|
|
167
|
+
}
|