2024-03-01 10:16:35 +00:00
|
|
|
import ContentCopyIcon from '@mui/icons-material/ContentCopy'
|
2024-05-10 10:16:28 +00:00
|
|
|
import {
|
2024-05-21 07:07:52 +00:00
|
|
|
Box,
|
2024-05-14 12:39:05 +00:00
|
|
|
IconButton,
|
2024-05-21 07:07:52 +00:00
|
|
|
SxProps,
|
2024-05-10 10:16:28 +00:00
|
|
|
Typography,
|
2024-05-21 07:07:52 +00:00
|
|
|
useTheme,
|
|
|
|
Theme
|
2024-05-10 10:16:28 +00:00
|
|
|
} from '@mui/material'
|
2024-05-21 07:07:52 +00:00
|
|
|
import { nip19, VerifiedEvent } from 'nostr-tools'
|
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
|
|
|
import { Link, useNavigate, useParams } from 'react-router-dom'
|
2024-03-01 10:16:35 +00:00
|
|
|
import { toast } from 'react-toastify'
|
2024-05-21 07:07:52 +00:00
|
|
|
import { MetadataController } from '../../controllers'
|
2024-05-10 10:57:04 +00:00
|
|
|
import { NostrJoiningBlock, ProfileMetadata } from '../../types'
|
2024-03-01 10:16:35 +00:00
|
|
|
import styles from './style.module.scss'
|
2024-05-21 07:07:52 +00:00
|
|
|
import { useSelector } from 'react-redux'
|
2024-03-01 10:16:35 +00:00
|
|
|
import { State } from '../../store/rootReducer'
|
2024-05-21 07:07:52 +00:00
|
|
|
import { getRoboHashPicture, shorten } from '../../utils'
|
|
|
|
import { truncate } from 'lodash'
|
|
|
|
import { getProfileSettingsRoute } from '../../routes'
|
|
|
|
import EditIcon from '@mui/icons-material/Edit';
|
|
|
|
import LinkIcon from '@mui/icons-material/Link';
|
|
|
|
import RestoreIcon from '@mui/icons-material/Restore';
|
2024-03-01 10:16:35 +00:00
|
|
|
import { LoadingSpinner } from '../../components/LoadingSpinner'
|
|
|
|
|
|
|
|
export const ProfilePage = () => {
|
2024-05-21 07:07:52 +00:00
|
|
|
const navigate = useNavigate()
|
2024-05-10 10:16:28 +00:00
|
|
|
const theme = useTheme()
|
|
|
|
|
2024-03-01 10:16:35 +00:00
|
|
|
const { npub } = useParams()
|
|
|
|
|
|
|
|
const metadataController = useMemo(() => new MetadataController(), [])
|
|
|
|
|
|
|
|
const [pubkey, setPubkey] = useState<string>()
|
2024-05-10 10:57:04 +00:00
|
|
|
const [nostrJoiningBlock, setNostrJoiningBlock] =
|
|
|
|
useState<NostrJoiningBlock | null>(null)
|
2024-03-01 10:16:35 +00:00
|
|
|
const [profileMetadata, setProfileMetadata] = useState<ProfileMetadata>()
|
|
|
|
const metadataState = useSelector((state: State) => state.metadata)
|
2024-05-21 07:07:52 +00:00
|
|
|
const { usersPubkey } = useSelector((state: State) => state.auth)
|
2024-05-17 11:33:01 +00:00
|
|
|
const userRobotImage = useSelector((state: State) => state.userRobotImage)
|
2024-03-01 10:16:35 +00:00
|
|
|
|
|
|
|
const [isUsersOwnProfile, setIsUsersOwnProfile] = useState(false)
|
|
|
|
|
|
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
|
|
const [loadingSpinnerDesc] = useState('Fetching metadata')
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (npub) {
|
|
|
|
try {
|
|
|
|
const hexPubkey = nip19.decode(npub).data as string
|
|
|
|
setPubkey(hexPubkey)
|
|
|
|
|
|
|
|
if (hexPubkey === usersPubkey) setIsUsersOwnProfile(true)
|
|
|
|
} catch (error) {
|
|
|
|
toast.error('Error occurred in decoding npub' + error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [npub, usersPubkey])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-10 10:16:28 +00:00
|
|
|
if (pubkey) {
|
|
|
|
metadataController
|
|
|
|
.getNostrJoiningBlockNumber(pubkey)
|
|
|
|
.then((res) => {
|
2024-05-10 10:57:04 +00:00
|
|
|
setNostrJoiningBlock(res)
|
2024-05-10 10:16:28 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
// todo: handle error
|
|
|
|
console.log('err :>> ', err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-01 10:16:35 +00:00
|
|
|
if (isUsersOwnProfile && metadataState) {
|
|
|
|
const metadataContent = metadataController.extractProfileMetadataContent(
|
|
|
|
metadataState as VerifiedEvent
|
|
|
|
)
|
|
|
|
if (metadataContent) {
|
|
|
|
setProfileMetadata(metadataContent)
|
|
|
|
setIsLoading(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pubkey) {
|
|
|
|
const getMetadata = async (pubkey: string) => {
|
|
|
|
const metadataEvent = await metadataController
|
|
|
|
.findMetadata(pubkey)
|
|
|
|
.catch((err) => {
|
|
|
|
toast.error(err)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
if (metadataEvent) {
|
|
|
|
const metadataContent =
|
|
|
|
metadataController.extractProfileMetadataContent(metadataEvent)
|
2024-05-13 06:00:44 +00:00
|
|
|
if (metadataContent) {
|
|
|
|
setProfileMetadata(metadataContent)
|
|
|
|
}
|
2024-03-01 10:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setIsLoading(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
getMetadata(pubkey)
|
|
|
|
}
|
|
|
|
}, [isUsersOwnProfile, metadataState, pubkey, metadataController])
|
|
|
|
|
2024-05-21 07:07:52 +00:00
|
|
|
const textElementWithCopyIcon = (text: string) => {
|
|
|
|
const onClick = () => {
|
|
|
|
navigator.clipboard.writeText(text)
|
2024-03-01 10:16:35 +00:00
|
|
|
|
2024-05-21 07:07:52 +00:00
|
|
|
toast.success('Copied to clipboard', {
|
|
|
|
autoClose: 1000,
|
|
|
|
hideProgressBar: true
|
2024-03-01 10:16:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-15 08:50:21 +00:00
|
|
|
return (
|
2024-05-21 07:07:52 +00:00
|
|
|
<Typography variant="caption" className={styles.npubNipItem}>
|
|
|
|
<span onClick={onClick}>{shorten(text)}</span>
|
|
|
|
<ContentCopyIcon onClick={onClick} className={styles.copyIcon} />
|
|
|
|
</Typography>
|
2024-05-15 08:50:21 +00:00
|
|
|
)
|
2024-05-14 12:39:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 07:07:52 +00:00
|
|
|
const titleElement = (text: string, sx?: SxProps<Theme>) => (
|
|
|
|
<Typography sx={sx} variant="h6" className={styles.bold}>
|
|
|
|
{text}
|
|
|
|
</Typography>
|
|
|
|
)
|
|
|
|
|
2024-05-17 11:33:01 +00:00
|
|
|
/**
|
|
|
|
* Handles the logic for Image URL.
|
|
|
|
* If no picture in kind 0 found - use robohash avatar
|
2024-05-17 11:35:37 +00:00
|
|
|
*
|
2024-05-17 11:33:01 +00:00
|
|
|
* @returns robohash image url
|
|
|
|
*/
|
|
|
|
const getProfileImage = (metadata: ProfileMetadata) => {
|
2024-05-21 07:07:52 +00:00
|
|
|
if (!metadata) return ''
|
|
|
|
|
2024-05-17 11:33:01 +00:00
|
|
|
if (!isUsersOwnProfile) {
|
|
|
|
return metadata.picture || getRoboHashPicture(npub!)
|
|
|
|
}
|
2024-05-17 11:35:37 +00:00
|
|
|
|
2024-05-17 11:33:01 +00:00
|
|
|
// userRobotImage is used only when visiting own profile
|
|
|
|
// while kind 0 picture is not set
|
|
|
|
return metadata.picture || userRobotImage || getRoboHashPicture(npub!)
|
|
|
|
}
|
|
|
|
|
2024-03-01 10:16:35 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{isLoading && <LoadingSpinner desc={loadingSpinnerDesc} />}
|
2024-05-21 07:07:52 +00:00
|
|
|
{pubkey && (
|
|
|
|
<Box className={styles.container}>
|
|
|
|
<Box className={styles.upper}>
|
|
|
|
<Box className={styles.left}>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
borderWidth: '2px',
|
|
|
|
borderStyle: pubkey ? 'solid' : 'none',
|
|
|
|
borderColor: `#${pubkey?.substring(0, 6)}`
|
2024-03-01 10:16:35 +00:00
|
|
|
}}
|
2024-05-21 07:07:52 +00:00
|
|
|
className={styles.imageWrapper}
|
2024-03-01 10:16:35 +00:00
|
|
|
>
|
|
|
|
<img
|
2024-05-21 07:07:52 +00:00
|
|
|
className={styles['image-placeholder']}
|
|
|
|
src={getProfileImage(profileMetadata!)}
|
2024-03-01 10:16:35 +00:00
|
|
|
/>
|
2024-05-21 07:07:52 +00:00
|
|
|
</div>
|
|
|
|
</Box>
|
|
|
|
<Box>
|
|
|
|
<Box className={styles.head}>
|
|
|
|
<Box className={styles.npubNip}>
|
|
|
|
<Box
|
2024-05-10 10:16:28 +00:00
|
|
|
sx={{
|
2024-05-21 07:07:52 +00:00
|
|
|
display: 'flex'
|
2024-05-10 10:16:28 +00:00
|
|
|
}}
|
|
|
|
>
|
2024-05-21 07:07:52 +00:00
|
|
|
{profileMetadata &&
|
|
|
|
titleElement(
|
|
|
|
truncate(
|
|
|
|
profileMetadata.display_name ||
|
|
|
|
profileMetadata.name ||
|
|
|
|
pubkey,
|
|
|
|
{
|
|
|
|
length: 16
|
|
|
|
}
|
|
|
|
),
|
|
|
|
{ marginRight: 1 }
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
<Box>
|
|
|
|
{textElementWithCopyIcon(pubkey || '')}
|
|
|
|
</Box>
|
|
|
|
<Box>
|
|
|
|
{profileMetadata?.nip05 &&
|
|
|
|
textElementWithCopyIcon(profileMetadata.nip05)}
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
|
|
|
|
<Box>
|
|
|
|
{profileMetadata?.website && (
|
|
|
|
<Typography sx={{ marginTop: '10px' }} variant='caption' className={`${styles.website} ${styles.captionWrapper}`}>
|
|
|
|
<LinkIcon className={styles.captionIcon}/>
|
|
|
|
{profileMetadata.website}
|
|
|
|
</Typography>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
|
|
|
|
<Box>
|
|
|
|
{nostrJoiningBlock && (
|
|
|
|
<Typography
|
|
|
|
variant='caption'
|
|
|
|
sx={{
|
|
|
|
color: theme.palette.getContrastText(
|
|
|
|
theme.palette.background.paper
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
className={styles.captionWrapper}
|
|
|
|
component={Link}
|
|
|
|
to={`https://njump.me/${nostrJoiningBlock.encodedEventPointer}`}
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
<RestoreIcon className={styles.captionIcon}/>
|
|
|
|
On nostr since {nostrJoiningBlock.block.toLocaleString()}
|
|
|
|
</Typography>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
<Box className={styles.right}>
|
2024-03-01 10:16:35 +00:00
|
|
|
{isUsersOwnProfile && (
|
2024-05-21 07:07:52 +00:00
|
|
|
<IconButton onClick={() => navigate(getProfileSettingsRoute(pubkey))}>
|
|
|
|
<EditIcon/>
|
|
|
|
</IconButton>
|
2024-03-01 10:16:35 +00:00
|
|
|
)}
|
2024-05-21 07:07:52 +00:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
<Box>
|
|
|
|
{profileMetadata?.about && (
|
|
|
|
<Typography mt={1} className={styles.about}>
|
|
|
|
{profileMetadata.about}
|
|
|
|
</Typography>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
)}
|
2024-03-01 10:16:35 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|