This commit is contained in:
Mekan1206 committed 2026-08-16 20:29:31 +05:00
1 parent db9a8e9d7b
commit bd92137fdc
1 file changed
+41 -33
+41 -33
View File
@@ -22,7 +22,7 @@
}; };
// ========================================================================= // =========================================================================
// 1. AUDIO MANAGER (RAM Buffer + Sound Toggle) // 1. AUDIO MANAGER (Fixed Preloader & Clean Floating Sound Toggle)
// ========================================================================= // =========================================================================
const SOUND_URL = "/assets/panel/audio/sound.mp3"; const SOUND_URL = "/assets/panel/audio/sound.mp3";
const AudioContextClass = window.AudioContext || window.webkitAudioContext; const AudioContextClass = window.AudioContext || window.webkitAudioContext;
@@ -38,7 +38,6 @@
const soundedIds = new Set(); const soundedIds = new Set();
let inboxReady = false; let inboxReady = false;
// Preload audio file ONCE into memory buffer
function preloadSoundBuffer() { function preloadSoundBuffer() {
fetch(SOUND_URL, { cache: "force-cache" }) fetch(SOUND_URL, { cache: "force-cache" })
.then((res) => { .then((res) => {
@@ -89,7 +88,6 @@
function playNotifySound() { function playNotifySound() {
if (!isAudioUnlocked) return; if (!isAudioUnlocked) return;
// 1. Play from Web Audio API RAM buffer
if (audioCtx && audioCtx.state === "running" && soundBuffer) { if (audioCtx && audioCtx.state === "running" && soundBuffer) {
try { try {
const source = audioCtx.createBufferSource(); const source = audioCtx.createBufferSource();
@@ -100,7 +98,6 @@
} catch (e) { } } catch (e) { }
} }
// 2. HTML5 Audio Fallback
try { try {
fallbackAudio.currentTime = 0; fallbackAudio.currentTime = 0;
fallbackAudio.volume = 1; fallbackAudio.volume = 1;
@@ -117,59 +114,66 @@
playNotifySound(); playNotifySound();
} }
// Create & inject "Enable Sound" button into header // Inject a floating non-intrusive sound toggle inside the chat pane
function initSoundButton() { function initSoundButton() {
if (!headEl || soundButtonEl) return; if (soundButtonEl) return;
soundButtonEl = document.createElement("button"); soundButtonEl = document.createElement("button");
soundButtonEl.type = "button"; soundButtonEl.type = "button";
soundButtonEl.id = "mm-chat-sound-toggle"; soundButtonEl.id = "mm-chat-sound-toggle";
soundButtonEl.style.cssText = ` soundButtonEl.style.cssText = `
margin-left: auto; position: absolute;
padding: 4px 10px; top: 12px;
right: 12px;
z-index: 999;
padding: 5px 12px;
font-size: 12px; font-size: 12px;
border-radius: 4px; font-weight: 500;
border: 1px solid #ccc; border-radius: 6px;
background-color: #f8f9fa; border: 1px solid #cbd5e1;
background-color: #ffffff;
color: #334155;
cursor: pointer; cursor: pointer;
display: inline-flex; box-shadow: 0 2px 4px rgba(0,0,0,0.05);
align-items: center; transition: all 0.2s ease;
gap: 5px;
`; `;
soundButtonEl.addEventListener("click", (e) => { soundButtonEl.addEventListener("click", (e) => {
e.stopPropagation(); e.stopPropagation();
e.preventDefault();
unlockAudio(); unlockAudio();
playNotifySound(); // Plays test sound on click playNotifySound();
}); });
updateSoundButtonUI(); // Append to messages panel container safely without touching flex layout
const chatPane = messagesEl ? messagesEl.parentElement : document.body;
// Place header in container flex if not already if (chatPane) {
if (headEl.parentElement) { if (getComputedStyle(chatPane).position === "static") {
headEl.parentElement.style.display = "flex"; chatPane.style.position = "relative";
headEl.parentElement.style.alignItems = "center"; }
headEl.parentElement.appendChild(soundButtonEl); chatPane.appendChild(soundButtonEl);
} }
updateSoundButtonUI();
} }
function updateSoundButtonUI() { function updateSoundButtonUI() {
if (!soundButtonEl) return; if (!soundButtonEl) return;
if (isAudioUnlocked) { if (isAudioUnlocked) {
soundButtonEl.innerHTML = "🔊 Sound On"; soundButtonEl.innerHTML = "🔊 Sound On";
soundButtonEl.style.backgroundColor = "#e6fffa"; soundButtonEl.style.backgroundColor = "#f0fdf4";
soundButtonEl.style.borderColor = "#319795"; soundButtonEl.style.borderColor = "#86efac";
soundButtonEl.style.color = "#234e52"; soundButtonEl.style.color = "#166534";
} else { } else {
soundButtonEl.innerHTML = "🔇 Enable Sound"; soundButtonEl.innerHTML = "🔇 Enable Sound";
soundButtonEl.style.backgroundColor = "#fff5f5"; soundButtonEl.style.backgroundColor = "#fef2f2";
soundButtonEl.style.borderColor = "#feb2b2"; soundButtonEl.style.borderColor = "#fca5a5";
soundButtonEl.style.color = "#9b2c2c"; soundButtonEl.style.color = "#991b1b";
} }
} }
// ========================================================================= // =========================================================================
// 2. CHAT LOGIC & HELPERS // 2. CHAT HELPERS & RENDERING
// ========================================================================= // =========================================================================
function headers(json) { function headers(json) {
const next = { const next = {
@@ -261,9 +265,12 @@
function isOutgoing(message, conversation) { function isOutgoing(message, conversation) {
if (!message) return false; if (!message) return false;
if (message.tempId || message.is_outgoing || message.user?.id === "operator") return true; if (message.tempId || message.is_outgoing || message.sender_type === "admin" || message.sender_type === "operator") {
return true;
}
const otherId = conversation && conversation.user && conversation.user.id; const otherId = conversation && conversation.user && conversation.user.id;
return !otherId || message.user?.id !== otherId; if (!otherId) return true;
return String(message.user?.id) !== String(otherId);
} }
function messageKey(message) { function messageKey(message) {
@@ -424,7 +431,7 @@
conversation_id: state.activeId, conversation_id: state.activeId,
body: body, body: body,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
user: { id: "operator" } sender_type: "admin"
}; };
state.messages.push(optimisticMsg); state.messages.push(optimisticMsg);
@@ -471,6 +478,7 @@
} }
if (state.activeId === message.conversation_id) { if (state.activeId === message.conversation_id) {
// Deduplicate by message ID or text body matching optimistic message
const existingIdx = state.messages.findIndex( const existingIdx = state.messages.findIndex(
(item) => String(item.id) === String(message.id) || (item.tempId && item.body === message.body) (item) => String(item.id) === String(message.id) || (item.tempId && item.body === message.body)
); );
@@ -599,7 +607,7 @@
} }
}); });
// Global listeners to unlock audio on first page click // Global listeners to unlock audio on first interaction
const userGestureEvents = ["click", "pointerdown", "keydown", "touchstart"]; const userGestureEvents = ["click", "pointerdown", "keydown", "touchstart"];
function handleInitialGesture() { function handleInitialGesture() {
unlockAudio(); unlockAudio();