@umituz/react-native-ai-generation-content 1.17.160 → 1.17.162
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/domains/creations/index.ts +7 -0
- package/src/domains/creations/presentation/hooks/index.ts +7 -0
- package/src/domains/creations/presentation/hooks/useCreationPersistence.ts +133 -0
- package/src/features/anime-selfie/domain/types/anime-selfie.types.ts +8 -2
- package/src/features/anime-selfie/domain/types/index.ts +1 -0
- package/src/features/anime-selfie/index.ts +1 -0
- package/src/features/anime-selfie/presentation/hooks/useAnimeSelfieFeature.ts +10 -5
package/package.json
CHANGED
|
@@ -116,6 +116,13 @@ export { useCreations } from "./presentation/hooks/useCreations";
|
|
|
116
116
|
export { useDeleteCreation } from "./presentation/hooks/useDeleteCreation";
|
|
117
117
|
export { useCreationsFilter } from "./presentation/hooks/useCreationsFilter";
|
|
118
118
|
export { useAdvancedFilter } from "./presentation/hooks/useAdvancedFilter";
|
|
119
|
+
export { useCreationPersistence } from "./presentation/hooks/useCreationPersistence";
|
|
120
|
+
export type {
|
|
121
|
+
UseCreationPersistenceConfig,
|
|
122
|
+
UseCreationPersistenceReturn,
|
|
123
|
+
ProcessingStartData,
|
|
124
|
+
ProcessingResult,
|
|
125
|
+
} from "./presentation/hooks/useCreationPersistence";
|
|
119
126
|
|
|
120
127
|
// =============================================================================
|
|
121
128
|
// PRESENTATION LAYER - Components
|
|
@@ -14,3 +14,10 @@ export type {
|
|
|
14
14
|
export { useFilter, useStatusFilter, useMediaFilter } from "./useFilter";
|
|
15
15
|
export type { UseFilterProps, UseFilterReturn } from "./useFilter";
|
|
16
16
|
export { useGalleryFilters } from "./useGalleryFilters";
|
|
17
|
+
export { useCreationPersistence } from "./useCreationPersistence";
|
|
18
|
+
export type {
|
|
19
|
+
UseCreationPersistenceConfig,
|
|
20
|
+
UseCreationPersistenceReturn,
|
|
21
|
+
ProcessingStartData,
|
|
22
|
+
ProcessingResult,
|
|
23
|
+
} from "./useCreationPersistence";
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useCreationPersistence Hook
|
|
3
|
+
* Encapsulates Firestore persistence logic for AI generation features
|
|
4
|
+
* Eliminates boilerplate code in feature screens
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { useCallback, useMemo } from "react";
|
|
8
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
9
|
+
import { useAuth } from "@umituz/react-native-auth";
|
|
10
|
+
import { createCreationsRepository } from "../../infrastructure/adapters";
|
|
11
|
+
import type { Creation } from "../../domain/entities/Creation";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Configuration for creation persistence
|
|
15
|
+
*/
|
|
16
|
+
export interface UseCreationPersistenceConfig {
|
|
17
|
+
/** Creation type identifier (e.g., "anime-selfie", "ai-kiss") */
|
|
18
|
+
readonly type: string;
|
|
19
|
+
/** Collection name in Firestore (defaults to "creations") */
|
|
20
|
+
readonly collectionName?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Generic processing start data - all features must have creationId
|
|
25
|
+
*/
|
|
26
|
+
export interface ProcessingStartData {
|
|
27
|
+
readonly creationId: string;
|
|
28
|
+
readonly [key: string]: unknown;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Generic processing result - all features should have creationId
|
|
33
|
+
*/
|
|
34
|
+
export interface ProcessingResult {
|
|
35
|
+
readonly creationId?: string;
|
|
36
|
+
readonly imageUrl?: string;
|
|
37
|
+
readonly videoUrl?: string;
|
|
38
|
+
readonly [key: string]: unknown;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Return type for useCreationPersistence
|
|
43
|
+
*/
|
|
44
|
+
export interface UseCreationPersistenceReturn {
|
|
45
|
+
readonly onProcessingStart: (data: ProcessingStartData) => void;
|
|
46
|
+
readonly onProcessingComplete: (result: ProcessingResult) => void;
|
|
47
|
+
readonly onError: (error: string, creationId?: string) => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Hook that provides Firestore persistence callbacks for AI features
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // In AnimeSelfieScreen:
|
|
55
|
+
* const persistence = useCreationPersistence({ type: "anime-selfie" });
|
|
56
|
+
* const config = useMemo(() => ({
|
|
57
|
+
* prepareImage,
|
|
58
|
+
* ...persistence,
|
|
59
|
+
* }), [persistence]);
|
|
60
|
+
*/
|
|
61
|
+
export function useCreationPersistence(
|
|
62
|
+
config: UseCreationPersistenceConfig,
|
|
63
|
+
): UseCreationPersistenceReturn {
|
|
64
|
+
const { type, collectionName = "creations" } = config;
|
|
65
|
+
const { userId } = useAuth();
|
|
66
|
+
const queryClient = useQueryClient();
|
|
67
|
+
|
|
68
|
+
const repository = useMemo(
|
|
69
|
+
() => createCreationsRepository(collectionName),
|
|
70
|
+
[collectionName],
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const onProcessingStart = useCallback(
|
|
74
|
+
(data: ProcessingStartData) => {
|
|
75
|
+
if (!userId) return;
|
|
76
|
+
|
|
77
|
+
const { creationId, ...rest } = data;
|
|
78
|
+
const creation: Creation = {
|
|
79
|
+
id: creationId,
|
|
80
|
+
uri: "",
|
|
81
|
+
type,
|
|
82
|
+
status: "processing",
|
|
83
|
+
createdAt: new Date(),
|
|
84
|
+
isShared: false,
|
|
85
|
+
isFavorite: false,
|
|
86
|
+
metadata: rest,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
repository.create(userId, creation);
|
|
90
|
+
queryClient.invalidateQueries({ queryKey: ["creations"] });
|
|
91
|
+
},
|
|
92
|
+
[userId, repository, queryClient, type],
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const onProcessingComplete = useCallback(
|
|
96
|
+
(result: ProcessingResult) => {
|
|
97
|
+
if (!userId || !result.creationId) return;
|
|
98
|
+
|
|
99
|
+
const uri = result.imageUrl || result.videoUrl || "";
|
|
100
|
+
const output = result.imageUrl
|
|
101
|
+
? { imageUrl: result.imageUrl }
|
|
102
|
+
: result.videoUrl
|
|
103
|
+
? { videoUrl: result.videoUrl }
|
|
104
|
+
: undefined;
|
|
105
|
+
|
|
106
|
+
repository.update(userId, result.creationId, {
|
|
107
|
+
uri,
|
|
108
|
+
status: "completed",
|
|
109
|
+
output,
|
|
110
|
+
});
|
|
111
|
+
queryClient.invalidateQueries({ queryKey: ["creations"] });
|
|
112
|
+
},
|
|
113
|
+
[userId, repository, queryClient],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const onError = useCallback(
|
|
117
|
+
(error: string, creationId?: string) => {
|
|
118
|
+
if (!userId || !creationId) return;
|
|
119
|
+
|
|
120
|
+
repository.update(userId, creationId, {
|
|
121
|
+
status: "failed",
|
|
122
|
+
metadata: { error },
|
|
123
|
+
});
|
|
124
|
+
queryClient.invalidateQueries({ queryKey: ["creations"] });
|
|
125
|
+
},
|
|
126
|
+
[userId, repository, queryClient],
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
return useMemo(
|
|
130
|
+
() => ({ onProcessingStart, onProcessingComplete, onError }),
|
|
131
|
+
[onProcessingStart, onProcessingComplete, onError],
|
|
132
|
+
);
|
|
133
|
+
}
|
|
@@ -24,6 +24,12 @@ export interface AnimeSelfieResult {
|
|
|
24
24
|
imageBase64?: string;
|
|
25
25
|
error?: string;
|
|
26
26
|
requestId?: string;
|
|
27
|
+
creationId?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface AnimeSelfieProcessingStartData {
|
|
31
|
+
creationId: string;
|
|
32
|
+
imageUri: string;
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
export interface AnimeSelfieFeatureState {
|
|
@@ -58,7 +64,7 @@ export interface AnimeSelfieFeatureConfig {
|
|
|
58
64
|
extractResult?: AnimeSelfieResultExtractor;
|
|
59
65
|
prepareImage: (imageUri: string) => Promise<string>;
|
|
60
66
|
onImageSelect?: (uri: string) => void;
|
|
61
|
-
onProcessingStart?: () => void;
|
|
67
|
+
onProcessingStart?: (data: AnimeSelfieProcessingStartData) => void;
|
|
62
68
|
onProcessingComplete?: (result: AnimeSelfieResult) => void;
|
|
63
|
-
onError?: (error: string) => void;
|
|
69
|
+
onError?: (error: string, creationId?: string) => void;
|
|
64
70
|
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* Manages anime selfie feature state and actions
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { useState, useCallback } from "react";
|
|
6
|
+
import { useState, useCallback, useRef } from "react";
|
|
7
|
+
import { generateUUID } from "@umituz/react-native-uuid";
|
|
7
8
|
import { executeImageFeature } from "../../../../infrastructure/services";
|
|
8
9
|
import type {
|
|
9
10
|
AnimeSelfieFeatureState,
|
|
@@ -38,6 +39,7 @@ export function useAnimeSelfieFeature(
|
|
|
38
39
|
): UseAnimeSelfieFeatureReturn {
|
|
39
40
|
const { config, onSelectImage, onSaveImage, onBeforeProcess } = props;
|
|
40
41
|
const [state, setState] = useState<AnimeSelfieFeatureState>(initialState);
|
|
42
|
+
const creationIdRef = useRef<string | null>(null);
|
|
41
43
|
|
|
42
44
|
const selectImage = useCallback(async () => {
|
|
43
45
|
try {
|
|
@@ -64,6 +66,9 @@ export function useAnimeSelfieFeature(
|
|
|
64
66
|
if (!canProceed) return;
|
|
65
67
|
}
|
|
66
68
|
|
|
69
|
+
const creationId = generateUUID();
|
|
70
|
+
creationIdRef.current = creationId;
|
|
71
|
+
|
|
67
72
|
setState((prev) => ({
|
|
68
73
|
...prev,
|
|
69
74
|
isProcessing: true,
|
|
@@ -71,7 +76,7 @@ export function useAnimeSelfieFeature(
|
|
|
71
76
|
error: null,
|
|
72
77
|
}));
|
|
73
78
|
|
|
74
|
-
config.onProcessingStart?.();
|
|
79
|
+
config.onProcessingStart?.({ creationId, imageUri: state.imageUri });
|
|
75
80
|
|
|
76
81
|
try {
|
|
77
82
|
const imageBase64 = await config.prepareImage(state.imageUri);
|
|
@@ -89,7 +94,7 @@ export function useAnimeSelfieFeature(
|
|
|
89
94
|
processedUrl: result.imageUrl!,
|
|
90
95
|
progress: 100,
|
|
91
96
|
}));
|
|
92
|
-
config.onProcessingComplete?.(result as AnimeSelfieResult);
|
|
97
|
+
config.onProcessingComplete?.({ ...result, creationId } as AnimeSelfieResult);
|
|
93
98
|
} else {
|
|
94
99
|
const errorMessage = result.error || "Processing failed";
|
|
95
100
|
setState((prev) => ({
|
|
@@ -98,7 +103,7 @@ export function useAnimeSelfieFeature(
|
|
|
98
103
|
error: errorMessage,
|
|
99
104
|
progress: 0,
|
|
100
105
|
}));
|
|
101
|
-
config.onError?.(errorMessage);
|
|
106
|
+
config.onError?.(errorMessage, creationId);
|
|
102
107
|
}
|
|
103
108
|
} catch (error) {
|
|
104
109
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
@@ -108,7 +113,7 @@ export function useAnimeSelfieFeature(
|
|
|
108
113
|
error: errorMessage,
|
|
109
114
|
progress: 0,
|
|
110
115
|
}));
|
|
111
|
-
config.onError?.(errorMessage);
|
|
116
|
+
config.onError?.(errorMessage, creationIdRef.current ?? undefined);
|
|
112
117
|
}
|
|
113
118
|
}, [state.imageUri, config, handleProgress, onBeforeProcess]);
|
|
114
119
|
|