WIP
This commit is contained in:
1 parent
c9f584ce2a
commit
aa23e73b34
1 file changed
+52
-40
@@ -330,6 +330,21 @@
|
|||||||
if (!body || !state.activeId || !conversation || conversation.can_reply === false) return;
|
if (!body || !state.activeId || !conversation || conversation.can_reply === false) return;
|
||||||
|
|
||||||
sendEl.disabled = true;
|
sendEl.disabled = true;
|
||||||
|
inputEl.value = "";
|
||||||
|
|
||||||
|
// 1. OPTIMISTIC UPDATE: Render local message immediately
|
||||||
|
const tempId = "temp-" + Date.now();
|
||||||
|
const optimisticMsg = {
|
||||||
|
id: tempId,
|
||||||
|
conversation_id: state.activeId,
|
||||||
|
body: body,
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
user: { id: config.userId || "current" }
|
||||||
|
};
|
||||||
|
|
||||||
|
state.messages.push(optimisticMsg);
|
||||||
|
renderMessages();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = await request(
|
const payload = await request(
|
||||||
`${config.messagesUrl}/${state.activeId}/messages`,
|
`${config.messagesUrl}/${state.activeId}/messages`,
|
||||||
@@ -339,16 +354,24 @@
|
|||||||
body: JSON.stringify({ body }),
|
body: JSON.stringify({ body }),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
const message = payload.data;
|
const serverMsg = payload.data;
|
||||||
if (message && message.id) {
|
if (serverMsg && serverMsg.id) {
|
||||||
soundedIds.add(messageKey(message));
|
soundedIds.add(messageKey(serverMsg));
|
||||||
}
|
// Replace temp message with server message
|
||||||
if (message && !state.messages.some((item) => String(item.id) === String(message.id))) {
|
const idx = state.messages.findIndex(m => m.id === tempId);
|
||||||
state.messages.push(message);
|
if (idx !== -1) {
|
||||||
|
state.messages[idx] = serverMsg;
|
||||||
|
} else if (!state.messages.some(m => String(m.id) === String(serverMsg.id))) {
|
||||||
|
state.messages.push(serverMsg);
|
||||||
|
}
|
||||||
renderMessages();
|
renderMessages();
|
||||||
}
|
}
|
||||||
inputEl.value = "";
|
|
||||||
await loadInbox();
|
await loadInbox();
|
||||||
|
} catch (err) {
|
||||||
|
// Remove temp message if request failed
|
||||||
|
state.messages = state.messages.filter(m => m.id !== tempId);
|
||||||
|
renderMessages();
|
||||||
|
alert("Failed to send message");
|
||||||
} finally {
|
} finally {
|
||||||
sendEl.disabled = false;
|
sendEl.disabled = false;
|
||||||
inputEl.focus();
|
inputEl.focus();
|
||||||
@@ -405,18 +428,23 @@
|
|||||||
function bootEcho() {
|
function bootEcho() {
|
||||||
const pusher = config.pusher || {};
|
const pusher = config.pusher || {};
|
||||||
const EchoClass = window.Echo;
|
const EchoClass = window.Echo;
|
||||||
if (typeof EchoClass !== "function" || !window.Pusher || !pusher.key) return;
|
if (typeof EchoClass !== "function" || !window.Pusher || !pusher.key) {
|
||||||
|
console.warn("Echo/Pusher assets missing.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const endpoint = echoEndpoint(pusher);
|
const isHttps = window.location.protocol === "https:";
|
||||||
|
const host = window.location.hostname;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
state.echo = new EchoClass({
|
state.echo = new EchoClass({
|
||||||
broadcaster: "reverb",
|
broadcaster: "reverb",
|
||||||
key: pusher.key,
|
key: pusher.key,
|
||||||
wsHost: endpoint.host,
|
wsHost: host,
|
||||||
wsPort: endpoint.port,
|
// Match standard HTTPS (443) or custom port passed from backend
|
||||||
wssPort: endpoint.port,
|
wsPort: pusher.port || (isHttps ? 443 : 80),
|
||||||
forceTLS: endpoint.forceTLS,
|
wssPort: pusher.port || (isHttps ? 443 : 80),
|
||||||
|
forceTLS: isHttps,
|
||||||
enabledTransports: ["ws", "wss"],
|
enabledTransports: ["ws", "wss"],
|
||||||
disableStats: true,
|
disableStats: true,
|
||||||
cluster: "mt1",
|
cluster: "mt1",
|
||||||
@@ -429,37 +457,21 @@
|
|||||||
"X-Requested-With": "XMLHttpRequest",
|
"X-Requested-With": "XMLHttpRequest",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
authorizer: (channel) => ({
|
|
||||||
authorize: (socketId, callback) => {
|
|
||||||
fetch(config.broadcastAuthPath || config.broadcastAuth || "/panel/broadcasting/auth", {
|
|
||||||
method: "POST",
|
|
||||||
credentials: "same-origin",
|
|
||||||
headers: headers(true),
|
|
||||||
body: JSON.stringify({
|
|
||||||
socket_id: socketId,
|
|
||||||
channel_name: channel.name,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
.then(async (response) => {
|
|
||||||
let data = {};
|
|
||||||
try {
|
|
||||||
data = await response.json();
|
|
||||||
} catch (error) {
|
|
||||||
data = {};
|
|
||||||
}
|
|
||||||
if (!response.ok) {
|
|
||||||
callback(true, data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
callback(false, data);
|
|
||||||
})
|
|
||||||
.catch((error) => callback(true, error));
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Debugging connection states in console
|
||||||
|
if (state.echo.connector && state.echo.connector.pusher) {
|
||||||
|
state.echo.connector.pusher.connection.bind("state_change", (states) => {
|
||||||
|
console.log("[Reverb WS State]:", states.current);
|
||||||
|
});
|
||||||
|
state.echo.connector.pusher.connection.bind("error", (err) => {
|
||||||
|
console.error("[Reverb WS Error]:", err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
state.echo.private(config.inboxChannel || "chat.support").listen(".message.sent", upsertIncoming);
|
state.echo.private(config.inboxChannel || "chat.support").listen(".message.sent", upsertIncoming);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error("Failed to initialize Echo:", err);
|
||||||
state.echo = null;
|
state.echo = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user