Skip to content

Commit

Permalink
Further improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Jul 23, 2024
1 parent d74529e commit 83a9ce2
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ const Commands = {
// Get notes from storage.
const getNotes = (caido: Caido): PluginStorage["notes"] => {
const storage = caido.storage.get() as PluginStorage | undefined;
if (storage && storage.notes) {
console.log("Retrieved notes from storage: ", storage.notes);
return storage.notes;
}
return [];
return storage?.notes ?? [];
};

// Add note to storage.
Expand All @@ -28,8 +24,7 @@ const addNoteStorage = async (
note: string,
projectName?: string,
) => {
const storage = caido.storage.get() as PluginStorage | undefined;
const currentNotes = storage?.notes ?? [];
const currentNotes = getNotes(caido);
const updatedNotes = [...currentNotes, { datetime, note, projectName }];
await caido.storage.set({ notes: updatedNotes });

Expand Down Expand Up @@ -161,12 +156,16 @@ const addPage = (caido: Caido) => {
});
};

const displayNotes = (notes: PluginStorage["notes"]) => {
const displayNotes = (notes: PluginStorage["notes"] | undefined) => {
const tbody = table.querySelector("tbody");
if (tbody) {
table.textContent = "";
}

if (!notes) {
return;
}

notes.forEach((note) => {
const row = table.insertRow();
const datetimeCell = row.insertCell();
Expand All @@ -184,15 +183,10 @@ export const init = (caido: Caido) => {
console.log("Current notes:", notes);

// Populate table with stored notes.
if (notes) {
displayNotes(notes);
}
displayNotes(notes);

caido.storage.onChange((value) => {
const notes = (value as PluginStorage | undefined)?.notes;
if (notes) {
displayNotes(notes);
}
displayNotes((value as PluginStorage | undefined)?.notes);
});

// Register commands.
Expand Down

0 comments on commit 83a9ce2

Please sign in to comment.