From 323f10d2b81d7b2d89157838b795330f97562c02 Mon Sep 17 00:00:00 2001 From: marcoelissa Date: Fri, 9 Dec 2022 23:14:45 +0700 Subject: [PATCH] update get notifications logic --- src/@context/Orbis.tsx | 72 ++++++++++++------- src/@types/Orbis.d.ts | 16 +++-- .../@shared/Orbis/DirectMessages/Header.tsx | 1 - 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/@context/Orbis.tsx b/src/@context/Orbis.tsx index cdce128a5..60a6760b2 100644 --- a/src/@context/Orbis.tsx +++ b/src/@context/Orbis.tsx @@ -139,6 +139,34 @@ function OrbisProvider({ children }: { children: ReactNode }): ReactElement { return null } + const getDid = async (address: string) => { + if (!address) return null + + const { data, error } = await orbis.getDids(address) + + if (error) { + return + } + + let _did: string = null + + if (data && data.length > 0) { + console.log(data) + + // Try to get mainnet did + const mainnetDid = data.find((o: any) => { + const did = o.did.split(':') + return did[3] === '1' + }) + + _did = mainnetDid?.did || data[0].did + } else { + _did = `did:pkh:eip155:0:${address.toLowerCase()}` + } + + return _did + } + const getConversations = async (did: string = null) => { const { data } = await orbis.getConversations({ did, @@ -205,41 +233,35 @@ function OrbisProvider({ children }: { children: ReactNode }): ReactElement { } const getMessageNotifs = async () => { + let did = account?.did + + if (!did && accountId) { + did = await getDid(accountId) + } + const { data, error } = await orbis.api.rpc('orbis_f_notifications', { - user_did: account?.did || 'none', + user_did: did || 'none', notif_type: 'messages' }) + console.log({ did, data, error }) + if (error) { setUnreadMessages([]) } else if (data.length > 0) { - const _unreads = data.filter((o: IOrbisNotification) => { - return o.status === 'new' - }) - setUnreadMessages(_unreads) + // Check if did is mainnet + const chainId = parseInt(did.split(':')[3]) + if (chainId === 0) { + setUnreadMessages(data) + } else { + const _unreads = data.filter((o: IOrbisNotification) => { + return o.status === 'new' + }) + setUnreadMessages(_unreads) + } } } - const getDid = async (address: string) => { - if (!address) return null - - const { data, error } = await orbis.getDids(address) - - if (error) { - return - } - - let _did: string = null - - if (data && data.length > 0) { - _did = data[0].did - } else { - _did = `did:pkh:eip155:0:${address.toLowerCase()}` - } - - return _did - } - useInterval(async () => { await getMessageNotifs() }, NOTIFICATION_REFRESH_INTERVAL) diff --git a/src/@types/Orbis.d.ts b/src/@types/Orbis.d.ts index 0da522130..2a859a2b1 100644 --- a/src/@types/Orbis.d.ts +++ b/src/@types/Orbis.d.ts @@ -164,11 +164,17 @@ declare interface IOrbis { error: any status: number }> - getNotifications: (options: { type: string; context?: string }) => Promise<{ - data: any - error: any - status: number - }> + getNotifications: ( + options: { + context: string + did: string + master?: string + only_master?: boolean + tag?: string + algorithm?: string + }, + page: number + ) => Promise getPost: (post_id: string) => Promise<{ data: IOrbisPost error: any diff --git a/src/components/@shared/Orbis/DirectMessages/Header.tsx b/src/components/@shared/Orbis/DirectMessages/Header.tsx index d504cf572..6c626ca49 100644 --- a/src/components/@shared/Orbis/DirectMessages/Header.tsx +++ b/src/components/@shared/Orbis/DirectMessages/Header.tsx @@ -7,7 +7,6 @@ import ChevronUp from '@images/chevronup.svg' export default function Header() { const { - // unreadMessages, conversationId, openConversations, conversationTitle,