Skip to content

Build your personal knowledge base with Trilium Notes

chore(client/ts): port setup.js entrypoint to TS

Panagiotis Papadopoulos52814030d800
diff --git a/src/public/app/setup.js b/src/public/app/setup.js
deleted file mode 100644
index 0c12534a81..0000000000
--- a/src/public/app/setup.js
+++ /dev/null
@@ -1,107 +0,0 @@
-import utils from "./services/utils.js";
-import macInit from "./services/mac_init.js";
-
-macInit.init();
-
-function SetupModel() {
- if (syncInProgress) {
- setInterval(checkOutstandingSyncs, 1000);
- }
-
- const serverAddress = `${location.protocol}//${location.host}`;
-
- $("#current-host").html(serverAddress);
-
- this.step = ko.observable(syncInProgress ? "sync-in-progress" : "setup-type");
- this.setupType = ko.observable();
-
- this.setupNewDocument = ko.observable(false);
- this.setupSyncFromDesktop = ko.observable(false);
- this.setupSyncFromServer = ko.observable(false);
-
- this.syncServerHost = ko.observable();
- this.syncProxy = ko.observable();
- this.password = ko.observable();
-
- this.setupTypeSelected = () => !!this.setupType();
-
- this.selectSetupType = () => {
- if (this.setupType() === "new-document") {
- this.step("new-document-in-progress");
-
- $.post("api/setup/new-document").then(() => {
- window.location.replace("./setup");
- });
- } else {
- this.step(this.setupType());
- }
- };
-
- this.back = () => {
- this.step("setup-type");
-
- this.setupType("");
- };
-
- this.finish = async () => {
- const syncServerHost = this.syncServerHost();
- const syncProxy = this.syncProxy();
- const password = this.password();
-
- if (!syncServerHost) {
- showAlert("Trilium server address can't be empty");
- return;
- }
-
- if (!password) {
- showAlert("Password can't be empty");
- return;
- }
-
- // not using server.js because it loads too many dependencies
- const resp = await $.post("api/setup/sync-from-server", {
- syncServerHost: syncServerHost,
- syncProxy: syncProxy,
- password: password
- });
-
- if (resp.result === "success") {
- this.step("sync-in-progress");
-
- setInterval(checkOutstandingSyncs, 1000);
-
- hideAlert();
- } else {
- showAlert(`Sync setup failed: ${resp.error}`);
- }
- };
-}
-
-async function checkOutstandingSyncs() {
- const { outstandingPullCount, initialized } = await $.get("api/sync/stats");
-
- if (initialized) {
- if (utils.isElectron()) {
- const remote = utils.dynamicRequire("@electron/remote");
- remote.app.relaunch();
- remote.app.exit(0);
- } else {
- utils.reloadFrontendApp();
- }
- } else {
- $("#outstanding-syncs").html(outstandingPullCount);
- }
-}
-
-function showAlert(message) {
- $("#alert").html(message);
- $("#alert").show();
-}
-
-function hideAlert() {
- $("#alert").hide();
-}
-
-ko.applyBindings(new SetupModel(), document.getElementById("setup-dialog"));
-
-$("#setup-dialog").show();
diff --git a/src/public/app/setup.ts b/src/public/app/setup.ts
new file mode 100644
index 0000000000..8e88d44e99
--- /dev/null
+++ b/src/public/app/setup.ts
@@ -0,0 +1,134 @@
+import utils from "./services/utils.js";
+import macInit from "./services/mac_init.js";
+import ko from "knockout";
+
+macInit.init();
+
+// TriliumNextTODO: properly make use of below types
+// type SetupModelSetupType = "new-document" | "sync-from-desktop" | "sync-from-server" | "";
+// type SetupModelStep = "sync-in-progress" | "setup-type" | "new-document-in-progress" | "sync-from-desktop";
+
+class SetupModel {
+ syncInProgress: boolean;
+ step: ko.Observable<string>;
+ setupType: ko.Observable<string>;
+ setupNewDocument: ko.Observable<boolean>;
+ setupSyncFromDesktop: ko.Observable<boolean>;
+ setupSyncFromServer: ko.Observable<boolean>;
+ syncServerHost: ko.Observable<string|undefined>;
+ syncProxy: ko.Observable<string|undefined>;
+ password: ko.Observable<string|undefined>;
+
+ constructor(syncInProgress: boolean) {
+ this.syncInProgress = syncInProgress;
+ this.step = ko.observable(syncInProgress ? "sync-in-progress" : "setup-type");
+ this.setupType = ko.observable("");
+ this.setupNewDocument = ko.observable(false);
+ this.setupSyncFromDesktop = ko.observable(false);
+ this.setupSyncFromServer = ko.observable(false);
+ this.syncServerHost = ko.observable();
+ this.syncProxy = ko.observable();
+ this.password = ko.observable();
+
+ if (this.syncInProgress) {
+ setInterval(checkOutstandingSyncs, 1000);
+ }
+ const serverAddress = `${location.protocol}//${location.host}`;
+ $("#current-host").html(serverAddress);
+
+ }
+
+ // this is called in setup.ejs
+ setupTypeSelected() {
+ return !!this.setupType();
+ }
+
+ selectSetupType() {
+ if (this.setupType() === "new-document") {
+ this.step("new-document-in-progress");
+
+ $.post("api/setup/new-document").then(() => {
+ window.location.replace("./setup");
+ });
+ } else {
+ this.step(this.setupType());
+ }
+ };
+
+ back() {
+ this.step("setup-type");
+ this.setupType("");
+ };
+
+
+ async finish () {
+ const syncServerHost = this.syncServerHost();
+ const syncProxy = this.syncProxy();
+ const password = this.password();
+
+ if (!syncServerHost) {
+ showAlert("Trilium server address can't be empty");
+ return;
+ }
+
+ if (!password) {
+ showAlert("Password can't be empty");
+ return;
+ }
+
+ // not using server.js because it loads too many dependencies
+ const resp = await $.post("api/setup/sync-from-server", {
+ syncServerHost: syncServerHost,
+ syncProxy: syncProxy,
+ password: password
+ });
+
+ if (resp.result === "success") {
+ this.step("sync-in-progress");
+
+ setInterval(checkOutstandingSyncs, 1000);
+
+ hideAlert();
+ } else {
+ showAlert(`Sync setup failed: ${resp.error}`);
+ }
+ };
+}
+
+async function checkOutstandingSyncs() {
+ const { outstandingPullCount, initialized } = await $.get("api/sync/stats");
+
+ if (initialized) {
+ if (utils.isElectron()) {
+ const remote = utils.dynamicRequire("@electron/remote");
+ remote.app.relaunch();
+ remote.app.exit(0);
+ } else {
+ utils.reloadFrontendApp();
+ }
+ } else {
+ $("#outstanding-syncs").html(outstandingPullCount);
+ }
+}
+
+function showAlert(message: string) {
+ $("#alert").html(message);
+ $("#alert").show();
+}
+
+function hideAlert() {
+ $("#alert").hide();
+}
+
+function getSyncInProgress() {
+ const el = document.getElementById('syncInProgress');
+ if (!el || !(el instanceof HTMLMetaElement)) return false;
+ return !!parseInt(el.content);
+}
+
+addEventListener("DOMContentLoaded", (event) => {
+ ko.applyBindings(new SetupModel(getSyncInProgress()), document.getElementById("setup-dialog"));
+ $("#setup-dialog").show();
+});
+
+