From e30ca0ff82dc1619d7058252b52a6d33f5a036f0 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Tue, 25 Jul 2023 17:45:32 +0700 Subject: [PATCH] add hashtag support in composer --- src/shared/composer/composer.tsx | 29 +++++++++++++++++++++-------- src/shared/notes/actions.tsx | 4 +++- src/shared/notes/actions/reply.tsx | 12 ++++++++++-- src/shared/notes/kinds/sub.tsx | 4 ++-- src/shared/notes/kinds/thread.tsx | 2 +- src/shared/notes/replies/item.tsx | 8 ++++++-- src/shared/notes/replies/list.tsx | 5 +++-- src/stores/composer.tsx | 8 ++++---- src/utils/parser.tsx | 19 ++++++------------- 9 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/shared/composer/composer.tsx b/src/shared/composer/composer.tsx index ca2a7308..486ae364 100644 --- a/src/shared/composer/composer.tsx +++ b/src/shared/composer/composer.tsx @@ -69,11 +69,23 @@ export function Composer() { const submit = async () => { setStatus('loading'); + try { + // get plaintext content + const html = editor.getHTML(); + const serializedContent = convert(html, { + selectors: [ + { selector: 'a', options: { linkBrackets: false } }, + { selector: 'img', options: { linkBrackets: false } }, + ], + }); + + // define tags let tags: string[][] = []; + // add reply to tags if present if (reply.id && reply.pubkey) { - if (reply.root) { + if (reply.root && reply.root.length > 1) { tags = [ ['e', reply.root, FULL_RELAYS[0], 'root'], ['e', reply.id, FULL_RELAYS[0], 'reply'], @@ -87,15 +99,16 @@ export function Composer() { } } - // get plaintext content - const html = editor.getHTML(); - const serializedContent = convert(html, { - selectors: [ - { selector: 'a', options: { linkBrackets: false } }, - { selector: 'img', options: { linkBrackets: false } }, - ], + // add hashtag to tags if present + const hashtags = serializedContent + .split(/\s/gm) + .filter((s: string) => s.startsWith('#')); + hashtags?.forEach((tag: string) => { + tags.push(['t', tag.replace('#', '')]); }); + console.log(tags); + // publish message await publish({ content: serializedContent, kind: 1, tags }); diff --git a/src/shared/notes/actions.tsx b/src/shared/notes/actions.tsx index 624d3ba0..b17bf512 100644 --- a/src/shared/notes/actions.tsx +++ b/src/shared/notes/actions.tsx @@ -14,10 +14,12 @@ export function NoteActions({ id, pubkey, noOpenThread, + root, }: { id: string; pubkey: string; noOpenThread?: boolean; + root?: string; }) { const { add } = useBlock(); @@ -25,7 +27,7 @@ export function NoteActions({
- + diff --git a/src/shared/notes/actions/reply.tsx b/src/shared/notes/actions/reply.tsx index cbda02a8..3962ca35 100644 --- a/src/shared/notes/actions/reply.tsx +++ b/src/shared/notes/actions/reply.tsx @@ -4,7 +4,15 @@ import { ReplyIcon } from '@shared/icons'; import { useComposer } from '@stores/composer'; -export function NoteReply({ id, pubkey }: { id: string; pubkey: string }) { +export function NoteReply({ + id, + pubkey, + root, +}: { + id: string; + pubkey: string; + root?: string; +}) { const setReply = useComposer((state) => state.setReply); return ( @@ -12,7 +20,7 @@ export function NoteReply({ id, pubkey }: { id: string; pubkey: string }) {
diff --git a/src/shared/notes/kinds/thread.tsx b/src/shared/notes/kinds/thread.tsx index bb30576d..383947b3 100644 --- a/src/shared/notes/kinds/thread.tsx +++ b/src/shared/notes/kinds/thread.tsx @@ -21,7 +21,7 @@ export function NoteThread({
{root && }
-
{reply && }
+
{reply && }
diff --git a/src/shared/notes/replies/item.tsx b/src/shared/notes/replies/item.tsx index dea8373b..131cd6d2 100644 --- a/src/shared/notes/replies/item.tsx +++ b/src/shared/notes/replies/item.tsx @@ -6,7 +6,7 @@ import { User } from '@shared/user'; import { parser } from '@utils/parser'; import { LumeEvent } from '@utils/types'; -export function Reply({ event }: { event: LumeEvent }) { +export function Reply({ event, root }: { event: LumeEvent; root?: string }) { const content = useMemo(() => parser(event), [event]); return ( @@ -18,7 +18,11 @@ export function Reply({ event }: { event: LumeEvent }) {
- +
diff --git a/src/shared/notes/replies/list.tsx b/src/shared/notes/replies/list.tsx index ce1be588..1475afdc 100644 --- a/src/shared/notes/replies/list.tsx +++ b/src/shared/notes/replies/list.tsx @@ -1,4 +1,3 @@ -import { NDKEvent } from '@nostr-dev-kit/ndk'; import { useQuery } from '@tanstack/react-query'; import { useNDK } from '@libs/ndk/provider'; @@ -68,7 +67,9 @@ export function RepliesList({ id }: { id: string }) {
) : ( - data.reverse().map((event: NDKEvent) => ) + data + .reverse() + .map((event: LumeEvent) => ) )}
diff --git a/src/stores/composer.tsx b/src/stores/composer.tsx index 49d114d6..3cb93c62 100644 --- a/src/stores/composer.tsx +++ b/src/stores/composer.tsx @@ -4,21 +4,21 @@ interface ComposerState { open: boolean; reply: { id: string; pubkey: string; root?: string }; toggleModal: (status: boolean) => void; - setReply: (id: string, pubkey: string) => void; + setReply: (id: string, pubkey: string, root?: string) => void; clearReply: () => void; } export const useComposer = create((set) => ({ open: false, - reply: { id: null, root: null, pubkey: null }, + reply: { id: null, pubkey: null, root: null }, toggleModal: (status: boolean) => { set({ open: status }); }, setReply: (id: string, pubkey: string, root?: string) => { - set({ reply: { id: id, root: root, pubkey: pubkey } }); + set({ reply: { id: id, pubkey: pubkey, root: root } }); set({ open: true }); }, clearReply: () => { - set({ reply: { id: null, root: null, pubkey: null } }); + set({ reply: { id: null, pubkey: null, root: null } }); }, })); diff --git a/src/utils/parser.tsx b/src/utils/parser.tsx index de84283f..714f06df 100644 --- a/src/utils/parser.tsx +++ b/src/utils/parser.tsx @@ -2,22 +2,15 @@ import getUrls from 'get-urls'; import { Event, parseReferences } from 'nostr-tools'; import ReactPlayer from 'react-player'; -import { LumeEvent } from '@utils/types'; +import { Content, LumeEvent } from '@utils/types'; export function parser(event: LumeEvent) { - const references = parseReferences(event as Event); - const urls = getUrls(event.content); + const references = parseReferences(event as unknown as Event); + const urls = getUrls(event.content as unknown as string); - const content: { - original: string; - parsed: string; - notes: string[]; - images: string[]; - videos: string[]; - links: string[]; - } = { - original: event.content, - parsed: event.content, + const content: Content = { + original: event.content as unknown as string, + parsed: event.content as unknown as string, notes: [], images: [], videos: [],