11import { app } from "electron"
22import * as fs from "fs"
33import * as path from "path"
4+ import type { RecentLog } from "../types/flaTypes"
45
56export default function createRecentLogsManager ( maxRecentLogs : number = 10 ) {
67 // JSON file to hold paths to recently opened logs
@@ -9,13 +10,38 @@ export default function createRecentLogsManager(maxRecentLogs: number = 10) {
910 "recentLogs.json" ,
1011 )
1112
12- function loadRecentLogs ( ) : string [ ] {
13+ function loadRecentLogs ( ) : RecentLog [ ] {
1314 try {
1415 if ( fs . existsSync ( recentLogsPath ) ) {
1516 const data = fs . readFileSync ( recentLogsPath , "utf8" )
1617 const parsed = JSON . parse ( data )
17- // Ensure we return an array of strings
18- return Array . isArray ( parsed ) ? parsed : [ ]
18+
19+ // Backward compatibility: convert array of strings to array of objects
20+ if ( Array . isArray ( parsed ) ) {
21+ return parsed . map ( ( item ) => {
22+ if ( typeof item === "string" ) {
23+ // Old format: use mtime as fallback
24+ try {
25+ const stats = fs . statSync ( item )
26+ return { path : item , timestamp : stats . mtime . getTime ( ) }
27+ } catch {
28+ return { path : item , timestamp : Date . now ( ) }
29+ }
30+ }
31+ // New format: validate and return
32+ if (
33+ typeof item === "object" &&
34+ item !== null &&
35+ "path" in item &&
36+ "timestamp" in item
37+ ) {
38+ return item as RecentLog
39+ }
40+ // Invalid format: treat as current time
41+ return { path : String ( item ) , timestamp : Date . now ( ) }
42+ } )
43+ }
44+ return [ ]
1945 }
2046 return [ ]
2147 } catch ( error ) {
@@ -24,7 +50,7 @@ export default function createRecentLogsManager(maxRecentLogs: number = 10) {
2450 }
2551 }
2652
27- let recentLogs : string [ ] = loadRecentLogs ( )
53+ let recentLogs : RecentLog [ ] = loadRecentLogs ( )
2854
2955 function saveRecentLogs ( ) : void {
3056 try {
@@ -37,10 +63,10 @@ export default function createRecentLogsManager(maxRecentLogs: number = 10) {
3763 return {
3864 addRecentLog ( filePath : string ) : void {
3965 // Remove the file if it already exists in the list
40- recentLogs = recentLogs . filter ( ( file : string ) => file !== filePath )
66+ recentLogs = recentLogs . filter ( ( log : RecentLog ) => log . path !== filePath )
4167
42- // Add the file to the beginning of the list
43- recentLogs . unshift ( filePath )
68+ // Add the file to the beginning of the list with current timestamp
69+ recentLogs . unshift ( { path : filePath , timestamp : Date . now ( ) } )
4470
4571 // Trim the list if it exceeds the maximum allowed
4672 if ( recentLogs . length > maxRecentLogs ) {
@@ -49,10 +75,10 @@ export default function createRecentLogsManager(maxRecentLogs: number = 10) {
4975 saveRecentLogs ( )
5076 } ,
5177
52- getRecentLogs ( ) : string [ ] {
78+ getRecentLogs ( ) : RecentLog [ ] {
5379 // Filter out files that no longer exist
54- const existingFiles : string [ ] = recentLogs . filter ( ( file : string ) =>
55- fs . existsSync ( file ) ,
80+ const existingFiles : RecentLog [ ] = recentLogs . filter ( ( log : RecentLog ) =>
81+ fs . existsSync ( log . path ) ,
5682 )
5783
5884 // Update the list if files were removed
0 commit comments