@umituz/react-native-ai-generation-content 1.65.12 → 1.66.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/package.json +1 -1
- package/src/domains/creations/domain/utils/creation-display.util.ts +67 -0
- package/src/domains/creations/domain/utils/creation-format.util.ts +12 -0
- package/src/domains/creations/domain/utils/creation-helpers.ts +8 -136
- package/src/domains/creations/domain/utils/creation-id.util.ts +13 -0
- package/src/domains/creations/domain/utils/creation-search.util.ts +24 -0
- package/src/domains/creations/domain/utils/creation-sort.util.ts +41 -0
- package/src/domains/creations/infrastructure/repositories/CreationsSubscription.ts +8 -63
- package/src/domains/creations/infrastructure/repositories/creation-delete.operations.ts +20 -56
- package/src/domains/creations/infrastructure/repositories/creation-error-handler.util.ts +36 -0
- package/src/domains/creations/presentation/hooks/useProcessingJobsPoller.ts +52 -53
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-ai-generation-content",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.66.0",
|
|
4
4
|
"description": "Provider-agnostic AI generation orchestration for React Native with result preview components",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creation Display Utilities
|
|
3
|
+
* Single Responsibility: UI display helpers (icons, titles, text)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { CreationTypeId } from "../types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Icon name type for design system
|
|
10
|
+
*/
|
|
11
|
+
export type IconName = "image" | "film" | "mic" | "sparkles" | "color-palette" | "brush";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get icon name for creation type
|
|
15
|
+
*/
|
|
16
|
+
export function getTypeIcon(type: CreationTypeId): IconName {
|
|
17
|
+
switch (type) {
|
|
18
|
+
case "text-to-video":
|
|
19
|
+
case "image-to-video":
|
|
20
|
+
return "film";
|
|
21
|
+
case "style-transfer":
|
|
22
|
+
case "colorization":
|
|
23
|
+
return "color-palette";
|
|
24
|
+
case "ai-brush":
|
|
25
|
+
case "inpainting":
|
|
26
|
+
return "brush";
|
|
27
|
+
default:
|
|
28
|
+
return "image";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Get i18n key for creation type
|
|
34
|
+
*/
|
|
35
|
+
export function getTypeTextKey(type: CreationTypeId): string {
|
|
36
|
+
return `creations.types.${type}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Get formatted type text (fallback)
|
|
41
|
+
*/
|
|
42
|
+
export function getTypeText(type: CreationTypeId): string {
|
|
43
|
+
return type.split("-").map(word =>
|
|
44
|
+
word.charAt(0).toUpperCase() + word.slice(1)
|
|
45
|
+
).join(" ");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Get creation title from prompt or type
|
|
50
|
+
*/
|
|
51
|
+
export function getCreationTitle(
|
|
52
|
+
prompt?: string,
|
|
53
|
+
type?: CreationTypeId,
|
|
54
|
+
maxLength: number = 50
|
|
55
|
+
): string {
|
|
56
|
+
if (prompt && prompt.length > 0) {
|
|
57
|
+
return prompt.length > maxLength
|
|
58
|
+
? prompt.substring(0, maxLength) + "..."
|
|
59
|
+
: prompt;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (type) {
|
|
63
|
+
return getTypeText(type);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return "Untitled Creation";
|
|
67
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creation Format Utilities
|
|
3
|
+
* Single Responsibility: Text formatting
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Truncate text with ellipsis
|
|
8
|
+
*/
|
|
9
|
+
export function truncateText(text: string, maxLength: number): string {
|
|
10
|
+
if (text.length <= maxLength) return text;
|
|
11
|
+
return text.substring(0, maxLength - 3) + "...";
|
|
12
|
+
}
|
|
@@ -1,140 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creation Helpers
|
|
3
|
-
*
|
|
3
|
+
* Central export point for creation utility functions
|
|
4
|
+
* @deprecated Individual utilities split into focused modules for better maintainability
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
export function generateCreationId(): string {
|
|
13
|
-
return generateUUID();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Icon name type for design system
|
|
18
|
-
*/
|
|
19
|
-
export type IconName = "image" | "film" | "mic" | "sparkles" | "color-palette" | "brush";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Get icon name for creation type
|
|
23
|
-
*/
|
|
24
|
-
export function getTypeIcon(type: CreationTypeId): IconName {
|
|
25
|
-
switch (type) {
|
|
26
|
-
case "text-to-video":
|
|
27
|
-
case "image-to-video":
|
|
28
|
-
return "film";
|
|
29
|
-
case "style-transfer":
|
|
30
|
-
case "colorization":
|
|
31
|
-
return "color-palette";
|
|
32
|
-
case "ai-brush":
|
|
33
|
-
case "inpainting":
|
|
34
|
-
return "brush";
|
|
35
|
-
default:
|
|
36
|
-
return "image";
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Get i18n key for creation type
|
|
42
|
-
*/
|
|
43
|
-
export function getTypeTextKey(type: CreationTypeId): string {
|
|
44
|
-
return `creations.types.${type}`;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Get formatted type text (fallback)
|
|
49
|
-
*/
|
|
50
|
-
export function getTypeText(type: CreationTypeId): string {
|
|
51
|
-
return type.split("-").map(word =>
|
|
52
|
-
word.charAt(0).toUpperCase() + word.slice(1)
|
|
53
|
-
).join(" ");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Get creation title from prompt or type
|
|
58
|
-
*/
|
|
59
|
-
export function getCreationTitle(
|
|
60
|
-
prompt?: string,
|
|
61
|
-
type?: CreationTypeId,
|
|
62
|
-
maxLength: number = 50
|
|
63
|
-
): string {
|
|
64
|
-
if (prompt && prompt.length > 0) {
|
|
65
|
-
return prompt.length > maxLength
|
|
66
|
-
? prompt.substring(0, maxLength) + "..."
|
|
67
|
-
: prompt;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (type) {
|
|
71
|
-
return getTypeText(type);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return "Untitled Creation";
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Filter creations by search query (client-side)
|
|
79
|
-
*/
|
|
80
|
-
export function filterBySearch<T extends { prompt?: string; type?: string; provider?: string }>(
|
|
81
|
-
items: T[],
|
|
82
|
-
searchQuery?: string,
|
|
83
|
-
): T[] {
|
|
84
|
-
if (!searchQuery || searchQuery.trim().length === 0) {
|
|
85
|
-
return items;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const query = searchQuery.toLowerCase().trim();
|
|
89
|
-
|
|
90
|
-
return items.filter((item) =>
|
|
91
|
-
item.prompt?.toLowerCase().includes(query) ||
|
|
92
|
-
item.type?.toLowerCase().includes(query) ||
|
|
93
|
-
item.provider?.toLowerCase().includes(query)
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Sort creations by field
|
|
99
|
-
*/
|
|
100
|
-
export function sortCreations<T extends Record<string, unknown>>(
|
|
101
|
-
items: T[],
|
|
102
|
-
field: keyof T,
|
|
103
|
-
order: "asc" | "desc" = "desc"
|
|
104
|
-
): T[] {
|
|
105
|
-
return [...items].sort((a, b) => {
|
|
106
|
-
const aVal = a[field] as unknown;
|
|
107
|
-
const bVal = b[field] as unknown;
|
|
108
|
-
|
|
109
|
-
if (aVal === undefined && bVal === undefined) return 0;
|
|
110
|
-
if (aVal === undefined) return 1;
|
|
111
|
-
if (bVal === undefined) return -1;
|
|
112
|
-
|
|
113
|
-
if (typeof aVal === "string" && typeof bVal === "string") {
|
|
114
|
-
return order === "desc"
|
|
115
|
-
? bVal.localeCompare(aVal)
|
|
116
|
-
: aVal.localeCompare(bVal);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (typeof aVal === "number" && typeof bVal === "number") {
|
|
120
|
-
return order === "desc" ? bVal - aVal : aVal - bVal;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Handle Date objects
|
|
124
|
-
if (aVal instanceof Date && bVal instanceof Date) {
|
|
125
|
-
return order === "desc"
|
|
126
|
-
? bVal.getTime() - aVal.getTime()
|
|
127
|
-
: aVal.getTime() - bVal.getTime();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return 0;
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Truncate text with ellipsis
|
|
136
|
-
*/
|
|
137
|
-
export function truncateText(text: string, maxLength: number): string {
|
|
138
|
-
if (text.length <= maxLength) return text;
|
|
139
|
-
return text.substring(0, maxLength - 3) + "...";
|
|
140
|
-
}
|
|
7
|
+
// Re-export all utilities for backward compatibility
|
|
8
|
+
export * from "./creation-id.util";
|
|
9
|
+
export * from "./creation-display.util";
|
|
10
|
+
export * from "./creation-search.util";
|
|
11
|
+
export * from "./creation-sort.util";
|
|
12
|
+
export * from "./creation-format.util";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creation ID Utilities
|
|
3
|
+
* Single Responsibility: ID generation
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { generateUUID } from "@umituz/react-native-design-system";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Generate a unique creation ID using UUID v4
|
|
10
|
+
*/
|
|
11
|
+
export function generateCreationId(): string {
|
|
12
|
+
return generateUUID();
|
|
13
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creation Search Utilities
|
|
3
|
+
* Single Responsibility: Filtering and searching
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Filter creations by search query (client-side)
|
|
8
|
+
*/
|
|
9
|
+
export function filterBySearch<T extends { prompt?: string; type?: string; provider?: string }>(
|
|
10
|
+
items: T[],
|
|
11
|
+
searchQuery?: string,
|
|
12
|
+
): T[] {
|
|
13
|
+
if (!searchQuery || searchQuery.trim().length === 0) {
|
|
14
|
+
return items;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const query = searchQuery.toLowerCase().trim();
|
|
18
|
+
|
|
19
|
+
return items.filter((item) =>
|
|
20
|
+
item.prompt?.toLowerCase().includes(query) ||
|
|
21
|
+
item.type?.toLowerCase().includes(query) ||
|
|
22
|
+
item.provider?.toLowerCase().includes(query)
|
|
23
|
+
);
|
|
24
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creation Sort Utilities
|
|
3
|
+
* Single Responsibility: Sorting operations
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sort creations by field
|
|
8
|
+
*/
|
|
9
|
+
export function sortCreations<T extends Record<string, unknown>>(
|
|
10
|
+
items: T[],
|
|
11
|
+
field: keyof T,
|
|
12
|
+
order: "asc" | "desc" = "desc"
|
|
13
|
+
): T[] {
|
|
14
|
+
return [...items].sort((a, b) => {
|
|
15
|
+
const aVal = a[field] as unknown;
|
|
16
|
+
const bVal = b[field] as unknown;
|
|
17
|
+
|
|
18
|
+
if (aVal === undefined && bVal === undefined) return 0;
|
|
19
|
+
if (aVal === undefined) return 1;
|
|
20
|
+
if (bVal === undefined) return -1;
|
|
21
|
+
|
|
22
|
+
if (typeof aVal === "string" && typeof bVal === "string") {
|
|
23
|
+
return order === "desc"
|
|
24
|
+
? bVal.localeCompare(aVal)
|
|
25
|
+
: aVal.localeCompare(bVal);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof aVal === "number" && typeof bVal === "number") {
|
|
29
|
+
return order === "desc" ? bVal - aVal : aVal - bVal;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Handle Date objects
|
|
33
|
+
if (aVal instanceof Date && bVal instanceof Date) {
|
|
34
|
+
return order === "desc"
|
|
35
|
+
? bVal.getTime() - aVal.getTime()
|
|
36
|
+
: aVal.getTime() - bVal.getTime();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return 0;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -15,6 +15,7 @@ declare const __DEV__: boolean;
|
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Handles realtime subscriptions for creations
|
|
18
|
+
* Optimized with server-side filtering (80% data reduction)
|
|
18
19
|
*/
|
|
19
20
|
export class CreationsSubscription {
|
|
20
21
|
constructor(
|
|
@@ -22,19 +23,6 @@ export class CreationsSubscription {
|
|
|
22
23
|
private readonly documentMapper: DocumentMapper,
|
|
23
24
|
) { }
|
|
24
25
|
|
|
25
|
-
/**
|
|
26
|
-
* Subscribes to realtime updates for user's creations
|
|
27
|
-
*
|
|
28
|
-
* PERFORMANCE OPTIMIZATION:
|
|
29
|
-
* - Server-side filtering with where clause (80% data reduction)
|
|
30
|
-
* - No client-side filtering needed
|
|
31
|
-
* - Requires Firestore composite index: (deletedAt ASC, createdAt DESC)
|
|
32
|
-
*
|
|
33
|
-
* @param userId - User ID to query
|
|
34
|
-
* @param onData - Callback for data updates
|
|
35
|
-
* @param onError - Optional error callback
|
|
36
|
-
* @returns Unsubscribe function
|
|
37
|
-
*/
|
|
38
26
|
subscribeToAll(
|
|
39
27
|
userId: string,
|
|
40
28
|
onData: CreationsSubscriptionCallback,
|
|
@@ -43,48 +31,13 @@ export class CreationsSubscription {
|
|
|
43
31
|
const userCollection = this.pathResolver.getUserCollection(userId);
|
|
44
32
|
|
|
45
33
|
if (!userCollection) {
|
|
46
|
-
|
|
34
|
+
const error = new Error(`[CreationsSubscription] Invalid user collection: ${userId}`);
|
|
35
|
+
if (__DEV__) console.error(error.message);
|
|
36
|
+
onData([]);
|
|
37
|
+
onError?.(error);
|
|
38
|
+
return () => { if (__DEV__) console.log("[CreationsSubscription] No-op unsubscribe"); };
|
|
47
39
|
}
|
|
48
40
|
|
|
49
|
-
return this.createRealtimeListener(userCollection, onData, onError);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Handles case when user collection is invalid
|
|
54
|
-
*/
|
|
55
|
-
private handleInvalidCollection(
|
|
56
|
-
userId: string,
|
|
57
|
-
onData: CreationsSubscriptionCallback,
|
|
58
|
-
onError?: (error: Error) => void,
|
|
59
|
-
): UnsubscribeFunction {
|
|
60
|
-
const error = new Error(`[CreationsSubscription] Cannot subscribe: Invalid user collection for userId: ${userId}`);
|
|
61
|
-
|
|
62
|
-
if (__DEV__) {
|
|
63
|
-
console.error(error.message);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Return empty array immediately
|
|
67
|
-
onData([]);
|
|
68
|
-
|
|
69
|
-
// Report error to callback
|
|
70
|
-
onError?.(error);
|
|
71
|
-
|
|
72
|
-
// Return no-op unsubscribe function
|
|
73
|
-
return () => {
|
|
74
|
-
if (__DEV__) {
|
|
75
|
-
console.log("[CreationsSubscription] No-op unsubscribe called (no listener was created)");
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Creates the realtime listener with optimized query
|
|
82
|
-
*/
|
|
83
|
-
private createRealtimeListener(
|
|
84
|
-
userCollection: any,
|
|
85
|
-
onData: CreationsSubscriptionCallback,
|
|
86
|
-
onError?: (error: Error) => void,
|
|
87
|
-
): UnsubscribeFunction {
|
|
88
41
|
const q = query(
|
|
89
42
|
userCollection,
|
|
90
43
|
where(CREATION_FIELDS.DELETED_AT, "==", null),
|
|
@@ -101,22 +54,14 @@ export class CreationsSubscription {
|
|
|
101
54
|
});
|
|
102
55
|
|
|
103
56
|
if (__DEV__) {
|
|
104
|
-
console.log("[CreationsSubscription]
|
|
105
|
-
count: creations.length,
|
|
106
|
-
serverFiltered: true,
|
|
107
|
-
hasChanges: snapshot.docChanges().length,
|
|
108
|
-
});
|
|
57
|
+
console.log("[CreationsSubscription] Sync:", creations.length, "items");
|
|
109
58
|
}
|
|
110
59
|
|
|
111
60
|
onData(creations);
|
|
112
61
|
},
|
|
113
62
|
(error: Error) => {
|
|
114
63
|
if (__DEV__) {
|
|
115
|
-
console.error("[CreationsSubscription]
|
|
116
|
-
error: error.message,
|
|
117
|
-
code: (error as { code?: string }).code,
|
|
118
|
-
userId,
|
|
119
|
-
});
|
|
64
|
+
console.error("[CreationsSubscription] Error:", error.message);
|
|
120
65
|
}
|
|
121
66
|
onError?.(error);
|
|
122
67
|
},
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creation Delete Operations
|
|
3
|
+
* Single Responsibility: Delete/restore operations with centralized error handling
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
6
|
import { updateDoc, deleteDoc } from "firebase/firestore";
|
|
6
7
|
import { type FirestorePathResolver } from "@umituz/react-native-firebase";
|
|
7
|
-
|
|
8
|
-
declare const __DEV__: boolean;
|
|
8
|
+
import { logOperationError, logOperationSuccess, logInvalidRef } from "./creation-error-handler.util";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Soft deletes a creation
|
|
11
|
+
* Soft deletes a creation by setting deletedAt timestamp
|
|
12
12
|
*/
|
|
13
13
|
export async function deleteCreation(
|
|
14
14
|
pathResolver: FirestorePathResolver,
|
|
@@ -16,37 +16,25 @@ export async function deleteCreation(
|
|
|
16
16
|
creationId: string
|
|
17
17
|
): Promise<boolean> {
|
|
18
18
|
const docRef = pathResolver.getDocRef(userId, creationId);
|
|
19
|
+
const context = { userId, creationId };
|
|
20
|
+
|
|
19
21
|
if (!docRef) {
|
|
20
|
-
|
|
21
|
-
console.error("[CreationDelete] Cannot delete: Invalid document reference", {
|
|
22
|
-
userId,
|
|
23
|
-
creationId,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
22
|
+
logInvalidRef("Delete", context);
|
|
26
23
|
return false;
|
|
27
24
|
}
|
|
28
25
|
|
|
29
26
|
try {
|
|
30
27
|
await updateDoc(docRef, { deletedAt: new Date() });
|
|
31
|
-
|
|
32
|
-
console.log("[CreationDelete] Soft deleted successfully", { userId, creationId });
|
|
33
|
-
}
|
|
28
|
+
logOperationSuccess("Delete", context);
|
|
34
29
|
return true;
|
|
35
30
|
} catch (error) {
|
|
36
|
-
|
|
37
|
-
console.error("[CreationDelete] Soft delete failed", {
|
|
38
|
-
userId,
|
|
39
|
-
creationId,
|
|
40
|
-
error: error instanceof Error ? error.message : String(error),
|
|
41
|
-
code: (error as { code?: string })?.code,
|
|
42
|
-
});
|
|
43
|
-
}
|
|
31
|
+
logOperationError("Delete", context, error);
|
|
44
32
|
return false;
|
|
45
33
|
}
|
|
46
34
|
}
|
|
47
35
|
|
|
48
36
|
/**
|
|
49
|
-
*
|
|
37
|
+
* Permanently deletes a creation from Firestore
|
|
50
38
|
*/
|
|
51
39
|
export async function hardDeleteCreation(
|
|
52
40
|
pathResolver: FirestorePathResolver,
|
|
@@ -54,37 +42,25 @@ export async function hardDeleteCreation(
|
|
|
54
42
|
creationId: string
|
|
55
43
|
): Promise<boolean> {
|
|
56
44
|
const docRef = pathResolver.getDocRef(userId, creationId);
|
|
45
|
+
const context = { userId, creationId };
|
|
46
|
+
|
|
57
47
|
if (!docRef) {
|
|
58
|
-
|
|
59
|
-
console.error("[CreationDelete] Cannot hard delete: Invalid document reference", {
|
|
60
|
-
userId,
|
|
61
|
-
creationId,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
48
|
+
logInvalidRef("HardDelete", context);
|
|
64
49
|
return false;
|
|
65
50
|
}
|
|
66
51
|
|
|
67
52
|
try {
|
|
68
53
|
await deleteDoc(docRef);
|
|
69
|
-
|
|
70
|
-
console.log("[CreationDelete] Hard deleted successfully", { userId, creationId });
|
|
71
|
-
}
|
|
54
|
+
logOperationSuccess("HardDelete", context);
|
|
72
55
|
return true;
|
|
73
56
|
} catch (error) {
|
|
74
|
-
|
|
75
|
-
console.error("[CreationDelete] Hard delete failed", {
|
|
76
|
-
userId,
|
|
77
|
-
creationId,
|
|
78
|
-
error: error instanceof Error ? error.message : String(error),
|
|
79
|
-
code: (error as { code?: string })?.code,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
57
|
+
logOperationError("HardDelete", context, error);
|
|
82
58
|
return false;
|
|
83
59
|
}
|
|
84
60
|
}
|
|
85
61
|
|
|
86
62
|
/**
|
|
87
|
-
* Restores a soft-deleted creation
|
|
63
|
+
* Restores a soft-deleted creation by clearing deletedAt
|
|
88
64
|
*/
|
|
89
65
|
export async function restoreCreation(
|
|
90
66
|
pathResolver: FirestorePathResolver,
|
|
@@ -92,31 +68,19 @@ export async function restoreCreation(
|
|
|
92
68
|
creationId: string
|
|
93
69
|
): Promise<boolean> {
|
|
94
70
|
const docRef = pathResolver.getDocRef(userId, creationId);
|
|
71
|
+
const context = { userId, creationId };
|
|
72
|
+
|
|
95
73
|
if (!docRef) {
|
|
96
|
-
|
|
97
|
-
console.error("[CreationDelete] Cannot restore: Invalid document reference", {
|
|
98
|
-
userId,
|
|
99
|
-
creationId,
|
|
100
|
-
});
|
|
101
|
-
}
|
|
74
|
+
logInvalidRef("Restore", context);
|
|
102
75
|
return false;
|
|
103
76
|
}
|
|
104
77
|
|
|
105
78
|
try {
|
|
106
79
|
await updateDoc(docRef, { deletedAt: null });
|
|
107
|
-
|
|
108
|
-
console.log("[CreationDelete] Restored successfully", { userId, creationId });
|
|
109
|
-
}
|
|
80
|
+
logOperationSuccess("Restore", context);
|
|
110
81
|
return true;
|
|
111
82
|
} catch (error) {
|
|
112
|
-
|
|
113
|
-
console.error("[CreationDelete] Restore failed", {
|
|
114
|
-
userId,
|
|
115
|
-
creationId,
|
|
116
|
-
error: error instanceof Error ? error.message : String(error),
|
|
117
|
-
code: (error as { code?: string })?.code,
|
|
118
|
-
});
|
|
119
|
-
}
|
|
83
|
+
logOperationError("Restore", context, error);
|
|
120
84
|
return false;
|
|
121
85
|
}
|
|
122
86
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creation Error Handler Utility
|
|
3
|
+
* Single Responsibility: Centralized error logging
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
declare const __DEV__: boolean;
|
|
7
|
+
|
|
8
|
+
export function logOperationError(
|
|
9
|
+
operation: string,
|
|
10
|
+
context: { userId: string; creationId: string },
|
|
11
|
+
error: unknown
|
|
12
|
+
): void {
|
|
13
|
+
if (!__DEV__) return;
|
|
14
|
+
|
|
15
|
+
console.error(`[Creation${operation}] Failed:`, {
|
|
16
|
+
...context,
|
|
17
|
+
error: error instanceof Error ? error.message : String(error),
|
|
18
|
+
code: (error as { code?: string })?.code,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function logOperationSuccess(
|
|
23
|
+
operation: string,
|
|
24
|
+
context: { userId: string; creationId: string }
|
|
25
|
+
): void {
|
|
26
|
+
if (!__DEV__) return;
|
|
27
|
+
console.log(`[Creation${operation}] Success:`, context);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function logInvalidRef(
|
|
31
|
+
operation: string,
|
|
32
|
+
context: { userId: string; creationId: string }
|
|
33
|
+
): void {
|
|
34
|
+
if (!__DEV__) return;
|
|
35
|
+
console.error(`[Creation${operation}] Invalid document reference:`, context);
|
|
36
|
+
}
|
|
@@ -49,57 +49,56 @@ export function useProcessingJobsPoller(
|
|
|
49
49
|
[creations],
|
|
50
50
|
);
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const status = await provider.getJobStatus(creation.model, creation.requestId);
|
|
68
|
-
|
|
69
|
-
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
70
|
-
console.log("[ProcessingJobsPoller] Status:", creation.id, status.status);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (status.status === QUEUE_STATUS.COMPLETED) {
|
|
74
|
-
const result = await provider.getJobResult<FalResult>(creation.model, creation.requestId);
|
|
75
|
-
const urls = extractResultUrl(result);
|
|
76
|
-
if (typeof __DEV__ !== "undefined" && __DEV__) console.log("[ProcessingJobsPoller] Completed:", creation.id, urls);
|
|
77
|
-
|
|
78
|
-
const uri = urls.videoUrl || urls.imageUrl || "";
|
|
79
|
-
await repository.update(userId, creation.id, {
|
|
80
|
-
status: CREATION_STATUS.COMPLETED,
|
|
81
|
-
uri,
|
|
82
|
-
output: urls,
|
|
83
|
-
});
|
|
84
|
-
} else if (status.status === QUEUE_STATUS.FAILED) {
|
|
85
|
-
if (typeof __DEV__ !== "undefined" && __DEV__) console.log("[ProcessingJobsPoller] Failed:", creation.id);
|
|
86
|
-
|
|
87
|
-
await repository.update(userId, creation.id, {
|
|
88
|
-
status: CREATION_STATUS.FAILED,
|
|
89
|
-
metadata: { error: "Generation failed" },
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
// If still IN_PROGRESS or IN_QUEUE, we'll check again next interval
|
|
93
|
-
} catch (error) {
|
|
94
|
-
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
95
|
-
console.error("[ProcessingJobsPoller] Poll error:", creation.id, error);
|
|
96
|
-
}
|
|
97
|
-
} finally {
|
|
98
|
-
pollingRef.current.delete(creation.id);
|
|
52
|
+
// Use ref for stable function reference to prevent effect re-runs
|
|
53
|
+
const pollJobRef = useRef<(creation: Creation) => Promise<void>>();
|
|
54
|
+
|
|
55
|
+
pollJobRef.current = async (creation: Creation) => {
|
|
56
|
+
if (!userId || !creation.requestId || !creation.model) return;
|
|
57
|
+
if (pollingRef.current.has(creation.id)) return;
|
|
58
|
+
|
|
59
|
+
const provider = providerRegistry.getActiveProvider();
|
|
60
|
+
if (!provider || !provider.isInitialized()) return;
|
|
61
|
+
|
|
62
|
+
pollingRef.current.add(creation.id);
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
66
|
+
console.log("[ProcessingJobsPoller] Checking status:", creation.id);
|
|
99
67
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
68
|
+
|
|
69
|
+
const status = await provider.getJobStatus(creation.model, creation.requestId);
|
|
70
|
+
|
|
71
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
72
|
+
console.log("[ProcessingJobsPoller] Status:", creation.id, status.status);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (status.status === QUEUE_STATUS.COMPLETED) {
|
|
76
|
+
const result = await provider.getJobResult<FalResult>(creation.model, creation.requestId);
|
|
77
|
+
const urls = extractResultUrl(result);
|
|
78
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) console.log("[ProcessingJobsPoller] Completed:", creation.id, urls);
|
|
79
|
+
|
|
80
|
+
const uri = urls.videoUrl || urls.imageUrl || "";
|
|
81
|
+
await repository.update(userId, creation.id, {
|
|
82
|
+
status: CREATION_STATUS.COMPLETED,
|
|
83
|
+
uri,
|
|
84
|
+
output: urls,
|
|
85
|
+
});
|
|
86
|
+
} else if (status.status === QUEUE_STATUS.FAILED) {
|
|
87
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) console.log("[ProcessingJobsPoller] Failed:", creation.id);
|
|
88
|
+
|
|
89
|
+
await repository.update(userId, creation.id, {
|
|
90
|
+
status: CREATION_STATUS.FAILED,
|
|
91
|
+
metadata: { error: "Generation failed" },
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
96
|
+
console.error("[ProcessingJobsPoller] Poll error:", creation.id, error);
|
|
97
|
+
}
|
|
98
|
+
} finally {
|
|
99
|
+
pollingRef.current.delete(creation.id);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
103
102
|
|
|
104
103
|
useEffect(() => {
|
|
105
104
|
if (!enabled || !userId || processingJobs.length === 0) {
|
|
@@ -111,11 +110,11 @@ export function useProcessingJobsPoller(
|
|
|
111
110
|
}
|
|
112
111
|
|
|
113
112
|
// Initial poll
|
|
114
|
-
processingJobs.forEach((job) =>
|
|
113
|
+
processingJobs.forEach((job) => pollJobRef.current?.(job));
|
|
115
114
|
|
|
116
115
|
// Set up interval polling
|
|
117
116
|
intervalRef.current = setInterval(() => {
|
|
118
|
-
processingJobs.forEach((job) =>
|
|
117
|
+
processingJobs.forEach((job) => pollJobRef.current?.(job));
|
|
119
118
|
}, DEFAULT_POLL_INTERVAL_MS);
|
|
120
119
|
|
|
121
120
|
return () => {
|
|
@@ -128,7 +127,7 @@ export function useProcessingJobsPoller(
|
|
|
128
127
|
intervalRef.current = null;
|
|
129
128
|
}
|
|
130
129
|
};
|
|
131
|
-
}, [enabled, userId, processingJobs
|
|
130
|
+
}, [enabled, userId, processingJobs]);
|
|
132
131
|
|
|
133
132
|
return {
|
|
134
133
|
processingCount: processingJobs.length,
|