From c14f60a36e5b073fa632546d8248f9b7030dc465 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Sat, 19 Jul 2025 16:25:14 -0400 Subject: [PATCH] feat(nostr): Fill in Nostr Profile form with existing data --- .../app-body/nostr/nostr-post/profile-post.js | 240 ++++++++++-------- 1 file changed, 137 insertions(+), 103 deletions(-) diff --git a/src/components/app-body/nostr/nostr-post/profile-post.js b/src/components/app-body/nostr/nostr-post/profile-post.js index 56b49d0..d3b37e0 100644 --- a/src/components/app-body/nostr/nostr-post/profile-post.js +++ b/src/components/app-body/nostr/nostr-post/profile-post.js @@ -3,17 +3,19 @@ Component for posting nostr information on kind 0. */ // Global npm libraries -import React, { useState } from 'react' +import React, { useState, useEffect } from 'react' import { Container, Form, Button, Spinner } from 'react-bootstrap' import Accordion from 'react-bootstrap/Accordion' import { finalizeEvent } from 'nostr-tools/pure' import { Relay } from 'nostr-tools/relay' import { hexToBytes } from '@noble/hashes/utils' // already an installed dependency +import { RelayPool } from 'nostr' function ProfilePost (props) { + const { bchWalletState } = props.appData const [accordionKey, setAccordionKey] = useState(null) const [onFetch, setOnFetch] = useState(false) - const { bchWalletState } = props.appData + const [formLoaded, setFormLoaded] = useState(false) const [formData, setFormData] = useState({ name: '', about: '', @@ -27,6 +29,43 @@ function ProfilePost (props) { const [errorMsg, setErrorMsg] = useState('') const [successMsg, setSuccessMsg] = useState('') + useEffect(() => { + // Get Last post from + const getLastPost = () => { + const { nostrKeyPair } = bchWalletState + + const psf = 'wss://nostr-relay.psfoundation.info' + + const pool = RelayPool([psf]) + pool.on('open', relay => { + relay.subscribe('subid', { limit: 1, kinds: [0], authors: [nostrKeyPair.pubHex] }) + setFormLoaded(true) + }) + + pool.on('eose', relay => { + console.log('Closing Relay') + setFormLoaded(true) + relay.close() + }) + + pool.on('event', (relay, subId, ev) => { + console.log('Received event:', ev) + // setPosts(currentPosts => [...currentPosts, ev]) + try { + const data = JSON.parse(ev.content) + console.log('data', data) + setFormData(data) + } catch (error) { + + } + }) + } + + if (!formLoaded) { + getLastPost() + } + }, [bchWalletState, formLoaded]) + const handleInputChange = (e) => { const { name, value } = e.target setFormData({ @@ -78,7 +117,6 @@ function ProfilePost (props) { // Close the connection to the relay. relay.close() - resetForm() setSuccessMsg('Post successfully published!') setOnFetch(false) } catch (error) { @@ -92,18 +130,6 @@ function ProfilePost (props) { setAccordionKey(key) } - const resetForm = () => { - setFormData({ - name: '', - about: '', - picture: '', - display_name: '', - website: '', - banner: '', - bot: false - }) - } - return ( <> @@ -121,103 +147,111 @@ function ProfilePost (props) { {successMsg} )} -
- - Name - - + {formLoaded && ( + + + Name + + - - About - - + + About + + - - Profile Picture URL - - + + Profile Picture URL + + - - Display Name - - + + Display Name + + - - Website - - + + Website + + - - Banner URL - - + + Banner URL + + - - { - setFormData({ - ...formData, - bot: e.target.checked - }) - setSuccessMsg('') - setErrorMsg('') - }} - /> - + + { + setFormData({ + ...formData, + bot: e.target.checked + }) + setSuccessMsg('') + setErrorMsg('') + }} + /> + -
- {!onFetch && ( - - )} - {onFetch && } +
+ {!onFetch && ( + + )} + {onFetch && } +
+ + + )} + {!formLoaded && ( +
+
+ )} -