@umituz/react-native-ai-generation-content 1.17.308 → 1.17.310
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 +1 -1
- package/src/features/couple-future/index.ts +1 -1
- package/src/features/couple-future/presentation/hooks/useCoupleFutureGeneration.ts +54 -64
- package/src/index.ts +10 -7
- package/src/presentation/hooks/generation/errors.ts +58 -0
- package/src/presentation/hooks/generation/index.ts +42 -0
- package/src/presentation/hooks/generation/orchestrator.ts +146 -0
- package/src/presentation/hooks/generation/types.ts +72 -0
- package/src/presentation/hooks/generation/useImageGeneration.ts +157 -0
- package/src/presentation/hooks/generation/useVideoGeneration.ts +107 -0
- package/src/presentation/hooks/index.ts +25 -32
- package/src/presentation/hooks/base/index.ts +0 -9
- package/src/presentation/hooks/base/types.ts +0 -47
- package/src/presentation/hooks/base/use-dual-image-feature.ts +0 -170
- package/src/presentation/hooks/base/use-image-with-prompt-feature.ts +0 -167
- package/src/presentation/hooks/base/use-single-image-feature.ts +0 -154
- package/src/presentation/hooks/base/utils/feature-state.factory.ts +0 -133
- package/src/presentation/hooks/generation-callbacks.types.ts +0 -42
- package/src/presentation/hooks/photo-generation.types.ts +0 -61
- package/src/presentation/hooks/useGenerationCallbacksBuilder.ts +0 -126
- package/src/presentation/hooks/useGenerationCredits.ts +0 -77
- package/src/presentation/hooks/usePhotoGeneration.ts +0 -207
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* usePhotoGeneration Hook
|
|
3
|
-
* Generic hook for photo-based AI generation workflows
|
|
4
|
-
* Uses centralized credit management
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { useState, useCallback, useRef } from "react";
|
|
8
|
-
import { useOfflineStore, useAlert } from "@umituz/react-native-design-system";
|
|
9
|
-
import { useGenerationCredits, CreditError } from "./useGenerationCredits";
|
|
10
|
-
import type {
|
|
11
|
-
PhotoGenerationConfig,
|
|
12
|
-
PhotoGenerationState,
|
|
13
|
-
PhotoGenerationError,
|
|
14
|
-
PhotoGenerationStatus,
|
|
15
|
-
} from "./photo-generation.types";
|
|
16
|
-
|
|
17
|
-
export interface UsePhotoGenerationReturn<TInput, TResult>
|
|
18
|
-
extends PhotoGenerationState<TResult> {
|
|
19
|
-
generate: (input: TInput) => Promise<void>;
|
|
20
|
-
reset: () => void;
|
|
21
|
-
status: PhotoGenerationStatus;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const usePhotoGeneration = <TInput, TResult, TSaveInput = unknown>(
|
|
25
|
-
config: PhotoGenerationConfig<TInput, TResult, TSaveInput>,
|
|
26
|
-
): UsePhotoGenerationReturn<TInput, TResult> => {
|
|
27
|
-
const {
|
|
28
|
-
userId,
|
|
29
|
-
creditCost = 1,
|
|
30
|
-
generate: generateFn,
|
|
31
|
-
save: saveFn,
|
|
32
|
-
onCreditsExhausted,
|
|
33
|
-
onSuccess,
|
|
34
|
-
onError,
|
|
35
|
-
onSaveComplete,
|
|
36
|
-
alertMessages,
|
|
37
|
-
} = config;
|
|
38
|
-
|
|
39
|
-
const [state, setState] = useState<PhotoGenerationState<TResult>>({
|
|
40
|
-
isGenerating: false,
|
|
41
|
-
result: null,
|
|
42
|
-
error: null,
|
|
43
|
-
progress: 0,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
const [status, setStatus] = useState<PhotoGenerationStatus>("idle");
|
|
47
|
-
const isGeneratingRef = useRef(false);
|
|
48
|
-
const offlineStore = useOfflineStore();
|
|
49
|
-
const { showError } = useAlert();
|
|
50
|
-
|
|
51
|
-
const { checkCredits, deductCredits } = useGenerationCredits({
|
|
52
|
-
userId,
|
|
53
|
-
onCreditsExhausted,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const createError = useCallback(
|
|
57
|
-
(
|
|
58
|
-
type: PhotoGenerationError["type"],
|
|
59
|
-
message: string,
|
|
60
|
-
originalError?: Error,
|
|
61
|
-
): PhotoGenerationError => ({
|
|
62
|
-
type,
|
|
63
|
-
message,
|
|
64
|
-
originalError,
|
|
65
|
-
}),
|
|
66
|
-
[],
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const generate = useCallback(
|
|
70
|
-
async (input: TInput) => {
|
|
71
|
-
if (isGeneratingRef.current) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
isGeneratingRef.current = true;
|
|
76
|
-
setState({ isGenerating: true, result: null, error: null, progress: 0 });
|
|
77
|
-
setStatus("validating");
|
|
78
|
-
|
|
79
|
-
try {
|
|
80
|
-
if (!offlineStore.isOnline) {
|
|
81
|
-
throw createError("network_error", "No internet connection");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const hasCredits = await checkCredits(creditCost);
|
|
85
|
-
if (!hasCredits) {
|
|
86
|
-
throw createError("credit_failed", "Insufficient credits");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
setStatus("generating");
|
|
90
|
-
setState((prev) => ({ ...prev, progress: 20 }));
|
|
91
|
-
|
|
92
|
-
const result = await generateFn(input, (newProgress) => {
|
|
93
|
-
setState((prev) => ({ ...prev, progress: newProgress }));
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
setState((prev) => ({ ...prev, progress: 60 }));
|
|
97
|
-
|
|
98
|
-
if (saveFn) {
|
|
99
|
-
setStatus("saving");
|
|
100
|
-
try {
|
|
101
|
-
const saveResult = await saveFn(result, input);
|
|
102
|
-
onSaveComplete?.(saveResult);
|
|
103
|
-
} catch (saveError) {
|
|
104
|
-
throw createError(
|
|
105
|
-
"save_failed",
|
|
106
|
-
"Failed to save result",
|
|
107
|
-
saveError instanceof Error ? saveError : new Error(String(saveError)),
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
setState((prev) => ({ ...prev, progress: 80 }));
|
|
113
|
-
|
|
114
|
-
await deductCredits(creditCost);
|
|
115
|
-
|
|
116
|
-
setState({
|
|
117
|
-
isGenerating: false,
|
|
118
|
-
result,
|
|
119
|
-
error: null,
|
|
120
|
-
progress: 100,
|
|
121
|
-
});
|
|
122
|
-
setStatus("success");
|
|
123
|
-
onSuccess?.(result);
|
|
124
|
-
} catch (err: unknown) {
|
|
125
|
-
let generationError: PhotoGenerationError;
|
|
126
|
-
|
|
127
|
-
if (err instanceof CreditError) {
|
|
128
|
-
generationError = createError("credit_failed", err.message);
|
|
129
|
-
} else if (
|
|
130
|
-
err &&
|
|
131
|
-
typeof err === "object" &&
|
|
132
|
-
"type" in err &&
|
|
133
|
-
"message" in err
|
|
134
|
-
) {
|
|
135
|
-
generationError = err as PhotoGenerationError;
|
|
136
|
-
} else if (err instanceof Error) {
|
|
137
|
-
if (err.name === "ContentPolicyViolationError") {
|
|
138
|
-
generationError = createError(
|
|
139
|
-
"policy_violation",
|
|
140
|
-
"Content policy violation",
|
|
141
|
-
err,
|
|
142
|
-
);
|
|
143
|
-
} else {
|
|
144
|
-
generationError = createError(
|
|
145
|
-
"unknown",
|
|
146
|
-
err.message || "Generation failed",
|
|
147
|
-
err,
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
} else {
|
|
151
|
-
generationError = createError("unknown", "Generation failed");
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
setState({
|
|
155
|
-
isGenerating: false,
|
|
156
|
-
result: null,
|
|
157
|
-
error: generationError,
|
|
158
|
-
progress: 0,
|
|
159
|
-
});
|
|
160
|
-
setStatus("error");
|
|
161
|
-
|
|
162
|
-
const errorMessage =
|
|
163
|
-
generationError.type === "network_error"
|
|
164
|
-
? alertMessages.networkError
|
|
165
|
-
: generationError.type === "policy_violation"
|
|
166
|
-
? alertMessages.policyViolation
|
|
167
|
-
: generationError.type === "save_failed"
|
|
168
|
-
? alertMessages.saveFailed
|
|
169
|
-
: generationError.type === "credit_failed"
|
|
170
|
-
? alertMessages.creditFailed
|
|
171
|
-
: alertMessages.unknown;
|
|
172
|
-
|
|
173
|
-
void showError("Error", errorMessage);
|
|
174
|
-
onError?.(generationError);
|
|
175
|
-
} finally {
|
|
176
|
-
isGeneratingRef.current = false;
|
|
177
|
-
}
|
|
178
|
-
},
|
|
179
|
-
[
|
|
180
|
-
generateFn,
|
|
181
|
-
saveFn,
|
|
182
|
-
checkCredits,
|
|
183
|
-
deductCredits,
|
|
184
|
-
creditCost,
|
|
185
|
-
onSuccess,
|
|
186
|
-
onError,
|
|
187
|
-
onSaveComplete,
|
|
188
|
-
createError,
|
|
189
|
-
offlineStore,
|
|
190
|
-
alertMessages,
|
|
191
|
-
showError,
|
|
192
|
-
],
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
const reset = useCallback(() => {
|
|
196
|
-
setState({ isGenerating: false, result: null, error: null, progress: 0 });
|
|
197
|
-
setStatus("idle");
|
|
198
|
-
isGeneratingRef.current = false;
|
|
199
|
-
}, []);
|
|
200
|
-
|
|
201
|
-
return {
|
|
202
|
-
...state,
|
|
203
|
-
generate,
|
|
204
|
-
reset,
|
|
205
|
-
status,
|
|
206
|
-
};
|
|
207
|
-
};
|