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-13 06:24:54 +00:00
|
|
|
CircularProgress,
|
2024-05-14 12:39:05 +00:00
|
|
|
IconButton,
|
|
|
|
InputProps,
|
2024-05-10 10:16:28 +00:00
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
ListSubheader,
|
|
|
|
TextField,
|
2024-05-14 12:39:05 +00:00
|
|
|
Tooltip,
|
2024-05-10 10:16:28 +00:00
|
|
|
Typography,
|
|
|
|
useTheme
|
|
|
|
} from '@mui/material'
|
2024-03-01 10:16:35 +00:00
|
|
|
import { UnsignedEvent, nip19, kinds, VerifiedEvent } from 'nostr-tools'
|
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
2024-05-10 10:57:04 +00:00
|
|
|
import { Link, useParams } from 'react-router-dom'
|
2024-03-01 10:16:35 +00:00
|
|
|
import { toast } from 'react-toastify'
|
|
|
|
import placeholderAvatar from '../../assets/images/nostr-logo.jpg'
|
|
|
|
import { MetadataController, NostrController } 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'
|
|
|
|
import { useDispatch, useSelector } from 'react-redux'
|
|
|
|
import { State } from '../../store/rootReducer'
|
|
|
|
import { LoadingButton } from '@mui/lab'
|
|
|
|
import { Dispatch } from '../../store/store'
|
|
|
|
import { setMetadataEvent } from '../../store/actions'
|
|
|
|
import { LoadingSpinner } from '../../components/LoadingSpinner'
|
2024-03-19 09:36:34 +00:00
|
|
|
import { LoginMethods } from '../../store/auth/types'
|
2024-05-14 12:39:05 +00:00
|
|
|
import { SmartToy } from '@mui/icons-material'
|
2024-03-01 10:16:35 +00:00
|
|
|
|
|
|
|
export const ProfilePage = () => {
|
2024-05-10 10:16:28 +00:00
|
|
|
const theme = useTheme()
|
|
|
|
|
2024-03-01 10:16:35 +00:00
|
|
|
const { npub } = useParams()
|
|
|
|
|
|
|
|
const dispatch: Dispatch = useDispatch()
|
|
|
|
|
|
|
|
const metadataController = useMemo(() => new MetadataController(), [])
|
|
|
|
const nostrController = NostrController.getInstance()
|
|
|
|
|
|
|
|
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 [savingProfileMetadata, setSavingProfileMetadata] = useState(false)
|
2024-05-13 06:00:44 +00:00
|
|
|
const [avatarLoading, setAvatarLoading] = useState(false)
|
2024-03-01 10:16:35 +00:00
|
|
|
const metadataState = useSelector((state: State) => state.metadata)
|
|
|
|
const keys = useSelector((state: State) => state.auth?.keyPair)
|
2024-03-19 09:36:34 +00:00
|
|
|
const { usersPubkey, loginMethod } = useSelector((state: State) => state.auth)
|
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])
|
|
|
|
|
|
|
|
const editItem = (
|
|
|
|
key: keyof ProfileMetadata,
|
|
|
|
label: string,
|
|
|
|
multiline = false,
|
2024-05-14 12:39:05 +00:00
|
|
|
rows = 1,
|
|
|
|
inputProps?: InputProps
|
2024-03-01 10:16:35 +00:00
|
|
|
) => (
|
|
|
|
<ListItem sx={{ marginTop: 1 }}>
|
|
|
|
<TextField
|
|
|
|
label={label}
|
|
|
|
id={label.split(' ').join('-')}
|
|
|
|
value={profileMetadata![key] || ''}
|
|
|
|
size='small'
|
|
|
|
multiline={multiline}
|
|
|
|
rows={rows}
|
|
|
|
className={styles.textField}
|
|
|
|
disabled={!isUsersOwnProfile}
|
2024-05-14 12:39:05 +00:00
|
|
|
InputProps={inputProps}
|
2024-03-01 10:16:35 +00:00
|
|
|
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const { value } = event.target
|
|
|
|
|
|
|
|
setProfileMetadata((prev) => ({
|
|
|
|
...prev,
|
|
|
|
[key]: value
|
|
|
|
}))
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ListItem>
|
|
|
|
)
|
|
|
|
|
|
|
|
const copyItem = (
|
|
|
|
value: string,
|
|
|
|
label: string,
|
|
|
|
copyValue?: string,
|
|
|
|
isPassword = false
|
|
|
|
) => (
|
|
|
|
<ListItem
|
|
|
|
sx={{ marginTop: 1 }}
|
|
|
|
onClick={() => {
|
|
|
|
navigator.clipboard.writeText(copyValue || value)
|
|
|
|
|
|
|
|
toast.success('Copied to clipboard', {
|
|
|
|
autoClose: 1000,
|
|
|
|
hideProgressBar: true
|
|
|
|
})
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<TextField
|
|
|
|
label={label}
|
|
|
|
id={label.split(' ').join('-')}
|
|
|
|
defaultValue={value}
|
|
|
|
size='small'
|
|
|
|
className={styles.textField}
|
|
|
|
disabled
|
|
|
|
type={isPassword ? 'password' : 'text'}
|
2024-03-05 09:08:18 +00:00
|
|
|
InputProps={{
|
|
|
|
endAdornment: <ContentCopyIcon className={styles.copyItem} />
|
|
|
|
}}
|
2024-03-01 10:16:35 +00:00
|
|
|
/>
|
|
|
|
</ListItem>
|
|
|
|
)
|
|
|
|
|
|
|
|
const handleSaveMetadata = async () => {
|
|
|
|
setSavingProfileMetadata(true)
|
|
|
|
|
|
|
|
const content = JSON.stringify(profileMetadata)
|
|
|
|
|
|
|
|
// We need to omit cachedAt and create new event
|
|
|
|
// Relay will reject if created_at is too late
|
|
|
|
const updatedMetadataState: UnsignedEvent = {
|
|
|
|
content: content,
|
|
|
|
created_at: Math.round(Date.now() / 1000),
|
|
|
|
kind: kinds.Metadata,
|
|
|
|
pubkey: pubkey!,
|
|
|
|
tags: metadataState?.tags || []
|
|
|
|
}
|
|
|
|
|
|
|
|
const signedEvent = await nostrController
|
|
|
|
.signEvent(updatedMetadataState)
|
|
|
|
.catch((error) => {
|
|
|
|
toast.error(`Error saving profile metadata. ${error}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (signedEvent) {
|
|
|
|
if (!metadataController.validate(signedEvent)) {
|
|
|
|
toast.error(`Metadata is not valid.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
await metadataController.publishMetadataEvent(signedEvent)
|
|
|
|
|
|
|
|
dispatch(setMetadataEvent(signedEvent))
|
|
|
|
}
|
|
|
|
|
|
|
|
setSavingProfileMetadata(false)
|
|
|
|
}
|
|
|
|
|
2024-05-13 06:00:44 +00:00
|
|
|
const generateRobotAvatar = () => {
|
|
|
|
setAvatarLoading(true)
|
|
|
|
|
|
|
|
const robotAvatarLink = `https://robohash.org/${npub}.png?set=set3`
|
|
|
|
|
|
|
|
setProfileMetadata((prev) => ({
|
|
|
|
...prev,
|
|
|
|
picture: ''
|
|
|
|
}))
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
setProfileMetadata((prev) => ({
|
|
|
|
...prev,
|
|
|
|
picture: robotAvatarLink
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-14 12:39:05 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns robohash generate button, loading spinner or no button
|
|
|
|
*/
|
|
|
|
const robohashButton = () => {
|
|
|
|
if (profileMetadata?.picture?.includes('robohash')) return null
|
|
|
|
|
|
|
|
return <Tooltip title="Generate a robohash avatar">
|
|
|
|
{avatarLoading ? <CircularProgress style={{padding: 8}} size={22}/>
|
|
|
|
: <IconButton onClick={generateRobotAvatar}>
|
|
|
|
<SmartToy/>
|
|
|
|
</IconButton>}
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
|
2024-03-01 10:16:35 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{isLoading && <LoadingSpinner desc={loadingSpinnerDesc} />}
|
|
|
|
<div className={styles.container}>
|
|
|
|
<List
|
|
|
|
sx={{
|
|
|
|
bgcolor: 'background.paper',
|
|
|
|
marginTop: 2
|
|
|
|
}}
|
|
|
|
subheader={
|
|
|
|
<ListSubheader
|
|
|
|
sx={{
|
|
|
|
paddingBottom: 1,
|
|
|
|
paddingTop: 1,
|
|
|
|
fontSize: '1.5rem'
|
|
|
|
}}
|
|
|
|
className={styles.subHeader}
|
|
|
|
>
|
|
|
|
Profile Settings
|
|
|
|
</ListSubheader>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{profileMetadata && (
|
|
|
|
<div>
|
|
|
|
<ListItem
|
|
|
|
sx={{
|
|
|
|
marginTop: 1,
|
|
|
|
display: 'flex',
|
2024-05-10 10:16:28 +00:00
|
|
|
flexDirection: 'column'
|
2024-03-01 10:16:35 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<img
|
|
|
|
onError={(event: any) => {
|
|
|
|
event.target.src = placeholderAvatar
|
|
|
|
}}
|
2024-05-13 06:00:44 +00:00
|
|
|
onLoad={() => {
|
|
|
|
setAvatarLoading(false)
|
|
|
|
}}
|
2024-03-01 10:16:35 +00:00
|
|
|
className={styles.img}
|
|
|
|
src={profileMetadata.picture || placeholderAvatar}
|
|
|
|
alt='Profile Image'
|
|
|
|
/>
|
2024-05-13 06:00:44 +00:00
|
|
|
|
2024-05-10 10:57:04 +00:00
|
|
|
{nostrJoiningBlock && (
|
2024-05-10 10:16:28 +00:00
|
|
|
<Typography
|
|
|
|
sx={{
|
|
|
|
color: theme.palette.getContrastText(
|
|
|
|
theme.palette.background.paper
|
|
|
|
)
|
|
|
|
}}
|
2024-05-10 10:57:04 +00:00
|
|
|
component={Link}
|
|
|
|
to={`https://njump.me/${nostrJoiningBlock.encodedEventPointer}`}
|
|
|
|
target='_blank'
|
2024-05-10 10:16:28 +00:00
|
|
|
>
|
2024-05-10 10:57:04 +00:00
|
|
|
On nostr since {nostrJoiningBlock.block.toLocaleString()}
|
2024-05-10 10:16:28 +00:00
|
|
|
</Typography>
|
|
|
|
)}
|
2024-03-01 10:16:35 +00:00
|
|
|
</ListItem>
|
|
|
|
|
2024-05-14 12:39:05 +00:00
|
|
|
{editItem('picture', 'Picture URL', undefined, undefined, {
|
|
|
|
endAdornment: robohashButton()
|
|
|
|
})}
|
|
|
|
|
2024-03-01 10:16:35 +00:00
|
|
|
{editItem('name', 'Username')}
|
|
|
|
{editItem('display_name', 'Display Name')}
|
|
|
|
{editItem('nip05', 'Nostr Address (nip05)')}
|
|
|
|
{editItem('lud16', 'Lightning Address (lud16)')}
|
|
|
|
{editItem('about', 'About', true, 4)}
|
2024-05-13 06:00:44 +00:00
|
|
|
{editItem('website', 'Website')}
|
2024-03-01 10:16:35 +00:00
|
|
|
{isUsersOwnProfile && (
|
|
|
|
<>
|
2024-03-19 09:36:34 +00:00
|
|
|
{usersPubkey &&
|
|
|
|
copyItem(nip19.npubEncode(usersPubkey), 'Public Key')}
|
|
|
|
{loginMethod === LoginMethods.privateKey &&
|
|
|
|
keys &&
|
2024-03-01 10:16:35 +00:00
|
|
|
keys.private &&
|
|
|
|
copyItem(
|
|
|
|
'••••••••••••••••••••••••••••••••••••••••••••••••••',
|
|
|
|
'Private Key',
|
|
|
|
keys.private
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</List>
|
|
|
|
{isUsersOwnProfile && (
|
|
|
|
<LoadingButton
|
|
|
|
loading={savingProfileMetadata}
|
|
|
|
variant='contained'
|
|
|
|
onClick={handleSaveMetadata}
|
|
|
|
>
|
|
|
|
SAVE
|
|
|
|
</LoadingButton>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|