add noti bubble to chats

This commit is contained in:
Ren Amamiya
2023-05-31 10:45:20 +07:00
parent 1e5bd7e21f
commit 118c9d931c
5 changed files with 117 additions and 26 deletions

View File

@@ -1,13 +1,37 @@
import { getChatMessages, getChatsByPubkey } from "@utils/storage";
import { create } from "zustand";
import { immer } from "zustand/middleware/immer";
export const useChats = create((set) => ({
chats: [],
fetch: async (pubkey: string) => {
const response = await getChatsByPubkey(pubkey);
set({ chats: response });
},
}));
export const useChats = create(
immer((set: any, get: any) => ({
chats: [],
fetch: async (pubkey: string) => {
const response: any = await getChatsByPubkey(pubkey);
set({ chats: response });
},
add: (pubkey: string) => {
set((state) => {
const target = state.chats.findIndex(
(m: { sender_pubkey: string }) => m.sender_pubkey === pubkey,
);
if (target !== -1) {
state.chats[target]["new_messages"] =
state.chats[target]["new_messages"] + 1 || 1;
} else {
state.chats.push({ sender_pubkey: pubkey, new_messages: 1 });
}
});
},
clearBubble: (pubkey: string) => {
set((state) => {
const target = state.chats.findIndex(
(m: { sender_pubkey: string }) => m.sender_pubkey === pubkey,
);
state.chats[target]["new_messages"] = 0;
});
},
})),
);
export const useChatMessages = create((set) => ({
messages: [],