Skip to content

Commit 451a701

Browse files
committed
Fix: image generation page loading history
1 parent 1c32b8f commit 451a701

File tree

4 files changed

+85
-19
lines changed

4 files changed

+85
-19
lines changed

src/components/pages/ImageGenerationPage.tsx

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from "react";
1+
import { useState, useEffect, useCallback } from "react";
22
import {
33
SettingsService,
44
SETTINGS_CHANGE_EVENT,
@@ -28,31 +28,52 @@ export const ImageGenerationPage = () => {
2828
const [historyResults, setHistoryResults] = useState<ImageGenerationResult[]>([]);
2929
const [isLoadingHistory, setIsLoadingHistory] = useState(true);
3030

31+
// Load image generation history from database
32+
const refreshImageHistory = useCallback(async () => {
33+
try {
34+
const dbService = DatabaseIntegrationService.getInstance();
35+
const results = await dbService.getImageGenerationResults();
36+
if (results && results.length > 0) {
37+
// Sort by most recent first
38+
const sortedResults = results.sort((a, b) =>
39+
b.imageResultId.localeCompare(a.imageResultId)
40+
);
41+
setHistoryResults(sortedResults);
42+
}
43+
} catch (error) {
44+
console.error('Error refreshing image history:', error);
45+
}
46+
}, []);
47+
3148
// Initialize image generation manager
3249
useEffect(() => {
33-
const imageManager = ImageGenerationManager.getInstance();
34-
35-
// Register for updates on generation status changes
36-
imageManager.setUpdateCallback((handlers) => {
37-
setGenerationResults(new Map(handlers));
38-
});
39-
40-
// Load history from database
41-
const loadHistoryResults = async () => {
50+
const initialize = async () => {
51+
// Initialize image generation manager
52+
const imageManager = ImageGenerationManager.getInstance();
53+
54+
// Register for updates on generation status changes
55+
imageManager.setUpdateCallback((handlers) => {
56+
setGenerationResults(new Map(handlers));
57+
});
58+
59+
// Initialize database and load history
4260
try {
4361
setIsLoadingHistory(true);
4462
const dbService = DatabaseIntegrationService.getInstance();
4563
await dbService.initialize();
46-
// TODO: Implement loading image history from database when available
64+
65+
// Load image generation history from database
66+
await refreshImageHistory();
67+
4768
setIsLoadingHistory(false);
4869
} catch (error) {
49-
console.error('Error loading image history:', error);
70+
console.error('Error initializing database or loading image history:', error);
5071
setIsLoadingHistory(false);
5172
}
5273
};
5374

54-
loadHistoryResults();
55-
}, []);
75+
initialize();
76+
}, [refreshImageHistory]);
5677

5778
// Check if API key is available
5879
useEffect(() => {
@@ -129,6 +150,9 @@ export const ImageGenerationPage = () => {
129150
// Update the handler with successful results
130151
handler.setSuccess(processedImages);
131152

153+
// Refresh history to include the new generation
154+
refreshImageHistory();
155+
132156
// Generate new seed for next generation
133157
generateNewSeed();
134158
} else {
@@ -146,6 +170,7 @@ export const ImageGenerationPage = () => {
146170

147171
// Get all results to display in order (active generations first, then history)
148172
const getAllResults = () => {
173+
// Get results from active generation handlers
149174
const handlerResults = Array.from(generationResults.values())
150175
.map(handler => handler.getResult())
151176
.sort((a, b) => {
@@ -160,9 +185,17 @@ export const ImageGenerationPage = () => {
160185
return b.imageResultId.localeCompare(a.imageResultId);
161186
});
162187

188+
// Combine with historical results
163189
return [...handlerResults, ...historyResults];
164190
};
165191

192+
// Check if any images are currently generating
193+
const isAnyImageGenerating = () => {
194+
return Array.from(generationResults.values()).some(
195+
h => h.getStatus() === ImageGenerationStatus.GENERATING
196+
);
197+
};
198+
166199
return (
167200
<div className="flex flex-col w-full h-full bg-white">
168201
{isApiKeyMissing && (
@@ -191,19 +224,19 @@ export const ImageGenerationPage = () => {
191224
<div className="flex flex-row gap-2 p-2 border border-gray-300 rounded-lg shadow-sm">
192225
<button
193226
onClick={handleGenerateImage}
194-
disabled={!prompt.trim() || isApiKeyMissing}
227+
disabled={!prompt.trim() || isApiKeyMissing || isAnyImageGenerating()}
195228
className="px-4 py-2.5 text-nowrap flex flex-row gap-1 text-white text-center confirm-btn"
196229
>
197230
<Settings></Settings>
198231
{t("imageGeneration.settingsButton")}
199232
</button>
200233
<button
201234
onClick={handleGenerateImage}
202-
disabled={!prompt.trim() || isApiKeyMissing}
235+
disabled={!prompt.trim() || isApiKeyMissing || isAnyImageGenerating()}
203236
className="px-4 py-2.5 text-nowrap flex flex-row gap-1 text-white text-center confirm-btn"
204237
>
205238
<Zap></Zap>
206-
{Array.from(generationResults.values()).some(h => h.getStatus() === ImageGenerationStatus.GENERATING)
239+
{isAnyImageGenerating()
207240
? t("imageGeneration.generating")
208241
: t("imageGeneration.generateButton")}
209242
</button>
@@ -237,7 +270,12 @@ export const ImageGenerationPage = () => {
237270
{/* Loading indicator for history */}
238271
{isLoadingHistory && (
239272
<div className="flex items-center justify-center h-24 rounded-lg bg-gray-50">
240-
<div className="w-8 h-8 border-4 rounded-full border-primary-300 border-t-primary-600 animate-spin"></div>
273+
<div className="flex flex-col items-center">
274+
<div className="w-8 h-8 border-4 rounded-full border-primary-300 border-t-primary-600 animate-spin"></div>
275+
<p className="mt-2 text-sm text-gray-600">
276+
{t("imageGeneration.loading")}
277+
</p>
278+
</div>
241279
</div>
242280
)}
243281

src/locales/en/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@
121121
"results": "Generated Results",
122122
"placeholderText": "Enter a prompt and click generate to create images",
123123
"apiKeyMissing": "Please set your API key for the selected provider in the settings.",
124-
"seedHelp": "Seed for reproducible results"
124+
"seedHelp": "Seed for reproducible results",
125+
"loading": "Loading image history..."
125126
},
126127
"mcpServer": {
127128
"title": "MCP Servers",

src/services/database-integration.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ export class DatabaseIntegrationService {
361361
}
362362
}
363363

364+
/**
365+
* Get all image generation results from the database
366+
*/
367+
public async getImageGenerationResults(): Promise<ImageGenerationResult[]> {
368+
try {
369+
return await this.dbService.getImageGenerationResults();
370+
} catch (error) {
371+
console.error('Error getting image generation results:', error);
372+
return [];
373+
}
374+
}
364375

365376
public async saveFile(fileData: FileJsonData, arrayBuffer: ArrayBuffer): Promise<string> {
366377
const fileId = uuidv4();

src/services/database.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,22 @@ export class DatabaseService {
567567
});
568568
}
569569

570+
/**
571+
* Get all image generation results from the database
572+
*/
573+
public async getImageGenerationResults(): Promise<ImageGenerationResult[]> {
574+
return new Promise((resolve, reject) => {
575+
if (!this.db) throw new Error('Database not initialized');
576+
577+
const transaction = this.db.transaction('imageGenerationResults', 'readonly');
578+
const store = transaction.objectStore('imageGenerationResults');
579+
const request = store.getAll();
580+
581+
request.onsuccess = () => resolve(request.result);
582+
request.onerror = () => reject(request.error);
583+
});
584+
}
585+
570586
/**
571587
* Get all files from the database
572588
* @returns List of files

0 commit comments

Comments
 (0)