From fdbdaaa384634985e964fbd14e4a6ead5106a17d Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Sun, 30 Apr 2023 11:48:38 +0700 Subject: [PATCH] add single note page --- src/app/chat/components/messages/user.tsx | 2 +- src/app/newsfeed/components/note/base.tsx | 15 +--- src/app/newsfeed/components/note/metadata.tsx | 2 +- src/app/newsfeed/components/note/parent.tsx | 2 +- src/app/newsfeed/components/note/quote.tsx | 15 +++- src/app/newsfeed/components/note/rootNote.tsx | 14 +-- src/app/note/_default.page.tsx | 1 + src/app/note/components/form.tsx | 71 +++++++++++++++ src/app/note/components/replies.tsx | 63 ++++++++++++++ src/app/note/components/reply.tsx | 18 ++++ src/app/note/components/user.tsx | 48 ++++++++++ src/app/note/layout.tsx | 29 +++++++ src/app/note/pages/index.page.tsx | 87 +++++++++++++++++++ src/shared/form/comment.tsx | 69 --------------- src/stores/note.tsx | 7 -- src/utils/transform.tsx | 6 +- 16 files changed, 349 insertions(+), 100 deletions(-) create mode 100644 src/app/note/_default.page.tsx create mode 100644 src/app/note/components/form.tsx create mode 100644 src/app/note/components/replies.tsx create mode 100644 src/app/note/components/reply.tsx create mode 100644 src/app/note/components/user.tsx create mode 100644 src/app/note/layout.tsx create mode 100644 src/app/note/pages/index.page.tsx delete mode 100644 src/shared/form/comment.tsx diff --git a/src/app/chat/components/messages/user.tsx b/src/app/chat/components/messages/user.tsx index 8cb7f8c8..cf0d3094 100644 --- a/src/app/chat/components/messages/user.tsx +++ b/src/app/chat/components/messages/user.tsx @@ -29,7 +29,7 @@ export default function ChatMessageUser({ pubkey, time }: { pubkey: string; time alt={pubkey} className="h-9 w-9 rounded-md object-cover" loading="lazy" - fetchpriority="high" + decoding="async" />
diff --git a/src/app/newsfeed/components/note/base.tsx b/src/app/newsfeed/components/note/base.tsx index 7cbc902b..ae42a125 100644 --- a/src/app/newsfeed/components/note/base.tsx +++ b/src/app/newsfeed/components/note/base.tsx @@ -18,15 +18,10 @@ export const NoteBase = memo(function NoteBase({ event }: { event: any }) { return <>; }; - const openUserPage = (e) => { - e.stopPropagation(); - navigate(`/user?pubkey=${event.pubkey}`); - }; - - const openThread = (e) => { + const openNote = (e) => { const selection = window.getSelection(); if (selection.toString().length === 0) { - navigate(`/newsfeed/note?id=${event.parent_id}`); + navigate(`/app/note?id=${event.parent_id}`); } else { e.stopPropagation(); } @@ -34,14 +29,12 @@ export const NoteBase = memo(function NoteBase({ event }: { event: any }) { return (
openThread(e)} + onClick={(e) => openNote(e)} className="relative z-10 flex h-min min-h-min w-full select-text flex-col border-b border-zinc-800 px-3 py-5 hover:bg-black/20" > {parentNote()}
-
openUserPage(e)}> - -
+
{content}
diff --git a/src/app/newsfeed/components/note/metadata.tsx b/src/app/newsfeed/components/note/metadata.tsx index 10c67eab..507f33ed 100644 --- a/src/app/newsfeed/components/note/metadata.tsx +++ b/src/app/newsfeed/components/note/metadata.tsx @@ -16,7 +16,7 @@ export default function NoteMetadata({ id, eventPubkey }: { id: string; eventPub const [likes, setLikes] = useState(0); const [reposts, setReposts] = useState(0); - useSWRSubscription(id ? ['metadata', id] : null, ([, key], {}) => { + useSWRSubscription(id ? ['note-metadata', id] : null, ([, key], {}) => { const unsubscribe = pool.subscribe( [ { diff --git a/src/app/newsfeed/components/note/parent.tsx b/src/app/newsfeed/components/note/parent.tsx index 426918cb..26b27c55 100644 --- a/src/app/newsfeed/components/note/parent.tsx +++ b/src/app/newsfeed/components/note/parent.tsx @@ -69,7 +69,7 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) {
e.stopPropagation()} className="mt-5 pl-[52px]"> - +
diff --git a/src/app/newsfeed/components/note/quote.tsx b/src/app/newsfeed/components/note/quote.tsx index 2b2c1e87..9cdf910f 100644 --- a/src/app/newsfeed/components/note/quote.tsx +++ b/src/app/newsfeed/components/note/quote.tsx @@ -5,6 +5,7 @@ import { READONLY_RELAYS } from '@lume/stores/constants'; import { memo, useContext } from 'react'; import useSWRSubscription from 'swr/subscription'; +import { navigate } from 'vite-plugin-ssr/client/router'; export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) { const pool: any = useContext(RelayContext); @@ -32,8 +33,20 @@ export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) { }; }); + const openNote = (e) => { + const selection = window.getSelection(); + if (selection.toString().length === 0) { + navigate(`/app/note?id=${id}`); + } else { + e.stopPropagation(); + } + }; + return ( -
+
openNote(e)} + className="relative mb-2 mt-3 rounded-lg border border-zinc-700 bg-zinc-800 p-2 py-3" + > {error &&
failed to load
} {!data ? (
diff --git a/src/app/newsfeed/components/note/rootNote.tsx b/src/app/newsfeed/components/note/rootNote.tsx index 44a8c9a5..ad9e1a05 100644 --- a/src/app/newsfeed/components/note/rootNote.tsx +++ b/src/app/newsfeed/components/note/rootNote.tsx @@ -35,10 +35,10 @@ export const RootNote = memo(function RootNote({ id, fallback }: { id: string; f }; }); - const openThread = (e) => { + const openNote = (e) => { const selection = window.getSelection(); if (selection.toString().length === 0) { - navigate(`/newsfeed/note?id=${id}`); + navigate(`/app/note?id=${id}`); } else { e.stopPropagation(); } @@ -46,14 +46,16 @@ export const RootNote = memo(function RootNote({ id, fallback }: { id: string; f if (parseFallback) { return ( -
openThread(e)} className="relative z-10 flex flex-col"> +
openNote(e)} className="relative z-10 flex flex-col">
{contentParser(parseFallback.content, parseFallback.tags)}
-
e.stopPropagation()} className="mt-5 pl-[52px]">
+
e.stopPropagation()} className="mt-5 pl-[52px]"> + +
); } @@ -84,7 +86,7 @@ export const RootNote = memo(function RootNote({ id, fallback }: { id: string; f
) : ( -
openThread(e)} className="relative z-10 flex flex-col"> +
openNote(e)} className="relative z-10 flex flex-col">
@@ -92,7 +94,7 @@ export const RootNote = memo(function RootNote({ id, fallback }: { id: string; f
e.stopPropagation()} className="mt-5 pl-[52px]"> - +
)} diff --git a/src/app/note/_default.page.tsx b/src/app/note/_default.page.tsx new file mode 100644 index 00000000..7a0967d9 --- /dev/null +++ b/src/app/note/_default.page.tsx @@ -0,0 +1 @@ +export { LayoutNewsfeed as Layout } from './layout'; diff --git a/src/app/note/components/form.tsx b/src/app/note/components/form.tsx new file mode 100644 index 00000000..1816afff --- /dev/null +++ b/src/app/note/components/form.tsx @@ -0,0 +1,71 @@ +import { RelayContext } from '@lume/shared/relayProvider'; +import { WRITEONLY_RELAYS } from '@lume/stores/constants'; +import { dateToUnix } from '@lume/utils/getDate'; +import { useActiveAccount } from '@lume/utils/hooks/useActiveAccount'; + +import { getEventHash, signEvent } from 'nostr-tools'; +import { useContext, useState } from 'react'; + +export default function NoteReplyForm({ id }: { id: string }) { + const pool: any = useContext(RelayContext); + const { account, isLoading, isError } = useActiveAccount(); + + const [value, setValue] = useState(''); + const profile = account ? JSON.parse(account.metadata) : null; + + const submitEvent = () => { + if (!isLoading && !isError && account) { + const event: any = { + content: value, + created_at: dateToUnix(), + kind: 1, + pubkey: account.pubkey, + tags: [['e', id]], + }; + event.id = getEventHash(event); + event.sig = signEvent(event, account.privkey); + + // publish note + pool.publish(event, WRITEONLY_RELAYS); + // reset form + setValue(''); + } else { + console.log('error'); + } + }; + + return ( +
+
+
+ {account?.pubkey} +
+
+
+
+