From d3d81578c2248f2553c357800594d0ec5e9e2634 Mon Sep 17 00:00:00 2001 From: smolgrrr Date: Sun, 3 Dec 2023 20:48:50 +1100 Subject: [PATCH] add reposts --- client/src/components/Forms/RepostNote.tsx | 85 +++++++++++++++++++ client/src/components/Forms/handleSubmit.ts | 2 - client/src/components/Home.tsx | 28 ++++-- .../Modals/{Card.tsx => NoteCard.tsx} | 0 client/src/components/Modals/RepostCard.tsx | 67 +++++++++++++++ client/src/components/Thread.tsx | 24 +++++- client/src/utils/subscriptions.ts | 2 +- 7 files changed, 192 insertions(+), 16 deletions(-) create mode 100644 client/src/components/Forms/RepostNote.tsx rename client/src/components/Modals/{Card.tsx => NoteCard.tsx} (100%) create mode 100644 client/src/components/Modals/RepostCard.tsx diff --git a/client/src/components/Forms/RepostNote.tsx b/client/src/components/Forms/RepostNote.tsx new file mode 100644 index 0000000..6fdc26a --- /dev/null +++ b/client/src/components/Forms/RepostNote.tsx @@ -0,0 +1,85 @@ +import { + CpuChipIcon +} from "@heroicons/react/24/outline"; +import { useState, useEffect } from "react"; +import { UnsignedEvent, Event as NostrEvent, nip19 } from "nostr-tools"; +import { useSubmitForm } from "./handleSubmit"; +import "./Form.css"; + +interface FormProps { + refEvent: NostrEvent; +} + +const RepostNote = ({ + refEvent +}: FormProps) => { + const [difficulty, setDifficulty] = useState( + localStorage.getItem("difficulty") || "21" + ); + const [unsigned] = useState({ + kind: 6, + tags: [ + ['e', refEvent.id, 'wss://relay.damus.io'], + ['p', refEvent.pubkey] + ], + content: JSON.stringify(refEvent), + created_at: Math.floor(Date.now() / 1000), + pubkey: "", + }); + + useEffect(() => { + const handleDifficultyChange = (event: Event) => { + const customEvent = event as CustomEvent; + const { difficulty } = customEvent.detail; + setDifficulty(difficulty); + }; + + window.addEventListener("difficultyChanged", handleDifficultyChange); + + return () => { + window.removeEventListener("difficultyChanged", handleDifficultyChange); + }; + }, []); + + const { handleSubmit, doingWorkProp, doingWorkProgress } = useSubmitForm(unsigned, difficulty); + + return ( +
+
+
+
+
+ +
+

+ {difficulty} PoW +

+
+ +
+
+ {doingWorkProp ? ( +
+ + Generating Proof-of-Work. + {doingWorkProgress && Current iteration {doingWorkProgress}} +
+ ) : null} +
+ + ); +}; + +export default RepostNote; diff --git a/client/src/components/Forms/handleSubmit.ts b/client/src/components/Forms/handleSubmit.ts index d44b600..2f2b215 100644 --- a/client/src/components/Forms/handleSubmit.ts +++ b/client/src/components/Forms/handleSubmit.ts @@ -45,8 +45,6 @@ export const useSubmitForm = (unsigned: UnsignedEvent, difficulty: string) => { const { startWork, messageFromWorker, doingWorkProgress } = useWorkers(numCores, unsignedWithPubkey, difficulty, [unsignedWithPubkey]); - console.log(powServer[0]) - useEffect(() => { if (unsignedPoWEvent) { setDoingWorkProp(false); diff --git a/client/src/components/Home.tsx b/client/src/components/Home.tsx index cad22c7..1121126 100644 --- a/client/src/components/Home.tsx +++ b/client/src/components/Home.tsx @@ -1,10 +1,11 @@ import { useEffect, useState, useCallback } from "react"; -import PostCard from "./Modals/Card"; +import PostCard from "./Modals/NoteCard"; import { uniqBy } from "../utils/otherUtils"; // Assume getPow is a correct import now import { subGlobalFeed } from "../utils/subscriptions"; import { verifyPow } from "../utils/mine"; import { Event } from "nostr-tools"; import NewNoteCard from "./Forms/PostFormCard"; +import RepostCard from "./Modals/RepostCard"; const DEFAULT_DIFFICULTY = 20; @@ -29,8 +30,8 @@ const Home = () => { const postEvents = uniqEvents .filter((event) => verifyPow(event) >= Number(filterDifficulty) && - event.kind === 1 && - !event.tags.some((tag) => tag[0] === "e") + event.kind !== 0 && + (event.kind !== 1 || !event.tags.some((tag) => tag[0] === "e")) ) const sortedEvents = [...postEvents].sort((a, b) => @@ -49,6 +50,11 @@ const Home = () => { return uniqEvents.filter((e) => e.tags.some((tag) => tag[0] === "e" && tag[1] === event.id)).length; }; + useEffect(() => { + const kind6Events = sortedEvents.filter(event => event.kind === 6); + console.log('Kind 6 events:', kind6Events); + }, [uniqEvents]); + // Render the component return (
@@ -66,7 +72,7 @@ const Home = () => { onChange={toggleSort} />
-
+
{sortByTime ? 'Time' : 'PoW'} @@ -75,12 +81,16 @@ const Home = () => {
{sortedEvents.map((event) => ( - + : + + /> ))}
diff --git a/client/src/components/Modals/Card.tsx b/client/src/components/Modals/NoteCard.tsx similarity index 100% rename from client/src/components/Modals/Card.tsx rename to client/src/components/Modals/NoteCard.tsx diff --git a/client/src/components/Modals/RepostCard.tsx b/client/src/components/Modals/RepostCard.tsx new file mode 100644 index 0000000..1cd6084 --- /dev/null +++ b/client/src/components/Modals/RepostCard.tsx @@ -0,0 +1,67 @@ +import CardContainer from "./CardContainer"; +import { CpuChipIcon, ArrowPathRoundedSquareIcon } from "@heroicons/react/24/outline"; +import { parseContent } from "../../utils/content"; +import { Event, nip19 } from "nostr-tools"; +import { getMetadata } from "../../utils/otherUtils"; +import ContentPreview from "./CardModals/TextModal"; +import { renderMedia } from "../../utils/FileUpload"; +import { getIconFromHash, timeAgo } from "../../utils/cardUtils"; +import { verifyPow } from "../../utils/mine"; +import { useNavigate } from 'react-router-dom'; + +interface RepostProps { + key?: string | number; + event: Event; +} + +const RepostCard = ({ + key, + event +}: RepostProps) => { + const repostedEvent = JSON.parse(event.content); + const { files } = parseContent(event); + const icon = getIconFromHash(event.pubkey); + const navigate = useNavigate(); + + const handleClick = () => { + navigate(`/thread/${nip19.noteEncode(event.id)}`); + }; + + return ( +
+ +
+ Repost + @ + {verifyPow(event)} +
+
+
+
+ +
+
+ +
+ {renderMedia(files)} +
+
+
+
+ {verifyPow(repostedEvent)} +
+ ยท +
+ {timeAgo(repostedEvent.created_at)} +
+
+ +
+
+
+
+
+ ); +}; + +export default RepostCard; \ No newline at end of file diff --git a/client/src/components/Thread.tsx b/client/src/components/Thread.tsx index aef7cdb..d4d17fa 100644 --- a/client/src/components/Thread.tsx +++ b/client/src/components/Thread.tsx @@ -4,11 +4,12 @@ import { Event, nip19 } from "nostr-tools" import { subNote, subNotesOnce } from '../utils/subscriptions'; import { useEffect } from 'react'; import { uniqBy } from '../utils/otherUtils'; -import { DocumentTextIcon, FolderPlusIcon } from '@heroicons/react/24/outline'; +import { DocumentTextIcon, FolderPlusIcon, DocumentDuplicateIcon } from '@heroicons/react/24/outline'; import { getPow } from '../utils/mine'; -import PostCard from './Modals/Card'; +import PostCard from './Modals/NoteCard'; import Placeholder from './Modals/Placeholder'; import NewNoteCard from './Forms/PostFormCard'; +import RepostNote from './Forms/RepostNote'; type PostType = "" | "Reply" | "Quote" | undefined; @@ -17,6 +18,7 @@ const Thread = () => { const [events, setEvents] = useState([]); // Initialize state const [OPEvent, setOPEvent] = useState() const [showForm, setShowForm] = useState(false); + const [showRepost, setShowRepost] = useState(false); const [postType, setPostType] = useState(""); const [hasRun, setHasRun] = useState(false); const [preOPEvents, setPreOPEvents] = useState(['']); @@ -130,20 +132,34 @@ const Thread = () => { onClick={() => { setShowForm(prevShowForm => !prevShowForm); setPostType('Reply'); + setShowRepost(false) + }} + /> + { + setShowRepost(prevShowRepost => !prevShowRepost); + setShowForm(false); }} /> - { setShowForm(prevShowForm => !prevShowForm); setPostType('Quote'); + setShowRepost(false) }} />
- {(showForm && postType) &&
+ {(showForm && postType) && +
+ {postType}-post
} + {showRepost &&
+ Repost note + +
}