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);
+17
View File
@@ -287,6 +287,23 @@ try {
<button id="coldStartShow" class="link hidden" type="button">пройти опрос</button>
</div>
<div id="moodChips" class="chips"></div>
<div class="mood-tools">
<button id="moodCreate" class="link" type="button">+ свой профиль</button>
<button id="moodEdit" class="link hidden" type="button">редактировать</button>
<button id="moodDelete" class="link hidden" type="button">удалить</button>
</div>
<div id="moodEditor" class="mood-editor hidden">
<div class="me-grid">
<label>Название <input id="meName" maxlength="40" placeholder="autumn-cozy"></label>
<label>Описание <input id="meDesc" maxlength="200" placeholder="Осенний уют"></label>
</div>
<label>Жанры (через запятую) <input id="meGenres" placeholder="Adventure, Simulation"></label>
<label>Теги (строка: Тег: вес) <textarea id="meTags" rows="4" placeholder="Cozy: 1.5&#10;Relaxing: 1.2"></textarea></label>
<div class="me-actions">
<button id="meSave" class="primary" type="button">Сохранить</button>
<button id="meCancel" class="link" type="button">отмена</button>
</div>
</div>
<textarea id="moodText" rows="2" placeholder="например: приключение, рпг, прокачка группы, подземелья"></textarea>
<div class="options">
<label class="check"><input type="checkbox" id="optUnplayed" checked> только неигранное</label>
+15
View File
@@ -743,6 +743,20 @@ html[data-theme="light"] .game-table tbody tr:hover { background: rgba(23, 32, 5
}
.mood-panel h1 { font-size: 26px; }
.mood-panel p { margin: 6px 0 0; }
.mood-tools { display: flex; flex-wrap: wrap; gap: 12px; margin-top: -4px; }
.mood-editor {
display: flex;
flex-direction: column;
gap: 10px;
padding: 14px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--bg-2);
}
.mood-editor.hidden { display: none; }
.mood-editor label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.me-grid { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; }
.me-actions { display: flex; align-items: center; gap: 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
@@ -1118,4 +1132,5 @@ html[data-theme="light"] ::-webkit-scrollbar-thumb { background: #c9c0ad; }
.game-table .t-duration { display: none; }
.game-table .t-name { min-width: 0; }
.cover-inline { width: 28px; margin-right: 7px; }
.me-grid { grid-template-columns: 1fr; }
}