@@ -14,15 +14,16 @@ export interface SummaryData {
1414
1515export class Database {
1616 private context : vscode . ExtensionContext ;
17+ private entries : TimeEntry [ ] | null = null ;
1718
1819 constructor ( context : vscode . ExtensionContext ) {
1920 this . context = context ;
20- // Log the storage URI path
21- const storagePath = this . context . storageUri ?. fsPath ;
22- if ( storagePath ) {
23- console . log ( 'Extension Data Storage Path:' , storagePath ) ;
24- //vscode.window.showInformationMessage(`Data is stored at: ${storagePath}`);
21+ // Initialize storage if empty
22+ if ( ! this . context . globalState . get ( 'timeEntries' ) ) {
23+ this . context . globalState . update ( 'timeEntries' , [ ] ) ;
2524 }
25+ // Load entries into memory
26+ this . entries = this . context . globalState . get < TimeEntry [ ] > ( 'timeEntries' , [ ] ) ;
2627 }
2728
2829 private getLocalDateString ( date : Date ) : string {
@@ -34,21 +35,33 @@ export class Database {
3435 async addEntry ( date : Date , project : string , timeSpent : number ) {
3536 const dateString = this . getLocalDateString ( date ) ;
3637 const entries = this . getEntries ( ) ;
38+
3739 const existingEntryIndex = entries . findIndex ( entry => entry . date === dateString && entry . project === project ) ;
3840
3941 if ( existingEntryIndex !== - 1 ) {
40- // Update existing entry
4142 entries [ existingEntryIndex ] . timeSpent += timeSpent ;
4243 } else {
43- // Add new entry
4444 entries . push ( { date : dateString , project, timeSpent } ) ;
4545 }
4646
47- await this . context . globalState . update ( 'timeEntries' , entries ) ;
47+ try {
48+ await this . updateEntries ( entries ) ;
49+ } catch ( error ) {
50+ console . error ( 'Error saving entry:' , error ) ;
51+ vscode . window . showErrorMessage ( 'Failed to save time entry' ) ;
52+ }
4853 }
4954
5055 getEntries ( ) : TimeEntry [ ] {
51- return this . context . globalState . get < TimeEntry [ ] > ( 'timeEntries' , [ ] ) ;
56+ if ( ! this . entries ) {
57+ this . entries = this . context . globalState . get < TimeEntry [ ] > ( 'timeEntries' , [ ] ) ;
58+ }
59+ return this . entries ;
60+ }
61+
62+ private async updateEntries ( entries : TimeEntry [ ] ) : Promise < void > {
63+ this . entries = entries ;
64+ await this . context . globalState . update ( 'timeEntries' , entries ) ;
5265 }
5366
5467 async getSummaryData ( ) : Promise < SummaryData > {
@@ -83,10 +96,10 @@ export class Database {
8396 const today = this . getLocalDateString ( new Date ( ) ) ;
8497 const entries = this . getEntries ( ) ;
8598 const updatedEntries = entries . filter ( entry => entry . date !== today ) ;
86- await this . context . globalState . update ( 'timeEntries' , updatedEntries ) ;
99+ await this . updateEntries ( updatedEntries ) ;
87100 }
88101
89102 async resetAllTime ( ) : Promise < void > {
90- await this . context . globalState . update ( 'timeEntries' , [ ] ) ;
103+ await this . updateEntries ( [ ] ) ;
91104 }
92105}
0 commit comments