@@ -43,11 +43,17 @@ export const ImageGenerationPage = () => {
4343 const dbService = DatabaseIntegrationService . getInstance ( ) ;
4444 const results = await dbService . getImageGenerationResults ( ) ;
4545 if ( results && results . length > 0 ) {
46- // Sort by most recent first
47- const sortedResults = results . sort ( ( a , b ) =>
48- b . imageResultId . localeCompare ( a . imageResultId )
49- ) ;
46+ // Sort by most recent first using updatedAt timestamp
47+ const sortedResults = results . sort ( ( a , b ) => {
48+ // Convert string dates to Date objects if necessary
49+ const dateA = a . updatedAt instanceof Date ? a . updatedAt : new Date ( a . updatedAt ) ;
50+ const dateB = b . updatedAt instanceof Date ? b . updatedAt : new Date ( b . updatedAt ) ;
51+ // Sort newest first
52+ return dateB . getTime ( ) - dateA . getTime ( ) ;
53+ } ) ;
5054 setHistoryResults ( sortedResults ) ;
55+ } else {
56+ setHistoryResults ( [ ] ) ;
5157 }
5258 } catch ( error ) {
5359 console . error ( 'Error refreshing image history:' , error ) ;
@@ -142,6 +148,32 @@ export const ImageGenerationPage = () => {
142148 } ;
143149 } , [ ] ) ;
144150
151+ // Process and update the result after generation
152+ const processGenerationResult = async ( handler : ImageGenerationHandler , images : string [ ] ) => {
153+ // Convert image data to full URLs if needed
154+ const processedImages = images . map ( img => {
155+ const base64Data = img as string ;
156+ if ( base64Data . startsWith ( 'data:image' ) ) {
157+ return base64Data ;
158+ } else {
159+ return `data:image/png;base64,${ base64Data } ` ;
160+ }
161+ } ) ;
162+
163+ // Update the handler with successful results
164+ await handler . setSuccess ( processedImages ) ;
165+
166+ // Refresh history to include the new generation
167+ await refreshImageHistory ( ) ;
168+
169+ // Remove the handler to prevent duplication with database results
170+ const imageManager = ImageGenerationManager . getInstance ( ) ;
171+ imageManager . removeHandler ( handler . getId ( ) ) ;
172+
173+ // Generate new seed for next generation
174+ generateNewSeed ( ) ;
175+ } ;
176+
145177 // Handle generating an image using selected provider
146178 const handleGenerateImage = async ( ) => {
147179 if ( ! prompt . trim ( ) ) return ;
@@ -195,24 +227,7 @@ export const ImageGenerationPage = () => {
195227
196228 // Process the result
197229 if ( images && images . length > 0 ) {
198- // Convert image data to full URLs if needed
199- const processedImages = images . map ( img => {
200- const base64Data = img as string ;
201- if ( base64Data . startsWith ( 'data:image' ) ) {
202- return base64Data ;
203- } else {
204- return `data:image/png;base64,${ base64Data } ` ;
205- }
206- } ) ;
207-
208- // Update the handler with successful results
209- handler . setSuccess ( processedImages ) ;
210-
211- // Refresh history to include the new generation
212- refreshImageHistory ( ) ;
213-
214- // Generate new seed for next generation
215- generateNewSeed ( ) ;
230+ await processGenerationResult ( handler , images as string [ ] ) ;
216231 } else {
217232 throw new Error ( "No images generated" ) ;
218233 }
@@ -239,11 +254,13 @@ export const ImageGenerationPage = () => {
239254 if ( b . status === ImageGenerationStatus . GENERATING && a . status !== ImageGenerationStatus . GENERATING ) {
240255 return 1 ;
241256 }
242- // Then sort by most recent first (assuming imageResultId is a UUID with timestamp components)
243- return b . imageResultId . localeCompare ( a . imageResultId ) ;
257+ // Then sort by most recent first using updatedAt timestamp
258+ const dateA = a . updatedAt instanceof Date ? a . updatedAt : new Date ( a . updatedAt ) ;
259+ const dateB = b . updatedAt instanceof Date ? b . updatedAt : new Date ( b . updatedAt ) ;
260+ return dateB . getTime ( ) - dateA . getTime ( ) ;
244261 } ) ;
245262
246- // Combine with historical results
263+ // Combine with historical results and ensure newest is first
247264 return [ ...handlerResults , ...historyResults ] ;
248265 } ;
249266
0 commit comments