add history of previous connections, remodel storageModel to use backslash as path separator

This commit is contained in:
marlon-erler
2024-07-20 02:56:44 +02:00
parent aef01cb17b
commit ff27112733
10 changed files with 309 additions and 156 deletions
+190 -102
View File
@@ -32,6 +32,50 @@
return JSON.stringify(this._value); return JSON.stringify(this._value);
} }
}; };
var ListState = class extends State {
additionHandlers = /* @__PURE__ */ new Set();
removalHandlers = /* @__PURE__ */ new Map();
// init
constructor(initialItems) {
super(new Set(initialItems));
}
// list
add(...items) {
items.forEach((item) => {
this.value.add(item);
this.additionHandlers.forEach((handler) => handler(item));
});
this.callSubscriptions();
}
remove(...items) {
items.forEach((item) => {
this.value.delete(item);
if (!this.removalHandlers.has(item)) return;
this.removalHandlers.get(item).forEach((handler) => handler(item));
this.removalHandlers.delete(item);
});
this.callSubscriptions();
}
clear() {
this.remove(...this.value.values());
}
// handlers
handleAddition(handler) {
this.additionHandlers.add(handler);
[...this.value.values()].forEach(handler);
}
handleRemoval(item, handler) {
if (!this.removalHandlers.has(item))
this.removalHandlers.set(item, /* @__PURE__ */ new Set());
this.removalHandlers.get(item).add(handler);
}
// stringification
toString() {
const array = [...this.value.values()];
const json = JSON.stringify(array);
return json;
}
};
function createProxyState(statesToSubscibe, fn) { function createProxyState(statesToSubscibe, fn) {
const proxyState = new State(fn()); const proxyState = new State(fn());
statesToSubscibe.forEach( statesToSubscibe.forEach(
@@ -192,8 +236,8 @@
addChatPlaceholder: "Add chat", addChatPlaceholder: "Add chat",
addChatButton: "Add chat" addChatButton: "Add chat"
}, },
settings: { connectionModal: {
settingsHeadline: "Settings" connectionModalHeadline: "Manage Connections"
/// ///
} }
}; };
@@ -211,10 +255,108 @@
class: "modal", class: "modal",
"toggle:open": connectionViewModel2.isShowingConnectionModal "toggle:open": connectionViewModel2.isShowingConnectionModal
}, },
/* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("main", null), /* @__PURE__ */ createElement("button", { "on:click": connectionViewModel2.hideConnectionModal }, translations.general.closeButton, /* @__PURE__ */ createElement("span", { class: "icon" }, "close"))) /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("main", null, /* @__PURE__ */ createElement("h2", null, translations.connectionModal.connectionModalHeadline)), /* @__PURE__ */ createElement("button", { "on:click": connectionViewModel2.hideConnectionModal }, translations.general.closeButton, /* @__PURE__ */ createElement("span", { class: "icon" }, "close")))
); );
} }
// src/Model/Utility/utility.ts
function stringify(data) {
return JSON.stringify(data);
}
function parse(string) {
return JSON.parse(string);
}
// src/Model/Utility/typeSafety.ts
var DATA_VERSION = "v2";
// src/Model/storageModel.ts
var PATH_COMPONENT_SEPARATOR = "\\";
var StorageModel = class _StorageModel {
storageEntryTree = {};
// basic
store = (pathComponents, value) => {
const key = _StorageModel.pathComponentsToKey(...pathComponents);
localStorage.setItem(key, value);
this.updateTree(...pathComponents);
};
restore = (pathComponents) => {
const key = _StorageModel.pathComponentsToKey(...pathComponents);
return localStorage.getItem(key);
};
remove = (pathComponents) => {
const key = _StorageModel.pathComponentsToKey(...pathComponents);
localStorage.removeItem(key);
};
list = (pathComponents) => {
let currentParent = this.storageEntryTree;
for (const component of pathComponents) {
const nextParent = currentParent[component];
if (nextParent == void 0) return [];
currentParent = nextParent;
}
return [...Object.keys(currentParent)];
};
// stringifiable
storeStringifiable = (pathComponents, value) => {
const valueString = stringify(value);
this.store(pathComponents, valueString);
};
restoreStringifiable = (pathComponents) => {
const valueString = this.restore(pathComponents);
if (!valueString) return null;
return parse(valueString);
};
// init
constructor() {
for (const key of Object.keys(localStorage)) {
const components = _StorageModel.keyToPathComponents(key);
this.updateTree(...components);
}
}
// utility
updateTree = (...pathComponents) => {
let currentParent = this.storageEntryTree;
for (const pathPart of pathComponents) {
if (!currentParent[pathPart]) {
currentParent[pathPart] = {};
}
currentParent = currentParent[pathPart];
}
};
static pathComponentsToKey = (...pathComponents) => {
return pathComponents.join(PATH_COMPONENT_SEPARATOR);
};
static keyToPathComponents = (key) => {
return key.split(PATH_COMPONENT_SEPARATOR);
};
static join = (...items) => {
let allComponents = [];
for (const item of items) {
const parts = this.keyToPathComponents(item);
allComponents.push(...parts);
}
return _StorageModel.pathComponentsToKey(...allComponents);
};
};
var storageKeys = {
// connection
socketAddress: [DATA_VERSION, "connection", "socket-address"],
// settings
username: [DATA_VERSION, "settings", "user-name"],
firstDayOfWeek: [DATA_VERSION, "settings", "first-day-of-week"],
// history
previousAddresses: [DATA_VERSION, "history", "previous-addresses"],
previousObjectCategories: [DATA_VERSION, "history", "object-categories"],
previousObjectStatuses: [DATA_VERSION, "history", "object-statuses"],
previousObjectFilters: [DATA_VERSION, "history", "object-filters"],
// chat etc
chatInfo: (id) => [DATA_VERSION, "chat", id, "info"],
chatMessages: (id) => [DATA_VERSION, "chat", id, "messages"],
chatObjects: (id) => [DATA_VERSION, "chat", id, "objects"],
chatOutbox: (id) => [DATA_VERSION, "chat", id, "outbox"]
};
// node_modules/udn-frontend/index.ts // node_modules/udn-frontend/index.ts
var UDNFrontend = class { var UDNFrontend = class {
ws; ws;
@@ -327,18 +469,10 @@
} }
}; };
// src/Model/Utility/utility.ts
function stringify(data) {
return JSON.stringify(data);
}
function parse(string) {
return JSON.parse(string);
}
// src/Model/connectionModel.ts // src/Model/connectionModel.ts
var ConnectionModel = class { var ConnectionModel = class {
// basic
udn; udn;
storageModel;
// data // data
get isConnected() { get isConnected() {
return this.udn.ws != void 0 && this.udn.ws.readyState == 1; return this.udn.ws != void 0 && this.udn.ws.readyState == 1;
@@ -355,15 +489,37 @@
}; };
handleConnectionChange = () => { handleConnectionChange = () => {
console.log("connection status:", this.isConnected, this.address); console.log("connection status:", this.isConnected, this.address);
if (this.isConnected == false) return;
if (this.address) {
this.storeAddress(this.address);
}
}; };
// messaging // messaging
sendChatMessage = (chatMessage) => { sendChatMessage = (chatMessage) => {
const stringifiedBody = stringify(chatMessage); const stringifiedBody = stringify(chatMessage);
return this.udn.sendMessage(chatMessage.channel, stringifiedBody); return this.udn.sendMessage(chatMessage.channel, stringifiedBody);
}; };
// storage
getAddressPath = (address) => {
const dirPath = storageKeys.previousAddresses;
return [...dirPath, address];
};
storeAddress = (address) => {
const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, "");
};
removeAddress = (address) => {
const addressPath = this.getAddressPath(address);
this.storageModel.remove(addressPath);
};
getAddresses = () => {
const dirPath = storageKeys.previousAddresses;
return this.storageModel.list(dirPath);
};
// setup // setup
constructor(configuration) { constructor(configuration) {
this.udn = new UDNFrontend(); this.udn = new UDNFrontend();
this.storageModel = configuration.storageModel;
this.udn.onmessage = (data) => { this.udn.onmessage = (data) => {
configuration.messageHandler(data); configuration.messageHandler(data);
}; };
@@ -385,6 +541,7 @@
serverAddressInput = new State(""); serverAddressInput = new State("");
isConnected = new State(false); isConnected = new State(false);
isShowingConnectionModal = new State(false); isShowingConnectionModal = new State(false);
previousAddresses = new ListState();
// toggles // toggles
cannotConnect = createProxyState( cannotConnect = createProxyState(
[this.serverAddressInput, this.isConnected], [this.serverAddressInput, this.isConnected],
@@ -397,9 +554,12 @@
// handlers // handlers
connectionChangeHandler = () => { connectionChangeHandler = () => {
this.isConnected.value = this.connectionModel.isConnected; this.isConnected.value = this.connectionModel.isConnected;
if (this.connectionModel.isConnected == false) return;
if (this.connectionModel.address) { if (this.connectionModel.address) {
this.serverAddressInput.value = this.connectionModel.address; this.serverAddressInput.value = this.connectionModel.address;
} }
this.previousAddresses.clear();
this.previousAddresses.add(...this.connectionModel.getAddresses());
}; };
messageHandler = (data) => { messageHandler = (data) => {
}; };
@@ -418,8 +578,9 @@
this.isShowingConnectionModal.value = false; this.isShowingConnectionModal.value = false;
}; };
// init // init
constructor() { constructor(storageModel2) {
const connectionModel = new ConnectionModel({ const connectionModel = new ConnectionModel({
storageModel: storageModel2,
connectionChangeHandler: this.connectionChangeHandler, connectionChangeHandler: this.connectionChangeHandler,
messageHandler: this.messageHandler messageHandler: this.messageHandler
}); });
@@ -431,16 +592,30 @@
function Option(text, value, selectedOnCreate) { function Option(text, value, selectedOnCreate) {
return /* @__PURE__ */ createElement("option", { value, "toggle:selected": selectedOnCreate }, text); return /* @__PURE__ */ createElement("option", { value, "toggle:selected": selectedOnCreate }, text);
} }
var StringToOption = (string) => {
return Option(string, string, false);
};
// src/View/homePage.tsx // src/View/homePage.tsx
function HomePage(settingsViewModel2, connectionViewModel2) { function HomePage(settingsViewModel2, connectionViewModel2) {
const overviewSection = /* @__PURE__ */ createElement("div", { id: "overview-section" }, /* @__PURE__ */ createElement("h2", null, translations.homePage.overviewHeadline), /* @__PURE__ */ createElement("label", { class: "tile flex-no" }, /* @__PURE__ */ createElement("span", { class: "icon" }, "cell_tower"), /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("span", null, translations.homePage.serverAddress), /* @__PURE__ */ createElement( const overviewSection = /* @__PURE__ */ createElement("div", { id: "overview-section" }, /* @__PURE__ */ createElement("h2", null, translations.homePage.overviewHeadline), /* @__PURE__ */ createElement("label", { class: "tile flex-no" }, /* @__PURE__ */ createElement("span", { class: "icon" }, "cell_tower"), /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("span", null, translations.homePage.serverAddress), /* @__PURE__ */ createElement(
"input", "input",
{ {
list: "previous-connection-list",
placeholder: translations.homePage.serverAddressPlaceholder, placeholder: translations.homePage.serverAddressPlaceholder,
"bind:value": connectionViewModel2.serverAddressInput, "bind:value": connectionViewModel2.serverAddressInput,
"on:enter": connectionViewModel2.connect "on:enter": connectionViewModel2.connect
} }
), /* @__PURE__ */ createElement(
"datalist",
{
hidden: true,
id: "previous-connection-list",
"children:append": [
connectionViewModel2.previousAddresses,
StringToOption
]
}
))), /* @__PURE__ */ createElement("div", { class: "flex-row" }, /* @__PURE__ */ createElement( ))), /* @__PURE__ */ createElement("div", { class: "flex-row" }, /* @__PURE__ */ createElement(
"button", "button",
{ {
@@ -510,94 +685,6 @@
return /* @__PURE__ */ createElement("article", { id: "home-page" }, /* @__PURE__ */ createElement("header", null, /* @__PURE__ */ createElement("span", null, translations.homePage.appName)), /* @__PURE__ */ createElement("div", null, overviewSection, chatSection)); return /* @__PURE__ */ createElement("article", { id: "home-page" }, /* @__PURE__ */ createElement("header", null, /* @__PURE__ */ createElement("span", null, translations.homePage.appName)), /* @__PURE__ */ createElement("div", null, overviewSection, chatSection));
} }
// src/Model/Utility/typeSafety.ts
var DATA_VERSION = "v2";
// src/Model/storageModel.ts
var StorageModel = class _StorageModel {
storageEntryTree = {};
// basic
store = (key, value) => {
localStorage.setItem(key, value);
const pathComponents = _StorageModel.keyToPathComponents(key);
this.updateTree(...pathComponents);
};
restore = (key) => {
return localStorage.getItem(key);
};
remove = (key) => {
localStorage.removeItem(key);
};
list = (key) => {
const pathComponents = _StorageModel.keyToPathComponents(key);
let currentParent = this.storageEntryTree;
for (const component of pathComponents) {
const nextParent = currentParent[component];
if (nextParent == void 0) return [];
currentParent = nextParent;
}
return [...Object.keys(currentParent)];
};
// stringifiable
storeStringifiable = (key, value) => {
const valueString = stringify(value);
this.store(key, valueString);
};
restoreStringifiable = (key) => {
const valueString = this.restore(key);
if (!valueString) return null;
return parse(valueString);
};
// init
constructor() {
for (const key of Object.keys(localStorage)) {
const components = _StorageModel.keyToPathComponents(key);
this.updateTree(...components);
}
}
// utility
updateTree = (...pathComponents) => {
let currentParent = this.storageEntryTree;
for (const pathPart of pathComponents) {
if (!currentParent[pathPart]) {
currentParent[pathPart] = {};
}
currentParent = currentParent[pathPart];
}
};
static pathComponentsToKey = (...pathComponents) => {
return pathComponents.join("/");
};
static keyToPathComponents = (key) => {
return key.split("/");
};
static join = (...items) => {
let allComponents = [];
for (const item of items) {
const parts = this.keyToPathComponents(item);
allComponents.push(...parts);
}
return _StorageModel.pathComponentsToKey(...allComponents);
};
};
var storageKeys = {
// connection
socketAddress: `${DATA_VERSION}/connection/socket-address`,
// settings
username: `${DATA_VERSION}/settings/user-name`,
firstDayOfWeek: `${DATA_VERSION}/settings/first-day-of-week`,
// history
previousAddresses: `${DATA_VERSION}/history/previous-addresses`,
previousObjectCategories: `${DATA_VERSION}/history/object-categories`,
previousObjectStatuses: `${DATA_VERSION}/history/object-statuses`,
previousObjectFilters: `${DATA_VERSION}/history/object-filters`,
// chat etc
chatInfo: (id) => `${DATA_VERSION}/chat/${id}/info`,
chatMessages: (id) => `${DATA_VERSION}/chat/${id}/messages`,
chatObjects: (id) => `${DATA_VERSION}/chat/${id}/objects`,
chatOutbox: (id) => `${DATA_VERSION}/chat/${id}/outbox`
};
// src/Model/settingsModel.ts // src/Model/settingsModel.ts
var SettingsModel = class { var SettingsModel = class {
storageModel; storageModel;
@@ -664,8 +751,9 @@
// src/index.tsx // src/index.tsx
var storageModel = new StorageModel(); var storageModel = new StorageModel();
console.log(JSON.stringify(storageModel.storageEntryTree, null, 4));
var settingsViewModel = new SettingsViewModel(storageModel); var settingsViewModel = new SettingsViewModel(storageModel);
var connectionViewModel = new ConnectionViewModel(); var connectionViewModel = new ConnectionViewModel(storageModel);
document.querySelector("main").append( document.querySelector("main").append(
HomePage(settingsViewModel, connectionViewModel), HomePage(settingsViewModel, connectionViewModel),
ConnectionModal(connectionViewModel) ConnectionModal(connectionViewModel)
+17 -14
View File
@@ -17,28 +17,28 @@ export class ChatModel {
outbox = new Set<ChatMessage>(); outbox = new Set<ChatMessage>();
// paths // paths
get infoPath(): string { get infoPath(): string[] {
return storageKeys.chatInfo(this.id); return storageKeys.chatInfo(this.id);
} }
get messageDirPath(): string { get messageDirPath(): string[] {
return storageKeys.chatMessages(this.id); return storageKeys.chatMessages(this.id);
} }
get objectDirPath(): string { get objectDirPath(): string[] {
return storageKeys.chatObjects(this.id); return storageKeys.chatObjects(this.id);
} }
get outboxDirPath(): string { get outboxDirPath(): string[] {
return storageKeys.chatOutbox(this.id); return storageKeys.chatOutbox(this.id);
} }
getMessagePath(id: string): string { getMessagePath(id: string): string[] {
return StorageModel.join(this.messageDirPath, id); return [...this.messageDirPath, id];
} }
getObjectPath(id: string): string { getObjectPath(id: string): string[] {
return StorageModel.join(this.objectDirPath, id); return [...this.objectDirPath, id];
} }
// set // set
@@ -61,13 +61,13 @@ export class ChatModel {
if (!this.messages.has(message)) { if (!this.messages.has(message)) {
this.messages.add(message); this.messages.add(message);
} }
const messagePath: string = this.getMessagePath(message.id); const messagePath: string[] = this.getMessagePath(message.id);
this.storageModel.storeStringifiable(messagePath, message); this.storageModel.storeStringifiable(messagePath, message);
} }
addObject(object: ChatObject): void { addObject(object: ChatObject): void {
this.objects.set(object.id, object); this.objects.set(object.id, object);
const objectPath: string = this.getObjectPath(object.id); const objectPath: string[] = this.getObjectPath(object.id);
this.storageModel.storeStringifiable(objectPath, object); this.storageModel.storeStringifiable(objectPath, object);
} }
@@ -82,7 +82,7 @@ export class ChatModel {
if (!Array.isArray(messageIds)) return; if (!Array.isArray(messageIds)) return;
for (const messageId of messageIds) { for (const messageId of messageIds) {
const messagePath: string = this.getMessagePath(messageId); const messagePath: string[] = this.getMessagePath(messageId);
const message: any = this.storageModel.restoreStringifiable(messagePath); const message: any = this.storageModel.restoreStringifiable(messagePath);
if (checkIsValidObject(message) == false) return; if (checkIsValidObject(message) == false) return;
@@ -95,8 +95,8 @@ export class ChatModel {
if (!Array.isArray(objectIds)) return; if (!Array.isArray(objectIds)) return;
for (const objectId of objectIds) { for (const objectId of objectIds) {
const objectPath: string = this.getObjectPath(objectId); const objectPathComponents: string[] = this.getObjectPath(objectId);
const object: any = this.storageModel.restoreStringifiable(objectPath); const object: any = this.storageModel.restoreStringifiable(objectPathComponents);
if (checkIsValidObject(object) == false) return; if (checkIsValidObject(object) == false) return;
if (object.id != objectId) return; if (object.id != objectId) return;
@@ -116,7 +116,10 @@ export class ChatModel {
} }
// creation methods // creation methods
export function createChat(storageModel: StorageModel, primaryChannel: string): ChatModel { export function createChat(
storageModel: StorageModel,
primaryChannel: string
): ChatModel {
const id: string = v4(); const id: string = v4();
const chatModel = new ChatModel(storageModel, id); const chatModel = new ChatModel(storageModel, id);
+31 -1
View File
@@ -1,13 +1,14 @@
// this file is responsible for managing UDN connections. // this file is responsible for managing UDN connections.
import StorageModel, { storageKeys } from "./storageModel";
import UDNFrontend, { Message } from "udn-frontend"; import UDNFrontend, { Message } from "udn-frontend";
import { ChatMessage } from "./chatModel"; import { ChatMessage } from "./chatModel";
import { stringify } from "./Utility/utility"; import { stringify } from "./Utility/utility";
export default class ConnectionModel { export default class ConnectionModel {
// basic
udn: UDNFrontend; udn: UDNFrontend;
storageModel: StorageModel;
// data // data
get isConnected(): boolean { get isConnected(): boolean {
@@ -29,6 +30,11 @@ export default class ConnectionModel {
handleConnectionChange = (): void => { handleConnectionChange = (): void => {
console.log("connection status:", this.isConnected, this.address); console.log("connection status:", this.isConnected, this.address);
if (this.isConnected == false) return;
if (this.address) {
this.storeAddress(this.address);
}
}; };
// messaging // messaging
@@ -37,10 +43,32 @@ export default class ConnectionModel {
return this.udn.sendMessage(chatMessage.channel, stringifiedBody); return this.udn.sendMessage(chatMessage.channel, stringifiedBody);
}; };
// storage
private getAddressPath = (address: string): string[] => {
const dirPath = storageKeys.previousAddresses;
return [...dirPath, address];
};
storeAddress = (address: string): void => {
const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, "");
};
removeAddress = (address: string): void => {
const addressPath = this.getAddressPath(address);
this.storageModel.remove(addressPath);
};
getAddresses = (): string[] => {
const dirPath = storageKeys.previousAddresses;
return this.storageModel.list(dirPath);
};
// setup // setup
constructor(configuration: ConnectionModelConfiguration) { constructor(configuration: ConnectionModelConfiguration) {
// create frontend // create frontend
this.udn = new UDNFrontend(); this.udn = new UDNFrontend();
this.storageModel = configuration.storageModel;
// setup handlers // setup handlers
this.udn.onmessage = (data: Message) => { this.udn.onmessage = (data: Message) => {
@@ -59,6 +87,8 @@ export default class ConnectionModel {
// types // types
export interface ConnectionModelConfiguration { export interface ConnectionModelConfiguration {
storageModel: StorageModel;
connectionChangeHandler: () => void; connectionChangeHandler: () => void;
messageHandler: (data: Message) => void; messageHandler: (data: Message) => void;
} }
+26 -24
View File
@@ -4,27 +4,29 @@ import { parse, stringify } from "./Utility/utility";
import { DATA_VERSION } from "./Utility/typeSafety"; import { DATA_VERSION } from "./Utility/typeSafety";
const PATH_COMPONENT_SEPARATOR = "\\";
export default class StorageModel { export default class StorageModel {
storageEntryTree: StorageEntry = {}; storageEntryTree: StorageEntry = {};
// basic // basic
store = (key: string, value: string): void => { store = (pathComponents: string[], value: string): void => {
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
localStorage.setItem(key, value); localStorage.setItem(key, value);
const pathComponents: string[] = StorageModel.keyToPathComponents(key);
this.updateTree(...pathComponents); this.updateTree(...pathComponents);
}; };
restore = (key: string): string | null => { restore = (pathComponents: string[]): string | null => {
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
return localStorage.getItem(key); return localStorage.getItem(key);
}; };
remove = (key: string): void => { remove = (pathComponents: string[]): void => {
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
localStorage.removeItem(key); localStorage.removeItem(key);
}; };
list = (key: string): string[] => { list = (pathComponents: string[]): string[] => {
const pathComponents: string[] = StorageModel.keyToPathComponents(key);
let currentParent: StorageEntry = this.storageEntryTree; let currentParent: StorageEntry = this.storageEntryTree;
for (const component of pathComponents) { for (const component of pathComponents) {
const nextParent: StorageEntry | undefined = currentParent[component]; const nextParent: StorageEntry | undefined = currentParent[component];
@@ -36,13 +38,13 @@ export default class StorageModel {
}; };
// stringifiable // stringifiable
storeStringifiable = (key: string, value: any): void => { storeStringifiable = (pathComponents: string[], value: any): void => {
const valueString: string = stringify(value); const valueString: string = stringify(value);
this.store(key, valueString); this.store(pathComponents, valueString);
}; };
restoreStringifiable = (key: string): any | null => { restoreStringifiable = (pathComponents: string[]): any | null => {
const valueString: string | null = this.restore(key); const valueString: string | null = this.restore(pathComponents);
if (!valueString) return null; if (!valueString) return null;
return parse(valueString); return parse(valueString);
@@ -69,11 +71,11 @@ export default class StorageModel {
}; };
static pathComponentsToKey = (...pathComponents: string[]): string => { static pathComponentsToKey = (...pathComponents: string[]): string => {
return pathComponents.join("/"); return pathComponents.join(PATH_COMPONENT_SEPARATOR);
}; };
static keyToPathComponents = (key: string): string[] => { static keyToPathComponents = (key: string): string[] => {
return key.split("/"); return key.split(PATH_COMPONENT_SEPARATOR);
}; };
static join = (...items: string[]): string => { static join = (...items: string[]): string => {
@@ -92,21 +94,21 @@ export type StorageEntry = { [key: string]: StorageEntry };
// keys // keys
export const storageKeys = { export const storageKeys = {
// connection // connection
socketAddress: `${DATA_VERSION}/connection/socket-address`, socketAddress: [DATA_VERSION, "connection", "socket-address"],
// settings // settings
username: `${DATA_VERSION}/settings/user-name`, username: [DATA_VERSION, "settings", "user-name"],
firstDayOfWeek: `${DATA_VERSION}/settings/first-day-of-week`, firstDayOfWeek: [DATA_VERSION, "settings", "first-day-of-week"],
// history // history
previousAddresses: `${DATA_VERSION}/history/previous-addresses`, previousAddresses: [DATA_VERSION, "history", "previous-addresses"],
previousObjectCategories: `${DATA_VERSION}/history/object-categories`, previousObjectCategories: [DATA_VERSION, "history", "object-categories"],
previousObjectStatuses: `${DATA_VERSION}/history/object-statuses`, previousObjectStatuses: [DATA_VERSION, "history", "object-statuses"],
previousObjectFilters: `${DATA_VERSION}/history/object-filters`, previousObjectFilters: [DATA_VERSION, "history", "object-filters"],
// chat etc // chat etc
chatInfo: (id: string) => `${DATA_VERSION}/chat/${id}/info`, chatInfo: (id: string) => [DATA_VERSION, "chat", id, "info"],
chatMessages: (id: string) => `${DATA_VERSION}/chat/${id}/messages`, chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"],
chatObjects: (id: string) => `${DATA_VERSION}/chat/${id}/objects`, chatObjects: (id: string) => [DATA_VERSION, "chat", id, "objects"],
chatOutbox: (id: string) => `${DATA_VERSION}/chat/${id}/outbox`, chatOutbox: (id: string) => [DATA_VERSION, "chat", id, "outbox"],
}; };
+6
View File
@@ -7,3 +7,9 @@ export function Option(text: string, value: string, selectedOnCreate: boolean) {
</option> </option>
); );
} }
export const StringToOption: React.StateItemConverter<string> = (
string: string
) => {
return Option(string, string, false);
};
+5 -1
View File
@@ -10,7 +10,11 @@ export function ConnectionModal(connectionViewModel: ConnectionViewModel) {
toggle:open={connectionViewModel.isShowingConnectionModal} toggle:open={connectionViewModel.isShowingConnectionModal}
> >
<div> <div>
<main></main> <main>
<h2>{translations.connectionModal.connectionModalHeadline}</h2>
</main>
<button on:click={connectionViewModel.hideConnectionModal}> <button on:click={connectionViewModel.hideConnectionModal}>
{translations.general.closeButton} {translations.general.closeButton}
<span class="icon">close</span> <span class="icon">close</span>
+11 -1
View File
@@ -2,8 +2,9 @@ import "./homePage.css";
import * as React from "bloatless-react"; import * as React from "bloatless-react";
import { Option, StringToOption } from "./Components/option";
import ConnectionViewModel from "../ViewModel/connectionViewModel"; import ConnectionViewModel from "../ViewModel/connectionViewModel";
import { Option } from "./Components/option";
import SettingsViewModel from "../ViewModel/settingsViewModel"; import SettingsViewModel from "../ViewModel/settingsViewModel";
import { translations } from "./translations"; import { translations } from "./translations";
@@ -21,10 +22,19 @@ export function HomePage(
<div> <div>
<span>{translations.homePage.serverAddress}</span> <span>{translations.homePage.serverAddress}</span>
<input <input
list="previous-connection-list"
placeholder={translations.homePage.serverAddressPlaceholder} placeholder={translations.homePage.serverAddressPlaceholder}
bind:value={connectionViewModel.serverAddressInput} bind:value={connectionViewModel.serverAddressInput}
on:enter={connectionViewModel.connect} on:enter={connectionViewModel.connect}
></input> ></input>
<datalist
hidden
id="previous-connection-list"
children:append={[
connectionViewModel.previousAddresses,
StringToOption,
]}
></datalist>
</div> </div>
</label> </label>
+2 -2
View File
@@ -64,8 +64,8 @@ const englishTranslations = {
addChatButton: "Add chat", addChatButton: "Add chat",
}, },
settings: { connectionModal: {
settingsHeadline: "Settings", connectionModalHeadline: "Manage Connections",
/// ///
}, },
+13 -4
View File
@@ -2,6 +2,7 @@ import * as React from "bloatless-react";
import ConnectionModel from "../Model/connectionModel"; import ConnectionModel from "../Model/connectionModel";
import { Message } from "udn-frontend"; import { Message } from "udn-frontend";
import StorageModel from "../Model/storageModel";
export default class ConnectionViewModel { export default class ConnectionViewModel {
connectionModel: ConnectionModel; connectionModel: ConnectionModel;
@@ -12,6 +13,8 @@ export default class ConnectionViewModel {
isShowingConnectionModal: React.State<boolean> = new React.State(false); isShowingConnectionModal: React.State<boolean> = new React.State(false);
previousAddresses: React.ListState<string> = new React.ListState();
// toggles // toggles
cannotConnect: React.State<boolean> = React.createProxyState( cannotConnect: React.State<boolean> = React.createProxyState(
[this.serverAddressInput, this.isConnected], [this.serverAddressInput, this.isConnected],
@@ -28,9 +31,14 @@ export default class ConnectionViewModel {
// handlers // handlers
connectionChangeHandler = (): void => { connectionChangeHandler = (): void => {
this.isConnected.value = this.connectionModel.isConnected; this.isConnected.value = this.connectionModel.isConnected;
if (this.connectionModel.address) { if (this.connectionModel.isConnected == false) return;
if (this.connectionModel.address) {
this.serverAddressInput.value = this.connectionModel.address; this.serverAddressInput.value = this.connectionModel.address;
} }
this.previousAddresses.clear();
this.previousAddresses.add(...this.connectionModel.getAddresses());
}; };
messageHandler = (data: Message): void => {}; messageHandler = (data: Message): void => {};
@@ -47,15 +55,16 @@ if (this.connectionModel.address) {
// view methods // view methods
showConnectionModal = (): void => { showConnectionModal = (): void => {
this.isShowingConnectionModal.value = true; this.isShowingConnectionModal.value = true;
} };
hideConnectionModal = (): void => { hideConnectionModal = (): void => {
this.isShowingConnectionModal.value = false; this.isShowingConnectionModal.value = false;
} };
// init // init
constructor() { constructor(storageModel: StorageModel) {
const connectionModel = new ConnectionModel({ const connectionModel = new ConnectionModel({
storageModel,
connectionChangeHandler: this.connectionChangeHandler, connectionChangeHandler: this.connectionChangeHandler,
messageHandler: this.messageHandler, messageHandler: this.messageHandler,
}); });
+2 -1
View File
@@ -6,10 +6,11 @@ import StorageModel from "./Model/storageModel";
// models // models
const storageModel = new StorageModel(); const storageModel = new StorageModel();
console.log(JSON.stringify(storageModel.storageEntryTree, null, 4));
// viewModels // viewModels
const settingsViewModel = new SettingsViewModel(storageModel); const settingsViewModel = new SettingsViewModel(storageModel);
const connectionViewModel = new ConnectionViewModel(); const connectionViewModel = new ConnectionViewModel(storageModel);
// view // view
document document