@@ -14,27 +14,19 @@ const Commands = {
14
14
// Get notes from storage.
15
15
const getNotes = ( caido : Caido ) : PluginStorage [ "notes" ] => {
16
16
const storage = caido . storage . get ( ) as PluginStorage | undefined ;
17
- if ( storage && storage . notes ) {
18
- console . log ( "Retrieved notes from storage: " , storage . notes ) ;
19
- return [ ...storage . notes ] ;
20
- }
21
- return [ ] ;
17
+ return storage ?. notes ?? [ ] ;
22
18
} ;
23
19
24
20
// Add note to storage.
25
- const addNoteStorage = (
21
+ const addNoteStorage = async (
26
22
caido : Caido ,
27
23
datetime : string ,
28
24
note : string ,
29
- projectName ?: string
25
+ projectName ?: string ,
30
26
) => {
31
- let storage = caido . storage . get ( ) as PluginStorage | undefined ;
32
- if ( ! storage ) {
33
- storage = { notes : [ ] } ;
34
- }
35
-
36
- const updatedNotes = [ ...storage . notes , { datetime, note, projectName } ] ;
37
- caido . storage . set ( { ...storage , notes : updatedNotes } ) ;
27
+ const currentNotes = getNotes ( caido ) ;
28
+ const updatedNotes = [ ...currentNotes , { datetime, note, projectName } ] ;
29
+ await caido . storage . set ( { notes : updatedNotes } ) ;
38
30
39
31
// Print added note to console.
40
32
console . log ( "Added Note:" , { datetime, note, projectName } ) ;
@@ -69,16 +61,9 @@ const addNoteMenu = async (caido: Caido) => {
69
61
if ( projectData ) {
70
62
const projectName = projectData . name || "No Project Selected" ;
71
63
const datetime = new Date ( ) . toLocaleString ( ) ;
72
- const row = table . insertRow ( ) ;
73
- const datetimeCell = row . insertCell ( ) ;
74
- const inputCell = row . insertCell ( ) ;
75
-
76
- datetimeCell . textContent = `${ datetime } Project: ${ projectName } ` ;
77
- datetimeCell . classList . add ( "datetime-cell" ) ;
78
- inputCell . textContent = currentSelect ;
79
64
80
65
// Add the note to storage.
81
- addNoteStorage ( caido , datetime , currentSelect , projectName ) ;
66
+ await addNoteStorage ( caido , datetime , currentSelect , projectName ) ;
82
67
}
83
68
}
84
69
} ;
@@ -130,16 +115,9 @@ const addPage = (caido: Caido) => {
130
115
const project = await caido . graphql . currentProject ( ) ;
131
116
const projectData = project ?. currentProject ;
132
117
const projectName = projectData ?. name || "No Project Selected" ;
133
- const row = table . insertRow ( ) ;
134
- const datetimeCell = row . insertCell ( ) ;
135
- const inputCell = row . insertCell ( ) ;
136
-
137
- datetimeCell . textContent = `${ datetime } Project: ${ projectName } ` ;
138
- datetimeCell . classList . add ( "datetime-cell" ) ;
139
- inputCell . textContent = inputValue ;
140
118
141
119
// Add the note to storage.
142
- addNoteStorage ( caido , datetime , inputValue , projectName ) ;
120
+ await addNoteStorage ( caido , datetime , inputValue , projectName ) ;
143
121
144
122
// Clear textarea and reset value.
145
123
inputValue = "" ;
@@ -178,23 +156,38 @@ const addPage = (caido: Caido) => {
178
156
} ) ;
179
157
} ;
180
158
159
+ const displayNotes = ( notes : PluginStorage [ "notes" ] | undefined ) => {
160
+ const tbody = table . querySelector ( "tbody" ) ;
161
+ if ( tbody ) {
162
+ table . textContent = "" ;
163
+ }
164
+
165
+ if ( ! notes ) {
166
+ return ;
167
+ }
168
+
169
+ notes . forEach ( ( note ) => {
170
+ const row = table . insertRow ( ) ;
171
+ const datetimeCell = row . insertCell ( ) ;
172
+ const noteCell = row . insertCell ( ) ;
173
+
174
+ datetimeCell . textContent = `${ note . datetime } Project: ${ note . projectName } ` ;
175
+ datetimeCell . classList . add ( "datetime-cell" ) ;
176
+ noteCell . textContent = note . note ;
177
+ } ) ;
178
+ } ;
179
+
181
180
export const init = ( caido : Caido ) => {
182
181
// Retrieve notes from storage
183
182
const notes = getNotes ( caido ) ;
184
183
console . log ( "Current notes:" , notes ) ;
185
184
186
185
// Populate table with stored notes.
187
- if ( notes && notes . length > 0 ) {
188
- notes . forEach ( ( note ) => {
189
- const row = table . insertRow ( ) ;
190
- const datetimeCell = row . insertCell ( ) ;
191
- const noteCell = row . insertCell ( ) ;
192
-
193
- datetimeCell . textContent = `${ note . datetime } Project: ${ note . projectName } ` ;
194
- datetimeCell . classList . add ( "datetime-cell" ) ;
195
- noteCell . textContent = note . note ;
196
- } ) ;
197
- }
186
+ displayNotes ( notes ) ;
187
+
188
+ caido . storage . onChange ( ( value ) => {
189
+ displayNotes ( ( value as PluginStorage | undefined ) ?. notes ) ;
190
+ } ) ;
198
191
199
192
// Register commands.
200
193
// Commands are registered with a unique identifier and a handler function.
0 commit comments