mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex-taker-v2.git
synced 2026-09-21 16:52:01 -07:00
feat(nostr): Refactored profile page
This commit is contained in:
@@ -24,7 +24,7 @@ import SweepWif from './sweep/index.js'
|
||||
import SignMessage from './sign/index.js'
|
||||
import ServerSelectView from './configuration/select-server-view'
|
||||
import NostrPost from './nostr/nostr-post/index.js'
|
||||
import NostrRead from './nostr/nostr-read/index.js'
|
||||
import Profile from './nostr/profile/index.js'
|
||||
import GlobalFeed from './nostr/global-feed/index.js'
|
||||
import ContentCreators from './nostr/content-creators/index.js'
|
||||
import UserDataReview from './user-data-review'
|
||||
@@ -49,7 +49,7 @@ function AppBody (props) {
|
||||
<Route path='/sign' element={<SignMessage appData={appData} />} />
|
||||
<Route path='/configuration' element={<ServerSelectView appData={appData} />} />
|
||||
<Route path='/nostr-post' element={<NostrPost appData={appData} />} />
|
||||
<Route path='/nostr-read/:npub' element={<NostrRead appData={appData} />} />
|
||||
<Route path='/profile/:npub' element={<Profile appData={appData} />} />
|
||||
<Route path='/global-feed' element={<GlobalFeed appData={appData} />} />
|
||||
<Route path='/content-creators' element={<ContentCreators appData={appData} />} />
|
||||
<Route path='/user-data/:tokenId' element={<UserDataReview appData={appData} />} />
|
||||
|
||||
@@ -46,7 +46,7 @@ function ContentCard (props) {
|
||||
}, [loaded, creator])
|
||||
|
||||
const goToProfile = () => {
|
||||
const profileUrl = `${window.location.origin}/nostr-read/${creator.npub}`
|
||||
const profileUrl = `${window.location.origin}/profile/${creator.npub}`
|
||||
window.open(profileUrl, '_blank')
|
||||
}
|
||||
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
/**
|
||||
* Component for read nostr information kind 1
|
||||
*/
|
||||
// Global npm libraries
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { Container, Card, Spinner } from 'react-bootstrap'
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
import { faUser } from '@fortawesome/free-solid-svg-icons'
|
||||
import NostrFormat from '../nostr-format'
|
||||
import { RelayPool } from 'nostr'
|
||||
import * as nip19 from 'nostr-tools/nip19'
|
||||
|
||||
function PublicRead (props) {
|
||||
const { npub } = props
|
||||
const { bchWalletState } = props.appData
|
||||
const [posts, setPosts] = useState([])
|
||||
const [loaded, setLoaded] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
// Get Last post from a author
|
||||
const start = () => {
|
||||
const pubHexData = nip19.decode(npub)
|
||||
const pubHex = pubHexData.data
|
||||
console.log('pubhex', pubHex)
|
||||
const psf = 'wss://nostr-relay.psfoundation.info'
|
||||
|
||||
const pool = RelayPool([psf])
|
||||
pool.on('open', relay => {
|
||||
relay.subscribe('subid', { limit: 5, kinds: [1], authors: [pubHex] })
|
||||
setLoaded(true)
|
||||
})
|
||||
|
||||
pool.on('eose', relay => {
|
||||
console.log('Closing Relay')
|
||||
setLoaded(true)
|
||||
relay.close()
|
||||
})
|
||||
|
||||
pool.on('event', (relay, subId, ev) => {
|
||||
console.log('Received event:', ev)
|
||||
setPosts(currentPosts => [...currentPosts, ev])
|
||||
})
|
||||
}
|
||||
|
||||
if (!loaded && npub) {
|
||||
start()
|
||||
}
|
||||
}, [bchWalletState, loaded, npub])
|
||||
|
||||
return (
|
||||
<Container className='mt-4'>
|
||||
<div>
|
||||
{posts.map((post, index) => (
|
||||
<Card key={index} className='mb-4 bg-light rounded-4 shadow-sm border-0'>
|
||||
<Card.Body className='p-3'>
|
||||
<div className='d-flex align-items-center gap-3 mb-3'>
|
||||
<div
|
||||
className='rounded-circle bg-gradient shadow d-flex align-items-center justify-content-center'
|
||||
style={{
|
||||
width: '48px',
|
||||
height: '48px',
|
||||
background: 'linear-gradient(45deg, #6c757d, #495057)'
|
||||
}}
|
||||
>
|
||||
<FontAwesomeIcon icon={faUser} size='1x' color='#7c7c7d' />
|
||||
</div>
|
||||
<div className='flex-grow-1'>
|
||||
<div className='fw-bold mb-1'>{props.profile?.name}</div>
|
||||
<small className='text-muted'>
|
||||
{`${bchWalletState.nostrKeyPair.npub.slice(0, 8)}...${bchWalletState.nostrKeyPair.npub.slice(-5)}`}
|
||||
</small>
|
||||
</div>
|
||||
<small className='text-muted'>
|
||||
{new Date(post.created_at * 1000).toLocaleString()}
|
||||
</small>
|
||||
</div>
|
||||
<div className='mb-4'>
|
||||
<div className='fs-5 ms-5 text-dark' style={{ lineHeight: '1.3' }}>
|
||||
<NostrFormat content={post.content} />
|
||||
</div>
|
||||
</div>
|
||||
</Card.Body>
|
||||
</Card>
|
||||
))}
|
||||
{!posts.length && loaded && (
|
||||
<div className='text-center text-muted py-5 bg-light rounded-4 shadow-sm'>
|
||||
<i className='bi bi-inbox-fill fs-1 mb-3 d-block opacity-50' />
|
||||
<div>No posts found</div>
|
||||
</div>
|
||||
)}
|
||||
{!loaded && (
|
||||
<div className='text-center text-muted py-5 bg-light rounded-4 shadow-sm'>
|
||||
<Spinner animation='border' />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
|
||||
export default PublicRead
|
||||
+4
-2
@@ -8,8 +8,9 @@ import { Container } from 'react-bootstrap'
|
||||
import ProfileRead from './profile-read.js'
|
||||
import PublicRead from './public-read.js'
|
||||
import { useParams } from 'react-router-dom'
|
||||
import SlpTokensDisplay from './slp-tokens-display'
|
||||
|
||||
function NostrRead (props) {
|
||||
function Profile (props) {
|
||||
const [profile, setProfile] = useState(false)
|
||||
const { npub } = useParams()
|
||||
console.log('npub', npub)
|
||||
@@ -18,9 +19,10 @@ function NostrRead (props) {
|
||||
<Container>
|
||||
<ProfileRead {...props} setProfile={setProfile} npub={npub} />
|
||||
<PublicRead {...props} profile={profile} npub={npub} />
|
||||
<SlpTokensDisplay {...props} npub={npub} />
|
||||
</Container>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default NostrRead
|
||||
export default Profile
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Component for read nostr information kind 1
|
||||
*/
|
||||
// Global npm libraries
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { Container, Card, Spinner } from 'react-bootstrap'
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
import { faUser } from '@fortawesome/free-solid-svg-icons'
|
||||
import NostrFormat from '../nostr-format'
|
||||
import { RelayPool } from 'nostr'
|
||||
import * as nip19 from 'nostr-tools/nip19'
|
||||
|
||||
function PublicRead (props) {
|
||||
const { npub } = props
|
||||
const { bchWalletState } = props.appData
|
||||
const [posts, setPosts] = useState([])
|
||||
const [loaded, setLoaded] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
// Get Last post from a author
|
||||
const start = () => {
|
||||
const pubHexData = nip19.decode(npub)
|
||||
const pubHex = pubHexData.data
|
||||
console.log('pubhex', pubHex)
|
||||
const psf = 'wss://nostr-relay.psfoundation.info'
|
||||
|
||||
const pool = RelayPool([psf])
|
||||
pool.on('open', relay => {
|
||||
relay.subscribe('subid', { limit: 5, kinds: [1], authors: [pubHex] })
|
||||
setLoaded(true)
|
||||
})
|
||||
|
||||
pool.on('eose', relay => {
|
||||
console.log('Closing Relay')
|
||||
setLoaded(true)
|
||||
relay.close()
|
||||
})
|
||||
|
||||
pool.on('event', (relay, subId, ev) => {
|
||||
console.log('Received event:', ev)
|
||||
setPosts(currentPosts => [...currentPosts, ev])
|
||||
})
|
||||
}
|
||||
|
||||
if (!loaded && npub) {
|
||||
start()
|
||||
}
|
||||
}, [bchWalletState, loaded, npub])
|
||||
|
||||
return (
|
||||
<Container className='mt-4 mb-5'>
|
||||
{/* Nostr Feed Section */}
|
||||
<div className='bg-white rounded-1 shadow-sm border mb-4'>
|
||||
<div className='p-3 border-bottom bg-light rounded-top-4'>
|
||||
<h5 className='mb-0 fw-bold text-dark'>Nostr Feed</h5>
|
||||
</div>
|
||||
<div
|
||||
className='p-3'
|
||||
style={{
|
||||
height: '800px',
|
||||
overflowY: 'auto',
|
||||
maxHeight: '800px'
|
||||
}}
|
||||
>
|
||||
{posts.map((post, index) => (
|
||||
<Card key={index} className='mb-4 bg-light rounded-4 shadow-sm border-0'>
|
||||
<Card.Body className='p-3'>
|
||||
<div className='d-flex align-items-center gap-3 mb-3'>
|
||||
<div
|
||||
className='rounded-circle bg-gradient shadow d-flex align-items-center justify-content-center'
|
||||
style={{
|
||||
width: '48px',
|
||||
height: '48px',
|
||||
background: 'linear-gradient(45deg, #6c757d, #495057)'
|
||||
}}
|
||||
>
|
||||
<FontAwesomeIcon icon={faUser} size='1x' color='#7c7c7d' />
|
||||
</div>
|
||||
<div className='flex-grow-1'>
|
||||
<div className='fw-bold mb-1'>{props.profile?.name}</div>
|
||||
<small className='text-muted'>
|
||||
{`${bchWalletState.nostrKeyPair.npub.slice(0, 8)}...${bchWalletState.nostrKeyPair.npub.slice(-5)}`}
|
||||
</small>
|
||||
</div>
|
||||
<small className='text-muted'>
|
||||
{new Date(post.created_at * 1000).toLocaleString()}
|
||||
</small>
|
||||
</div>
|
||||
<div className='mb-4'>
|
||||
<div className='fs-5 ms-5 text-dark' style={{ lineHeight: '1.3' }}>
|
||||
<NostrFormat content={post.content} />
|
||||
</div>
|
||||
</div>
|
||||
</Card.Body>
|
||||
</Card>
|
||||
))}
|
||||
{!posts.length && loaded && (
|
||||
<div className='text-center text-muted py-5 bg-light rounded-4 shadow-sm'>
|
||||
<i className='bi bi-inbox-fill fs-1 mb-3 d-block opacity-50' />
|
||||
<div>No posts found</div>
|
||||
</div>
|
||||
)}
|
||||
{!loaded && (
|
||||
<div className='text-center text-muted py-5 bg-light rounded-4 shadow-sm'>
|
||||
<Spinner animation='border' />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
|
||||
export default PublicRead
|
||||
@@ -0,0 +1,169 @@
|
||||
/**
|
||||
* Component for displaying SLP tokens in a grid layout
|
||||
*/
|
||||
import React, { useState, useEffect, useCallback } from 'react'
|
||||
import { Container, Row, Col, Spinner } from 'react-bootstrap'
|
||||
import config from '../../../../config'
|
||||
import axios from 'axios'
|
||||
import TokenCard from '../../slp-tokens/token-card'
|
||||
|
||||
function SlpTokensDisplay (props) {
|
||||
const { appData } = props
|
||||
const { bchWalletState, wallet } = props.appData
|
||||
const { npub } = props
|
||||
const [loaded, setLoaded] = useState(false)
|
||||
const [tokens, setTokens] = useState([])
|
||||
const [iconsAreLoaded, setIconsAreLoaded] = useState(false)
|
||||
|
||||
// Get Cid from url
|
||||
const parseCid = (url) => {
|
||||
// get the cid from the url format 'ipfs://bafybeicem27xbzs65uvbcgykcmscsgln3lmhbfrcoec3gdttkdgtxv5acq
|
||||
if (url && url.includes('ipfs://')) {
|
||||
const cid = url.split('ipfs://')[1]
|
||||
return cid
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
// Fetch mutable data if it exist and get the token icon url
|
||||
const fetchTokenIcon = useCallback(async (token) => {
|
||||
try {
|
||||
// Get the token data
|
||||
const tokenData = await appData.wallet.getTokenData(token.tokenId)
|
||||
if (!tokenData.mutableData) return false // Return false if no mutable data
|
||||
// Get the token icon from the mutable data
|
||||
const cid = parseCid(tokenData.mutableData)
|
||||
console.log('mutable data cid', cid)
|
||||
|
||||
const { json } = await appData.wallet.cid2json({ cid })
|
||||
|
||||
if (!json) return false
|
||||
|
||||
const iconUrl = json.tokenIcon
|
||||
// Return icon url
|
||||
return iconUrl
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}, [appData])
|
||||
|
||||
// This function loads the token icons from the ipfs gateways.
|
||||
const lazyLoadTokenIcons = useCallback(async (tokenList) => {
|
||||
try {
|
||||
setIconsAreLoaded(false)
|
||||
|
||||
// map each token and fetch the icon url
|
||||
for (let i = 0; i < tokenList.length; i++) {
|
||||
try {
|
||||
const thisToken = tokenList[i]
|
||||
|
||||
// Incon does not need to be downloaded, so continue with the next one
|
||||
if (thisToken.iconAlreadyDownloaded) continue
|
||||
|
||||
// Try to get token icon url from mutable data.
|
||||
const iconUrl = await fetchTokenIcon(thisToken)
|
||||
console.log('iconUrl', iconUrl)
|
||||
if (iconUrl) {
|
||||
// Set the icon url to the token , this can be used to display the icon in the token card component.
|
||||
thisToken.icon = iconUrl
|
||||
}
|
||||
|
||||
// Mark token to prevent fetch token icon again.
|
||||
thisToken.iconAlreadyDownloaded = true
|
||||
// replace the token in the tokens array
|
||||
const updatedTokens = [...tokenList]
|
||||
updatedTokens[i] = thisToken
|
||||
setTokens(updatedTokens)
|
||||
} catch (error) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
setIconsAreLoaded(true)
|
||||
} catch (error) {
|
||||
console.warn(error)
|
||||
setIconsAreLoaded(true)
|
||||
}
|
||||
}, [fetchTokenIcon, setTokens])
|
||||
|
||||
useEffect(() => {
|
||||
// setTokens(bchWalletState.slpTokens || [])
|
||||
const getTokens = async () => {
|
||||
try {
|
||||
const { nostrKeyPair } = bchWalletState
|
||||
if (nostrKeyPair.npub === npub) {
|
||||
setTokens(bchWalletState.slpTokens)
|
||||
setLoaded(true)
|
||||
return
|
||||
}
|
||||
setLoaded(false)
|
||||
const url = `${config.dexServer}/sm/npub/${npub}`
|
||||
const response = await axios.get(url)
|
||||
console.log('response', response.data)
|
||||
const addr = response.data.bchAddr
|
||||
|
||||
const tokenList = await wallet.listTokens(addr)
|
||||
setTokens(tokenList)
|
||||
// setTokens(response.data)
|
||||
setLoaded(true)
|
||||
|
||||
lazyLoadTokenIcons(tokenList)
|
||||
} catch (error) {
|
||||
console.error('Error fetching tokens:', error)
|
||||
setLoaded(true)
|
||||
}
|
||||
}
|
||||
if (!loaded) {
|
||||
getTokens()
|
||||
}
|
||||
}, [bchWalletState, npub, loaded, lazyLoadTokenIcons, wallet])
|
||||
|
||||
return (
|
||||
<Container>
|
||||
{!loaded && (
|
||||
<div className='d-flex justify-content-center align-items-center h-100 mb-4'>
|
||||
<Spinner />
|
||||
</div>
|
||||
)}
|
||||
{loaded && tokens.length !== 0 &&
|
||||
<div className='bg-white rounded-1 shadow-sm border mb-4'>
|
||||
<div className='p-3 border-bottom bg-light rounded-top-4'>
|
||||
<h5 className='mb-0 fw-bold text-dark'>SLP Tokens</h5>
|
||||
</div>
|
||||
<div
|
||||
className='p-3'
|
||||
style={{
|
||||
height: '800px',
|
||||
overflowY: 'auto',
|
||||
maxHeight: '800px'
|
||||
}}
|
||||
>
|
||||
<Row>
|
||||
{!iconsAreLoaded &&
|
||||
<Col xs={12} style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
|
||||
<div style={{ borderRadius: '10px', backgroundColor: '#f0f0f0', padding: '10px', display: 'flex', justifyContent: 'center', alignItems: 'center', width: 'fit-content' }}>
|
||||
<span style={{ marginRight: '10px' }}>Loading Token Icons </span>
|
||||
<Spinner animation='border' />
|
||||
</div>
|
||||
|
||||
</Col>}
|
||||
|
||||
{tokens.map(thisToken => (
|
||||
<TokenCard
|
||||
appData={appData}
|
||||
token={thisToken}
|
||||
key={`${thisToken.tokenId}`}
|
||||
refreshTokens={() => { }}
|
||||
hideSendBtn
|
||||
/>
|
||||
))}
|
||||
|
||||
</Row>
|
||||
|
||||
</div>
|
||||
</div>}
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
|
||||
export default SlpTokensDisplay
|
||||
@@ -12,7 +12,7 @@ import InfoButton from './info-button'
|
||||
import SendTokenButton from './send-token-button'
|
||||
|
||||
function TokenCard (props) {
|
||||
const { token } = props
|
||||
const { token, hideSendBtn } = props
|
||||
const [icon, setIcon] = useState(token.icon)
|
||||
|
||||
// Update icon state every token.icon changes
|
||||
@@ -64,13 +64,15 @@ function TokenCard (props) {
|
||||
<Col>
|
||||
<InfoButton token={props.token} />
|
||||
</Col>
|
||||
<Col>
|
||||
<SendTokenButton
|
||||
token={props.token}
|
||||
appData={props.appData}
|
||||
refreshTokens={props.refreshTokens}
|
||||
/>
|
||||
</Col>
|
||||
{!hideSendBtn && (
|
||||
<Col>
|
||||
<SendTokenButton
|
||||
token={props.token}
|
||||
appData={props.appData}
|
||||
refreshTokens={props.refreshTokens}
|
||||
/>
|
||||
</Col>
|
||||
)}
|
||||
</Row>
|
||||
</Container>
|
||||
</Card.Body>
|
||||
|
||||
@@ -71,8 +71,8 @@ function NavMenu (props) {
|
||||
Nostr Post
|
||||
</NavLink>
|
||||
<NavLink
|
||||
className={currentPath === '/nostr-read' ? 'nav-link-active' : 'nav-link-inactive'}
|
||||
to={`/nostr-read/${bchWalletState?.nostrKeyPair?.npub}`}
|
||||
className={currentPath === '/profile' ? 'nav-link-active' : 'nav-link-inactive'}
|
||||
to={`/profile/${bchWalletState?.nostrKeyPair?.npub}`}
|
||||
onClick={handleClickEvent}
|
||||
>
|
||||
Nostr Profile
|
||||
|
||||
Reference in New Issue
Block a user