diff --git a/src/app/chat/components/item.tsx b/src/app/chat/components/item.tsx index 13362056..9c969431 100644 --- a/src/app/chat/components/item.tsx +++ b/src/app/chat/components/item.tsx @@ -1,19 +1,16 @@ import { Image } from "@shared/image"; -import { useActiveAccount } from "@stores/accounts"; import { DEFAULT_AVATAR } from "@stores/constants"; import { usePageContext } from "@utils/hooks/usePageContext"; import { useProfile } from "@utils/hooks/useProfile"; import { shortenKey } from "@utils/shortenKey"; import { twMerge } from "tailwind-merge"; -export function ChatsListItem({ pubkey }: { pubkey: string }) { +export function ChatsListItem({ data }: { data: any }) { const pageContext = usePageContext(); - const searchParams: any = pageContext.urlParsed.search; const pagePubkey = searchParams.pubkey; - const account = useActiveAccount((state: any) => state.account); - const { user, isError, isLoading } = useProfile(pubkey); + const { user, isError, isLoading } = useProfile(data.sender_pubkey); return ( <> @@ -27,10 +24,10 @@ export function ChatsListItem({ pubkey }: { pubkey: string }) { ) : ( {pubkey} -
-
- {user?.nip05 || user?.name || shortenKey(pubkey)} -
- {account?.pubkey === pubkey && ( - (you) - )} +
+
+
+ {user?.nip05 || user?.name || shortenKey(data.sender_pubkey)} +
+
+
+ {data.new_messages && ( + + {data.new_messages} + + )} +
)} diff --git a/src/app/chat/components/list.tsx b/src/app/chat/components/list.tsx index 581fdce0..9fd9ce73 100644 --- a/src/app/chat/components/list.tsx +++ b/src/app/chat/components/list.tsx @@ -1,4 +1,5 @@ import { ChatsListItem } from "@app/chat/components/item"; +import { ChatsListSelfItem } from "@app/chat/components/self"; import { useActiveAccount } from "@stores/accounts"; import { useChats } from "@stores/chats"; import { useEffect } from "react"; @@ -15,6 +16,7 @@ export function ChatsList() { return (
+ {!chats ? ( <>
@@ -27,8 +29,8 @@ export function ChatsList() {
) : ( - chats.map((item: { sender_pubkey: string }) => ( - + chats.map((item) => ( + )) )}
diff --git a/src/app/chat/components/self.tsx b/src/app/chat/components/self.tsx new file mode 100644 index 00000000..9d3bb0f1 --- /dev/null +++ b/src/app/chat/components/self.tsx @@ -0,0 +1,52 @@ +import { Image } from "@shared/image"; +import { DEFAULT_AVATAR } from "@stores/constants"; +import { usePageContext } from "@utils/hooks/usePageContext"; +import { useProfile } from "@utils/hooks/useProfile"; +import { shortenKey } from "@utils/shortenKey"; +import { twMerge } from "tailwind-merge"; + +export function ChatsListSelfItem({ data }: { data: any }) { + const pageContext = usePageContext(); + const searchParams: any = pageContext.urlParsed.search; + const pagePubkey = searchParams.pubkey; + + const { user, isError, isLoading } = useProfile(data.pubkey); + + return ( + <> + {isError &&
error
} + {isLoading && !user ? ( +
+
+
+
+
+
+ ) : ( + +
+ {data.pubkey} +
+
+
+ {user?.nip05 || user?.name || shortenKey(data.pubkey)} +
+ (you) +
+
+ )} + + ); +} diff --git a/src/shared/accounts/active.tsx b/src/shared/accounts/active.tsx index 82fe1c7b..36914edd 100644 --- a/src/shared/accounts/active.tsx +++ b/src/shared/accounts/active.tsx @@ -1,6 +1,7 @@ import { Image } from "@shared/image"; import { RelayContext } from "@shared/relayProvider"; import { useActiveAccount } from "@stores/accounts"; +import { useChats } from "@stores/chats"; import { DEFAULT_AVATAR, READONLY_RELAYS } from "@stores/constants"; import { useProfile } from "@utils/hooks/useProfile"; import { sendNativeNotification } from "@utils/notification"; @@ -10,6 +11,7 @@ import useSWRSubscription from "swr/subscription"; export function ActiveAccount({ data }: { data: any }) { const pool: any = useContext(RelayContext); const lastLogin = useActiveAccount((state: any) => state.lastLogin); + const addChat = useChats((state: any) => state.add); const { user } = useProfile(data.pubkey); @@ -22,12 +24,20 @@ export function ActiveAccount({ data }: { data: any }) { { "#p": [key], since: lastLogin, - limit: 20, }, ], READONLY_RELAYS, (event) => { - sendNativeNotification(event.content); + switch (event.kind) { + case 4: + // update state + addChat(event.pubkey); + // send native notifiation + sendNativeNotification(event.content); + break; + default: + break; + } }, ); diff --git a/src/stores/chats.tsx b/src/stores/chats.tsx index 89b58b8e..edecd78d 100644 --- a/src/stores/chats.tsx +++ b/src/stores/chats.tsx @@ -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: [],