snort/src/pages/SettingsPage.js

193 lines
6.6 KiB
JavaScript
Raw Normal View History

2023-01-09 11:00:23 +00:00
import "./SettingsPage.css";
import Nostrich from "../nostrich.jpg";
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
2023-01-12 12:00:44 +00:00
import { useNavigate } from "react-router-dom";
2023-01-09 11:00:23 +00:00
import useEventPublisher from "../feed/EventPublisher";
import useProfile from "../feed/ProfileFeed";
import VoidUpload from "../feed/VoidUpload";
2023-01-09 12:40:10 +00:00
import { logout, setRelays } from "../state/Login";
2023-01-09 11:00:23 +00:00
import { resetProfile } from "../state/Users";
import { openFile } from "../Util";
2023-01-09 12:40:10 +00:00
import Relay from "../element/Relay";
2023-01-09 11:00:23 +00:00
export default function SettingsPage(props) {
2023-01-12 12:00:44 +00:00
const navigate = useNavigate();
2023-01-09 11:00:23 +00:00
const id = useSelector(s => s.login.publicKey);
2023-01-09 12:40:10 +00:00
const relays = useSelector(s => s.login.relays);
2023-01-09 11:00:23 +00:00
const dispatch = useDispatch();
const user = useProfile(id);
const publisher = useEventPublisher();
const [name, setName] = useState("");
2023-01-10 09:53:28 +00:00
const [displayName, setDisplayName] = useState("");
2023-01-09 11:00:23 +00:00
const [picture, setPicture] = useState("");
const [about, setAbout] = useState("");
const [website, setWebsite] = useState("");
const [nip05, setNip05] = useState("");
const [lud06, setLud06] = useState("");
const [lud16, setLud16] = useState("");
2023-01-09 12:40:10 +00:00
const [newRelay, setNewRelay] = useState("");
2023-01-09 11:00:23 +00:00
useEffect(() => {
if (user) {
setName(user.name ?? "");
2023-01-10 09:53:28 +00:00
setDisplayName(user.display_name ?? "")
2023-01-09 11:00:23 +00:00
setPicture(user.picture ?? "");
setAbout(user.about ?? "");
setWebsite(user.website ?? "");
setNip05(user.nip05 ?? "");
setLud06(user.lud06 ?? "");
setLud16(user.lud16 ?? "");
}
}, [user]);
async function saveProfile() {
// copy user object and delete internal fields
let userCopy = {
...user,
name,
2023-01-10 09:53:28 +00:00
display_name: displayName,
2023-01-09 11:00:23 +00:00
about,
picture,
website,
nip05,
lud16
};
delete userCopy["loaded"];
delete userCopy["fromEvent"];
// event top level props should not be copied into metadata (bug)
delete userCopy["pubkey"];
delete userCopy["sig"];
delete userCopy["pubkey"];
delete userCopy["tags"];
delete userCopy["content"];
delete userCopy["created_at"];
delete userCopy["id"];
delete userCopy["kind"]
// trim empty string fields
Object.keys(userCopy).forEach(k => {
if (userCopy[k] === "") {
delete userCopy[k];
}
});
console.debug(userCopy);
let ev = await publisher.metadata(userCopy);
console.debug(ev);
dispatch(resetProfile(id));
publisher.broadcast(ev);
}
async function setNewAvatar() {
let file = await openFile();
console.log(file);
let rsp = await VoidUpload(file);
if (!rsp) {
throw "Upload failed, please try again later";
}
console.log(rsp);
setPicture(rsp.metadata.url ?? `https://void.cat/d/${rsp.id}`)
}
2023-01-09 12:40:10 +00:00
async function saveRelays() {
let ev = await publisher.saveRelays();
publisher.broadcast(ev);
}
2023-01-09 11:00:23 +00:00
function editor() {
return (
<div className="editor">
<div className="form-group">
<div>Name:</div>
<div>
<input type="text" value={name} onChange={(e) => setName(e.target.value)} />
</div>
</div>
2023-01-10 09:53:28 +00:00
<div className="form-group">
<div>Display name:</div>
<div>
<input type="text" value={displayName} onChange={(e) => setDisplayName(e.target.value)} />
</div>
</div>
2023-01-09 11:00:23 +00:00
<div className="form-group f-col">
<div>About:</div>
<div className="w-max">
<textarea className="w-max" onChange={(e) => setAbout(e.target.value)} value={about}></textarea>
</div>
</div>
<div className="form-group">
<div>Website:</div>
<div>
<input type="text" value={website} onChange={(e) => setWebsite(e.target.value)} />
</div>
</div>
<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">
<div>LN Address:</div>
<div>
<input type="text" value={lud16} onChange={(e) => setLud16(e.target.value)} />
</div>
</div>
<div className="form-group">
<div>
2023-01-12 12:00:44 +00:00
<div className="btn" onClick={() => { dispatch(logout()); navigate("/"); }}>Logout</div>
2023-01-09 11:00:23 +00:00
</div>
<div>
<div className="btn" onClick={() => saveProfile()}>Save</div>
</div>
</div>
</div>
)
}
2023-01-09 12:40:10 +00:00
function addRelay() {
return (
<>
<h4>Add Relays</h4>
<div className="flex mb10">
<input type="text" className="f-grow" placeholder="wss://my-relay.com" value={newRelay} onChange={(e) => setNewRelay(e.target.value)} />
</div>
<div className="btn mb10" onClick={() => dispatch(setRelays({ [newRelay]: { read: false, write: false } }))}>Add</div>
</>
)
}
2023-01-12 12:00:44 +00:00
function settings() {
if (!id) return null;
return (
<>
<h1>Settings</h1>
<div className="flex f-center">
<div style={{ backgroundImage: `url(${picture.length === 0 ? Nostrich : picture})` }} className="avatar">
<div className="edit" onClick={() => setNewAvatar()}>Edit</div>
</div>
</div>
{editor()}
</>
)
}
2023-01-09 11:00:23 +00:00
return (
<div className="settings">
2023-01-12 12:00:44 +00:00
{settings()}
2023-01-09 12:40:10 +00:00
<h4>Relays</h4>
<div className="flex f-col">
{Object.keys(relays || {}).map(a => <Relay addr={a} key={a} />)}
</div>
<div className="flex">
<div className="f-grow"></div>
<div className="btn" onClick={() => saveRelays()}>Save</div>
</div>
{addRelay()}
2023-01-09 11:00:23 +00:00
</div>
);
}