@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
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useVideoGeneration Hook
|
|
3
|
+
* Generic video generation hook for dual-image video features (ai-hug, ai-kiss)
|
|
4
|
+
* Uses centralized orchestrator for credit/error handling
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { useMemo, useCallback } from "react";
|
|
8
|
+
import { useGenerationOrchestrator } from "./orchestrator";
|
|
9
|
+
import type { GenerationStrategy, AlertMessages } from "./types";
|
|
10
|
+
import { executeVideoFeature } from "../../../infrastructure/services";
|
|
11
|
+
import type { VideoFeatureType } from "../../../domain/interfaces";
|
|
12
|
+
import { createCreationsRepository } from "../../../domains/creations/infrastructure/adapters";
|
|
13
|
+
import type { Creation } from "../../../domains/creations/domain/entities/Creation";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Input for dual image video features (ai-hug, ai-kiss)
|
|
17
|
+
*/
|
|
18
|
+
export interface DualImageVideoInput {
|
|
19
|
+
sourceImageBase64: string;
|
|
20
|
+
targetImageBase64: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface VideoGenerationConfig<TResult> {
|
|
24
|
+
/** Feature type (ai-hug, ai-kiss) */
|
|
25
|
+
featureType: VideoFeatureType;
|
|
26
|
+
/** User ID for credit operations */
|
|
27
|
+
userId: string | undefined;
|
|
28
|
+
/** Transform video URL to result type */
|
|
29
|
+
processResult: (videoUrl: string, input: DualImageVideoInput) => TResult;
|
|
30
|
+
/** Optional: Build creation for saving */
|
|
31
|
+
buildCreation?: (result: TResult, input: DualImageVideoInput) => Creation | null;
|
|
32
|
+
/** Credit cost (default: 1) */
|
|
33
|
+
creditCost?: number;
|
|
34
|
+
/** Alert messages for errors */
|
|
35
|
+
alertMessages: AlertMessages;
|
|
36
|
+
/** Callbacks */
|
|
37
|
+
onCreditsExhausted?: () => void;
|
|
38
|
+
onSuccess?: (result: TResult) => void;
|
|
39
|
+
onError?: (error: string) => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const useVideoGeneration = <TResult>(
|
|
43
|
+
config: VideoGenerationConfig<TResult>,
|
|
44
|
+
) => {
|
|
45
|
+
const {
|
|
46
|
+
featureType,
|
|
47
|
+
userId,
|
|
48
|
+
processResult,
|
|
49
|
+
buildCreation,
|
|
50
|
+
creditCost = 1,
|
|
51
|
+
alertMessages,
|
|
52
|
+
onCreditsExhausted,
|
|
53
|
+
onSuccess,
|
|
54
|
+
onError,
|
|
55
|
+
} = config;
|
|
56
|
+
|
|
57
|
+
const repository = useMemo(
|
|
58
|
+
() => createCreationsRepository("creations"),
|
|
59
|
+
[],
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const strategy: GenerationStrategy<DualImageVideoInput, TResult> = useMemo(
|
|
63
|
+
() => ({
|
|
64
|
+
execute: async (input, onProgress) => {
|
|
65
|
+
const result = await executeVideoFeature(
|
|
66
|
+
featureType,
|
|
67
|
+
{
|
|
68
|
+
sourceImageBase64: input.sourceImageBase64,
|
|
69
|
+
targetImageBase64: input.targetImageBase64,
|
|
70
|
+
},
|
|
71
|
+
{ onProgress },
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
if (!result.success || !result.videoUrl) {
|
|
75
|
+
throw new Error(result.error || "Video generation failed");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return processResult(result.videoUrl, input);
|
|
79
|
+
},
|
|
80
|
+
getCreditCost: () => creditCost,
|
|
81
|
+
save: buildCreation
|
|
82
|
+
? async (result, uid) => {
|
|
83
|
+
const creation = buildCreation(result, {} as DualImageVideoInput);
|
|
84
|
+
if (creation) {
|
|
85
|
+
await repository.create(uid, creation);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
: undefined,
|
|
89
|
+
}),
|
|
90
|
+
[featureType, processResult, buildCreation, repository, creditCost],
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const handleError = useCallback(
|
|
94
|
+
(error: { message: string }) => {
|
|
95
|
+
onError?.(error.message);
|
|
96
|
+
},
|
|
97
|
+
[onError],
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return useGenerationOrchestrator(strategy, {
|
|
101
|
+
userId,
|
|
102
|
+
alertMessages,
|
|
103
|
+
onCreditsExhausted,
|
|
104
|
+
onSuccess: onSuccess as (result: unknown) => void,
|
|
105
|
+
onError: handleError,
|
|
106
|
+
});
|
|
107
|
+
};
|
|
@@ -2,8 +2,31 @@
|
|
|
2
2
|
* Presentation Hooks
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
//
|
|
6
|
-
export
|
|
5
|
+
// Generation Orchestrator (Centralized)
|
|
6
|
+
export {
|
|
7
|
+
useGenerationOrchestrator,
|
|
8
|
+
useImageGeneration,
|
|
9
|
+
useVideoGeneration,
|
|
10
|
+
createGenerationError,
|
|
11
|
+
getAlertMessage,
|
|
12
|
+
parseError,
|
|
13
|
+
} from "./generation";
|
|
14
|
+
export type {
|
|
15
|
+
GenerationStrategy,
|
|
16
|
+
GenerationConfig,
|
|
17
|
+
GenerationState,
|
|
18
|
+
OrchestratorStatus,
|
|
19
|
+
GenerationError,
|
|
20
|
+
GenerationErrorType,
|
|
21
|
+
AlertMessages,
|
|
22
|
+
UseGenerationOrchestratorReturn,
|
|
23
|
+
SingleImageInput,
|
|
24
|
+
DualImageInput,
|
|
25
|
+
ImageGenerationInput,
|
|
26
|
+
ImageGenerationConfig,
|
|
27
|
+
DualImageVideoInput,
|
|
28
|
+
VideoGenerationConfig,
|
|
29
|
+
} from "./generation";
|
|
7
30
|
|
|
8
31
|
export { useGeneration } from "./use-generation";
|
|
9
32
|
export type {
|
|
@@ -24,42 +47,12 @@ export type {
|
|
|
24
47
|
DirectExecutionResult,
|
|
25
48
|
} from "./use-background-generation";
|
|
26
49
|
|
|
27
|
-
export { usePhotoGeneration } from "./usePhotoGeneration";
|
|
28
|
-
export type {
|
|
29
|
-
UsePhotoGenerationReturn,
|
|
30
|
-
} from "./usePhotoGeneration";
|
|
31
|
-
|
|
32
|
-
export { useGenerationCredits, CreditError } from "./useGenerationCredits";
|
|
33
|
-
export type {
|
|
34
|
-
UseGenerationCreditsParams,
|
|
35
|
-
UseGenerationCreditsReturn,
|
|
36
|
-
GenerationCreditsError,
|
|
37
|
-
} from "./useGenerationCredits";
|
|
38
|
-
|
|
39
|
-
export type {
|
|
40
|
-
PhotoGenerationInput,
|
|
41
|
-
PhotoGenerationResult,
|
|
42
|
-
PhotoGenerationError,
|
|
43
|
-
PhotoGenerationConfig,
|
|
44
|
-
PhotoGenerationState,
|
|
45
|
-
PhotoGenerationStatus,
|
|
46
|
-
} from "./photo-generation.types";
|
|
47
|
-
|
|
48
50
|
export { useGenerationFlow } from "./useGenerationFlow";
|
|
49
51
|
export type {
|
|
50
52
|
UseGenerationFlowOptions,
|
|
51
53
|
UseGenerationFlowReturn,
|
|
52
54
|
} from "./useGenerationFlow";
|
|
53
55
|
|
|
54
|
-
export { useGenerationCallbacksBuilder } from "./useGenerationCallbacksBuilder";
|
|
55
|
-
export type {
|
|
56
|
-
CreditType,
|
|
57
|
-
GenerationExecutionResult,
|
|
58
|
-
GenerationCallbacksConfig,
|
|
59
|
-
GenerationCallbacks,
|
|
60
|
-
UseGenerationCallbacksBuilderOptions,
|
|
61
|
-
} from "./generation-callbacks.types";
|
|
62
|
-
|
|
63
56
|
export { useAIFeatureCallbacks } from "./useAIFeatureCallbacks";
|
|
64
57
|
export type {
|
|
65
58
|
AIFeatureCallbacksConfig,
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base Feature Hook Types
|
|
3
|
-
* Provider-agnostic types for feature hooks
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Result from AI processing
|
|
8
|
-
*/
|
|
9
|
-
export interface FeatureProcessResult {
|
|
10
|
-
readonly success: boolean;
|
|
11
|
-
readonly outputUrl?: string;
|
|
12
|
-
readonly error?: string;
|
|
13
|
-
readonly metadata?: Record<string, unknown>;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Base state shared by all feature hooks
|
|
18
|
-
*/
|
|
19
|
-
export interface BaseFeatureState {
|
|
20
|
-
readonly isProcessing: boolean;
|
|
21
|
-
readonly progress: number;
|
|
22
|
-
readonly error: string | null;
|
|
23
|
-
readonly processedUrl: string | null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Base actions shared by all feature hooks
|
|
28
|
-
*/
|
|
29
|
-
export interface BaseFeatureActions {
|
|
30
|
-
readonly reset: () => void;
|
|
31
|
-
readonly clearError: () => void;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Progress callback type
|
|
36
|
-
*/
|
|
37
|
-
export type OnProgressCallback = (progress: number) => void;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Image selection callback - provided by app
|
|
41
|
-
*/
|
|
42
|
-
export type OnSelectImageCallback = () => Promise<string | null>;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Save callback - provided by app
|
|
46
|
-
*/
|
|
47
|
-
export type OnSaveCallback = (url: string) => Promise<void>;
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useDualImageFeature Hook
|
|
3
|
-
* Provider-agnostic hook for dual image processing features
|
|
4
|
-
* Examples: AI Hug, AI Kiss, Face Swap
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { useCallback, useState } from "react";
|
|
8
|
-
import type {
|
|
9
|
-
BaseFeatureState,
|
|
10
|
-
BaseFeatureActions,
|
|
11
|
-
FeatureProcessResult,
|
|
12
|
-
OnProgressCallback,
|
|
13
|
-
OnSelectImageCallback,
|
|
14
|
-
OnSaveCallback,
|
|
15
|
-
} from "./types";
|
|
16
|
-
import { createFeatureStateHandlers, executeProcess, executeSave } from "./utils/feature-state.factory";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Request passed to processRequest callback
|
|
20
|
-
*/
|
|
21
|
-
export interface DualImageProcessRequest {
|
|
22
|
-
readonly firstImageUri: string;
|
|
23
|
-
readonly secondImageUri: string;
|
|
24
|
-
readonly onProgress: OnProgressCallback;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Configuration for dual image feature
|
|
29
|
-
*/
|
|
30
|
-
export interface UseDualImageFeatureConfig {
|
|
31
|
-
readonly onSelectFirstImage: OnSelectImageCallback;
|
|
32
|
-
readonly onSelectSecondImage: OnSelectImageCallback;
|
|
33
|
-
readonly processRequest: (
|
|
34
|
-
request: DualImageProcessRequest,
|
|
35
|
-
) => Promise<FeatureProcessResult>;
|
|
36
|
-
readonly onSave?: OnSaveCallback;
|
|
37
|
-
readonly onError?: (error: string) => void;
|
|
38
|
-
readonly onSuccess?: (url: string) => void;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* State for dual image feature
|
|
43
|
-
*/
|
|
44
|
-
export interface DualImageFeatureState extends BaseFeatureState {
|
|
45
|
-
readonly firstImageUri: string | null;
|
|
46
|
-
readonly secondImageUri: string | null;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Return type for dual image feature hook
|
|
51
|
-
*/
|
|
52
|
-
export interface UseDualImageFeatureReturn
|
|
53
|
-
extends DualImageFeatureState,
|
|
54
|
-
BaseFeatureActions {
|
|
55
|
-
readonly selectFirstImage: () => Promise<void>;
|
|
56
|
-
readonly selectSecondImage: () => Promise<void>;
|
|
57
|
-
readonly process: () => Promise<void>;
|
|
58
|
-
readonly save: () => Promise<void>;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const initialState: DualImageFeatureState = {
|
|
62
|
-
firstImageUri: null,
|
|
63
|
-
secondImageUri: null,
|
|
64
|
-
processedUrl: null,
|
|
65
|
-
isProcessing: false,
|
|
66
|
-
progress: 0,
|
|
67
|
-
error: null,
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export function useDualImageFeature(
|
|
71
|
-
config: UseDualImageFeatureConfig,
|
|
72
|
-
): UseDualImageFeatureReturn {
|
|
73
|
-
const [state, setState] = useState<DualImageFeatureState>(initialState);
|
|
74
|
-
|
|
75
|
-
const { reset, clearError } = createFeatureStateHandlers({
|
|
76
|
-
setState,
|
|
77
|
-
initialState,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const selectFirstImage = useCallback(async (): Promise<void> => {
|
|
81
|
-
try {
|
|
82
|
-
const uri = await config.onSelectFirstImage();
|
|
83
|
-
if (uri) {
|
|
84
|
-
setState((prev) => ({
|
|
85
|
-
...prev,
|
|
86
|
-
firstImageUri: uri,
|
|
87
|
-
error: null,
|
|
88
|
-
processedUrl: null,
|
|
89
|
-
}));
|
|
90
|
-
}
|
|
91
|
-
} catch (err) {
|
|
92
|
-
const message = err instanceof Error ? err.message : "error.selectImage";
|
|
93
|
-
setState((prev) => ({ ...prev, error: message }));
|
|
94
|
-
config.onError?.(message);
|
|
95
|
-
}
|
|
96
|
-
}, [config]);
|
|
97
|
-
|
|
98
|
-
const selectSecondImage = useCallback(async (): Promise<void> => {
|
|
99
|
-
try {
|
|
100
|
-
const uri = await config.onSelectSecondImage();
|
|
101
|
-
if (uri) {
|
|
102
|
-
setState((prev) => ({
|
|
103
|
-
...prev,
|
|
104
|
-
secondImageUri: uri,
|
|
105
|
-
error: null,
|
|
106
|
-
processedUrl: null,
|
|
107
|
-
}));
|
|
108
|
-
}
|
|
109
|
-
} catch (err) {
|
|
110
|
-
const message = err instanceof Error ? err.message : "error.selectImage";
|
|
111
|
-
setState((prev) => ({ ...prev, error: message }));
|
|
112
|
-
config.onError?.(message);
|
|
113
|
-
}
|
|
114
|
-
}, [config]);
|
|
115
|
-
|
|
116
|
-
const process = useCallback(async (): Promise<void> => {
|
|
117
|
-
if (!state.firstImageUri || !state.secondImageUri) {
|
|
118
|
-
const message = "error.noImages";
|
|
119
|
-
setState((prev) => ({ ...prev, error: message }));
|
|
120
|
-
config.onError?.(message);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const result = await executeProcess<FeatureProcessResult>({
|
|
125
|
-
canProcess: () => !!state.firstImageUri && !!state.secondImageUri,
|
|
126
|
-
setError: (error: string | null) => setState((prev) => ({ ...prev, error })),
|
|
127
|
-
setProcessing: (isProcessing: boolean) => setState((prev) => ({ ...prev, isProcessing })),
|
|
128
|
-
onError: config.onError,
|
|
129
|
-
processFn: () =>
|
|
130
|
-
config.processRequest({
|
|
131
|
-
firstImageUri: state.firstImageUri!,
|
|
132
|
-
secondImageUri: state.secondImageUri!,
|
|
133
|
-
onProgress: (progress) => setState((prev) => ({ ...prev, progress })),
|
|
134
|
-
}),
|
|
135
|
-
onSuccess: (result: FeatureProcessResult) => {
|
|
136
|
-
if (result.outputUrl) {
|
|
137
|
-
setState((prev) => ({ ...prev, processedUrl: result.outputUrl ?? null }));
|
|
138
|
-
config.onSuccess?.(result.outputUrl);
|
|
139
|
-
} else {
|
|
140
|
-
const message = result.error || "error.processing";
|
|
141
|
-
setState((prev) => ({ ...prev, error: message }));
|
|
142
|
-
config.onError?.(message);
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
if (!result) {
|
|
148
|
-
setState((prev) => ({ ...prev, progress: 0 }));
|
|
149
|
-
}
|
|
150
|
-
}, [state.firstImageUri, state.secondImageUri, config]);
|
|
151
|
-
|
|
152
|
-
const save = useCallback(async (): Promise<void> => {
|
|
153
|
-
await executeSave({
|
|
154
|
-
processedUrl: state.processedUrl,
|
|
155
|
-
onSave: config.onSave,
|
|
156
|
-
setError: (error) => setState((prev) => ({ ...prev, error })),
|
|
157
|
-
onError: config.onError,
|
|
158
|
-
});
|
|
159
|
-
}, [state.processedUrl, config]);
|
|
160
|
-
|
|
161
|
-
return {
|
|
162
|
-
...state,
|
|
163
|
-
selectFirstImage,
|
|
164
|
-
selectSecondImage,
|
|
165
|
-
process,
|
|
166
|
-
save,
|
|
167
|
-
reset,
|
|
168
|
-
clearError,
|
|
169
|
-
};
|
|
170
|
-
}
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useImageWithPromptFeature Hook
|
|
3
|
-
* Provider-agnostic hook for image + prompt processing features
|
|
4
|
-
* Examples: Inpainting, Style Transfer, Background Replacement
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { useCallback, useState } from "react";
|
|
8
|
-
import type {
|
|
9
|
-
BaseFeatureState,
|
|
10
|
-
BaseFeatureActions,
|
|
11
|
-
FeatureProcessResult,
|
|
12
|
-
OnProgressCallback,
|
|
13
|
-
OnSelectImageCallback,
|
|
14
|
-
OnSaveCallback,
|
|
15
|
-
} from "./types";
|
|
16
|
-
import { createFeatureStateHandlers, executeProcess, executeSave } from "./utils/feature-state.factory";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Request passed to processRequest callback
|
|
20
|
-
*/
|
|
21
|
-
export interface ImageWithPromptProcessRequest {
|
|
22
|
-
readonly imageUri: string;
|
|
23
|
-
readonly prompt: string;
|
|
24
|
-
readonly onProgress: OnProgressCallback;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Configuration for image with prompt feature
|
|
29
|
-
*/
|
|
30
|
-
export interface UseImageWithPromptFeatureConfig {
|
|
31
|
-
readonly onSelectImage: OnSelectImageCallback;
|
|
32
|
-
readonly processRequest: (
|
|
33
|
-
request: ImageWithPromptProcessRequest
|
|
34
|
-
) => Promise<FeatureProcessResult>;
|
|
35
|
-
readonly onSave?: OnSaveCallback;
|
|
36
|
-
readonly onError?: (error: string) => void;
|
|
37
|
-
readonly onSuccess?: (url: string) => void;
|
|
38
|
-
readonly requirePrompt?: boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* State for image with prompt feature
|
|
43
|
-
*/
|
|
44
|
-
export interface ImageWithPromptFeatureState extends BaseFeatureState {
|
|
45
|
-
readonly imageUri: string | null;
|
|
46
|
-
readonly prompt: string;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Return type for image with prompt feature hook
|
|
51
|
-
*/
|
|
52
|
-
export interface UseImageWithPromptFeatureReturn
|
|
53
|
-
extends ImageWithPromptFeatureState,
|
|
54
|
-
BaseFeatureActions {
|
|
55
|
-
readonly selectImage: () => Promise<void>;
|
|
56
|
-
readonly setPrompt: (prompt: string) => void;
|
|
57
|
-
readonly process: () => Promise<void>;
|
|
58
|
-
readonly save: () => Promise<void>;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const initialState: ImageWithPromptFeatureState = {
|
|
62
|
-
imageUri: null,
|
|
63
|
-
prompt: "",
|
|
64
|
-
processedUrl: null,
|
|
65
|
-
isProcessing: false,
|
|
66
|
-
progress: 0,
|
|
67
|
-
error: null,
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export function useImageWithPromptFeature(
|
|
71
|
-
config: UseImageWithPromptFeatureConfig,
|
|
72
|
-
): UseImageWithPromptFeatureReturn {
|
|
73
|
-
const [state, setState] = useState<ImageWithPromptFeatureState>(initialState);
|
|
74
|
-
|
|
75
|
-
const { reset, clearError } = createFeatureStateHandlers({
|
|
76
|
-
setState,
|
|
77
|
-
initialState,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const selectImage = useCallback(async (): Promise<void> => {
|
|
81
|
-
try {
|
|
82
|
-
const uri = await config.onSelectImage();
|
|
83
|
-
if (uri) {
|
|
84
|
-
setState((prev) => ({
|
|
85
|
-
...prev,
|
|
86
|
-
imageUri: uri,
|
|
87
|
-
error: null,
|
|
88
|
-
processedUrl: null,
|
|
89
|
-
}));
|
|
90
|
-
}
|
|
91
|
-
} catch (err) {
|
|
92
|
-
const message = err instanceof Error ? err.message : "error.selectImage";
|
|
93
|
-
setState((prev) => ({ ...prev, error: message }));
|
|
94
|
-
config.onError?.(message);
|
|
95
|
-
}
|
|
96
|
-
}, [config]);
|
|
97
|
-
|
|
98
|
-
const setPrompt = useCallback((prompt: string) => {
|
|
99
|
-
setState((prev) => ({ ...prev, prompt }));
|
|
100
|
-
}, []);
|
|
101
|
-
|
|
102
|
-
const process = useCallback(async (): Promise<void> => {
|
|
103
|
-
if (!state.imageUri) {
|
|
104
|
-
const message = "error.noImage";
|
|
105
|
-
setState((prev) => ({ ...prev, error: message }));
|
|
106
|
-
config.onError?.(message);
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (config.requirePrompt && !state.prompt.trim()) {
|
|
111
|
-
const message = "error.noPrompt";
|
|
112
|
-
setState((prev) => ({ ...prev, error: message }));
|
|
113
|
-
config.onError?.(message);
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const result = await executeProcess<FeatureProcessResult>({
|
|
118
|
-
canProcess: () => {
|
|
119
|
-
if (!state.imageUri) return false;
|
|
120
|
-
if (config.requirePrompt) return !!state.prompt.trim();
|
|
121
|
-
return true;
|
|
122
|
-
},
|
|
123
|
-
setError: (error: string | null) => setState((prev) => ({ ...prev, error })),
|
|
124
|
-
setProcessing: (isProcessing: boolean) => setState((prev) => ({ ...prev, isProcessing })),
|
|
125
|
-
onError: config.onError,
|
|
126
|
-
processFn: () =>
|
|
127
|
-
config.processRequest({
|
|
128
|
-
imageUri: state.imageUri!,
|
|
129
|
-
prompt: state.prompt.trim(),
|
|
130
|
-
onProgress: (progress) => setState((prev) => ({ ...prev, progress })),
|
|
131
|
-
}),
|
|
132
|
-
onSuccess: (result: FeatureProcessResult) => {
|
|
133
|
-
if (result.outputUrl) {
|
|
134
|
-
setState((prev) => ({ ...prev, processedUrl: result.outputUrl ?? null }));
|
|
135
|
-
config.onSuccess?.(result.outputUrl);
|
|
136
|
-
} else {
|
|
137
|
-
const message = result.error || "error.processing";
|
|
138
|
-
setState((prev) => ({ ...prev, error: message }));
|
|
139
|
-
config.onError?.(message);
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
if (!result) {
|
|
145
|
-
setState((prev) => ({ ...prev, progress: 0 }));
|
|
146
|
-
}
|
|
147
|
-
}, [state.imageUri, state.prompt, config]);
|
|
148
|
-
|
|
149
|
-
const save = useCallback(async (): Promise<void> => {
|
|
150
|
-
await executeSave({
|
|
151
|
-
processedUrl: state.processedUrl,
|
|
152
|
-
onSave: config.onSave,
|
|
153
|
-
setError: (error) => setState((prev) => ({ ...prev, error })),
|
|
154
|
-
onError: config.onError,
|
|
155
|
-
});
|
|
156
|
-
}, [state.processedUrl, config]);
|
|
157
|
-
|
|
158
|
-
return {
|
|
159
|
-
...state,
|
|
160
|
-
selectImage,
|
|
161
|
-
setPrompt,
|
|
162
|
-
process,
|
|
163
|
-
save,
|
|
164
|
-
reset,
|
|
165
|
-
clearError,
|
|
166
|
-
};
|
|
167
|
-
}
|