Skip to content

Commit 306e0ca

Browse files
authoredJul 23, 2024
Merge pull request #2 from caido-community/ef-fixes
Fixes
2 parents 9a67604 + 83a9ce2 commit 306e0ca

File tree

1 file changed

+34
-41
lines changed

1 file changed

+34
-41
lines changed
 

‎src/index.ts

+34-41
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,19 @@ const Commands = {
1414
// Get notes from storage.
1515
const getNotes = (caido: Caido): PluginStorage["notes"] => {
1616
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 ?? [];
2218
};
2319

2420
// Add note to storage.
25-
const addNoteStorage = (
21+
const addNoteStorage = async (
2622
caido: Caido,
2723
datetime: string,
2824
note: string,
29-
projectName?: string
25+
projectName?: string,
3026
) => {
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 });
3830

3931
// Print added note to console.
4032
console.log("Added Note:", { datetime, note, projectName });
@@ -69,16 +61,9 @@ const addNoteMenu = async (caido: Caido) => {
6961
if (projectData) {
7062
const projectName = projectData.name || "No Project Selected";
7163
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;
7964

8065
// Add the note to storage.
81-
addNoteStorage(caido, datetime, currentSelect, projectName);
66+
await addNoteStorage(caido, datetime, currentSelect, projectName);
8267
}
8368
}
8469
};
@@ -130,16 +115,9 @@ const addPage = (caido: Caido) => {
130115
const project = await caido.graphql.currentProject();
131116
const projectData = project?.currentProject;
132117
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;
140118

141119
// Add the note to storage.
142-
addNoteStorage(caido, datetime, inputValue, projectName);
120+
await addNoteStorage(caido, datetime, inputValue, projectName);
143121

144122
// Clear textarea and reset value.
145123
inputValue = "";
@@ -178,23 +156,38 @@ const addPage = (caido: Caido) => {
178156
});
179157
};
180158

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+
181180
export const init = (caido: Caido) => {
182181
// Retrieve notes from storage
183182
const notes = getNotes(caido);
184183
console.log("Current notes:", notes);
185184

186185
// 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+
});
198191

199192
// Register commands.
200193
// Commands are registered with a unique identifier and a handler function.

0 commit comments

Comments
 (0)
Please sign in to comment.