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
+18 -15
View File
@@ -17,28 +17,28 @@ export class ChatModel {
outbox = new Set<ChatMessage>();
// paths
get infoPath(): string {
get infoPath(): string[] {
return storageKeys.chatInfo(this.id);
}
get messageDirPath(): string {
get messageDirPath(): string[] {
return storageKeys.chatMessages(this.id);
}
get objectDirPath(): string {
get objectDirPath(): string[] {
return storageKeys.chatObjects(this.id);
}
get outboxDirPath(): string {
get outboxDirPath(): string[] {
return storageKeys.chatOutbox(this.id);
}
getMessagePath(id: string): string {
return StorageModel.join(this.messageDirPath, id);
getMessagePath(id: string): string[] {
return [...this.messageDirPath, id];
}
getObjectPath(id: string): string {
return StorageModel.join(this.objectDirPath, id);
getObjectPath(id: string): string[] {
return [...this.objectDirPath, id];
}
// set
@@ -61,13 +61,13 @@ export class ChatModel {
if (!this.messages.has(message)) {
this.messages.add(message);
}
const messagePath: string = this.getMessagePath(message.id);
const messagePath: string[] = this.getMessagePath(message.id);
this.storageModel.storeStringifiable(messagePath, message);
}
addObject(object: ChatObject): void {
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);
}
@@ -82,7 +82,7 @@ export class ChatModel {
if (!Array.isArray(messageIds)) return;
for (const messageId of messageIds) {
const messagePath: string = this.getMessagePath(messageId);
const messagePath: string[] = this.getMessagePath(messageId);
const message: any = this.storageModel.restoreStringifiable(messagePath);
if (checkIsValidObject(message) == false) return;
@@ -95,8 +95,8 @@ export class ChatModel {
if (!Array.isArray(objectIds)) return;
for (const objectId of objectIds) {
const objectPath: string = this.getObjectPath(objectId);
const object: any = this.storageModel.restoreStringifiable(objectPath);
const objectPathComponents: string[] = this.getObjectPath(objectId);
const object: any = this.storageModel.restoreStringifiable(objectPathComponents);
if (checkIsValidObject(object) == false) return;
if (object.id != objectId) return;
@@ -116,12 +116,15 @@ export class ChatModel {
}
// creation methods
export function createChat(storageModel: StorageModel, primaryChannel: string): ChatModel {
export function createChat(
storageModel: StorageModel,
primaryChannel: string
): ChatModel {
const id: string = v4();
const chatModel = new ChatModel(storageModel, id);
chatModel.setPrimaryChannel(primaryChannel);
return chatModel;
}
+36 -6
View File
@@ -1,13 +1,14 @@
// this file is responsible for managing UDN connections.
import StorageModel, { storageKeys } from "./storageModel";
import UDNFrontend, { Message } from "udn-frontend";
import { ChatMessage } from "./chatModel";
import { stringify } from "./Utility/utility";
export default class ConnectionModel {
// basic
udn: UDNFrontend;
storageModel: StorageModel;
// data
get isConnected(): boolean {
@@ -29,6 +30,11 @@ export default class ConnectionModel {
handleConnectionChange = (): void => {
console.log("connection status:", this.isConnected, this.address);
if (this.isConnected == false) return;
if (this.address) {
this.storeAddress(this.address);
}
};
// messaging
@@ -37,28 +43,52 @@ export default class ConnectionModel {
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
constructor(configuration: ConnectionModelConfiguration) {
// create frontend
this.udn = new UDNFrontend();
this.storageModel = configuration.storageModel;
// setup handlers
this.udn.onmessage = (data: Message) => {
configuration.messageHandler(data);
configuration.messageHandler(data);
};
this.udn.onconnect = () => {
this.handleConnectionChange();
configuration.connectionChangeHandler();
this.handleConnectionChange();
configuration.connectionChangeHandler();
};
this.udn.ondisconnect = () => {
this.handleConnectionChange();
configuration.connectionChangeHandler();
this.handleConnectionChange();
configuration.connectionChangeHandler();
};
}
}
// types
export interface ConnectionModelConfiguration {
storageModel: StorageModel;
connectionChangeHandler: () => 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";
const PATH_COMPONENT_SEPARATOR = "\\";
export default class StorageModel {
storageEntryTree: StorageEntry = {};
// basic
store = (key: string, value: string): void => {
store = (pathComponents: string[], value: string): void => {
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
localStorage.setItem(key, value);
const pathComponents: string[] = StorageModel.keyToPathComponents(key);
this.updateTree(...pathComponents);
};
restore = (key: string): string | null => {
restore = (pathComponents: string[]): string | null => {
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
return localStorage.getItem(key);
};
remove = (key: string): void => {
remove = (pathComponents: string[]): void => {
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
localStorage.removeItem(key);
};
list = (key: string): string[] => {
const pathComponents: string[] = StorageModel.keyToPathComponents(key);
list = (pathComponents: string[]): string[] => {
let currentParent: StorageEntry = this.storageEntryTree;
for (const component of pathComponents) {
const nextParent: StorageEntry | undefined = currentParent[component];
@@ -36,13 +38,13 @@ export default class StorageModel {
};
// stringifiable
storeStringifiable = (key: string, value: any): void => {
storeStringifiable = (pathComponents: string[], value: any): void => {
const valueString: string = stringify(value);
this.store(key, valueString);
this.store(pathComponents, valueString);
};
restoreStringifiable = (key: string): any | null => {
const valueString: string | null = this.restore(key);
restoreStringifiable = (pathComponents: string[]): any | null => {
const valueString: string | null = this.restore(pathComponents);
if (!valueString) return null;
return parse(valueString);
@@ -69,11 +71,11 @@ export default class StorageModel {
};
static pathComponentsToKey = (...pathComponents: string[]): string => {
return pathComponents.join("/");
return pathComponents.join(PATH_COMPONENT_SEPARATOR);
};
static keyToPathComponents = (key: string): string[] => {
return key.split("/");
return key.split(PATH_COMPONENT_SEPARATOR);
};
static join = (...items: string[]): string => {
@@ -92,21 +94,21 @@ export type StorageEntry = { [key: string]: StorageEntry };
// keys
export const storageKeys = {
// connection
socketAddress: `${DATA_VERSION}/connection/socket-address`,
socketAddress: [DATA_VERSION, "connection", "socket-address"],
// settings
username: `${DATA_VERSION}/settings/user-name`,
firstDayOfWeek: `${DATA_VERSION}/settings/first-day-of-week`,
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`,
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: string) => `${DATA_VERSION}/chat/${id}/info`,
chatMessages: (id: string) => `${DATA_VERSION}/chat/${id}/messages`,
chatObjects: (id: string) => `${DATA_VERSION}/chat/${id}/objects`,
chatOutbox: (id: string) => `${DATA_VERSION}/chat/${id}/outbox`,
chatInfo: (id: string) => [DATA_VERSION, "chat", id, "info"],
chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"],
chatObjects: (id: string) => [DATA_VERSION, "chat", id, "objects"],
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>
);
}
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}
>
<div>
<main></main>
<main>
<h2>{translations.connectionModal.connectionModalHeadline}</h2>
</main>
<button on:click={connectionViewModel.hideConnectionModal}>
{translations.general.closeButton}
<span class="icon">close</span>
+11 -1
View File
@@ -2,8 +2,9 @@ import "./homePage.css";
import * as React from "bloatless-react";
import { Option, StringToOption } from "./Components/option";
import ConnectionViewModel from "../ViewModel/connectionViewModel";
import { Option } from "./Components/option";
import SettingsViewModel from "../ViewModel/settingsViewModel";
import { translations } from "./translations";
@@ -21,10 +22,19 @@ export function HomePage(
<div>
<span>{translations.homePage.serverAddress}</span>
<input
list="previous-connection-list"
placeholder={translations.homePage.serverAddressPlaceholder}
bind:value={connectionViewModel.serverAddressInput}
on:enter={connectionViewModel.connect}
></input>
<datalist
hidden
id="previous-connection-list"
children:append={[
connectionViewModel.previousAddresses,
StringToOption,
]}
></datalist>
</div>
</label>
+2 -2
View File
@@ -64,8 +64,8 @@ const englishTranslations = {
addChatButton: "Add chat",
},
settings: {
settingsHeadline: "Settings",
connectionModal: {
connectionModalHeadline: "Manage Connections",
///
},
+13 -4
View File
@@ -2,6 +2,7 @@ import * as React from "bloatless-react";
import ConnectionModel from "../Model/connectionModel";
import { Message } from "udn-frontend";
import StorageModel from "../Model/storageModel";
export default class ConnectionViewModel {
connectionModel: ConnectionModel;
@@ -12,6 +13,8 @@ export default class ConnectionViewModel {
isShowingConnectionModal: React.State<boolean> = new React.State(false);
previousAddresses: React.ListState<string> = new React.ListState();
// toggles
cannotConnect: React.State<boolean> = React.createProxyState(
[this.serverAddressInput, this.isConnected],
@@ -28,9 +31,14 @@ export default class ConnectionViewModel {
// handlers
connectionChangeHandler = (): void => {
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.previousAddresses.clear();
this.previousAddresses.add(...this.connectionModel.getAddresses());
};
messageHandler = (data: Message): void => {};
@@ -47,15 +55,16 @@ if (this.connectionModel.address) {
// view methods
showConnectionModal = (): void => {
this.isShowingConnectionModal.value = true;
}
};
hideConnectionModal = (): void => {
this.isShowingConnectionModal.value = false;
}
};
// init
constructor() {
constructor(storageModel: StorageModel) {
const connectionModel = new ConnectionModel({
storageModel,
connectionChangeHandler: this.connectionChangeHandler,
messageHandler: this.messageHandler,
});
+2 -1
View File
@@ -6,10 +6,11 @@ import StorageModel from "./Model/storageModel";
// models
const storageModel = new StorageModel();
console.log(JSON.stringify(storageModel.storageEntryTree, null, 4));
// viewModels
const settingsViewModel = new SettingsViewModel(storageModel);
const connectionViewModel = new ConnectionViewModel();
const connectionViewModel = new ConnectionViewModel(storageModel);
// view
document