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

create new DM button component and add to asset

This commit is contained in:
marcoelissa 2023-01-19 17:33:06 +07:00
parent be49e6502d
commit 2d2dbc69e5
5 changed files with 106 additions and 95 deletions

View File

@ -0,0 +1,13 @@
.dmButton {
display: flex;
flex-direction: column;
align-items: flex-end;
margin-left: auto;
padding: calc(var(--spacer) / 4) 0;
}
.dmButton span {
font-size: var(--font-size-small);
color: var(--color-secondary);
margin-top: 4px;
}

View File

@ -0,0 +1,89 @@
import React, { useEffect, useState } from 'react'
import Button from '@shared/atoms/Button'
import styles from './DmButton.module.css'
import { accountTruncate } from '@utils/web3'
import { useWeb3 } from '@context/Web3'
import { useProfile } from '@context/Profile'
import { useOrbis } from '@context/Orbis'
export default function DmButton({
accountId,
text = 'Send Direct Message'
}: {
accountId: string
text?: string
}) {
const { profile, ownAccount } = useProfile()
const { accountId: ownAccountId, connect } = useWeb3()
const {
checkOrbisConnection,
getConversationByDid,
setNewConversation,
setConversationId,
setOpenConversations,
getDid
} = useOrbis()
const [userDid, setUserDid] = useState<string | undefined>()
const [isCreatingConversation, setIsCreatingConversation] = useState(false)
const handleActivation = async () => {
const resConnect = await connect()
if (resConnect) {
await checkOrbisConnection({
address: resConnect,
autoConnect: true,
lit: true
})
}
}
useEffect(() => {
const getUserDid = async () => {
const did = await getDid(accountId)
setUserDid(did)
}
if (accountId) {
getUserDid()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [accountId])
if (!ownAccount && userDid) {
return (
<div className={styles.dmButton}>
<Button
style="primary"
size="small"
disabled={isCreatingConversation}
onClick={async () => {
if (!ownAccountId) {
handleActivation()
} else {
setIsCreatingConversation(true)
const conversation = await getConversationByDid(userDid)
if (conversation) {
setConversationId(conversation.stream_id)
} else {
console.log('need to create new conversation')
const suffix =
profile && profile?.name
? profile?.name
: accountTruncate(accountId.toLowerCase())
setConversationId(`new-${suffix}`)
setNewConversation({
recipients: [userDid]
})
}
setOpenConversations(true)
setIsCreatingConversation(false)
}
}}
>
{isCreatingConversation ? 'Loading...' : text}
</Button>
</div>
)
}
}

View File

@ -6,6 +6,7 @@ import { useAsset } from '@context/Asset'
// import { useWeb3 } from '@context/Web3'
import { getDummyWeb3 } from '@utils/web3'
import { Asset, Datatoken, LoggerInstance } from '@oceanprotocol/lib'
import DmButton from '@shared/Orbis/DirectMessages/DmButton'
export default function MetaFull({ ddo }: { ddo: Asset }): ReactElement {
const [paymentCollector, setPaymentCollector] = useState<string>()
@ -54,6 +55,7 @@ export default function MetaFull({ ddo }: { ddo: Asset }): ReactElement {
<MetaItem title="Docker Image" content={<DockerImage />} />
)}
<MetaItem title="DID" content={<code>{ddo?.id}</code>} />
<DmButton accountId={ddo?.nft?.owner} text="Contact Publisher" />
</div>
) : null
}

View File

@ -52,17 +52,3 @@
right: calc(var(--spacer) / 3);
bottom: calc(var(--spacer) / 6);
}
.dmButton {
display: flex;
flex-direction: column;
align-items: flex-end;
margin-left: auto;
padding: calc(var(--spacer) / 4) 0;
}
.dmButton span {
font-size: var(--font-size-small);
color: var(--color-secondary);
margin-top: 4px;
}

View File

@ -1,14 +1,11 @@
import React, { ReactElement, useEffect, useState } from 'react'
import React, { ReactElement, useState } from 'react'
import PublisherLinks from './PublisherLinks'
import Markdown from '@shared/Markdown'
import Button from '@shared/atoms/Button'
import Stats from './Stats'
import Account from './Account'
import styles from './index.module.css'
import { accountTruncate } from '@utils/web3'
import { useWeb3 } from '@context/Web3'
import { useProfile } from '@context/Profile'
import { useOrbis } from '@context/Orbis'
import DmButton from '@shared/Orbis/DirectMessages/DmButton'
const isDescriptionTextClamped = () => {
const el = document.getElementById('description')
@ -23,82 +20,6 @@ const LinkExternal = ({ url, text }: { url: string; text: string }) => {
)
}
const DmButton = ({ accountId }: { accountId: string }) => {
const { profile, ownAccount } = useProfile()
const { accountId: ownAccountId, connect } = useWeb3()
const {
checkOrbisConnection,
getConversationByDid,
setNewConversation,
setConversationId,
setOpenConversations,
getDid
} = useOrbis()
const [userDid, setUserDid] = useState<string | undefined>()
const [isCreatingConversation, setIsCreatingConversation] = useState(false)
const handleActivation = async () => {
const resConnect = await connect()
if (resConnect) {
await checkOrbisConnection({
address: resConnect,
autoConnect: true,
lit: true
})
}
}
useEffect(() => {
const getUserDid = async () => {
const did = await getDid(accountId)
setUserDid(did)
}
if (accountId) {
getUserDid()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [accountId])
if (!ownAccount && userDid) {
return (
<div className={styles.dmButton}>
<Button
style="primary"
size="small"
disabled={isCreatingConversation}
onClick={async () => {
if (!ownAccountId) {
handleActivation()
} else {
setIsCreatingConversation(true)
const conversation = await getConversationByDid(userDid)
if (conversation) {
setConversationId(conversation.stream_id)
} else {
console.log('need to create new conversation')
const suffix =
profile && profile?.name
? profile?.name
: accountTruncate(accountId.toLowerCase())
setConversationId(`new-${suffix}`)
setNewConversation({
recipients: [userDid]
})
}
setOpenConversations(true)
setIsCreatingConversation(false)
}
}}
>
{isCreatingConversation ? 'Loading...' : 'Send Direct Message'}
</Button>
</div>
)
}
}
export default function AccountHeader({
accountId
}: {