Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 34 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,19 @@ 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.
const addNoteStorage = (
const addNoteStorage = async (
caido: Caido,
datetime: string,
note: string,
projectName?: string
projectName?: string,
) => {
let storage = caido.storage.get() as PluginStorage | undefined;
if (!storage) {
storage = { notes: [] };
}

const updatedNotes = [...storage.notes, { datetime, note, projectName }];
caido.storage.set({ ...storage, notes: updatedNotes });
const currentNotes = getNotes(caido);
const updatedNotes = [...currentNotes, { datetime, note, projectName }];
await caido.storage.set({ notes: updatedNotes });

// Print added note to console.
console.log("Added Note:", { datetime, note, projectName });
Expand Down Expand Up @@ -69,16 +61,9 @@ const addNoteMenu = async (caido: Caido) => {
if (projectData) {
const projectName = projectData.name || "No Project Selected";
const datetime = new Date().toLocaleString();
const row = table.insertRow();
const datetimeCell = row.insertCell();
const inputCell = row.insertCell();

datetimeCell.textContent = `${datetime} Project: ${projectName}`;
datetimeCell.classList.add("datetime-cell");
inputCell.textContent = currentSelect;

// Add the note to storage.
addNoteStorage(caido, datetime, currentSelect, projectName);
await addNoteStorage(caido, datetime, currentSelect, projectName);
}
}
};
Expand Down Expand Up @@ -130,16 +115,9 @@ const addPage = (caido: Caido) => {
const project = await caido.graphql.currentProject();
const projectData = project?.currentProject;
const projectName = projectData?.name || "No Project Selected";
const row = table.insertRow();
const datetimeCell = row.insertCell();
const inputCell = row.insertCell();

datetimeCell.textContent = `${datetime} Project: ${projectName}`;
datetimeCell.classList.add("datetime-cell");
inputCell.textContent = inputValue;

// Add the note to storage.
addNoteStorage(caido, datetime, inputValue, projectName);
await addNoteStorage(caido, datetime, inputValue, projectName);

// Clear textarea and reset value.
inputValue = "";
Expand Down Expand Up @@ -178,23 +156,38 @@ const addPage = (caido: Caido) => {
});
};

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();
const noteCell = row.insertCell();

datetimeCell.textContent = `${note.datetime} Project: ${note.projectName}`;
datetimeCell.classList.add("datetime-cell");
noteCell.textContent = note.note;
});
};

export const init = (caido: Caido) => {
// Retrieve notes from storage
const notes = getNotes(caido);
console.log("Current notes:", notes);

// Populate table with stored notes.
if (notes && notes.length > 0) {
notes.forEach((note) => {
const row = table.insertRow();
const datetimeCell = row.insertCell();
const noteCell = row.insertCell();

datetimeCell.textContent = `${note.datetime} Project: ${note.projectName}`;
datetimeCell.classList.add("datetime-cell");
noteCell.textContent = note.note;
});
}
displayNotes(notes);

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

// Register commands.
// Commands are registered with a unique identifier and a handler function.
Expand Down