Implement pending/delivered status for messages

This commit is contained in:
2022-08-27 22:34:10 +02:00
parent b480d35cb1
commit 6f76004dc0
4 changed files with 45 additions and 17 deletions

View File

@ -148,9 +148,18 @@ export class App {
this.windows.appendChild(win);
}
addMessage(message: string) {
addMessage(id: string, message: string, delivered: boolean = true) {
const test = document.getElementById(`msg-${id}`);
if (test) return;
const msg = document.createElement("div");
msg.id = `msg-${id}`;
msg.classList.add("message");
if (delivered) {
msg.classList.add("delivered");
} else {
msg.classList.add("pending");
}
msg.innerText = message;
@ -161,6 +170,14 @@ export class App {
chat.scrollTop = chat.scrollHeight;
}
confirmMessage(id: string) {
const msg = document.getElementById(`msg-${id}`);
if (!msg) return;
msg.classList.remove("pending");
msg.classList.add("delivered");
}
addLoginWindow() {
const win = document.createElement("div");
win.classList.add("window-login");
@ -243,21 +260,28 @@ export class App {
const input = document.getElementsByTagName("input")[0];
if (!input) throw new Error("Element <input> not found");
input.addEventListener("keyup", async event => {
input.addEventListener("keydown", event => {
if (event.key === "Enter" && input.value.length > 0) {
if (!this.session) throw new Error("No session found");
this.addMessage(input.value);
const id = crypto.randomUUID();
const val = input.value;
input.value = "";
await this.session.newEvent("24595934-4540-4333-ac2b-78796ac3f25f", {
message: val,
this.addMessage(id, input.value, false);
this.session.newEvent(
"24595934-4540-4333-ac2b-78796ac3f25f",
{message: val},
id
).then(() => {
this.confirmMessage(id);
});
input.value = "";
}
});
this.session.subscribe(event => {
this.addMessage(event.data.message);
});
this.addMessage(event.id, event.data.message);
}).then();
}
}

View File

@ -9,8 +9,8 @@ window.addEventListener("DOMContentLoaded", () => {
}
const locutus = new Locutus.App();
if (isMobilePlatform()) {
console.log("MOBILE");
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === 'hidden') {
locutus.sleep();
@ -18,12 +18,11 @@ window.addEventListener("DOMContentLoaded", () => {
locutus.wakeup().then();
}
});
} else {
console.log("DESKTOP");
window.addEventListener("beforeunload", (evt) => {
locutus.close();
});
}
window.addEventListener("beforeunload", () => {
locutus.close();
});
});
function isMobilePlatform(): boolean {

View File

@ -34,7 +34,7 @@ interface EventJson {
data: {
message: string,
}
uuid: string,
id: string,
}
interface WellKnownJson {
@ -390,11 +390,12 @@ export class Session {
return response;
}
async newEvent(roomId: string, data: any) {
async newEvent(roomId: string, data: any, id: string | undefined = undefined) {
return this.send("new_event", {
room_id: roomId,
events: [{
data: data
data: data,
id: id,
}],
});
}

View File

@ -235,6 +235,10 @@ main .chat .message {
font-size: 0.875rem;
}
main .chat .message.pending {
border-color: #C00000;
}
main .chat ::-webkit-scrollbar {
width: 4px;
}