@umituz/react-native-ai-feature-background 1.0.0
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/README.md +152 -0
- package/package.json +50 -0
- package/src/domain/entities/background.types.ts +47 -0
- package/src/domain/entities/component.types.ts +76 -0
- package/src/domain/entities/config.types.ts +31 -0
- package/src/domain/entities/index.ts +27 -0
- package/src/index.ts +65 -0
- package/src/infrastructure/constants/index.ts +5 -0
- package/src/infrastructure/constants/prompts.constants.ts +15 -0
- package/src/presentation/components/BackgroundFeature.tsx +145 -0
- package/src/presentation/components/ErrorDisplay.tsx +58 -0
- package/src/presentation/components/FeatureHeader.tsx +80 -0
- package/src/presentation/components/GenerateButton.tsx +86 -0
- package/src/presentation/components/ImagePicker.tsx +136 -0
- package/src/presentation/components/ProcessingModal.tsx +113 -0
- package/src/presentation/components/PromptInput.tsx +142 -0
- package/src/presentation/components/ResultDisplay.tsx +123 -0
- package/src/presentation/components/index.ts +14 -0
- package/src/presentation/hooks/index.ts +7 -0
- package/src/presentation/hooks/useBackgroundFeature.ts +111 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useBackgroundFeature Hook
|
|
3
|
+
* @description Main hook for background feature state and actions
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { useCallback, useState } from "react";
|
|
7
|
+
import type {
|
|
8
|
+
BackgroundFeatureState,
|
|
9
|
+
BackgroundProcessResult,
|
|
10
|
+
UseBackgroundFeatureConfig,
|
|
11
|
+
} from "../../domain/entities";
|
|
12
|
+
|
|
13
|
+
export interface UseBackgroundFeatureReturn extends BackgroundFeatureState {
|
|
14
|
+
readonly selectImage: () => Promise<void>;
|
|
15
|
+
readonly process: (prompt?: string) => Promise<void>;
|
|
16
|
+
readonly save: () => Promise<void>;
|
|
17
|
+
readonly reset: () => void;
|
|
18
|
+
readonly setPrompt: (prompt: string) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function useBackgroundFeature(
|
|
22
|
+
config: UseBackgroundFeatureConfig
|
|
23
|
+
): UseBackgroundFeatureReturn {
|
|
24
|
+
const [imageUri, setImageUri] = useState<string | null>(null);
|
|
25
|
+
const [prompt, setPrompt] = useState<string>("");
|
|
26
|
+
const [processedUrl, setProcessedUrl] = useState<string | null>(null);
|
|
27
|
+
const [isProcessing, setIsProcessing] = useState(false);
|
|
28
|
+
const [progress, setProgress] = useState(0);
|
|
29
|
+
const [error, setError] = useState<string | null>(null);
|
|
30
|
+
|
|
31
|
+
const selectImage = useCallback(async (): Promise<void> => {
|
|
32
|
+
if (!config.onSelectImage) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
const uri = await config.onSelectImage();
|
|
38
|
+
if (uri) {
|
|
39
|
+
setImageUri(uri);
|
|
40
|
+
setError(null);
|
|
41
|
+
setProcessedUrl(null);
|
|
42
|
+
}
|
|
43
|
+
} catch (err) {
|
|
44
|
+
setError(err instanceof Error ? err.message : "Failed to select image");
|
|
45
|
+
}
|
|
46
|
+
}, [config]);
|
|
47
|
+
|
|
48
|
+
const process = useCallback(
|
|
49
|
+
async (newPrompt?: string): Promise<void> => {
|
|
50
|
+
if (!imageUri) {
|
|
51
|
+
setError("Please select an image first");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const currentPrompt = newPrompt ?? prompt;
|
|
56
|
+
setIsProcessing(true);
|
|
57
|
+
setProgress(0);
|
|
58
|
+
setError(null);
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const result: BackgroundProcessResult = await config.processRequest({
|
|
62
|
+
imageUri,
|
|
63
|
+
prompt: currentPrompt,
|
|
64
|
+
onProgress: setProgress,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (result.success && result.imageUrl) {
|
|
68
|
+
setProcessedUrl(result.imageUrl);
|
|
69
|
+
} else {
|
|
70
|
+
setError(result.error || "Processing failed");
|
|
71
|
+
}
|
|
72
|
+
} catch (err) {
|
|
73
|
+
setError(err instanceof Error ? err.message : "Processing failed");
|
|
74
|
+
} finally {
|
|
75
|
+
setIsProcessing(false);
|
|
76
|
+
setProgress(0);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
[imageUri, prompt, config]
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const save = useCallback(async (): Promise<void> => {
|
|
83
|
+
if (!processedUrl) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
// Save logic is handled by parent component via config
|
|
87
|
+
}, [processedUrl]);
|
|
88
|
+
|
|
89
|
+
const reset = useCallback((): void => {
|
|
90
|
+
setImageUri(null);
|
|
91
|
+
setPrompt("");
|
|
92
|
+
setProcessedUrl(null);
|
|
93
|
+
setIsProcessing(false);
|
|
94
|
+
setProgress(0);
|
|
95
|
+
setError(null);
|
|
96
|
+
}, []);
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
imageUri,
|
|
100
|
+
prompt,
|
|
101
|
+
processedUrl,
|
|
102
|
+
isProcessing,
|
|
103
|
+
progress,
|
|
104
|
+
error,
|
|
105
|
+
selectImage,
|
|
106
|
+
process,
|
|
107
|
+
save,
|
|
108
|
+
reset,
|
|
109
|
+
setPrompt,
|
|
110
|
+
};
|
|
111
|
+
}
|