@umituz/react-native-ai-generation-content 1.17.16 → 1.17.17

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.
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Text-to-Image Form State Hook
3
+ * Manages form state for text-to-image generation
4
+ */
5
+
6
+ import { useState, useCallback, useMemo } from "react";
7
+ import type {
8
+ AspectRatio,
9
+ ImageSize,
10
+ NumImages,
11
+ OutputFormat,
12
+ TextToImageFormState,
13
+ TextToImageFormActions,
14
+ TextToImageFormDefaults,
15
+ } from "../../domain/types/form.types";
16
+ import { DEFAULT_FORM_VALUES } from "../../domain/constants/options.constants";
17
+
18
+ export interface UseFormStateOptions {
19
+ defaults?: TextToImageFormDefaults;
20
+ }
21
+
22
+ export interface UseFormStateReturn {
23
+ state: TextToImageFormState;
24
+ actions: TextToImageFormActions;
25
+ }
26
+
27
+ export function useFormState(options?: UseFormStateOptions): UseFormStateReturn {
28
+ const defaults = useMemo(
29
+ () => ({ ...DEFAULT_FORM_VALUES, ...options?.defaults }),
30
+ [options?.defaults]
31
+ );
32
+
33
+ const [prompt, setPrompt] = useState("");
34
+ const [aspectRatio, setAspectRatio] = useState<AspectRatio>(
35
+ defaults.aspectRatio ?? "9:16"
36
+ );
37
+ const [size, setSize] = useState<ImageSize>(defaults.size ?? "512x512");
38
+ const [numImages, setNumImages] = useState<NumImages>(defaults.numImages ?? 1);
39
+ const [negativePrompt, setNegativePrompt] = useState("");
40
+ const [guidanceScale, setGuidanceScale] = useState(defaults.guidanceScale ?? 7.5);
41
+ const [selectedModel, setSelectedModel] = useState<string | null>(null);
42
+ const [outputFormat, setOutputFormat] = useState<OutputFormat>(
43
+ defaults.outputFormat ?? "png"
44
+ );
45
+ const [selectedStyle, setSelectedStyle] = useState(
46
+ defaults.selectedStyle ?? "realistic"
47
+ );
48
+
49
+ const reset = useCallback(() => {
50
+ setPrompt("");
51
+ setAspectRatio(defaults.aspectRatio ?? "9:16");
52
+ setSize(defaults.size ?? "512x512");
53
+ setNumImages(defaults.numImages ?? 1);
54
+ setNegativePrompt("");
55
+ setGuidanceScale(defaults.guidanceScale ?? 7.5);
56
+ setSelectedModel(null);
57
+ setOutputFormat(defaults.outputFormat ?? "png");
58
+ setSelectedStyle(defaults.selectedStyle ?? "realistic");
59
+ }, [defaults]);
60
+
61
+ const state: TextToImageFormState = useMemo(
62
+ () => ({
63
+ prompt,
64
+ aspectRatio,
65
+ size,
66
+ numImages,
67
+ negativePrompt,
68
+ guidanceScale,
69
+ selectedModel,
70
+ outputFormat,
71
+ selectedStyle,
72
+ }),
73
+ [
74
+ prompt,
75
+ aspectRatio,
76
+ size,
77
+ numImages,
78
+ negativePrompt,
79
+ guidanceScale,
80
+ selectedModel,
81
+ outputFormat,
82
+ selectedStyle,
83
+ ]
84
+ );
85
+
86
+ const actions: TextToImageFormActions = useMemo(
87
+ () => ({
88
+ setPrompt,
89
+ setAspectRatio,
90
+ setSize,
91
+ setNumImages,
92
+ setNegativePrompt,
93
+ setGuidanceScale,
94
+ setSelectedModel,
95
+ setOutputFormat,
96
+ setSelectedStyle,
97
+ reset,
98
+ }),
99
+ [reset]
100
+ );
101
+
102
+ return { state, actions };
103
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Text-to-Image Generation Hook
3
+ * Orchestrates generation with app-provided callbacks
4
+ */
5
+
6
+ import { useState, useCallback } from "react";
7
+ import type {
8
+ TextToImageFormState,
9
+ TextToImageCallbacks,
10
+ GenerationResult,
11
+ GenerationRequest,
12
+ } from "../../domain/types";
13
+
14
+ export interface GenerationState {
15
+ isGenerating: boolean;
16
+ progress: number;
17
+ error: string | null;
18
+ }
19
+
20
+ export interface UseGenerationOptions {
21
+ formState: TextToImageFormState;
22
+ callbacks: TextToImageCallbacks;
23
+ onPromptCleared?: () => void;
24
+ }
25
+
26
+ export interface UseGenerationReturn {
27
+ generationState: GenerationState;
28
+ totalCost: number;
29
+ handleGenerate: () => Promise<GenerationResult | null>;
30
+ }
31
+
32
+ const initialState: GenerationState = {
33
+ isGenerating: false,
34
+ progress: 0,
35
+ error: null,
36
+ };
37
+
38
+ export function useGeneration(options: UseGenerationOptions): UseGenerationReturn {
39
+ const { formState, callbacks, onPromptCleared } = options;
40
+ const [generationState, setGenerationState] = useState<GenerationState>(initialState);
41
+
42
+ const totalCost = callbacks.calculateCost(formState.numImages, formState.selectedModel);
43
+
44
+ const handleGenerate = useCallback(async (): Promise<GenerationResult | null> => {
45
+ const trimmedPrompt = formState.prompt.trim();
46
+
47
+ if (!trimmedPrompt) {
48
+ setGenerationState((prev) => ({ ...prev, error: "Prompt is required" }));
49
+ return null;
50
+ }
51
+
52
+ if (!callbacks.isAuthenticated()) {
53
+ callbacks.onAuthRequired?.();
54
+ return null;
55
+ }
56
+
57
+ if (!callbacks.canAfford(totalCost)) {
58
+ callbacks.onCreditsRequired?.(totalCost);
59
+ return null;
60
+ }
61
+
62
+ setGenerationState({ isGenerating: true, progress: 0, error: null });
63
+
64
+ const request: GenerationRequest = {
65
+ prompt: trimmedPrompt,
66
+ model: formState.selectedModel ?? undefined,
67
+ aspectRatio: formState.aspectRatio,
68
+ size: formState.size,
69
+ negativePrompt: formState.negativePrompt.trim() || undefined,
70
+ guidanceScale: formState.guidanceScale,
71
+ numImages: formState.numImages,
72
+ style: formState.selectedStyle,
73
+ outputFormat: formState.outputFormat,
74
+ };
75
+
76
+ try {
77
+ const result = await callbacks.executeGeneration(request);
78
+
79
+ if (result.success) {
80
+ callbacks.onSuccess?.(result.imageUrls);
81
+ onPromptCleared?.();
82
+ } else {
83
+ const errorMessage = result.error;
84
+ setGenerationState((prev) => ({ ...prev, error: errorMessage }));
85
+ callbacks.onError?.(errorMessage);
86
+ }
87
+
88
+ setGenerationState({ isGenerating: false, progress: 100, error: null });
89
+ return result;
90
+ } catch (error) {
91
+ const message = error instanceof Error ? error.message : String(error);
92
+ setGenerationState({ isGenerating: false, progress: 0, error: message });
93
+ callbacks.onError?.(message);
94
+ return null;
95
+ }
96
+ }, [formState, callbacks, totalCost, onPromptCleared]);
97
+
98
+ return { generationState, totalCost, handleGenerate };
99
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Text-to-Image Form Hook
3
+ * Composes form state and generation for complete form management
4
+ */
5
+
6
+ import { useMemo } from "react";
7
+ import { useFormState, type UseFormStateOptions } from "./useFormState";
8
+ import { useGeneration, type GenerationState } from "./useGeneration";
9
+ import type {
10
+ TextToImageFormState,
11
+ TextToImageFormActions,
12
+ TextToImageCallbacks,
13
+ } from "../../domain/types";
14
+
15
+ export interface UseTextToImageFormOptions extends UseFormStateOptions {
16
+ callbacks: TextToImageCallbacks;
17
+ }
18
+
19
+ export interface UseTextToImageFormReturn {
20
+ state: TextToImageFormState;
21
+ actions: TextToImageFormActions;
22
+ generationState: GenerationState;
23
+ totalCost: number;
24
+ handleGenerate: () => Promise<void>;
25
+ isReady: boolean;
26
+ }
27
+
28
+ export function useTextToImageForm(
29
+ options: UseTextToImageFormOptions
30
+ ): UseTextToImageFormReturn {
31
+ const { callbacks, defaults } = options;
32
+
33
+ const { state, actions } = useFormState({ defaults });
34
+
35
+ const { generationState, totalCost, handleGenerate } = useGeneration({
36
+ formState: state,
37
+ callbacks,
38
+ onPromptCleared: actions.reset,
39
+ });
40
+
41
+ const isReady = useMemo(
42
+ () => state.prompt.trim().length > 0 && !generationState.isGenerating,
43
+ [state.prompt, generationState.isGenerating]
44
+ );
45
+
46
+ const wrappedHandleGenerate = async () => {
47
+ await handleGenerate();
48
+ };
49
+
50
+ return {
51
+ state,
52
+ actions,
53
+ generationState,
54
+ totalCost,
55
+ handleGenerate: wrappedHandleGenerate,
56
+ isReady,
57
+ };
58
+ }
@@ -1 +1,7 @@
1
+ /**
2
+ * Text-to-Image Presentation Layer
3
+ * Hooks and components exports
4
+ */
5
+
1
6
  export * from "./hooks";
7
+ export * from "./components";