diff --git a/src/app/chat/components/list.tsx b/src/app/chat/components/list.tsx index ee53e31e..b4ffed92 100644 --- a/src/app/chat/components/list.tsx +++ b/src/app/chat/components/list.tsx @@ -6,7 +6,10 @@ import { ChatsListSelfItem } from '@app/chat/components/self'; import { getChats } from '@libs/storage'; +import { StrangersIcon } from '@shared/icons'; + import { useAccount } from '@utils/hooks/useAccount'; +import { compactNumber } from '@utils/number'; export function ChatsList() { const { account } = useAccount(); @@ -15,11 +18,7 @@ export function ChatsList() { data: chats, isFetching, } = useQuery(['chats'], async () => { - const chats = await getChats(); - const sorted = chats.sort( - (a, b) => parseInt(a.new_messages) - parseInt(b.new_messages) - ); - return sorted; + return await getChats(); }); if (status === 'loading') { @@ -39,7 +38,6 @@ export function ChatsList() { return (
- {account ? ( ) : ( @@ -48,11 +46,25 @@ export function ChatsList() {
)} - {chats.map((item) => { + {chats.follows.map((item) => { if (account.pubkey !== item.sender_pubkey) { return ; } })} + + {isFetching && (
diff --git a/src/app/chat/components/modal.tsx b/src/app/chat/components/modal.tsx index ced45948..018c3210 100644 --- a/src/app/chat/components/modal.tsx +++ b/src/app/chat/components/modal.tsx @@ -36,7 +36,7 @@ export function NewMessageModal() { className="inline-flex h-9 items-center gap-2.5 rounded-md px-2.5" >
- +
New chat
diff --git a/src/libs/storage.tsx b/src/libs/storage.tsx index 4c7735cf..7d00f51e 100644 --- a/src/libs/storage.tsx +++ b/src/libs/storage.tsx @@ -3,7 +3,7 @@ import destr from 'destr'; import Database from 'tauri-plugin-sql-api'; import { getParentID } from '@utils/transform'; -import { LumeEvent } from '@utils/types'; +import { Chats, LumeEvent } from '@utils/types'; let db: null | Database = null; @@ -295,11 +295,30 @@ export async function getChannelUsers(channel_id: string) { export async function getChats() { const db = await connect(); const account = await getActiveAccount(); - const result: any = await db.select( + const follows = + typeof account.follows === 'string' ? JSON.parse(account.follows) : account.follows; + + const chats: { follows: Array | null; unknown: number } = { + follows: [], + unknown: 0, + }; + + let result: Array = await db.select( `SELECT DISTINCT sender_pubkey FROM chats WHERE receiver_pubkey = "${account.pubkey}" ORDER BY created_at DESC;` ); - const newArr: any = result.map((v) => ({ ...v, new_messages: 0 })); - return newArr; + + result = result.map((v) => ({ ...v, new_messages: 0 })); + result = result.sort((a, b) => a.new_messages - b.new_messages); + + chats.follows = result.filter((el) => { + return follows.some((i) => { + return i === el.sender_pubkey; + }); + }); + + chats.unknown = result.length - chats.follows.length; + + return chats; } // get chat messages diff --git a/src/shared/icons/index.tsx b/src/shared/icons/index.tsx index 8227667d..8a2deb45 100644 --- a/src/shared/icons/index.tsx +++ b/src/shared/icons/index.tsx @@ -45,4 +45,5 @@ export * from './follow'; export * from './unfollow'; export * from './reaction'; export * from './thread'; +export * from './strangers'; // @endindex diff --git a/src/shared/icons/strangers.tsx b/src/shared/icons/strangers.tsx new file mode 100644 index 00000000..c0521707 --- /dev/null +++ b/src/shared/icons/strangers.tsx @@ -0,0 +1,22 @@ +import { SVGProps } from 'react'; + +export function StrangersIcon(props: JSX.IntrinsicAttributes & SVGProps) { + return ( + + + + ); +} diff --git a/src/utils/types.d.ts b/src/utils/types.d.ts new file mode 100644 index 00000000..60702a63 --- /dev/null +++ b/src/utils/types.d.ts @@ -0,0 +1,17 @@ +import { NDKEvent } from '@nostr-dev-kit/ndk'; + +export interface LumeEvent extends NDKEvent { + event_id: string; + parent_id: string; +} + +export interface Chats { + id: string; + event_id: string; + receiver_pubkey: string; + sender_pubkey: string; + content: string; + tags: string[][]; + created_at: number; + new_messages: number; +} diff --git a/src/utils/types.ts b/src/utils/types.ts deleted file mode 100644 index 49def25d..00000000 --- a/src/utils/types.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { NDKEvent } from '@nostr-dev-kit/ndk'; - -export interface LumeEvent extends NDKEvent { - event_id: string; - parent_id: string; -}