1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00
Bogdan Fazakas 165a9b0fb3
Feature/wagmi (#1912)
* wagmi + ethers + web3modal setup

* refactor wallet components

* fallback providers, more config

* kick out useWeb3

* remove all useWeb3 imports

* more web3.js usage removal

* isAddress utils replacement

* restore add token / add network

* less accountId changes

* web3 legacy tinkering, utils/web3 → utils/wallet

* legacy web3 object for ocean.js

* graph sync fix, remove custom network switching code

* package updates, merge fixes

* downgrade to ethers v5

* fix project id

* switch to ConnectKit

* connectkit theming

* add existing chains to wagmi

* rewrite getPaymentCollector()

* kick out getPaymentCollector completely, use wagmi hooks instead

* Revert "kick out getPaymentCollector completely, use wagmi hooks instead"

This reverts commit 54c7d1ef1a2dec0b1575a685125ba78336b30f59.

* switch getPaymentCollector

* calcBaseInGivenDatatokensOut reorg

* wip integrate ocean lib 3.0.0

* update orbis components to use wagmi instead of web hooks

* more oceanjs integration updates

* more refactors

* fix build

* update ocean lib

* fix publish

* fix order fixed rate

* remove logs

* debug and stop infinite cycle orbis connect

* fix orbis dm connection

* mock use network and fix some more tests

* mock wagmi switch network

* mock wagmi  useProvider createClient and connectKit getDefaultClient

* fix jest tests

* try storybook fix

* cleanups and bump ocean lib

* fix order

* bump lib to next.5 and add more modal style

* bump ocean.js lib to 3.0.0

---------

Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2023-05-29 13:28:41 +03:00

122 lines
3.4 KiB
TypeScript

import React, { useRef } from 'react'
import styles from './Postbox.module.css'
import { useOrbis } from '@context/DirectMessages'
import SendIcon from '@images/send.svg'
import { accountTruncate } from '@utils/wallet'
import { didToAddress } from './_utils'
import {
IOrbisMessage,
IOrbisMessageContent
} from '@context/DirectMessages/_types'
export default function Postbox({
replyTo = null,
cancelReplyTo,
callback
}: {
replyTo?: IOrbisMessage
cancelReplyTo?: () => void
callback: (value: IOrbisMessage) => void
}) {
const postBoxArea = useRef(null)
const {
orbis,
account,
conversationId,
updateConversationEmptyMessageStatus
} = useOrbis()
const share = async () => {
if (!account || postBoxArea.current.innerText.trim() === '') return false
const body = postBoxArea.current.innerText.trim()
postBoxArea.current.innerText = ''
const content: IOrbisMessageContent & { decryptedMessage?: string } = {
encryptedMessage: null,
decryptedMessage: body,
master: replyTo ? replyTo.master || replyTo.stream_id : undefined,
reply_to: replyTo ? replyTo.stream_id : undefined
}
const timestamp = Math.floor(Date.now() / 1000)
const _callbackContent: IOrbisMessage = {
conversation_id: conversationId,
content,
creator: account.did,
creator_details: {
did: account.did,
profile: account.details?.profile,
metadata: account.details?.metadata
},
master: replyTo ? replyTo.master || replyTo.stream_id : null,
reply_to: replyTo ? replyTo.stream_id : null,
reply_to_creator_details: replyTo ? replyTo.creator_details : null,
reply_to_details: replyTo ? replyTo.content : null,
stream_id: 'new_post--' + timestamp,
timestamp
}
if (callback) callback(_callbackContent)
const res = await orbis.sendMessage({
conversation_id: conversationId,
body
})
if (res.status === 200) {
_callbackContent.stream_id = res.doc
if (callback) callback(_callbackContent)
updateConversationEmptyMessageStatus(conversationId, false)
}
}
const handleKeyDown = (e: React.KeyboardEvent<HTMLElement>) => {
if (!e.key) return
if (e.key === 'Enter' && !e.shiftKey) {
// Don't generate a new line
e.preventDefault()
share()
}
}
return (
<div className={styles.postbox}>
{replyTo && (
<div className={styles.replyto}>
<div className={styles.replytoDetails}>
Replying to{' '}
<strong>
{replyTo?.creator_details?.metadata?.ensName ||
accountTruncate(didToAddress(replyTo?.creator_details?.did))}
</strong>
</div>
<button className={styles.replytoCancel} onClick={cancelReplyTo}>
&times;
</button>
</div>
)}
<div className={styles.postboxInput}>
<div
id="postbox-area"
ref={postBoxArea}
className={styles.editable}
contentEditable={true}
data-placeholder="Type your message here..."
onKeyDown={handleKeyDown}
/>
<button
type="button"
className={styles.sendButton}
onClick={share}
disabled={!conversationId}
>
<SendIcon className={styles.icon} />
</button>
</div>
</div>
)
}