-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profile fn comp, restore reposts in feeds
- Loading branch information
Showing
5 changed files
with
133 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,191 +1,165 @@ | ||
import { useMemo } from 'react'; | ||
import { useEffect, useState } from 'preact/hooks'; | ||
import { route } from 'preact-router'; | ||
|
||
import BaseComponent from '@/BaseComponent.ts'; | ||
import SimpleImageModal from '@/components/modal/Image.tsx'; | ||
import { getEventReplyingTo } from '@/nostr/utils.ts'; | ||
import { getEventReplyingTo, isRepost } from '@/nostr/utils.ts'; | ||
import ProfileHelmet from '@/views/profile/Helmet.tsx'; | ||
|
||
import Feed from '../../components/feed/Feed.tsx'; | ||
import Show from '../../components/helpers/Show.tsx'; | ||
import { isSafeOrigin } from '../../components/SafeImg.tsx'; | ||
import ProfileCard from '../../components/user/ProfileCard.tsx'; | ||
import localState from '../../LocalState.ts'; | ||
import { useLocalState } from '../../LocalState.ts'; | ||
import Key from '../../nostr/Key.ts'; | ||
import SocialNetwork from '../../nostr/SocialNetwork.ts'; | ||
import { translate as t } from '../../translations/Translation.mjs'; | ||
import View from '../View.tsx'; | ||
|
||
class Profile extends BaseComponent { | ||
subscriptions: any[]; | ||
unsub: any; | ||
state = { | ||
hexPub: '', | ||
npub: '', | ||
name: '', | ||
display_name: '', | ||
profile: {} as any, | ||
banner: '', | ||
fullBanner: '', | ||
picture: '', | ||
website: '', | ||
lightning: '', | ||
blocked: false, | ||
bannerModalOpen: false, | ||
notFound: false, | ||
}; | ||
|
||
constructor() { | ||
super(); | ||
this.subscriptions = []; | ||
} | ||
function Profile(props) { | ||
const [blocked, setBlocked] = useState(false); | ||
const [hexPub, setHexPub] = useState(''); | ||
const [npub, setNpub] = useState(''); | ||
const [profile, setProfile] = useState({} as any); | ||
const [banner, setBanner] = useState(''); | ||
const [bannerModalOpen, setBannerModalOpen] = useState(false); | ||
const setIsMyProfile = useLocalState('isMyProfile', false)[1]; | ||
|
||
render() { | ||
const { | ||
hexPub, | ||
display_name, | ||
name, | ||
profile, | ||
banner, | ||
picture, | ||
blocked, | ||
bannerModalOpen, | ||
fullBanner, | ||
} = this.state; | ||
|
||
if (!hexPub) { | ||
return <div></div>; | ||
} | ||
|
||
const title = display_name || name || 'Profile'; | ||
const ogTitle = `${title} | Iris`; | ||
const description = `Latest posts by ${display_name || name || 'user'}. ${ | ||
profile?.about || '' | ||
}`; | ||
const setBannerModalOpen = (bannerModalOpen) => this.setState({ bannerModalOpen }); | ||
|
||
return ( | ||
<View> | ||
<Show when={banner}> | ||
<div | ||
className="mb-4 h-48 bg-cover bg-center cursor-pointer" | ||
style={{ backgroundImage: `url(${banner})` }} | ||
onClick={() => setBannerModalOpen(true)} | ||
></div> | ||
<Show when={bannerModalOpen}> | ||
<SimpleImageModal imageUrl={fullBanner} onClose={() => setBannerModalOpen(false)} /> | ||
</Show> | ||
</Show> | ||
<div> | ||
<ProfileHelmet | ||
title={title} | ||
description={description} | ||
picture={picture} | ||
ogTitle={ogTitle} | ||
/> | ||
<ProfileCard npub={this.state.npub} hexPub={this.state.hexPub} /> | ||
<Show when={!blocked}> | ||
<Feed | ||
key={`posts${this.state.hexPub}`} | ||
filterOptions={[ | ||
{ | ||
name: t('posts'), | ||
filter: { authors: [this.state.hexPub], kinds: [1, 6], limit: 10 }, | ||
filterFn: (event) => !getEventReplyingTo(event), | ||
eventProps: { showRepliedMsg: true }, | ||
}, | ||
{ | ||
name: t('posts_and_replies'), | ||
filter: { authors: [this.state.hexPub], kinds: [1, 6], limit: 5 }, | ||
eventProps: { showRepliedMsg: true, fullWidth: false }, | ||
}, | ||
{ | ||
name: t('likes'), | ||
filter: { authors: [this.state.hexPub], kinds: [7], limit: 5 }, | ||
}, | ||
]} | ||
/> | ||
</Show> | ||
</div> | ||
</View> | ||
); | ||
} | ||
|
||
componentWillUnmount() { | ||
super.componentWillUnmount(); | ||
this.unsub?.(); | ||
localState.get('isMyProfile').put(false); | ||
} | ||
useEffect(() => { | ||
const pub = props.id; | ||
const npubComputed = Key.toNostrBech32Address(pub, 'npub'); | ||
|
||
componentDidUpdate(_prevProps, prevState) { | ||
if (!prevState.name && this.state.name) { | ||
this.unsub?.(); | ||
setTimeout(() => { | ||
// important for SEO: prerenderReady is false until page content is loaded | ||
window.prerenderReady = true; | ||
}, 1000); // give feed a sec to load | ||
} | ||
} | ||
|
||
loadProfile(hexPub: string) { | ||
const isMyProfile = hexPub === Key.getPubKey(); | ||
localState.get('isMyProfile').put(isMyProfile); | ||
this.subscriptions.push( | ||
SocialNetwork.getProfile(hexPub, (profile) => { | ||
let banner, fullBanner; | ||
|
||
try { | ||
banner = profile.banner && new URL(profile.banner).toString(); | ||
if (!banner) { | ||
return; | ||
} | ||
fullBanner = banner; | ||
banner = isSafeOrigin(banner) | ||
? banner | ||
: `https://imgproxy.iris.to/insecure/rs:fit:948:948/plain/${banner}`; | ||
this.setState({ banner, fullBanner }); | ||
} catch (e) { | ||
console.log('Invalid banner URL', profile.banner); | ||
} | ||
}), | ||
); | ||
} | ||
|
||
componentDidMount() { | ||
const pub = this.props.id; | ||
const npub = Key.toNostrBech32Address(pub, 'npub'); | ||
//localState.get('loggedIn').on(this.inject()); | ||
if (npub && npub !== pub) { | ||
route(`/${npub}`, true); | ||
if (npubComputed && npubComputed !== pub) { | ||
route(`/${npubComputed}`, true); | ||
return; | ||
} | ||
const hexPub = Key.toNostrHexAddress(pub); | ||
if (!hexPub) { | ||
// id is not a nostr address, but maybe it's a username | ||
|
||
const hexPubComputed = Key.toNostrHexAddress(pub) || ''; | ||
if (!hexPubComputed) { | ||
let nostrAddress = pub; | ||
if (!nostrAddress.match(/.+@.+\..+/)) { | ||
// domain name? | ||
if (nostrAddress.match(/.+\..+/)) { | ||
nostrAddress = '_@' + nostrAddress; | ||
} else { | ||
nostrAddress = nostrAddress + '@iris.to'; | ||
} | ||
} | ||
|
||
Key.getPubKeyByNip05Address(nostrAddress).then((pubKey) => { | ||
if (pubKey) { | ||
const npub = Key.toNostrBech32Address(pubKey, 'npub'); | ||
if (npub && npub !== pubKey) { | ||
this.setState({ npub, hexPub: pubKey }); | ||
this.loadProfile(pubKey); | ||
const npubComputed = Key.toNostrBech32Address(pubKey, 'npub'); | ||
if (npubComputed && npubComputed !== pubKey) { | ||
setNpub(npubComputed); | ||
setHexPub(pubKey); | ||
loadProfile(pubKey); | ||
} | ||
} else { | ||
this.setState({ notFound: true }); | ||
setNpub(''); // To indicate not found | ||
} | ||
}); | ||
} | ||
|
||
setHexPub(hexPubComputed); | ||
setNpub(Key.toNostrBech32Address(hexPubComputed, 'npub') || ''); | ||
loadProfile(hexPubComputed); | ||
setTimeout(() => { | ||
window.prerenderReady = true; | ||
}, 1000); | ||
return () => { | ||
setIsMyProfile(false); | ||
}; | ||
}, [props.id]); | ||
|
||
const filterOptions = useMemo(() => { | ||
return [ | ||
{ | ||
name: t('posts'), | ||
filter: { authors: [hexPub], kinds: [1, 6], limit: 10 }, | ||
filterFn: (event) => !getEventReplyingTo(event) || isRepost(event), | ||
eventProps: { showRepliedMsg: true }, | ||
}, | ||
{ | ||
name: t('posts_and_replies'), | ||
filter: { authors: [hexPub], kinds: [1, 6], limit: 5 }, | ||
eventProps: { showRepliedMsg: true, fullWidth: false }, | ||
}, | ||
{ | ||
name: t('likes'), | ||
filter: { authors: [hexPub], kinds: [7], limit: 5 }, | ||
}, | ||
]; | ||
}, [hexPub]); | ||
|
||
const loadProfile = (hexPub) => { | ||
if (!hexPub) { | ||
return; | ||
} | ||
this.setState({ hexPub, npub: Key.toNostrBech32Address(hexPub, 'npub') }); | ||
this.loadProfile(hexPub); | ||
const isMyProfile = hexPub === Key.getPubKey(); | ||
setIsMyProfile(isMyProfile); | ||
SocialNetwork.getBlockedUsers((blockedUsers) => { | ||
setBlocked(blockedUsers.has(hexPub)); | ||
}); | ||
|
||
SocialNetwork.getProfile(hexPub, (profileData) => { | ||
if (!profileData) { | ||
return; | ||
} | ||
setProfile(profileData); | ||
let bannerURL; | ||
|
||
try { | ||
bannerURL = profileData.banner && new URL(profileData.banner).toString(); | ||
if (!bannerURL) { | ||
return; | ||
} | ||
|
||
bannerURL = isSafeOrigin(bannerURL) | ||
? bannerURL | ||
: `https://imgproxy.iris.to/insecure/rs:fit:948:948/plain/${bannerURL}`; | ||
|
||
setBanner(bannerURL); | ||
} catch (e) { | ||
console.log('Invalid banner URL', profileData.banner); | ||
} | ||
}); | ||
}; | ||
|
||
if (!hexPub) { | ||
return <div></div>; | ||
} | ||
|
||
const title = profile.display_name || profile.name || 'Profile'; | ||
const ogTitle = `${title} | Iris`; | ||
const description = `Latest posts by ${profile.display_name || profile.name || 'user'}. ${ | ||
profile.about || '' | ||
}`; | ||
|
||
return ( | ||
<View> | ||
<Show when={banner && !blocked}> | ||
<div | ||
className="mb-4 h-48 bg-cover bg-center cursor-pointer" | ||
style={{ backgroundImage: `url(${banner})` }} | ||
onClick={() => setBannerModalOpen(true)} | ||
></div> | ||
<Show when={bannerModalOpen}> | ||
<SimpleImageModal imageUrl={profile.banner} onClose={() => setBannerModalOpen(false)} /> | ||
</Show> | ||
</Show> | ||
<div> | ||
<ProfileHelmet | ||
title={title} | ||
description={description} | ||
picture={profile.picture} | ||
ogTitle={ogTitle} | ||
/> | ||
<ProfileCard npub={npub} hexPub={hexPub} /> | ||
<Show when={!blocked}> | ||
<Feed key={`posts${hexPub}`} filterOptions={filterOptions} /> | ||
</Show> | ||
</div> | ||
</View> | ||
); | ||
} | ||
|
||
export default Profile; |