Files
CT/src/ViewModel/Global/coreViewModel.ts
T

239 lines
6.9 KiB
TypeScript
Raw Normal View History

2026-04-18 11:55:01 +00:00
import { v4 } from "uuid";
2026-09-22 21:57:22 +02:00
import * as React from "../../react";
import { allTranslations, Translations } from "../../View/translations";
2026-09-25 19:05:36 +02:00
import { formatTime, HandlerManager } from "../../Model/Utility/utility";
import StorageModel from "../../Model/Global/storageModel";
2026-04-18 11:55:01 +00:00
import SettingsModel from "../../Model/Global/settingsModel";
import FileTransferModel from "../../Model/Global/fileTransferModel";
import ConnectionModel from "../../Model/Global/connectionModel";
import ChatListModel from "../../Model/Chat/chatListModel";
2024-07-25 16:02:49 +02:00
export default class CoreViewModel {
swRegistration: ServiceWorkerRegistration|undefined = undefined;
version: React.State<string> = new React.State("");
latestVersion: React.State<string> = new React.State("");
noUpdateAvailable: React.State<boolean> = React.createProxyState([this.latestVersion], () => {
return (this.latestVersion.value == "" || this.latestVersion.value == this.version.value)
})
2026-09-25 00:03:20 +02:00
updateText: React.State<string> = new React.State("");
translations: Translations;
2026-04-08 09:56:37 +00:00
2026-04-12 19:20:37 +00:00
// CONTEXT
private contextStack = new Map<string, Context>();
2026-04-14 17:19:35 +00:00
2026-04-12 19:20:37 +00:00
get contexts(): Context[] {
return [...this.contextStack.values()];
}
get context(): Context | undefined {
return this.contexts.pop();
}
set context(context: Context) {
if (this.contextStack.has(context.contextId)) return;
this.contextStack.set(context.contextId, context);
2026-04-14 17:19:35 +00:00
history.pushState({ id: context.contextId }, "");
2026-04-12 19:20:37 +00:00
}
2026-04-14 17:19:35 +00:00
2026-04-15 12:47:10 +00:00
closeContext = (
contextId: string,
fromHistoryEvent: boolean = false,
): void => {
2026-04-13 11:23:17 +00:00
if (
!this.contexts
.map((context) => context.contextId)
.includes(contextId)
)
return;
2026-04-14 17:19:35 +00:00
2026-04-12 19:20:37 +00:00
while (this.contexts.length > 0) {
2026-09-23 00:09:09 +02:00
const currentContext: Context | undefined = this.context;
if (!currentContext) return;
2026-04-14 17:19:35 +00:00
currentContext.handleContextClose(fromHistoryEvent);
2026-04-12 19:20:37 +00:00
this.contextStack.delete(currentContext.contextId);
if (currentContext.contextId == contextId) break;
}
};
handleKeyDown = (e: KeyboardEvent): void => {
2026-09-23 00:09:09 +02:00
if (!(e instanceof KeyboardEvent))
return console.trace("NOT A KEY EVENT");
2026-04-13 11:23:17 +00:00
if (CoreViewModel.checkIsKeystroke(e) == false) return;
2026-04-14 09:41:02 +00:00
e.preventDefault();
2026-04-13 11:23:17 +00:00
const contexts: Context[] = this.contexts;
2026-04-12 19:20:37 +00:00
while (contexts.length > 0) {
2026-04-13 11:23:17 +00:00
const currentContext: Context | undefined = contexts.pop();
2026-09-09 00:04:27 +02:00
if (!currentContext) return;
2026-04-12 19:20:37 +00:00
const isHandled: boolean = currentContext.handleKeystroke(e);
if (isHandled == true) break;
}
};
2026-04-16 11:56:42 +00:00
// CHRON
chronHandlerManager = new HandlerManager<void>();
todayDate = new React.State<Date>(new Date());
time = new React.State<string>("99:99");
2026-09-25 19:05:36 +02:00
get unwrappedTodayDate(): Date {
2026-04-16 11:56:42 +00:00
return new Date(this.todayDate.value);
}
startChron = (): void => {
setInterval(this.chronHandlerManager.trigger, 1000);
2026-04-16 11:56:42 +00:00
};
handleChron = (): void => {
2026-09-25 19:05:36 +02:00
const newDate = new Date();
if (this.unwrappedTodayDate.toDateString() != newDate.toDateString())
this.todayDate.value = new Date();
if (formatTime(newDate) != this.time.value)
this.time.value = formatTime(newDate);
2026-04-16 11:56:42 +00:00
};
2026-04-01 08:29:39 +00:00
// DRAG & DROP
draggedObject: React.State<any> = new React.State<any>(undefined);
// OFFLINE MODE & UPDATE
configureServiceWorker = async (): Promise<void> => {
if ("serviceWorker" in navigator) {
try {
this.swRegistration = await navigator.serviceWorker.register(
"sw.js",
{
scope: "/",
},
);
if (this.swRegistration.active) {
console.log("Service worker active");
}
} catch (error) {
console.error(`Could not install service worker: ${error}`);
}
}
2026-09-25 00:03:20 +02:00
}
checkUpdates = (): void => {
fetch("/version").then(async response => {
this.version.value = await response.text();
});
fetch("/latestVersion").then(async response => {
this.latestVersion.value = (await response.text()).replace("\n", "");
})
}
update = async (): Promise<void> => {
if (!this.swRegistration) return;
await this.swRegistration.update();
2026-09-25 20:11:33 +02:00
setTimeout(()=>{
window.location.reload();
2026-09-25 20:11:33 +02:00
}, 500);
}
// init
constructor(
public readonly storageModel: StorageModel,
public readonly settingsModel: SettingsModel,
public readonly connectionModel: ConnectionModel,
public readonly chatListModel: ChatListModel,
public readonly fileTransferModel: FileTransferModel,
) {
this.translations =
allTranslations[settingsModel.language] || allTranslations.en;
2026-09-25 00:03:20 +02:00
this.latestVersion.subscribe(latestVersion => {
this.updateText.value = this.translations.homePage.updateButton(latestVersion);
});
document.body.addEventListener("keydown", this.handleKeyDown);
window.onpopstate = () => {
if (!this.context) return;
this.closeContext(this.context.contextId, true);
};
this.configureServiceWorker();
2026-09-25 00:03:20 +02:00
this.checkUpdates();
this.startChron();
this.chronHandlerManager.setHandler(
"core-view-model",
this.handleChron,
);
2026-04-12 19:20:37 +00:00
}
// util
static checkIsKeystroke(e: KeyboardEvent): boolean {
return (e.metaKey || e.altKey) && e.ctrlKey;
2026-04-12 19:20:37 +00:00
}
}
export class Context {
contextId = v4();
keystrokes = new Map<string, () => void>();
handleKeystroke = (e: KeyboardEvent): boolean => {
const fn: (() => void) | undefined = this.keystrokes.get(
e.key.toLowerCase(),
);
if (!fn) return false;
fn();
return true;
2026-04-12 19:20:37 +00:00
};
2026-04-14 17:19:35 +00:00
close = (): void => {};
handleContextClose = (fromHistoryEvent: boolean): void => {};
2026-04-12 19:20:37 +00:00
registerKeyStroke = (key: string, fn: () => void): void => {
this.keystrokes.set(key, fn);
2026-04-12 19:20:37 +00:00
};
2026-04-13 19:45:52 +00:00
2026-04-15 12:47:10 +00:00
constructor(public contextDebugDescription: string) {}
2026-04-12 19:20:37 +00:00
}
export class ContextHost<T> extends Context {
contexts = new Map<T, Context>();
currentContext = new React.State<Context | undefined>(undefined);
get isOpen(): boolean {
return false;
2026-04-12 19:20:37 +00:00
}
get contextSelection(): T | undefined {
return undefined;
}
2026-04-12 19:20:37 +00:00
registerContext = (key: T, context: Context): void => {
this.contexts.set(key, context);
2026-04-12 19:20:37 +00:00
};
2026-04-14 17:19:35 +00:00
closeCurrentContext = (): void => {
if (this.currentContext.value) {
this.coreViewModel.closeContext(
this.currentContext.value.contextId,
);
}
this.currentContext.value = undefined;
2026-04-12 19:20:37 +00:00
};
updateContexts = (): void => {
if (this.isOpen == false) return;
const selection = this.contextSelection;
if (!selection) return;
const selectedContext: Context | undefined =
this.contexts.get(selection);
if (!selectedContext) return;
if (selectedContext != this.currentContext.value) {
this.closeCurrentContext();
}
this.coreViewModel.context = selectedContext;
this.currentContext.value = selectedContext;
2026-04-12 19:20:37 +00:00
};
2026-09-22 21:57:22 +02:00
2026-09-23 00:09:09 +02:00
constructor(
contextDebugDescription: string,
public coreViewModel: CoreViewModel,
2026-09-23 00:09:09 +02:00
) {
super(contextDebugDescription);
2026-09-09 00:04:27 +02:00
}
2024-07-25 16:02:49 +02:00
}