2024-07-09 05:26:54 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
import { Chat, createChatWithName } from "./chatModel";
|
2024-07-09 14:11:20 +02:00
|
|
|
|
2024-07-09 05:26:54 +02:00
|
|
|
import UDNFrontend from "udn-frontend";
|
|
|
|
|
|
|
|
|
|
export const UDN = new UDNFrontend();
|
|
|
|
|
|
|
|
|
|
// CONNECTION
|
2024-07-09 14:11:20 +02:00
|
|
|
export const isConnected = new React.State(false);
|
|
|
|
|
|
|
|
|
|
export const serverAddressInput = React.restoreState("socket-address", "");
|
2024-07-09 14:18:38 +02:00
|
|
|
export const didRequestConnection = React.restoreState(
|
|
|
|
|
"did-request-connection",
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
export const currentAddress = React.restoreState("current-address", "");
|
2024-07-09 20:20:25 +02:00
|
|
|
export const previousAddresses =
|
|
|
|
|
React.restoreListState<string>("previous-addresses");
|
2024-07-09 05:26:54 +02:00
|
|
|
|
|
|
|
|
export const cannotDisonnect = React.createProxyState(
|
|
|
|
|
[isConnected],
|
|
|
|
|
() => isConnected.value == false
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const cannotConnect = React.createProxyState(
|
2024-07-09 14:11:20 +02:00
|
|
|
[serverAddressInput, currentAddress, isConnected],
|
2024-07-09 05:26:54 +02:00
|
|
|
() =>
|
2024-07-09 14:11:20 +02:00
|
|
|
serverAddressInput.value == "" ||
|
|
|
|
|
(currentAddress.value == serverAddressInput.value &&
|
|
|
|
|
isConnected.value == true)
|
2024-07-09 05:26:54 +02:00
|
|
|
);
|
|
|
|
|
|
2024-07-09 14:18:38 +02:00
|
|
|
export const cannotResetAddress = React.createProxyState(
|
|
|
|
|
[serverAddressInput, currentAddress],
|
|
|
|
|
() => serverAddressInput.value == currentAddress.value
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-09 05:26:54 +02:00
|
|
|
// methods
|
|
|
|
|
export function connect(): void {
|
|
|
|
|
if (cannotConnect.value == true) return;
|
2024-07-09 15:15:17 +02:00
|
|
|
|
2024-07-09 14:11:20 +02:00
|
|
|
currentAddress.value = serverAddressInput.value;
|
2024-07-09 20:09:09 +02:00
|
|
|
previousAddresses.add(currentAddress.value);
|
2024-07-09 14:18:38 +02:00
|
|
|
|
2024-07-09 05:26:54 +02:00
|
|
|
isConnected.value = false;
|
2024-07-09 14:18:38 +02:00
|
|
|
didRequestConnection.value = true;
|
|
|
|
|
|
2024-07-09 14:11:20 +02:00
|
|
|
UDN.connect(serverAddressInput.value);
|
2024-07-09 23:25:51 +02:00
|
|
|
|
|
|
|
|
isMailboxActive.value = false;
|
2024-07-09 05:26:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function disconnect(): void {
|
2024-07-09 14:18:38 +02:00
|
|
|
didRequestConnection.value = false;
|
2024-07-09 05:26:54 +02:00
|
|
|
if (cannotDisonnect.value == true) return;
|
|
|
|
|
UDN.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 14:11:20 +02:00
|
|
|
export function resetAddressInput(): void {
|
2024-07-09 14:18:38 +02:00
|
|
|
serverAddressInput.value = currentAddress.value;
|
2024-07-09 14:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-09 18:54:44 +02:00
|
|
|
export function subscribeChannels() {
|
2024-07-09 17:23:29 +02:00
|
|
|
chats.value.forEach((chat) => {
|
|
|
|
|
UDN.subscribe(chat.primaryChannel.value);
|
|
|
|
|
});
|
2024-07-09 18:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// listeners
|
|
|
|
|
UDN.onconnect = () => {
|
|
|
|
|
isConnected.value = true;
|
|
|
|
|
subscribeChannels();
|
|
|
|
|
if (mailboxId.value != "") UDN.connectMailbox(mailboxId.value);
|
2024-07-09 05:26:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UDN.onmessage = (data) => {
|
2024-07-09 11:39:23 +02:00
|
|
|
chats.value.forEach((chat) => {
|
|
|
|
|
chat.onmessage(data);
|
2024-07-09 14:11:20 +02:00
|
|
|
});
|
2024-07-09 05:26:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UDN.ondisconnect = () => {
|
|
|
|
|
isConnected.value = false;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-09 14:11:20 +02:00
|
|
|
// MAILBOX
|
|
|
|
|
export const mailboxId = React.restoreState("mailbox-id", "");
|
|
|
|
|
export const isMailboxActive = new React.State(false);
|
|
|
|
|
|
|
|
|
|
export const cannotDeleteMailbox = React.createProxyState(
|
2024-07-09 22:05:08 +02:00
|
|
|
[mailboxId, isMailboxActive, isConnected],
|
|
|
|
|
() =>
|
|
|
|
|
mailboxId.value == "" ||
|
|
|
|
|
isMailboxActive.value == false ||
|
|
|
|
|
isConnected.value == false
|
2024-07-09 14:11:20 +02:00
|
|
|
);
|
|
|
|
|
export const cannotRequestMailbox = React.createProxyState(
|
|
|
|
|
[isConnected, isMailboxActive],
|
|
|
|
|
() => isConnected.value == false || isMailboxActive.value == true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export function requestMailbox(): void {
|
|
|
|
|
if (cannotRequestMailbox.value == true) return;
|
|
|
|
|
UDN.requestMailbox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function deleteMailbox(): void {
|
|
|
|
|
if (cannotDeleteMailbox.value == true) return;
|
|
|
|
|
UDN.deleteMailbox(mailboxId.value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 15:15:17 +02:00
|
|
|
export function updateMailbox(): void {
|
|
|
|
|
deleteMailbox();
|
2024-07-09 20:20:25 +02:00
|
|
|
setTimeout(() => requestMailbox(), 10);
|
2024-07-09 15:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-09 14:11:20 +02:00
|
|
|
UDN.onmailboxcreate = (id) => {
|
|
|
|
|
mailboxId.value = id;
|
|
|
|
|
UDN.connectMailbox(id);
|
2024-07-09 14:18:38 +02:00
|
|
|
};
|
2024-07-09 14:11:20 +02:00
|
|
|
|
2024-07-09 23:25:51 +02:00
|
|
|
UDN.onmailboxconnect = (id) => {
|
2024-07-09 14:11:20 +02:00
|
|
|
isMailboxActive.value = true;
|
2024-07-09 14:18:38 +02:00
|
|
|
};
|
2024-07-09 14:11:20 +02:00
|
|
|
|
|
|
|
|
UDN.onmailboxdelete = () => {
|
|
|
|
|
isMailboxActive.value = false;
|
|
|
|
|
mailboxId.value = "";
|
2024-07-09 18:54:44 +02:00
|
|
|
subscribeChannels();
|
2024-07-09 14:18:38 +02:00
|
|
|
};
|
2024-07-09 14:11:20 +02:00
|
|
|
|
|
|
|
|
// MISC
|
2024-07-09 17:23:29 +02:00
|
|
|
export const isEncryptionAvailable = window.crypto.subtle != undefined;
|
2024-07-09 14:11:20 +02:00
|
|
|
export const senderName = React.restoreState("sender-name", "");
|
|
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
// CHAT
|
|
|
|
|
export const chats = new React.ListState<Chat>();
|
|
|
|
|
export const chatIds = React.restoreListState<string>("chat-ids");
|
|
|
|
|
|
2024-07-09 14:11:20 +02:00
|
|
|
export const selectedChat = new React.State<Chat | undefined>(undefined);
|
2024-07-10 19:07:58 +02:00
|
|
|
export const isShowingChatTools = new React.State(false);
|
2024-07-09 15:15:17 +02:00
|
|
|
export const newChatName = new React.State("");
|
|
|
|
|
|
|
|
|
|
export const cannotCreateChat = React.createProxyState(
|
|
|
|
|
[newChatName],
|
|
|
|
|
() => newChatName.value == ""
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export function createChat() {
|
|
|
|
|
if (cannotCreateChat.value == true) return;
|
2024-07-09 17:23:29 +02:00
|
|
|
|
2024-07-09 15:15:17 +02:00
|
|
|
createChatWithName(newChatName.value);
|
|
|
|
|
newChatName.value = "";
|
|
|
|
|
}
|
2024-07-09 14:11:20 +02:00
|
|
|
|
2024-07-09 15:15:17 +02:00
|
|
|
export function closeChatView() {
|
2024-07-10 19:07:58 +02:00
|
|
|
isShowingChatTools.value = false;
|
2024-07-09 14:28:13 +02:00
|
|
|
selectedChat.value = undefined;
|
|
|
|
|
document.getElementById("settings-tab")?.scrollIntoView();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
export function selectChat(chat: Chat) {
|
|
|
|
|
selectedChat.value = chat;
|
2024-07-10 15:40:25 +02:00
|
|
|
chat.hasUnreadMessages.value = false;
|
2024-07-09 17:35:32 +02:00
|
|
|
document.getElementById("message-tab")?.scrollIntoView();
|
2024-07-09 17:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-10 19:07:58 +02:00
|
|
|
export function toggleChatTools() {
|
|
|
|
|
isShowingChatTools.value = !isShowingChatTools.value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
chatIds.value.forEach((id) => chats.add(new Chat(id)));
|
|
|
|
|
|
2024-07-09 05:26:54 +02:00
|
|
|
// INIT
|
2024-07-09 14:18:38 +02:00
|
|
|
if (serverAddressInput.value != "" && didRequestConnection.value == true) {
|
2024-07-09 05:26:54 +02:00
|
|
|
connect();
|
|
|
|
|
}
|