Files
splirate/history.js

168 lines
5.3 KiB
JavaScript

"use strict";
const statusEl = document.getElementById("status");
const listEl = document.getElementById("historyList");
const clearHistoryBtn = document.getElementById("clearHistoryBtn");
function isUserCancelledError(result) {
const error = result && typeof result.error === "string" ? result.error.toLowerCase() : "";
return error.includes("canceled by the user") || error.includes("cancelled by the user");
}
function formatMeta(entry) {
const packName = entry && entry.packName ? entry.packName : "n/a";
const key = entry && entry.key ? entry.key : "n/a";
const bpm = entry && entry.bpm ? entry.bpm : "n/a";
return { packName, key, bpm };
}
function formatTags(entry) {
if (!entry || !Array.isArray(entry.tags) || !entry.tags.length) {
return "No tags";
}
return entry.tags.join(" • ");
}
function createDownloadIcon() {
const ns = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(ns, "svg");
svg.setAttribute("viewBox", "0 0 24 24");
svg.setAttribute("width", "22");
svg.setAttribute("height", "22");
svg.setAttribute("aria-hidden", "true");
const arrow = document.createElementNS(ns, "path");
arrow.setAttribute("d", "M12 4v10m0 0l4-4m-4 4l-4-4");
arrow.setAttribute("fill", "none");
arrow.setAttribute("stroke", "currentColor");
arrow.setAttribute("stroke-width", "2");
arrow.setAttribute("stroke-linecap", "round");
arrow.setAttribute("stroke-linejoin", "round");
const tray = document.createElementNS(ns, "path");
tray.setAttribute("d", "M4 18v1a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-1");
tray.setAttribute("fill", "none");
tray.setAttribute("stroke", "currentColor");
tray.setAttribute("stroke-width", "2");
tray.setAttribute("stroke-linecap", "round");
tray.setAttribute("stroke-linejoin", "round");
svg.appendChild(arrow);
svg.appendChild(tray);
return svg;
}
function buildItem(entry) {
const li = document.createElement("li");
li.className = "history-item";
const meta = formatMeta(entry);
const head = document.createElement("div");
head.className = "history-head";
const cover = document.createElement("img");
cover.className = "history-cover-image";
cover.src = entry.coverImageUrl || "logo-single.png";
cover.alt = "Cover art";
cover.loading = "lazy";
const info = document.createElement("div");
info.className = "history-info";
const title = document.createElement("p");
title.className = "history-title";
title.textContent = entry.fileName || entry.sampleId || "(unknown filename)";
const subtitle = document.createElement("p");
subtitle.className = "history-subtitle";
const mode = entry.mode === "full" || entry.fullUrl ? "full candidate" : "preview fallback";
subtitle.textContent = `${meta.packName}${mode}`;
const badges = document.createElement("div");
badges.className = "history-badges";
const keyBadge = document.createElement("span");
keyBadge.className = "badge";
keyBadge.textContent = `Key ${meta.key}`;
const bpmBadge = document.createElement("span");
bpmBadge.className = "badge";
bpmBadge.textContent = `BPM ${meta.bpm}`;
badges.appendChild(keyBadge);
badges.appendChild(bpmBadge);
const tags = document.createElement("p");
tags.className = "history-tags";
tags.textContent = formatTags(entry);
info.appendChild(title);
info.appendChild(subtitle);
info.appendChild(badges);
info.appendChild(tags);
const download = document.createElement("button");
download.type = "button";
download.className = "download-icon-btn";
download.setAttribute("aria-label", "Download sample");
download.setAttribute("title", "Download sample");
download.appendChild(createDownloadIcon());
download.addEventListener("click", async () => {
const result = await browser.runtime.sendMessage({
type: "DOWNLOAD_ENTRY",
entry
});
if (!result || !result.ok) {
if (isUserCancelledError(result)) {
return;
}
window.alert((result && result.error) || "Download failed.");
return;
}
statusEl.textContent = result.mode === "full" ? "Downloaded full file URL." : "Downloaded decoded preview fallback.";
});
head.appendChild(cover);
head.appendChild(info);
head.appendChild(download);
li.appendChild(head);
return li;
}
async function loadHistory() {
const response = await browser.runtime.sendMessage({ type: "GET_HISTORY" });
if (!response || !response.ok) {
statusEl.textContent = "Unable to load history.";
return;
}
const rows = Array.isArray(response.history) ? response.history : [];
if (!rows.length) {
statusEl.textContent = "No captured samples yet.";
return;
}
statusEl.textContent = `Total captured URLs: ${rows.length}`;
listEl.innerHTML = "";
for (const entry of rows) {
listEl.appendChild(buildItem(entry));
}
}
loadHistory().catch(() => {
statusEl.textContent = "Unexpected error while loading history.";
});
if (clearHistoryBtn) {
clearHistoryBtn.addEventListener("click", async () => {
const confirmed = window.confirm("Clear all captured samples and history?");
if (!confirmed) {
return;
}
const result = await browser.runtime.sendMessage({ type: "CLEAR_CAPTURED_STATE" });
if (!result || !result.ok) {
statusEl.textContent = "Unable to clear captured samples.";
return;
}
await loadHistory();
statusEl.textContent = "Captured samples cleared.";
});
}