sort chats alphabetically

This commit is contained in:
marlon-erler
2024-07-20 16:49:01 +02:00
parent 588ae560a5
commit 54b7be421c
5 changed files with 75 additions and 6 deletions
+34 -4
View File
@@ -479,10 +479,12 @@
var ChatListModel = class { var ChatListModel = class {
storageModel; storageModel;
// data // data
chatModels = /* @__PURE__ */ new Map(); chatModels = /* @__PURE__ */ new Set();
sortedPrimaryChannels = [];
// store & add // store & add
addChatModel = (chatModel) => { addChatModel = (chatModel) => {
this.chatModels.set(chatModel.id, chatModel); this.chatModels.add(chatModel);
this.updateIndices();
}; };
createChat = (primaryChannel) => { createChat = (primaryChannel) => {
const id = v4_default(); const id = v4_default();
@@ -492,7 +494,17 @@
return chatModel; return chatModel;
}; };
untrackChat = (chat) => { untrackChat = (chat) => {
this.chatModels.delete(chat.id); this.chatModels.delete(chat);
this.updateIndices();
};
// sorting
updateIndices = () => {
this.sortedPrimaryChannels = [];
let allChannels = [];
for (const chatModel of this.chatModels) {
allChannels.push(chatModel.info.primaryChannel);
}
this.sortedPrimaryChannels = allChannels.sort((a, b) => a.localeCompare(b));
}; };
// restore // restore
loadChats = () => { loadChats = () => {
@@ -516,6 +528,7 @@
chatListViewModel; chatListViewModel;
storageModel; storageModel;
// state // state
index = new State(0);
primaryChannel = new State(""); primaryChannel = new State("");
primaryChannelInput = new State(""); primaryChannelInput = new State("");
selectedPage = new State( selectedPage = new State(
@@ -526,6 +539,11 @@
[this.primaryChannel, this.primaryChannelInput], [this.primaryChannel, this.primaryChannelInput],
() => this.primaryChannelInput.value == "" || this.primaryChannelInput.value == this.primaryChannel.value () => this.primaryChannelInput.value == "" || this.primaryChannelInput.value == this.primaryChannel.value
); );
// sorting
updateIndex = () => {
const index = this.chatListViewModel.getIndexOfChat(this);
this.index.value = index;
};
// methods // methods
open = () => { open = () => {
this.chatListViewModel.openChat(this); this.chatListViewModel.openChat(this);
@@ -552,6 +570,7 @@
this.primaryChannel.value = chatModel.info.primaryChannel; this.primaryChannel.value = chatModel.info.primaryChannel;
this.primaryChannelInput.value = chatModel.info.primaryChannel; this.primaryChannelInput.value = chatModel.info.primaryChannel;
this.restorePageSelection(); this.restorePageSelection();
this.updateIndex();
} }
}; };
@@ -568,6 +587,13 @@
[this.newChatPrimaryChannel], [this.newChatPrimaryChannel],
() => this.newChatPrimaryChannel.value == "" () => this.newChatPrimaryChannel.value == ""
); );
// sorting
get sortedPrimaryChannels() {
return this.chatListModel.sortedPrimaryChannels;
}
getIndexOfChat(chat) {
return this.sortedPrimaryChannels.indexOf(chat.primaryChannel.value);
}
// methods // methods
createChat = () => { createChat = () => {
const chatModel = this.chatListModel.createChat( const chatModel = this.chatListModel.createChat(
@@ -1078,13 +1104,17 @@
// src/View/Components/chatEntry.tsx // src/View/Components/chatEntry.tsx
function ChatEntry(chatViewModel) { function ChatEntry(chatViewModel) {
return /* @__PURE__ */ createElement("button", { class: "chat-entry tile", "on:click": chatViewModel.open }, /* @__PURE__ */ createElement( const view = /* @__PURE__ */ createElement("button", { class: "chat-entry tile", "on:click": chatViewModel.open }, /* @__PURE__ */ createElement(
"span", "span",
{ {
class: "shadow", class: "shadow",
"subscribe:innerText": chatViewModel.primaryChannel "subscribe:innerText": chatViewModel.primaryChannel
} }
), /* @__PURE__ */ createElement("h2", { "subscribe:innerText": chatViewModel.primaryChannel })); ), /* @__PURE__ */ createElement("h2", { "subscribe:innerText": chatViewModel.primaryChannel }));
chatViewModel.index.subscribe((newIndex) => {
view.style.order = newIndex;
});
return view;
} }
var ChatViewModelToChatEntry = (chatViewModel) => { var ChatViewModelToChatEntry = (chatViewModel) => {
return ChatEntry(chatViewModel); return ChatEntry(chatViewModel);
+15
View File
@@ -8,10 +8,12 @@ export default class ChatListModel {
// data // data
chatModels = new Set<ChatModel>(); chatModels = new Set<ChatModel>();
sortedPrimaryChannels: string[] = [];
// store & add // store & add
addChatModel = (chatModel: ChatModel) => { addChatModel = (chatModel: ChatModel) => {
this.chatModels.add(chatModel); this.chatModels.add(chatModel);
this.updateIndices();
}; };
createChat = (primaryChannel: string): ChatModel => { createChat = (primaryChannel: string): ChatModel => {
@@ -26,6 +28,19 @@ export default class ChatListModel {
untrackChat = (chat: ChatModel): void => { untrackChat = (chat: ChatModel): void => {
this.chatModels.delete(chat); this.chatModels.delete(chat);
this.updateIndices();
};
// sorting
updateIndices = () => {
this.sortedPrimaryChannels = [];
let allChannels: string[] = [];
for (const chatModel of this.chatModels) {
allChannels.push(chatModel.info.primaryChannel);
}
this.sortedPrimaryChannels = allChannels.sort((a, b) => a.localeCompare(b));
}; };
// restore // restore
+7 -1
View File
@@ -5,7 +5,7 @@ import * as React from "bloatless-react";
import ChatViewModel from "../../ViewModel/chatViewModel"; import ChatViewModel from "../../ViewModel/chatViewModel";
export function ChatEntry(chatViewModel: ChatViewModel) { export function ChatEntry(chatViewModel: ChatViewModel) {
return ( const view = (
<button class="chat-entry tile" on:click={chatViewModel.open}> <button class="chat-entry tile" on:click={chatViewModel.open}>
<span <span
class="shadow" class="shadow"
@@ -14,6 +14,12 @@ export function ChatEntry(chatViewModel: ChatViewModel) {
<h2 subscribe:innerText={chatViewModel.primaryChannel}></h2> <h2 subscribe:innerText={chatViewModel.primaryChannel}></h2>
</button> </button>
); );
chatViewModel.index.subscribe((newIndex) => {
view.style.order = newIndex;
});
return view;
} }
export const ChatViewModelToChatEntry: React.StateItemConverter< export const ChatViewModelToChatEntry: React.StateItemConverter<
+10 -1
View File
@@ -14,7 +14,7 @@ export default class ChatListViewModel {
chatViewModels: React.ListState<ChatViewModel> = new React.ListState(); chatViewModels: React.ListState<ChatViewModel> = new React.ListState();
selectedChat: React.State<ChatViewModel | undefined> = new React.State< selectedChat: React.State<ChatViewModel | undefined> = new React.State<
ChatViewModel | undefined ChatViewModel | undefined
>(undefined); >(undefined);
// guards // guards
@@ -23,6 +23,15 @@ export default class ChatListViewModel {
() => this.newChatPrimaryChannel.value == "" () => this.newChatPrimaryChannel.value == ""
); );
// sorting
get sortedPrimaryChannels(): string[] {
return this.chatListModel.sortedPrimaryChannels;
}
getIndexOfChat(chat: ChatViewModel): number {
return this.sortedPrimaryChannels.indexOf(chat.primaryChannel.value);
}
// methods // methods
createChat = (): void => { createChat = (): void => {
const chatModel: ChatModel = this.chatListModel.createChat( const chatModel: ChatModel = this.chatListModel.createChat(
+9
View File
@@ -11,6 +11,8 @@ export default class ChatViewModel {
storageModel: StorageModel; storageModel: StorageModel;
// state // state
index: React.State<number> = new React.State(0);
primaryChannel: React.State<string> = new React.State(""); primaryChannel: React.State<string> = new React.State("");
primaryChannelInput: React.State<string> = new React.State(""); primaryChannelInput: React.State<string> = new React.State("");
@@ -26,6 +28,12 @@ export default class ChatViewModel {
this.primaryChannelInput.value == this.primaryChannel.value this.primaryChannelInput.value == this.primaryChannel.value
); );
// sorting
updateIndex = (): void => {
const index = this.chatListViewModel.getIndexOfChat(this);
this.index.value = index;
};
// methods // methods
open = (): void => { open = (): void => {
this.chatListViewModel.openChat(this); this.chatListViewModel.openChat(this);
@@ -58,6 +66,7 @@ export default class ChatViewModel {
this.primaryChannelInput.value = chatModel.info.primaryChannel; this.primaryChannelInput.value = chatModel.info.primaryChannel;
this.restorePageSelection(); this.restorePageSelection();
this.updateIndex();
} }
} }