1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

use getEnsName util

This commit is contained in:
marcoelissa 2022-12-15 19:05:17 +07:00
parent 9b0da69637
commit b7d3119b6e
3 changed files with 100 additions and 100 deletions

View File

@ -12,6 +12,7 @@ import { Orbis } from '@orbisclub/orbis-sdk'
import { useWeb3 } from './Web3' import { useWeb3 } from './Web3'
import { accountTruncate } from '@utils/web3' import { accountTruncate } from '@utils/web3'
import { didToAddress } from '@utils/orbis' import { didToAddress } from '@utils/orbis'
import { getEnsName } from '@utils/ens'
import usePrevious from '@hooks/usePrevious' import usePrevious from '@hooks/usePrevious'
import useLocalStorage from '@hooks/useLocalStorage' import useLocalStorage from '@hooks/useLocalStorage'
@ -24,7 +25,6 @@ type IOrbisProvider = {
openConversations: boolean openConversations: boolean
conversationId: string conversationId: string
conversations: IOrbisConversation[] conversations: IOrbisConversation[]
conversationTitle: string
notifications: Record<string, string[]> notifications: Record<string, string[]>
connectOrbis: (options: { connectOrbis: (options: {
address: string address: string
@ -45,7 +45,7 @@ type IOrbisProvider = {
setConversationId: (value: string) => void setConversationId: (value: string) => void
createConversation: (value: string) => Promise<void> createConversation: (value: string) => Promise<void>
clearMessageNotifs: (conversationId: string) => void clearMessageNotifs: (conversationId: string) => void
getConversationTitle: (conversation: IOrbisConversation) => string getConversationTitle: (conversationId: string) => Promise<string>
getDid: (value: string) => Promise<string> getDid: (value: string) => Promise<string>
} }
@ -201,74 +201,6 @@ function OrbisProvider({ children }: { children: ReactNode }): ReactElement {
return _did return _did
} }
const getConversations = async (did: string = null) => {
const { data } = await orbis.getConversations({
did,
context: CONVERSATION_CONTEXT
})
// Only show conversations with exactly 2 unique participants and remove duplicates based on participants
const filteredConversations: IOrbisConversation[] = []
data.forEach((conversation: IOrbisConversation) => {
if (conversation.recipients.length === 2) {
const found = filteredConversations.find(
(o: IOrbisConversation) =>
o.recipients.length === 2 &&
o.recipients.includes(conversation.recipients[0]) &&
o.recipients.includes(conversation.recipients[1])
)
if (!found) {
filteredConversations.push(conversation)
}
}
})
setConversations(filteredConversations)
return filteredConversations
}
const createConversation = async (userDid: string) => {
let _account = account
if (!_account) {
// Check connection and force connect
_account = await checkOrbisConnection({
address: accountId,
autoConnect: true
})
}
if (!hasLit) {
const res = await connectLit()
if (res.status !== 200) return
}
let _conversations = [...conversations]
if (!_conversations.length) {
_conversations = await getConversations(_account?.did)
}
const existingConversations = _conversations.filter(
(conversation: IOrbisConversation) => {
return conversation.recipients.includes(userDid)
}
)
if (existingConversations.length > 0) {
setConversationId(existingConversations[0].stream_id)
setOpenConversations(true)
} else {
const res = await orbis.createConversation({
recipients: [userDid],
context: CONVERSATION_CONTEXT
})
if (res.status === 200) {
setConversationId(res.doc)
setOpenConversations(true)
}
}
}
const getMessageNotifs = async () => { const getMessageNotifs = async () => {
let did = account?.did let did = account?.did
@ -329,39 +261,96 @@ function OrbisProvider({ children }: { children: ReactNode }): ReactElement {
setUsersNotifications(_usersNotifications) setUsersNotifications(_usersNotifications)
} }
const getConversationTitle = (conversation: IOrbisConversation) => { const getConversations = async (did: string = null) => {
let title = null const { data } = await orbis.getConversations({
did,
context: CONVERSATION_CONTEXT
})
if (conversation) { // Only show conversations with exactly 2 unique participants and remove duplicates based on participants
const details = conversation.recipients_details.find( const filteredConversations: IOrbisConversation[] = []
(o: IOrbisProfile) => o.did !== account.did data.forEach((conversation: IOrbisConversation) => {
) if (conversation.recipients.length === 2) {
const did = conversation.recipients.find((o: string) => o !== account.did) const found = filteredConversations.find(
(o: IOrbisConversation) =>
o.recipients.length === 2 &&
o.recipients.includes(conversation.recipients[0]) &&
o.recipients.includes(conversation.recipients[1])
)
const address = didToAddress(did) if (!found) {
filteredConversations.push(conversation)
if (details) { }
title = details?.metadata?.ensName || accountTruncate(address)
} else {
title = accountTruncate(address)
} }
} })
return title // Also fetch message notifications
await getMessageNotifs()
setConversations(filteredConversations)
return filteredConversations
} }
const conversationTitle = useMemo(() => { const createConversation = async (userDid: string) => {
let title = null let _account = account
if (!_account) {
// Check connection and force connect
_account = await checkOrbisConnection({
address: accountId,
autoConnect: true
})
}
if (!hasLit) {
const res = await connectLit()
if (res.status !== 200) return
}
let _conversations = [...conversations]
if (!_conversations.length) {
_conversations = await getConversations(_account?.did)
}
const existingConversations = _conversations.filter(
(conversation: IOrbisConversation) => {
return conversation.recipients.includes(userDid)
}
)
if (existingConversations.length > 0) {
setConversationId(existingConversations[0].stream_id)
setOpenConversations(true)
} else {
const res = await orbis.createConversation({
recipients: [userDid],
context: CONVERSATION_CONTEXT
})
if (res.status === 200) {
setConversationId(res.doc)
setOpenConversations(true)
}
}
}
const getConversationTitle = async (conversationId: string) => {
if (conversationId && conversations.length) { if (conversationId && conversations.length) {
// Get conversation based on conversationId
const conversation = conversations.find( const conversation = conversations.find(
(o) => o.stream_id === conversationId (o) => o.stream_id === conversationId
) )
title = getConversationTitle(conversation)
}
return title // Get address from did
// eslint-disable-next-line react-hooks/exhaustive-deps const did = conversation.recipients.find((o: string) => o !== account.did)
}, [conversationId, conversations]) const address = didToAddress(did)
// Get ens name if exists
const ensName = await getEnsName(address)
return ensName || accountTruncate(address)
} else {
return null
}
}
const notifications = useMemo(() => { const notifications = useMemo(() => {
let _notifications = {} let _notifications = {}
@ -372,7 +361,7 @@ function OrbisProvider({ children }: { children: ReactNode }): ReactElement {
}, [accountId, usersNotifications]) }, [accountId, usersNotifications])
useInterval(async () => { useInterval(async () => {
await getMessageNotifs() await getConversations(account?.did)
}, NOTIFICATION_REFRESH_INTERVAL) }, NOTIFICATION_REFRESH_INTERVAL)
useEffect(() => { useEffect(() => {
@ -389,7 +378,6 @@ function OrbisProvider({ children }: { children: ReactNode }): ReactElement {
useEffect(() => { useEffect(() => {
if (account) { if (account) {
getConversations(account?.did) getConversations(account?.did)
getMessageNotifs()
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [account]) }, [account])
@ -403,7 +391,6 @@ function OrbisProvider({ children }: { children: ReactNode }): ReactElement {
openConversations, openConversations,
conversationId, conversationId,
conversations, conversations,
conversationTitle,
notifications, notifications,
connectOrbis, connectOrbis,
disconnectOrbis, disconnectOrbis,

View File

@ -1,4 +1,4 @@
import React from 'react' import React, { useState, useEffect } from 'react'
import styles from './Header.module.css' import styles from './Header.module.css'
import { useOrbis } from '@context/Orbis' import { useOrbis } from '@context/Orbis'
import ChatBubble from '@images/chatbubble.svg' import ChatBubble from '@images/chatbubble.svg'
@ -9,12 +9,14 @@ export default function Header() {
const { const {
conversationId, conversationId,
openConversations, openConversations,
conversationTitle,
notifications, notifications,
getConversationTitle,
setOpenConversations, setOpenConversations,
setConversationId setConversationId
} = useOrbis() } = useOrbis()
const [name, setName] = useState<string>(null)
const handleToggle = ( const handleToggle = (
e: React.MouseEvent<HTMLDivElement | HTMLButtonElement> e: React.MouseEvent<HTMLDivElement | HTMLButtonElement>
) => { ) => {
@ -27,6 +29,14 @@ export default function Header() {
} }
} }
useEffect(() => {
if (conversationId) {
getConversationTitle(conversationId).then((name) => setName(name))
} else {
setName(null)
}
}, [conversationId, getConversationTitle])
return ( return (
<div className={styles.header} onClick={handleToggle}> <div className={styles.header} onClick={handleToggle}>
{!conversationId ? ( {!conversationId ? (
@ -57,7 +67,7 @@ export default function Header() {
/> />
</button> </button>
)} )}
<span>{conversationTitle}</span> <span>{name}</span>
</> </>
)} )}
<div className={styles.toggleArrow}> <div className={styles.toggleArrow}>

View File

@ -19,7 +19,7 @@ export default function ConversationItem({
const newCancelToken = useCancelToken() const newCancelToken = useCancelToken()
const name = getConversationTitle(conversation) const [name, setName] = useState<string>(null)
const [address, setAddress] = useState(null) const [address, setAddress] = useState(null)
useEffect(() => { useEffect(() => {
@ -28,12 +28,15 @@ export default function ConversationItem({
const _address = didToAddress(did) const _address = didToAddress(did)
setAddress(_address) setAddress(_address)
const _name = await getConversationTitle(conversation?.stream_id)
setName(_name)
} }
if (conversation && account) { if (conversation && account) {
getProfile() getProfile()
} }
}, [conversation, account, newCancelToken]) }, [conversation, account, newCancelToken, getConversationTitle])
return ( return (
<div <div