This commit is contained in:
Mekan1206
2026-08-16 20:29:31 +05:00
parent db9a8e9d7b
commit bd92137fdc

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