snort/src/pages/ProfilePage.js

180 lines
6.5 KiB
JavaScript
Raw Normal View History

2022-12-27 23:46:13 +00:00
import "./ProfilePage.css";
2022-12-29 22:23:41 +00:00
import { useEffect, useRef, useState } from "react";
2022-12-28 14:51:33 +00:00
import { useDispatch, useSelector } from "react-redux";
2022-12-29 22:23:41 +00:00
import { bech32 } from "bech32";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faQrcode } from "@fortawesome/free-solid-svg-icons";
2022-12-18 14:51:32 +00:00
import { useParams } from "react-router-dom";
2022-12-29 22:23:41 +00:00
import useProfile from "../feed/ProfileFeed";
2022-12-28 14:51:33 +00:00
import { resetProfile } from "../state/Users";
2022-12-28 16:08:33 +00:00
import Nostrich from "../nostrich.jpg";
2022-12-29 22:23:41 +00:00
import useEventPublisher from "../feed/EventPublisher";
import useTimelineFeed from "../feed/TimelineFeed";
2022-12-28 23:28:28 +00:00
import Note from "../element/Note";
2022-12-29 22:23:41 +00:00
import QRCodeStyling from "qr-code-styling";
import ReactModal from "react-modal";
2022-12-18 14:51:32 +00:00
export default function ProfilePage() {
2022-12-28 14:51:33 +00:00
const dispatch = useDispatch();
2022-12-18 14:51:32 +00:00
const params = useParams();
const id = params.id;
2022-12-27 23:46:13 +00:00
const user = useProfile(id);
2022-12-28 22:09:39 +00:00
const publisher = useEventPublisher();
2022-12-28 23:28:28 +00:00
const { notes } = useTimelineFeed([id]);
2022-12-27 23:46:13 +00:00
const loginPubKey = useSelector(s => s.login.publicKey);
const isMe = loginPubKey === id;
2022-12-29 22:23:41 +00:00
const qrRef = useRef();
2022-12-27 23:46:13 +00:00
2022-12-29 22:23:41 +00:00
const [name, setName] = useState("");
const [picture, setPicture] = useState("");
const [about, setAbout] = useState("");
const [website, setWebsite] = useState("");
const [nip05, setNip05] = useState("");
const [lud16, setLud16] = useState("");
const [showLnQr, setShowLnQr] = useState(false);
2022-12-28 14:51:33 +00:00
useEffect(() => {
if (user) {
setName(user.name ?? "");
2022-12-28 16:08:33 +00:00
setPicture(user.picture ?? Nostrich);
2022-12-28 14:51:33 +00:00
setAbout(user.about ?? "");
setWebsite(user.website ?? "");
setNip05(user.nip05 ?? "");
setLud16(user.lud16 ?? "");
}
}, [user]);
2022-12-29 15:21:03 +00:00
useEffect(() => {
// some clients incorrectly set this to LNURL service, patch this
if (lud16.toLowerCase().startsWith("lnurl")) {
let decoded = bech32.decode(lud16, 1000);
let url = new TextDecoder().decode(Uint8Array.from(bech32.fromWords(decoded.words)));
if (url.startsWith("http")) {
let parsedUri = new URL(url);
// is lightning address
if (parsedUri.pathname.startsWith("/.well-known/lnurlp/")) {
let pathParts = parsedUri.pathname.split('/');
let username = pathParts[pathParts.length - 1];
setLud16(`${username}@${parsedUri.hostname}`);
}
}
}
}, [lud16]);
2022-12-29 22:23:41 +00:00
useEffect(() => {
if (qrRef.current && showLnQr) {
let qr = new QRCodeStyling({
data: "",
type: "canvas"
});
qrRef.current.innerHTML = "";
qr.append(qrRef.current);
}
}, [showLnQr]);
2022-12-28 14:51:33 +00:00
async function saveProfile() {
2022-12-28 22:09:39 +00:00
let ev = await publisher.metadata({
2022-12-28 14:51:33 +00:00
name,
about,
picture,
website,
nip05,
lud16
});
console.debug(ev);
2022-12-28 22:09:39 +00:00
publisher.broadcast(ev);
2022-12-28 14:51:33 +00:00
dispatch(resetProfile(id));
}
2022-12-27 23:46:13 +00:00
function editor() {
return (
<>
<div className="form-group">
<div>Name:</div>
<div>
<input type="text" value={name} onChange={(e) => setName(e.target.value)} />
</div>
</div>
<div className="form-group">
<div>About:</div>
<div>
<input type="text" value={about} onChange={(e) => setAbout(e.target.value)} />
</div>
</div>
<div className="form-group">
<div>Website:</div>
<div>
<input type="text" value={website} onChange={(e) => setWebsite(e.target.value)} />
</div>
</div>
2022-12-28 14:51:33 +00:00
<div className="form-group">
<div>NIP-05:</div>
<div>
<input type="text" value={nip05} onChange={(e) => setNip05(e.target.value)} />
</div>
</div>
<div className="form-group">
2022-12-29 15:21:03 +00:00
<div>LN Address:</div>
2022-12-28 14:51:33 +00:00
<div>
<input type="text" value={lud16} onChange={(e) => setLud16(e.target.value)} />
</div>
</div>
<div className="form-group">
<div></div>
<div>
<div className="btn" onClick={() => saveProfile()}>Save</div>
</div>
</div>
2022-12-27 23:46:13 +00:00
</>
)
}
2022-12-18 14:51:32 +00:00
2022-12-28 23:28:28 +00:00
function details() {
return (
<>
2022-12-29 22:23:41 +00:00
<h2>{name}</h2>
<p>{about}</p>
{website ? <a href={website} target="_blank" rel="noreferrer">{website}</a> : null}
{lud16 ? <div className="flex f-center">
<div className="btn" onClick={(e) => setShowLnQr(true)}>
<FontAwesomeIcon icon={faQrcode} size="xl" />
2022-12-28 23:28:28 +00:00
</div>
2022-12-29 22:23:41 +00:00
<div>&nbsp; {lud16}</div>
</div> : null}
{showLnQr === true ?
<ReactModal isOpen={showLnQr} onRequestClose={() => setShowLnQr(false)} overlayClassName="modal" className="modal-content" preventScroll={true}>
<div ref={qrRef}>QR</div>
</ReactModal> : null}
2022-12-28 23:28:28 +00:00
</>
)
}
2022-12-18 14:51:32 +00:00
return (
2022-12-28 23:28:28 +00:00
<>
<div className="profile">
2022-12-29 22:23:41 +00:00
<div>
<div style={{ backgroundImage: `url(${picture})` }} className="avatar">
{isMe ?
<div className="edit">
<div>Edit</div>
</div>
: null
}
</div>
2022-12-28 14:51:33 +00:00
</div>
2022-12-28 23:28:28 +00:00
<div>
{isMe ? editor() : details()}
</div>
2022-12-27 23:46:13 +00:00
</div>
2022-12-30 14:01:51 +00:00
<div className="tabs">
<div className="btn active">Notes</div>
<div className="btn">Reactions</div>
<div className="btn">Followers</div>
<div className="btn">Follows</div>
<div className="btn">Relays</div>
</div>
2022-12-28 23:28:28 +00:00
{notes?.sort((a, b) => b.created_at - a.created_at).map(a => <Note key={a.id} data={a} />)}
</>
2022-12-18 14:51:32 +00:00
)
}