From 5dedd9fb2395b3f38c868e8dd0132eb84d851c7f Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Sat, 20 Jul 2024 16:34:16 +0200 Subject: [PATCH] remember last used chat page --- dist/index.js | 29 +++++++++++++++++++------- src/Model/storageModel.ts | 1 + src/ViewModel/chatViewModel.tsx | 36 ++++++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/dist/index.js b/dist/index.js index a9d49812..a7687eef 100644 --- a/dist/index.js +++ b/dist/index.js @@ -352,6 +352,7 @@ // chat etc chats: [DATA_VERSION, "chat"], chatInfo: (id) => [DATA_VERSION, "chat", id, "info"], + chatLastUsedPage: (id) => [DATA_VERSION, "chat", id, "last-used-page"], chatMessages: (id) => [DATA_VERSION, "chat", id, "messages"], chatObjects: (id) => [DATA_VERSION, "chat", id, "objects"], chatOutbox: (id) => [DATA_VERSION, "chat", id, "outbox"] @@ -513,11 +514,12 @@ var ChatViewModel = class { chatModel; chatListViewModel; + storageModel; // state primaryChannel = new State(""); primaryChannelInput = new State(""); selectedPage = new State( - 1 /* Messages */ + "messages" /* Messages */ ); // guards cannotSetPrimaryChannel = createProxyState( @@ -531,12 +533,25 @@ close = () => { this.chatListViewModel.closeChat(); }; + // restore + restorePageSelection = () => { + const path = storageKeys.chatLastUsedPage(this.chatModel.id); + const lastUsedPage = this.storageModel.restore(path); + if (lastUsedPage != null) { + this.selectedPage.value = lastUsedPage; + } + this.selectedPage.subscribeSilent((newPage) => { + this.storageModel.store(path, newPage); + }); + }; // init constructor(chatListViewModel2, chatModel) { this.chatModel = chatModel; + this.storageModel = chatModel.storageModel; this.chatListViewModel = chatListViewModel2; this.primaryChannel.value = chatModel.info.primaryChannel; this.primaryChannelInput.value = chatModel.info.primaryChannel; + this.restorePageSelection(); } }; @@ -692,32 +707,32 @@ )), /* @__PURE__ */ createElement("span", null, ChatViewToggleButton( translations.chatPage.pages.calendar, "calendar_month", - 4 /* Calendar */, + "calendar" /* Calendar */, chatViewModel ), ChatViewToggleButton( translations.chatPage.pages.progress, "window", - 5 /* Progress */, + "progress" /* Progress */, chatViewModel ), ChatViewToggleButton( translations.chatPage.pages.kanban, "view_kanban", - 3 /* Kanban */, + "kanban" /* Kanban */, chatViewModel ), ChatViewToggleButton( translations.chatPage.pages.allObjects, "deployed_code", - 2 /* AllObjects */, + "all" /* AllObjects */, chatViewModel ), ChatViewToggleButton( translations.chatPage.pages.messages, "forum", - 1 /* Messages */, + "messages" /* Messages */, chatViewModel ), ChatViewToggleButton( translations.chatPage.pages.settings, "settings", - 0 /* Settings */, + "settings" /* Settings */, chatViewModel ))), /* @__PURE__ */ createElement("div", { id: "toolbar" }), /* @__PURE__ */ createElement("div", { id: "main" }))); } diff --git a/src/Model/storageModel.ts b/src/Model/storageModel.ts index 8d620626..05c0af9d 100644 --- a/src/Model/storageModel.ts +++ b/src/Model/storageModel.ts @@ -145,6 +145,7 @@ export const storageKeys = { // chat etc chats: [DATA_VERSION, "chat"], chatInfo: (id: string) => [DATA_VERSION, "chat", id, "info"], + chatLastUsedPage: (id: string) => [DATA_VERSION, "chat", id, "last-used-page"], chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"], chatObjects: (id: string) => [DATA_VERSION, "chat", id, "objects"], chatOutbox: (id: string) => [DATA_VERSION, "chat", id, "outbox"], diff --git a/src/ViewModel/chatViewModel.tsx b/src/ViewModel/chatViewModel.tsx index fa74e59c..76e519bc 100644 --- a/src/ViewModel/chatViewModel.tsx +++ b/src/ViewModel/chatViewModel.tsx @@ -1,12 +1,14 @@ import * as React from "bloatless-react"; +import StorageModel, { storageKeys } from "../Model/storageModel"; + import ChatListViewModel from "./chatListViewModel"; import { ChatModel } from "../Model/chatModel"; -import StorageModel from "../Model/storageModel"; export default class ChatViewModel { chatModel: ChatModel; chatListViewModel: ChatListViewModel; + storageModel: StorageModel; // state primaryChannel: React.State = new React.State(""); @@ -25,30 +27,46 @@ export default class ChatViewModel { ); // methods - open = () => { + open = (): void => { this.chatListViewModel.openChat(this); }; - close = () => { + close = (): void => { this.chatListViewModel.closeChat(); }; + // restore + restorePageSelection = (): void => { + const path: string[] = storageKeys.chatLastUsedPage(this.chatModel.id); + const lastUsedPage: string | null = this.storageModel.restore(path); + if (lastUsedPage != null) { + this.selectedPage.value = lastUsedPage as any; + } + + this.selectedPage.subscribeSilent((newPage) => { + this.storageModel.store(path, newPage); + }); + }; + // init constructor(chatListViewModel: ChatListViewModel, chatModel: ChatModel) { this.chatModel = chatModel; + this.storageModel = chatModel.storageModel; this.chatListViewModel = chatListViewModel; this.primaryChannel.value = chatModel.info.primaryChannel; this.primaryChannelInput.value = chatModel.info.primaryChannel; + + this.restorePageSelection(); } } // types export enum ChatPageType { - Settings, - Messages, - AllObjects, - Kanban, - Calendar, - Progress, + Settings = "settings", + Messages = "messages", + AllObjects = "all", + Kanban = "kanban", + Calendar = "calendar", + Progress = "progress", }