diff --git a/src-tauri/migrations/20230226004139_create_tables.sql b/src-tauri/migrations/20230226004139_create_tables.sql index 2855380c..24603813 100644 --- a/src-tauri/migrations/20230226004139_create_tables.sql +++ b/src-tauri/migrations/20230226004139_create_tables.sql @@ -85,5 +85,6 @@ CREATE TABLE kind INTEGER NOT NULL DEFAULT 1, tags TEXT NOT NULL, content TEXT NOT NULL, - parent_id TEXT + parent_id TEXT, + parent_comment_id TEXT ); \ No newline at end of file diff --git a/src/components/note/metadata.tsx b/src/components/note/metadata.tsx index 5b6a8110..ef0cf36a 100644 --- a/src/components/note/metadata.tsx +++ b/src/components/note/metadata.tsx @@ -4,6 +4,8 @@ import { RelayContext } from '@components/relaysProvider'; import { relaysAtom } from '@stores/relays'; +import { createCacheCommentNote } from '@utils/storage'; + import { useAtomValue } from 'jotai'; import { useContext, useEffect, useState } from 'react'; @@ -37,7 +39,10 @@ export default function NoteMetadata({ (event: any) => { switch (event.kind) { case 1: + // update state setComments((comments) => (comments += 1)); + // save comment to database + createCacheCommentNote(event, eventID); break; case 7: if (event.content === '🤙' || event.content === '+') { diff --git a/src/components/note/repost.tsx b/src/components/note/repost.tsx index 1f5dfddd..61fe6bae 100644 --- a/src/components/note/repost.tsx +++ b/src/components/note/repost.tsx @@ -86,7 +86,7 @@ export const NoteRepost = memo(function NoteRepost({ id }: { id: string }) { if (event) { return ( -
+
diff --git a/src/pages/newsfeed/[id].tsx b/src/pages/newsfeed/[id].tsx index 14ac76f7..4207a498 100644 --- a/src/pages/newsfeed/[id].tsx +++ b/src/pages/newsfeed/[id].tsx @@ -7,7 +7,7 @@ import { RelayContext } from '@components/relaysProvider'; import { relaysAtom } from '@stores/relays'; -import { getNoteByID } from '@utils/storage'; +import { getAllCommentNotes, getNoteByID } from '@utils/storage'; import { useAtomValue } from 'jotai'; import { useRouter } from 'next/router'; @@ -33,31 +33,12 @@ export default function Page() { const [comments, setComments] = useState([]); useEffect(() => { - let unsubscribe: () => void; - getNoteByID(id) .then((res) => { - // update state setRootEvent(res); - // get all comments - unsubscribe = pool.subscribe( - [ - { - '#e': [id], - kinds: [1], - }, - ], - relays, - (event: any) => { - setComments((comments) => [...comments, event]); - } - ); + getAllCommentNotes(id).then((res: any) => setComments(res)); }) .catch(console.error); - - return () => { - unsubscribe(); - }; }, [id, pool, relays]); return ( diff --git a/src/utils/storage.tsx b/src/utils/storage.tsx index e807127b..4fdd5892 100644 --- a/src/utils/storage.tsx +++ b/src/utils/storage.tsx @@ -88,7 +88,7 @@ export async function getCacheProfile(id) { return result[0]; } -// get note by id +// get all notes export async function getAllNotes() { const db = await connect(); return await db.select(`SELECT * FROM cache_notes GROUP BY parent_id ORDER BY created_at DESC LIMIT 1000`); @@ -117,3 +117,29 @@ export async function createCacheNote(data) { ] ); } + +// get all comment notes +export async function getAllCommentNotes(eid) { + const db = await connect(); + return await db.select( + `SELECT * FROM cache_notes WHERE parent_comment_id = "${eid}" ORDER BY created_at DESC LIMIT 1000` + ); +} + +// create cache comment note +export async function createCacheCommentNote(data, eid) { + const db = await connect(); + return await db.execute( + 'INSERT OR IGNORE INTO cache_notes (id, pubkey, created_at, kind, content, tags, parent_id, parent_comment_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?);', + [ + data.id, + data.pubkey, + data.created_at, + data.kind, + data.content, + JSON.stringify(data.tags), + getParentID(data.tags, data.id), + eid, + ] + ); +}