Show visitor online state in chat list
This commit is contained in:
+24
-1
@@ -3,6 +3,7 @@ const state = {
|
||||
snippet: "",
|
||||
conversations: [],
|
||||
visitors: [],
|
||||
visitorsLoaded: false,
|
||||
activeId: null,
|
||||
active: null,
|
||||
events: null,
|
||||
@@ -237,12 +238,16 @@ async function loadBootstrap() {
|
||||
async function loadConversations() {
|
||||
const data = await api("/api/admin/conversations");
|
||||
state.conversations = data.conversations;
|
||||
syncConversationVisitors();
|
||||
renderConversations();
|
||||
}
|
||||
|
||||
async function loadVisitors() {
|
||||
const data = await api("/api/admin/visitors");
|
||||
state.visitors = data.visitors;
|
||||
state.visitorsLoaded = true;
|
||||
syncConversationVisitors();
|
||||
renderConversations();
|
||||
renderVisitors();
|
||||
}
|
||||
|
||||
@@ -253,6 +258,7 @@ async function openConversation(id) {
|
||||
state.active = data;
|
||||
const index = state.conversations.findIndex((item) => item.id === id);
|
||||
if (index >= 0) state.conversations[index] = { ...state.conversations[index], ...data.conversation };
|
||||
syncConversationVisitors();
|
||||
renderConversations();
|
||||
renderActive();
|
||||
}
|
||||
@@ -271,6 +277,9 @@ function connectEvents() {
|
||||
}
|
||||
if (data.type === "visitors:update") {
|
||||
state.visitors = data.visitors;
|
||||
state.visitorsLoaded = true;
|
||||
syncConversationVisitors();
|
||||
renderConversations();
|
||||
renderVisitors();
|
||||
return;
|
||||
}
|
||||
@@ -311,7 +320,8 @@ function renderConversations() {
|
||||
<span class="row visitor-row">
|
||||
<span class="visitor-name-wrap">
|
||||
<span class="unread-dot" aria-label="${item.hasUnread ? "Neprecteno" : "Precteno"}"></span>
|
||||
<strong>${escapeHtml(item.visitor.name || item.visitor.email || "Navstevnik")}</strong>
|
||||
<strong>${escapeHtml(conversationVisitorName(item))}</strong>
|
||||
${item.visitorOnline ? `<span class="online-dot" aria-label="Online" title="Online"></span>` : ""}
|
||||
</span>
|
||||
${item.imageCount ? `<span class="photo-count">foto ${item.imageCount}</span>` : ""}
|
||||
</span>
|
||||
@@ -332,6 +342,19 @@ function renderConversations() {
|
||||
`).join("") : `<p class="empty-list">Zadny chat pro vybrany filtr.</p>`;
|
||||
}
|
||||
|
||||
function syncConversationVisitors() {
|
||||
if (!state.visitorsLoaded) return;
|
||||
const onlineIds = new Set(state.visitors.map((visitor) => visitor.id));
|
||||
state.conversations = state.conversations.map((item) => ({
|
||||
...item,
|
||||
visitorOnline: onlineIds.has(item.visitorToken)
|
||||
}));
|
||||
}
|
||||
|
||||
function conversationVisitorName(item) {
|
||||
return item.visitor?.name || item.visitor?.email || item.visitorLabel || "Navstevnik";
|
||||
}
|
||||
|
||||
function renderVisitors() {
|
||||
const visitors = state.hideBots ? state.visitors.filter((visitor) => !visitor.isBot) : state.visitors;
|
||||
const botCount = state.visitors.filter((visitor) => visitor.isBot).length;
|
||||
|
||||
Reference in New Issue
Block a user