@umituz/react-native-ai-generation-content 1.17.161 → 1.17.163

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/react-native-ai-generation-content",
3
- "version": "1.17.161",
3
+ "version": "1.17.163",
4
4
  "description": "Provider-agnostic AI generation orchestration for React Native",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -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
+ BaseProcessingStartData,
124
+ BaseProcessingResult,
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
+ BaseProcessingStartData,
22
+ BaseProcessingResult,
23
+ } from "./useCreationPersistence";
@@ -0,0 +1,131 @@
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
+ * Base processing start data - all features must have creationId
25
+ */
26
+ export interface BaseProcessingStartData {
27
+ readonly creationId: string;
28
+ }
29
+
30
+ /**
31
+ * Base processing result - all features should have creationId
32
+ */
33
+ export interface BaseProcessingResult {
34
+ readonly creationId?: string;
35
+ readonly imageUrl?: string;
36
+ readonly videoUrl?: string;
37
+ }
38
+
39
+ /**
40
+ * Return type for useCreationPersistence - uses generic callbacks
41
+ */
42
+ export interface UseCreationPersistenceReturn {
43
+ readonly onProcessingStart: <T extends BaseProcessingStartData>(data: T) => void;
44
+ readonly onProcessingComplete: <T extends BaseProcessingResult>(result: T) => void;
45
+ readonly onError: (error: string, creationId?: string) => void;
46
+ }
47
+
48
+ /**
49
+ * Hook that provides Firestore persistence callbacks for AI features
50
+ *
51
+ * @example
52
+ * // In AnimeSelfieScreen:
53
+ * const persistence = useCreationPersistence({ type: "anime-selfie" });
54
+ * const config = useMemo(() => ({
55
+ * prepareImage,
56
+ * ...persistence,
57
+ * }), [persistence]);
58
+ */
59
+ export function useCreationPersistence(
60
+ config: UseCreationPersistenceConfig,
61
+ ): UseCreationPersistenceReturn {
62
+ const { type, collectionName = "creations" } = config;
63
+ const { userId } = useAuth();
64
+ const queryClient = useQueryClient();
65
+
66
+ const repository = useMemo(
67
+ () => createCreationsRepository(collectionName),
68
+ [collectionName],
69
+ );
70
+
71
+ const onProcessingStart = useCallback(
72
+ <T extends BaseProcessingStartData>(data: T) => {
73
+ if (!userId) return;
74
+
75
+ const { creationId, ...rest } = data;
76
+ const creation: Creation = {
77
+ id: creationId,
78
+ uri: "",
79
+ type,
80
+ status: "processing",
81
+ createdAt: new Date(),
82
+ isShared: false,
83
+ isFavorite: false,
84
+ metadata: rest as Record<string, unknown>,
85
+ };
86
+
87
+ repository.create(userId, creation);
88
+ queryClient.invalidateQueries({ queryKey: ["creations"] });
89
+ },
90
+ [userId, repository, queryClient, type],
91
+ );
92
+
93
+ const onProcessingComplete = useCallback(
94
+ <T extends BaseProcessingResult>(result: T) => {
95
+ if (!userId || !result.creationId) return;
96
+
97
+ const uri = result.imageUrl || result.videoUrl || "";
98
+ const output = result.imageUrl
99
+ ? { imageUrl: result.imageUrl }
100
+ : result.videoUrl
101
+ ? { videoUrl: result.videoUrl }
102
+ : undefined;
103
+
104
+ repository.update(userId, result.creationId, {
105
+ uri,
106
+ status: "completed",
107
+ output,
108
+ });
109
+ queryClient.invalidateQueries({ queryKey: ["creations"] });
110
+ },
111
+ [userId, repository, queryClient],
112
+ );
113
+
114
+ const onError = useCallback(
115
+ (error: string, creationId?: string) => {
116
+ if (!userId || !creationId) return;
117
+
118
+ repository.update(userId, creationId, {
119
+ status: "failed",
120
+ metadata: { error },
121
+ });
122
+ queryClient.invalidateQueries({ queryKey: ["creations"] });
123
+ },
124
+ [userId, repository, queryClient],
125
+ );
126
+
127
+ return useMemo(
128
+ () => ({ onProcessingStart, onProcessingComplete, onError }),
129
+ [onProcessingStart, onProcessingComplete, onError],
130
+ );
131
+ }