Improve admin chat controls

This commit is contained in:
rajchales-monito
2026-07-23 22:35:36 +02:00
parent f2b388779d
commit 8c71dbf1bf
4 changed files with 211 additions and 36 deletions
+99 -23
View File
@@ -4,7 +4,8 @@ const state = {
conversations: [],
activeId: null,
active: null,
events: null
events: null,
statusFilter: "active"
};
const $ = (selector) => document.querySelector(selector);
@@ -78,13 +79,30 @@ $("#replyForm textarea").addEventListener("input", debounce(() => {
api(`/api/admin/conversations/${state.activeId}/typing`, { method: "POST" }).catch(() => {});
}, 600));
$("#statusSelect").addEventListener("change", async () => {
if (!state.activeId) return;
await api(`/api/admin/conversations/${state.activeId}/status`, {
method: "PATCH",
body: { status: $("#statusSelect").value }
});
await loadConversations();
$("#conversationFilter").addEventListener("change", () => {
state.statusFilter = $("#conversationFilter").value;
renderConversations();
});
$("#conversationList").addEventListener("click", async (event) => {
const photoButton = event.target.closest("[data-photo-url]");
const openButton = event.target.closest("[data-open]");
const deleteButton = event.target.closest("[data-delete]");
if (photoButton) {
openPhotoPreview(photoButton.dataset.photoUrl, photoButton.dataset.photoName);
return;
}
if (deleteButton) {
await deleteConversation(deleteButton.dataset.delete);
return;
}
if (openButton) await openConversation(openButton.dataset.open);
});
$("#conversationList").addEventListener("change", async (event) => {
const statusSelect = event.target.closest("[data-status-for]");
if (!statusSelect) return;
await updateConversationStatus(statusSelect.dataset.statusFor, statusSelect.value);
});
$("#messages").addEventListener("click", (event) => {
@@ -140,7 +158,11 @@ function connectEvents() {
return;
}
await loadConversations();
if (state.activeId) await openConversation(state.activeId);
if (data.type === "conversation:delete" && data.conversationId === state.activeId) {
clearActiveConversation();
return;
}
if (state.activeId) await openConversation(state.activeId).catch(() => clearActiveConversation());
if (data.type === "conversation:new" || data.type === "message:new") notify();
});
}
@@ -164,21 +186,29 @@ function renderSite() {
}
function renderConversations() {
$("#conversationList").innerHTML = state.conversations.map((item) => `
<button class="conversation-item ${item.id === state.activeId ? "active" : ""} ${item.status === "new" ? "fresh" : ""}" data-id="${item.id}">
<span class="row"><strong>${escapeHtml(item.visitor.name || item.visitor.email || "Navstevnik")}</strong><span class="status">${item.status}</span></span>
<span class="preview">${escapeHtml(item.lastBody || "")}</span>
<span class="meta">${flag(item.countryCode)} ${escapeHtml(item.language || "")} ${escapeHtml(item.currentUrl || "")}</span>
</button>
`).join("");
for (const button of document.querySelectorAll(".conversation-item")) {
button.addEventListener("click", () => openConversation(button.dataset.id));
}
const conversations = filteredConversations();
$("#conversationList").innerHTML = conversations.length ? conversations.map((item) => `
<article class="conversation-item ${item.id === state.activeId ? "active" : ""} ${item.status === "new" ? "fresh" : ""}">
<button class="conversation-open" type="button" data-open="${item.id}">
<span class="row"><strong>${escapeHtml(item.visitor.name || item.visitor.email || "Navstevnik")}</strong>${item.imageCount ? `<span class="photo-count">foto ${item.imageCount}</span>` : ""}</span>
<span class="preview">${escapeHtml(item.lastBody || "")}</span>
<span class="meta">${flag(item.countryCode)} ${escapeHtml(item.language || "")} ${escapeHtml(item.currentUrl || "")}</span>
</button>
${item.firstImageUrl ? `<button class="list-photo" type="button" data-photo-url="${escapeHtml(item.firstImageUrl)}" data-photo-name="Fotka v chatu"><img src="${escapeHtml(item.firstImageUrl)}" alt="Fotka v chatu"></button>` : ""}
<div class="conversation-actions">
<select class="status-select ${item.status}" data-status-for="${item.id}" aria-label="Zmenit stav chatu">
${statusOptions(item.status)}
</select>
<button class="delete-chat" type="button" data-delete="${item.id}" aria-label="Smazat chat" title="Smazat chat">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 3h6l1 2h4v2H4V5h4l1-2Zm-2 6h10l-.7 11H7.7L7 9Zm3 2 .2 7h1.6l-.2-7H10Zm4 0-.2 7h1.6l.2-7H14Z"></path></svg>
</button>
</div>
</article>
`).join("") : `<p class="empty-list">Zadny chat pro vybrany filtr.</p>`;
}
function renderActive() {
const { conversation, messages } = state.active;
const statusSelect = $("#statusSelect");
$("#conversationHeader").innerHTML = `
<div>
<h2>${escapeHtml(conversation.visitor.name || conversation.visitor.email || "Navstevnik")}</h2>
@@ -186,9 +216,6 @@ function renderActive() {
<p>${escapeHtml(conversation.device.userAgent || "")}</p>
</div>
`;
$("#conversationHeader").append(statusSelect);
statusSelect.classList.remove("hidden");
statusSelect.value = conversation.status;
$("#replyForm").classList.remove("hidden");
$("#messages").innerHTML = messages.map((message) => `
<article class="message ${message.senderType}">
@@ -200,6 +227,55 @@ function renderActive() {
$("#messages").scrollTop = $("#messages").scrollHeight;
}
function filteredConversations() {
if (state.statusFilter === "all") return state.conversations;
if (state.statusFilter === "active") return state.conversations.filter((item) => item.status === "new" || item.status === "open");
return state.conversations.filter((item) => item.status === state.statusFilter);
}
function statusOptions(currentStatus) {
return [
["new", "nove"],
["open", "otevrene"],
["resolved", "vyresene"],
["spam", "spam"]
].map(([value, label]) => `<option value="${value}" ${value === currentStatus ? "selected" : ""}>${label}</option>`).join("");
}
async function updateConversationStatus(id, status) {
await api(`/api/admin/conversations/${id}/status`, {
method: "PATCH",
body: { status }
});
const item = state.conversations.find((conversation) => conversation.id === id);
if (item) item.status = status;
if (state.active?.conversation?.id === id) state.active.conversation.status = status;
renderConversations();
}
async function deleteConversation(id) {
const item = state.conversations.find((conversation) => conversation.id === id);
const label = item?.visitor?.name || item?.lastBody || "tento chat";
if (!confirm(`Smazat ${label}? Tahle akce nejde vratit.`)) return;
await api(`/api/admin/conversations/${id}`, { method: "DELETE" });
state.conversations = state.conversations.filter((conversation) => conversation.id !== id);
if (state.activeId === id) clearActiveConversation();
renderConversations();
}
function clearActiveConversation() {
state.activeId = null;
state.active = null;
$("#conversationHeader").innerHTML = `
<div>
<h2>Vyber konverzaci</h2>
<p>Nova konverzace se tady zvyrazni a pusti zvuk.</p>
</div>
`;
$("#messages").innerHTML = "";
$("#replyForm").classList.add("hidden");
}
function renderAttachments(attachments) {
if (!attachments.length) return "";
return `<div class="attachments">${attachments.map((attachment) => {