feat: add custom mood profile editor
CI / test (push) Successful in 4m55s

This commit is contained in:
strsv committed 2026-11-06 21:52:24 +03:00
1 parent d860157540
commit b865a4a389
11 files changed
+274 -9

No files matched your search

+79 -1
View File
@@ -39,6 +39,8 @@ const state = {
recommendContext: null,
lastReco: null,
itemsById: new Map(),
moods: [],
editingMood: null,
};
const $ = (sel) => document.querySelector(sel);
@@ -885,9 +887,11 @@ async function loadGames(reset = true) {
async function loadMoods() {
try {
const data = await api("/api/moods");
state.moods = data.moods;
const box = $("#moodChips");
box.innerHTML = data.moods.map((m) =>
`<button class="chip" data-preset="${esc(m.name)}" title="${esc(m.description)}">${esc(m.description)}</button>`
`<button class="chip" data-preset="${esc(m.name)}" data-builtin="${m.builtin ? "1" : "0"}"
title="${esc(m.description)}">${esc(m.description)}</button>`
).join("");
box.querySelectorAll(".chip").forEach((chip) => {
chip.addEventListener("click", () => {
@@ -900,13 +904,86 @@ async function loadMoods() {
state.preset = chip.dataset.preset;
$("#moodText").value = "";
}
updateMoodTools();
});
if (chip.dataset.preset === state.preset) chip.classList.add("active");
});
updateMoodTools();
} catch (err) {
toast(err.message);
}
}
function updateMoodTools() {
const mood = state.moods.find((item) => item.name === state.preset);
const custom = !!(mood && !mood.builtin);
$("#moodEdit").classList.toggle("hidden", !custom);
$("#moodDelete").classList.toggle("hidden", !custom);
}
function openMoodEditor(mood = null) {
state.editingMood = mood ? mood.name : null;
$("#meName").value = mood ? mood.name : "";
$("#meName").disabled = !!mood;
$("#meDesc").value = mood ? (mood.description || "") : "";
$("#meGenres").value = mood ? (mood.genres || []).join(", ") : "";
$("#meTags").value = mood
? Object.entries(mood.tags || {}).map(([tag, weight]) => `${tag}: ${weight}`).join("\n")
: "";
$("#moodEditor").classList.remove("hidden");
$("#meName").focus();
}
function closeMoodEditor() {
$("#moodEditor").classList.add("hidden");
state.editingMood = null;
}
$("#moodCreate").addEventListener("click", () => openMoodEditor());
$("#moodEdit").addEventListener("click", () => {
const mood = state.moods.find((item) => item.name === state.preset);
if (mood) openMoodEditor(mood);
});
$("#meCancel").addEventListener("click", closeMoodEditor);
$("#moodDelete").addEventListener("click", async () => {
const name = state.preset;
if (!name || !confirm(`Удалить профиль «${name}»?`)) return;
try {
await api(`/api/moods/${encodeURIComponent(name)}`, { method: "DELETE" });
state.preset = null;
await loadMoods();
toast("Профиль удалён");
} catch (err) {
toast(err.message);
}
});
$("#meSave").addEventListener("click", async () => {
const name = $("#meName").value.trim().toLowerCase();
const tags = {};
for (const line of $("#meTags").value.split(/\n+/)) {
const index = line.lastIndexOf(":");
if (index < 1) continue;
const tag = line.slice(0, index).trim();
const weight = Number(line.slice(index + 1).trim().replace(",", "."));
if (tag && Number.isFinite(weight)) tags[tag] = weight;
}
const genres = $("#meGenres").value.split(",").map((item) => item.trim()).filter(Boolean);
try {
await api("/api/moods", {
method: "POST",
body: JSON.stringify({
name, description: $("#meDesc").value.trim(), genres, tags,
}),
});
state.preset = name;
closeMoodEditor();
await loadMoods();
toast("Профиль сохранён");
} catch (err) {
toast(err.message);
}
});
document.querySelectorAll("#coldStart .chips").forEach((group) => {
group.addEventListener("click", (event) => {
const chip = event.target instanceof Element && event.target.closest(".chip");
@@ -1655,6 +1732,7 @@ if ("serviceWorker" in navigator) {
state.preset = preset;
const chip = document.querySelector(`#moodChips .chip[data-preset="${CSS.escape(preset)}"]`);
if (chip) chip.classList.add("active");
updateMoodTools();
}
if (mood) $("#moodText").value = mood;
if (preset || mood) await runRecommend(false);