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";
|
2024-07-11 20:16:28 +02:00
|
|
|
import { translation } from "../translations";
|
2024-07-09 05:26:54 +02:00
|
|
|
|
|
|
|
|
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-13 11:29:39 +02:00
|
|
|
serverAddressInput.value = serverAddressInput.value.toLowerCase();
|
2024-07-09 14:11:20 +02:00
|
|
|
currentAddress.value = serverAddressInput.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;
|
2024-07-13 11:29:39 +02:00
|
|
|
const lowerCasedAddress = currentAddress.value.toLowerCase();
|
|
|
|
|
if (!previousAddresses.value.has(lowerCasedAddress)) {
|
|
|
|
|
previousAddresses.add(lowerCasedAddress);
|
2024-07-12 20:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-09 18:54:44 +02:00
|
|
|
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 {
|
2024-07-11 20:25:54 +02:00
|
|
|
if (isMailboxActive.value == false) return;
|
2024-07-09 15:15:17 +02:00
|
|
|
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
|
|
|
|
2024-07-11 20:16:28 +02:00
|
|
|
// OUTBOX
|
|
|
|
|
export const outboxItemCount = new React.State(0);
|
|
|
|
|
export const outboxText = React.createProxyState([outboxItemCount], () =>
|
|
|
|
|
translation.outboxText(outboxItemCount.value)
|
|
|
|
|
);
|
|
|
|
|
export const outboxTextStyle = React.createProxyState([outboxItemCount], () =>
|
|
|
|
|
outboxItemCount.value == 0 ? "success" : "warning"
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-09 14:11:20 +02:00
|
|
|
// MISC
|
2024-07-12 21:11:33 +02:00
|
|
|
export const isPresentingSettingsModal = new React.State(false);
|
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-11 19:52:18 +02:00
|
|
|
export const pageZoom = React.restoreState("page-zoom", 100);
|
|
|
|
|
pageZoom.subscribe(() => {
|
|
|
|
|
document.body.style.zoom = `${pageZoom.value}%`;
|
2024-07-13 12:16:35 +02:00
|
|
|
document.body.style.webkitTextSizeAdjust = `${pageZoom.value}%`;
|
2024-07-11 19:52:18 +02:00
|
|
|
});
|
|
|
|
|
|
2024-07-12 21:11:33 +02:00
|
|
|
export function toggleSettings() {
|
|
|
|
|
isPresentingSettingsModal.value = !isPresentingSettingsModal.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function repairApp() {
|
2024-07-13 11:41:01 +02:00
|
|
|
mapSetToLowercase(previousAddresses);
|
2024-07-12 21:11:33 +02:00
|
|
|
removeSetDuplicates(previousAddresses);
|
|
|
|
|
removeSetDuplicates(usedObjectCategories);
|
|
|
|
|
removeSetDuplicates(usedObjectStatuses);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-13 11:41:01 +02:00
|
|
|
function mapSetToLowercase(listState: React.ListState<string>) {
|
|
|
|
|
const lowerCased = [...listState.value].map((x) => x.toLowerCase());
|
|
|
|
|
listState.clear();
|
|
|
|
|
listState.add(...lowerCased);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 21:11:33 +02:00
|
|
|
function removeSetDuplicates(listState: React.ListState<any>) {
|
2024-07-13 11:41:01 +02:00
|
|
|
const values = [...listState.value];
|
|
|
|
|
listState.clear();
|
|
|
|
|
listState.add(...values);
|
2024-07-12 21:11:33 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-12 22:24:54 +02:00
|
|
|
export function clearAddresses() {
|
|
|
|
|
previousAddresses.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearCategories() {
|
|
|
|
|
usedObjectCategories.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearStatuses() {
|
|
|
|
|
usedObjectStatuses.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 19:52:18 +02:00
|
|
|
const zoomStep = 10;
|
|
|
|
|
export function zoomOut() {
|
|
|
|
|
pageZoom.value -= zoomStep;
|
|
|
|
|
}
|
|
|
|
|
export function zoomIn() {
|
|
|
|
|
pageZoom.value += zoomStep;
|
|
|
|
|
}
|
2024-07-09 14:11:20 +02:00
|
|
|
|
2024-07-12 02:24:48 +02:00
|
|
|
export const usedObjectCategories =
|
|
|
|
|
React.restoreListState<string>("object-categories");
|
2024-07-12 15:09:57 +02:00
|
|
|
export const usedObjectStatuses =
|
|
|
|
|
React.restoreListState<string>("object-statuses");
|
2024-07-12 02:24:48 +02:00
|
|
|
|
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-11 21:34:21 +02:00
|
|
|
export const isShowingObjects = React.restoreState("showing-objects", 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-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() {
|
2024-07-11 21:34:21 +02:00
|
|
|
isShowingObjects.value = !isShowingObjects.value;
|
2024-07-10 19:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|