Skip to content

Commit 5da34fc

Browse files
committed
fix: prevent duplicate image generation when prompt and id are both provided
1 parent 926bbad commit 5da34fc

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/hooks/image-gen/use-image-gen.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export function useImageGen({
133133
}
134134
return;
135135
}
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;
136140

137141
// Create a request signature to deduplicate identical requests
138142
const requestSignature = JSON.stringify({
@@ -198,6 +202,8 @@ export function useImageGen({
198202
// We'll use the document for info even during regeneration
199203
const hasDocumentId = !!_id;
200204

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
201207
if (hasDocumentId) {
202208
const existingDoc = await db.get(_id).catch((err) => {
203209
console.error(`[loadOrGenerateImage] Failed to load document ${_id}:`, err);
@@ -449,8 +455,9 @@ export function useImageGen({
449455
throw new Error(`Document exists but has no files: ${_id}`);
450456
}
451457
}
452-
} else if (prompt) {
458+
} else if (prompt && !hasBothPromptAndId) {
453459
// 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
454461

455462
// If we have a document in memory and a generationId, we should add a version
456463
// to the existing document instead of creating a new one

0 commit comments

Comments
 (0)