1
- import { useState , useEffect } from "react" ;
1
+ import { useState , useEffect , useCallback } from "react" ;
2
2
import {
3
3
SettingsService ,
4
4
SETTINGS_CHANGE_EVENT ,
@@ -28,31 +28,52 @@ export const ImageGenerationPage = () => {
28
28
const [ historyResults , setHistoryResults ] = useState < ImageGenerationResult [ ] > ( [ ] ) ;
29
29
const [ isLoadingHistory , setIsLoadingHistory ] = useState ( true ) ;
30
30
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
+
31
48
// Initialize image generation manager
32
49
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
42
60
try {
43
61
setIsLoadingHistory ( true ) ;
44
62
const dbService = DatabaseIntegrationService . getInstance ( ) ;
45
63
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
+
47
68
setIsLoadingHistory ( false ) ;
48
69
} catch ( error ) {
49
- console . error ( 'Error loading image history:' , error ) ;
70
+ console . error ( 'Error initializing database or loading image history:' , error ) ;
50
71
setIsLoadingHistory ( false ) ;
51
72
}
52
73
} ;
53
74
54
- loadHistoryResults ( ) ;
55
- } , [ ] ) ;
75
+ initialize ( ) ;
76
+ } , [ refreshImageHistory ] ) ;
56
77
57
78
// Check if API key is available
58
79
useEffect ( ( ) => {
@@ -129,6 +150,9 @@ export const ImageGenerationPage = () => {
129
150
// Update the handler with successful results
130
151
handler . setSuccess ( processedImages ) ;
131
152
153
+ // Refresh history to include the new generation
154
+ refreshImageHistory ( ) ;
155
+
132
156
// Generate new seed for next generation
133
157
generateNewSeed ( ) ;
134
158
} else {
@@ -146,6 +170,7 @@ export const ImageGenerationPage = () => {
146
170
147
171
// Get all results to display in order (active generations first, then history)
148
172
const getAllResults = ( ) => {
173
+ // Get results from active generation handlers
149
174
const handlerResults = Array . from ( generationResults . values ( ) )
150
175
. map ( handler => handler . getResult ( ) )
151
176
. sort ( ( a , b ) => {
@@ -160,9 +185,17 @@ export const ImageGenerationPage = () => {
160
185
return b . imageResultId . localeCompare ( a . imageResultId ) ;
161
186
} ) ;
162
187
188
+ // Combine with historical results
163
189
return [ ...handlerResults , ...historyResults ] ;
164
190
} ;
165
191
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
+
166
199
return (
167
200
< div className = "flex flex-col w-full h-full bg-white" >
168
201
{ isApiKeyMissing && (
@@ -191,19 +224,19 @@ export const ImageGenerationPage = () => {
191
224
< div className = "flex flex-row gap-2 p-2 border border-gray-300 rounded-lg shadow-sm" >
192
225
< button
193
226
onClick = { handleGenerateImage }
194
- disabled = { ! prompt . trim ( ) || isApiKeyMissing }
227
+ disabled = { ! prompt . trim ( ) || isApiKeyMissing || isAnyImageGenerating ( ) }
195
228
className = "px-4 py-2.5 text-nowrap flex flex-row gap-1 text-white text-center confirm-btn"
196
229
>
197
230
< Settings > </ Settings >
198
231
{ t ( "imageGeneration.settingsButton" ) }
199
232
</ button >
200
233
< button
201
234
onClick = { handleGenerateImage }
202
- disabled = { ! prompt . trim ( ) || isApiKeyMissing }
235
+ disabled = { ! prompt . trim ( ) || isApiKeyMissing || isAnyImageGenerating ( ) }
203
236
className = "px-4 py-2.5 text-nowrap flex flex-row gap-1 text-white text-center confirm-btn"
204
237
>
205
238
< Zap > </ Zap >
206
- { Array . from ( generationResults . values ( ) ) . some ( h => h . getStatus ( ) === ImageGenerationStatus . GENERATING )
239
+ { isAnyImageGenerating ( )
207
240
? t ( "imageGeneration.generating" )
208
241
: t ( "imageGeneration.generateButton" ) }
209
242
</ button >
@@ -237,7 +270,12 @@ export const ImageGenerationPage = () => {
237
270
{ /* Loading indicator for history */ }
238
271
{ isLoadingHistory && (
239
272
< 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 >
241
279
</ div >
242
280
) }
243
281
0 commit comments