sigit.io/src/pages/profile/index.tsx

287 lines
8.8 KiB
TypeScript
Raw Normal View History

2024-03-01 10:16:35 +00:00
import ContentCopyIcon from '@mui/icons-material/ContentCopy'
import {
2024-05-21 07:07:52 +00:00
Box,
IconButton,
2024-05-21 07:07:52 +00:00
SxProps,
Typography,
2024-05-21 07:07:52 +00:00
useTheme,
Theme
} 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'
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 13:21:07 +00:00
import { getRoboHashPicture, hexToNpub, shorten } from '../../utils'
2024-05-21 07:07:52 +00:00
import { truncate } from 'lodash'
import { getProfileSettingsRoute } from '../../routes'
2024-05-21 13:21:21 +00:00
import EditIcon from '@mui/icons-material/Edit'
import LinkIcon from '@mui/icons-material/Link'
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()
const theme = useTheme()
2024-03-01 10:16:35 +00:00
const { npub } = useParams()
const metadataController = useMemo(() => new MetadataController(), [])
const [pubkey, setPubkey] = useState<string>()
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)
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(() => {
if (pubkey) {
metadataController
.getNostrJoiningBlockNumber(pubkey)
.then((res) => {
setNostrJoiningBlock(res)
})
.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)
if (metadataContent) {
setProfileMetadata(metadataContent)
}
2024-03-01 10:16:35 +00:00
}
setIsLoading(false)
}
getMetadata(pubkey)
}
}, [isUsersOwnProfile, metadataState, pubkey, metadataController])
2024-05-21 13:21:07 +00:00
/**
* Rendering text with button which copies the provided text
* @param text to be visible
* @param sx props (MUI) to customize style
* @returns HTML rendered text
*/
2024-05-21 13:21:21 +00:00
const textElementWithCopyIcon = (
text: string,
sx?: SxProps<Theme>,
shortenOffset?: number
) => {
2024-05-21 07:07:52 +00:00
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 13:21:07 +00:00
<Typography variant="caption" sx={sx} className={styles.npubNipItem}>
<span onClick={onClick}>{shorten(text, shortenOffset)}</span>
2024-05-21 07:07:52 +00:00
<ContentCopyIcon onClick={onClick} className={styles.copyIcon} />
</Typography>
2024-05-15 08:50:21 +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
*
* @returns robohash image url
*/
const getProfileImage = (metadata: ProfileMetadata) => {
2024-05-21 07:07:52 +00:00
if (!metadata) return ''
if (!isUsersOwnProfile) {
return metadata.picture || getRoboHashPicture(npub!)
}
2024-05-17 11:35:37 +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}>
2024-05-21 13:21:21 +00:00
<Box
className={`${styles.banner} ${!profileMetadata || !profileMetadata.banner ? styles.noImage : ''}`}
>
{profileMetadata && profileMetadata.banner ? (
<img src={profileMetadata.banner} />
) : (
''
)}
2024-05-21 13:21:07 +00:00
</Box>
2024-05-21 07:07:52 +00:00
2024-05-21 13:21:07 +00:00
<Box className={styles.belowBanner}>
<Box className={styles.upper}>
<Box className={styles.left}>
<div
style={{
borderWidth: '2px',
borderStyle: pubkey ? 'solid' : 'none',
borderColor: `#${pubkey?.substring(0, 6)}`
}}
className={styles.imageWrapper}
>
<img
className={styles['image-placeholder']}
src={getProfileImage(profileMetadata!)}
/>
</div>
</Box>
<Box className={styles.middle}>
2024-05-21 13:21:21 +00:00
<Typography
2024-05-21 13:21:07 +00:00
component={Link}
to={`https://njump.me/${nostrJoiningBlock?.encodedEventPointer || ''}`}
2024-05-21 13:21:21 +00:00
target="_blank"
2024-05-22 09:09:41 +00:00
className={`${styles.nostrSince} ${styles.link}`}
2024-05-21 13:21:21 +00:00
variant="caption"
>
{nostrJoiningBlock
? `On nostr since ${nostrJoiningBlock.block.toLocaleString()}`
: 'On nostr since: unknown'}
2024-05-21 13:21:07 +00:00
</Typography>
</Box>
<Box className={styles.right}>
{isUsersOwnProfile && (
2024-05-21 13:21:21 +00:00
<IconButton
onClick={() => navigate(getProfileSettingsRoute(pubkey))}
>
<EditIcon />
2024-05-21 13:21:07 +00:00
</IconButton>
)}
</Box>
</Box>
<Box className={styles.head}>
2024-05-22 10:08:12 +00:00
<Box sx={{ lineHeight: 1 }} className={styles.npubNip}>
2024-05-21 13:21:21 +00:00
<Box
sx={{
display: 'flex'
}}
>
{profileMetadata && (
<Typography
2024-05-22 10:08:12 +00:00
sx={{ margin: '5px 0 5px 0' }}
2024-05-21 13:21:21 +00:00
variant="h6"
className={styles.bold}
2024-05-21 07:07:52 +00:00
>
2024-05-21 13:21:21 +00:00
{truncate(
profileMetadata.display_name ||
profileMetadata.name ||
hexToNpub(pubkey),
{
length: 16
}
)}
</Typography>
)}
</Box>
<Box>
{textElementWithCopyIcon(
hexToNpub(pubkey) || '',
2024-05-22 09:09:41 +00:00
undefined,
15
2024-05-21 13:21:21 +00:00
)}
</Box>
<Box>
{profileMetadata?.nip05 &&
2024-05-22 10:08:12 +00:00
textElementWithCopyIcon(
profileMetadata.nip05,
undefined,
15
)}
2024-05-21 13:21:21 +00:00
</Box>
<Box>
{profileMetadata?.lud16 &&
2024-05-22 10:08:12 +00:00
textElementWithCopyIcon(
profileMetadata.lud16,
undefined,
15
)}
2024-05-21 07:07:52 +00:00
</Box>
2024-05-21 13:21:21 +00:00
</Box>
<Box>
{profileMetadata?.website && (
<Typography
sx={{ marginTop: '10px' }}
variant="caption"
2024-05-22 09:09:41 +00:00
component={Link}
to={profileMetadata.website}
target="_blank"
className={`${styles.website} ${styles.link} ${styles.captionWrapper}`}
2024-05-21 13:21:21 +00:00
>
2024-05-22 09:09:41 +00:00
{profileMetadata.website}
2024-05-21 13:21:21 +00:00
</Typography>
)}
</Box>
</Box>
2024-05-21 13:21:07 +00:00
<Box>
{profileMetadata?.about && (
<Typography mt={1} className={styles.about}>
{profileMetadata.about}
</Typography>
2024-03-01 10:16:35 +00:00
)}
2024-05-21 07:07:52 +00:00
</Box>
</Box>
</Box>
)}
2024-03-01 10:16:35 +00:00
</>
)
}