@@ -133,6 +133,10 @@ export function useImageGen({
133
133
}
134
134
return ;
135
135
}
136
+
137
+ // Track whether this is a case where both prompt prop and _id are provided
138
+ // This helps us avoid the race condition where we might generate two images
139
+ const hasBothPromptAndId = ! ! prompt && ! ! _id ;
136
140
137
141
// Create a request signature to deduplicate identical requests
138
142
const requestSignature = JSON . stringify ( {
@@ -198,6 +202,8 @@ export function useImageGen({
198
202
// We'll use the document for info even during regeneration
199
203
const hasDocumentId = ! ! _id ;
200
204
205
+ // When both prompt and _id are provided, we want the document for its files and metadata,
206
+ // but use the provided prompt for generation to avoid a race condition
201
207
if ( hasDocumentId ) {
202
208
const existingDoc = await db . get ( _id ) . catch ( ( err ) => {
203
209
console . error ( `[loadOrGenerateImage] Failed to load document ${ _id } :` , err ) ;
@@ -449,8 +455,9 @@ export function useImageGen({
449
455
throw new Error ( `Document exists but has no files: ${ _id } ` ) ;
450
456
}
451
457
}
452
- } else if ( prompt ) {
458
+ } else if ( prompt && ! hasBothPromptAndId ) {
453
459
// No document ID provided but we have a prompt - generate a new image
460
+ // Skip this section if we have both prompt and ID - in that case we use the document path above
454
461
455
462
// If we have a document in memory and a generationId, we should add a version
456
463
// to the existing document instead of creating a new one
0 commit comments