@umituz/react-native-ai-generation-content 1.17.178 → 1.17.179
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
|
@@ -8,7 +8,6 @@ import { useCallback, useMemo } from "react";
|
|
|
8
8
|
import { useQueryClient } from "@tanstack/react-query";
|
|
9
9
|
import { useAuth } from "@umituz/react-native-auth";
|
|
10
10
|
import { createCreationsRepository } from "../../infrastructure/adapters";
|
|
11
|
-
import { getCreditService, isAppServicesConfigured } from "../../../../infrastructure/config/app-services.config";
|
|
12
11
|
import type { Creation } from "../../domain/entities/Creation";
|
|
13
12
|
|
|
14
13
|
declare const __DEV__: boolean;
|
|
@@ -21,8 +20,10 @@ export interface UseCreationPersistenceConfig {
|
|
|
21
20
|
readonly type: string;
|
|
22
21
|
/** Collection name in Firestore (defaults to "creations") */
|
|
23
22
|
readonly collectionName?: string;
|
|
24
|
-
/** Credit cost
|
|
23
|
+
/** Credit cost for this feature (passed to onCreditDeduct) */
|
|
25
24
|
readonly creditCost?: number;
|
|
25
|
+
/** Callback to deduct credits on successful processing */
|
|
26
|
+
readonly onCreditDeduct?: (cost: number) => Promise<void>;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
/**
|
|
@@ -54,17 +55,19 @@ export interface UseCreationPersistenceReturn {
|
|
|
54
55
|
* Hook that provides Firestore persistence callbacks for AI features
|
|
55
56
|
*
|
|
56
57
|
* @example
|
|
57
|
-
*
|
|
58
|
-
* const persistence = useCreationPersistence({
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
58
|
+
* const { deductCredit } = useDeductCredit({ userId, onCreditsExhausted: openPaywall });
|
|
59
|
+
* const persistence = useCreationPersistence({
|
|
60
|
+
* type: "anime-selfie",
|
|
61
|
+
* creditCost: AI_CREDIT_COST.ANIME_SELFIE,
|
|
62
|
+
* onCreditDeduct: async (cost) => {
|
|
63
|
+
* for (let i = 0; i < cost; i++) await deductCredit("image");
|
|
64
|
+
* },
|
|
65
|
+
* });
|
|
63
66
|
*/
|
|
64
67
|
export function useCreationPersistence(
|
|
65
68
|
config: UseCreationPersistenceConfig,
|
|
66
69
|
): UseCreationPersistenceReturn {
|
|
67
|
-
const { type, collectionName = "creations" } = config;
|
|
70
|
+
const { type, collectionName = "creations", creditCost, onCreditDeduct } = config;
|
|
68
71
|
const { userId } = useAuth();
|
|
69
72
|
const queryClient = useQueryClient();
|
|
70
73
|
|
|
@@ -76,7 +79,7 @@ export function useCreationPersistence(
|
|
|
76
79
|
const onProcessingStart = useCallback(
|
|
77
80
|
<T extends BaseProcessingStartData>(data: T) => {
|
|
78
81
|
if (__DEV__) {
|
|
79
|
-
console.log("[useCreationPersistence] onProcessingStart
|
|
82
|
+
console.log("[useCreationPersistence] onProcessingStart", { type, userId });
|
|
80
83
|
}
|
|
81
84
|
|
|
82
85
|
if (!userId) {
|
|
@@ -89,10 +92,6 @@ export function useCreationPersistence(
|
|
|
89
92
|
Object.entries(rest).filter(([, v]) => v !== undefined && v !== null),
|
|
90
93
|
);
|
|
91
94
|
|
|
92
|
-
if (__DEV__) {
|
|
93
|
-
console.log("[useCreationPersistence] cleanMetadata", cleanMetadata);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
95
|
const creation: Creation = {
|
|
97
96
|
id: creationId,
|
|
98
97
|
uri: "",
|
|
@@ -117,16 +116,15 @@ export function useCreationPersistence(
|
|
|
117
116
|
const onProcessingComplete = useCallback(
|
|
118
117
|
<T extends BaseProcessingResult>(result: T) => {
|
|
119
118
|
if (__DEV__) {
|
|
120
|
-
console.log("[useCreationPersistence] onProcessingComplete
|
|
119
|
+
console.log("[useCreationPersistence] onProcessingComplete", {
|
|
121
120
|
creationId: result.creationId,
|
|
122
121
|
hasImageUrl: !!result.imageUrl,
|
|
123
122
|
hasVideoUrl: !!result.videoUrl,
|
|
124
|
-
creditCost: config.creditCost,
|
|
125
123
|
});
|
|
126
124
|
}
|
|
127
125
|
|
|
128
126
|
if (!userId || !result.creationId) {
|
|
129
|
-
if (__DEV__) console.log("[useCreationPersistence] Missing userId or creationId
|
|
127
|
+
if (__DEV__) console.log("[useCreationPersistence] Missing userId or creationId");
|
|
130
128
|
return;
|
|
131
129
|
}
|
|
132
130
|
|
|
@@ -137,13 +135,6 @@ export function useCreationPersistence(
|
|
|
137
135
|
? { videoUrl: result.videoUrl }
|
|
138
136
|
: undefined;
|
|
139
137
|
|
|
140
|
-
if (__DEV__) {
|
|
141
|
-
console.log("[useCreationPersistence] Updating document to completed", {
|
|
142
|
-
creationId: result.creationId,
|
|
143
|
-
uri: uri.substring(0, 50) + "...",
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
138
|
repository.update(userId, result.creationId, {
|
|
148
139
|
uri,
|
|
149
140
|
status: "completed",
|
|
@@ -151,39 +142,32 @@ export function useCreationPersistence(
|
|
|
151
142
|
});
|
|
152
143
|
queryClient.invalidateQueries({ queryKey: ["creations"] });
|
|
153
144
|
|
|
154
|
-
// Deduct credits
|
|
155
|
-
if (
|
|
156
|
-
|
|
157
|
-
|
|
145
|
+
// Deduct credits via callback (app provides implementation)
|
|
146
|
+
if (creditCost && creditCost > 0 && onCreditDeduct) {
|
|
147
|
+
if (__DEV__) {
|
|
148
|
+
console.log("[useCreationPersistence] Deducting credits", { cost: creditCost });
|
|
149
|
+
}
|
|
150
|
+
onCreditDeduct(creditCost).catch((err) => {
|
|
158
151
|
if (__DEV__) {
|
|
159
|
-
console.
|
|
152
|
+
console.error("[useCreationPersistence] Credit deduction failed", err);
|
|
160
153
|
}
|
|
161
|
-
|
|
162
|
-
if (__DEV__) {
|
|
163
|
-
console.error("[useCreationPersistence] Credit deduction failed", err);
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
}
|
|
154
|
+
});
|
|
167
155
|
}
|
|
168
156
|
},
|
|
169
|
-
[userId, repository, queryClient,
|
|
157
|
+
[userId, repository, queryClient, creditCost, onCreditDeduct],
|
|
170
158
|
);
|
|
171
159
|
|
|
172
160
|
const onError = useCallback(
|
|
173
161
|
(error: string, creationId?: string) => {
|
|
174
162
|
if (__DEV__) {
|
|
175
|
-
console.log("[useCreationPersistence] onError
|
|
163
|
+
console.log("[useCreationPersistence] onError", { error, creationId });
|
|
176
164
|
}
|
|
177
165
|
|
|
178
166
|
if (!userId || !creationId) {
|
|
179
|
-
if (__DEV__) console.log("[useCreationPersistence] Missing userId or creationId
|
|
167
|
+
if (__DEV__) console.log("[useCreationPersistence] Missing userId or creationId");
|
|
180
168
|
return;
|
|
181
169
|
}
|
|
182
170
|
|
|
183
|
-
if (__DEV__) {
|
|
184
|
-
console.log("[useCreationPersistence] Updating document to failed", { creationId });
|
|
185
|
-
}
|
|
186
|
-
|
|
187
171
|
repository.update(userId, creationId, {
|
|
188
172
|
status: "failed",
|
|
189
173
|
metadata: { error },
|