import { LNURL } from "@snort/shared"; import { CachedMetadata, encodeTLVEntries, NostrLink, NostrPrefix, TLVEntryType } from "@snort/system"; import React, { useMemo, useState } from "react"; import { FormattedMessage, useIntl } from "react-intl"; import { Link, useNavigate } from "react-router-dom"; import { UserRelays } from "@/Cache"; import AsyncButton from "@/Components/Button/AsyncButton"; import IconButton from "@/Components/Button/IconButton"; import Copy from "@/Components/Copy/Copy"; import Modal from "@/Components/Modal/Modal"; import QrCode from "@/Components/QrCode"; import { SpotlightMediaModal } from "@/Components/Spotlight/SpotlightMedia"; import Avatar from "@/Components/User/Avatar"; import FollowButton from "@/Components/User/FollowButton"; import ProfileImage from "@/Components/User/ProfileImage"; import ZapModal from "@/Components/ZapModal/ZapModal"; import useModeration from "@/Hooks/useModeration"; import { hexToBech32 } from "@/Utils"; import { LoginSessionType, LoginStore } from "@/Utils/Login"; import { ZapTarget } from "@/Utils/Zapper"; import MuteButton from "@/Components/User/MuteButton"; const AvatarSection = ({ user, id, loginPubKey, readonly, lnurl, }: { user?: CachedMetadata; id?: string; loginPubKey?: string; lnurl?: LNURL; readonly?: boolean; }) => { const [showProfileQr, setShowProfileQr] = useState(false); const [modalImage, setModalImage] = useState(""); const [showLnQr, setShowLnQr] = useState(false); const [prefix, setPrefix] = useState(CONFIG.profileLinkPrefix); const { mute, unmute, isMuted } = useModeration(); const navigate = useNavigate(); const relays = UserRelays.getFromCache(id); const isMe = loginPubKey === id; const canWrite = !!loginPubKey && !readonly; const intl = useIntl(); const muted = id ? isMuted(id) : false; const profileId = useMemo(() => { if (!id) return; if (prefix === NostrPrefix.PublicKey) { return hexToBech32(NostrPrefix.PublicKey, id); } else if (prefix === NostrPrefix.Profile) { return NostrLink.profile(id, relays?.relays .filter(a => a.settings.write) .map(a => a.url)).encode(); } }, [id, relays, prefix]); const renderButtons = () => { if (!id) return null; return ( <> setShowProfileQr(true)} icon={{ name: "qr", size: 16 }} /> {showProfileQr && ( setShowProfileQr(false)}>
setPrefix(NostrPrefix.PublicKey)}> NPUB setPrefix(NostrPrefix.Profile)}> NPROFILE
)} {isMe ? ( <> ) : ( <> {lnurl && setShowLnQr(true)} icon={{ name: "zap", size: 16 }} />} {canWrite && ( navigate( `/messages/${encodeTLVEntries(NostrPrefix.Chat17, { type: TLVEntryType.Author, length: 64, value: id, })}`, ) } icon={{ name: "envelope", size: 16 }} /> )} {canWrite && muted && } {canWrite && !muted && { if (muted) { await unmute(id); } else { await mute(id); } }} icon={{ name: "mute", size: 16 }} /> } {!canWrite && !isMe && ( { if (confirm(intl.formatMessage({ defaultMessage: "View as user?", id: "LBAnc7" }))) { LoginStore.loginWithPubkey(id, LoginSessionType.PublicKey); } }} icon={{ name: "openeye", size: 16, className: "translate-y-0.5" }} /> )} )} ); }; return (
setModalImage(user?.picture || "")} className="pointer" size={100} />
{renderButtons()} {!isMe && id && }
{modalImage && setModalImage("")} media={[modalImage]} idx={0} />} setShowLnQr(false)} />
); }; export default AvatarSection;