added root note component
This commit is contained in:
@@ -5,7 +5,6 @@ import { atomHasNewerNote } from '@stores/note';
|
||||
|
||||
import { dateToUnix, hoursAgo } from '@utils/getDate';
|
||||
|
||||
import { SliderIcon } from '@radix-ui/react-icons';
|
||||
import { useLocalStorage } from '@rehooks/local-storage';
|
||||
import { useSetAtom } from 'jotai';
|
||||
import { memo, useCallback, useContext, useEffect, useRef } from 'react';
|
||||
@@ -26,14 +25,14 @@ export const NoteConnector = memo(function NoteConnector() {
|
||||
await db.execute(
|
||||
`INSERT OR IGNORE INTO
|
||||
cache_notes
|
||||
(id, pubkey, created_at, kind, tags, content) VALUES
|
||||
(id, pubkey, created_at, kind, content, tags) VALUES
|
||||
(
|
||||
"${event.id}",
|
||||
"${event.pubkey}",
|
||||
"${event.created_at}",
|
||||
"${event.kind}",
|
||||
'${JSON.stringify(event.tags)}',
|
||||
'${JSON.stringify(event.content)}'
|
||||
"${event.content}",
|
||||
'${JSON.stringify(event.tags)}'
|
||||
);`
|
||||
);
|
||||
},
|
||||
|
||||
@@ -15,7 +15,7 @@ const MarkdownPreview = dynamic(() => import('@uiw/react-markdown-preview'), {
|
||||
export const Content = memo(function Content({ data }: { data: any }) {
|
||||
const [preview, setPreview] = useState({});
|
||||
|
||||
const content = useRef(JSON.parse(data.content));
|
||||
const content = useRef(data.content);
|
||||
const urls = useMemo(
|
||||
() =>
|
||||
content.current.match(
|
||||
@@ -61,7 +61,7 @@ export const Content = memo(function Content({ data }: { data: any }) {
|
||||
}, [preview]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col">
|
||||
<div className="relative z-10 flex flex-col">
|
||||
<UserExtend pubkey={data.pubkey} time={data.created_at} />
|
||||
<div className="-mt-4 pl-[60px]">
|
||||
<div className="flex flex-col gap-6">
|
||||
@@ -70,7 +70,7 @@ export const Content = memo(function Content({ data }: { data: any }) {
|
||||
<MarkdownPreview
|
||||
source={content.current}
|
||||
className={
|
||||
'prose prose-zinc max-w-none break-words dark:prose-invert prose-headings:mt-3 prose-headings:mb-2 prose-p:m-0 prose-p:leading-normal prose-ul:mt-2 prose-li:my-1'
|
||||
'prose prose-zinc max-w-none break-words dark:prose-invert prose-headings:mt-3 prose-headings:mb-2 prose-p:m-0 prose-p:leading-normal prose-a:font-medium prose-a:text-fuchsia-500 prose-a:no-underline prose-ul:mt-2 prose-li:my-1'
|
||||
}
|
||||
linkTarget="_blank"
|
||||
disallowedElements={[
|
||||
|
||||
28
src/components/note/index.tsx
Normal file
28
src/components/note/index.tsx
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Content } from '@components/note/content';
|
||||
import { RootNote } from '@components/note/root';
|
||||
|
||||
import { memo, useEffect, useState } from 'react';
|
||||
|
||||
export const Note = memo(function Note({ event }: { event: any }) {
|
||||
const [root, setRoot] = useState(null);
|
||||
const tags = JSON.parse(event.tags);
|
||||
|
||||
useEffect(() => {
|
||||
if (tags.length > 0) {
|
||||
if (tags[0][0] === 'e') {
|
||||
setRoot(tags[0][1]);
|
||||
}
|
||||
}
|
||||
}, [tags]);
|
||||
|
||||
return (
|
||||
<div className="relative z-10 flex h-min min-h-min w-full cursor-pointer select-text flex-col border-b border-zinc-800 py-5 px-3">
|
||||
{root && (
|
||||
<>
|
||||
<RootNote id={root} />
|
||||
</>
|
||||
)}
|
||||
<Content data={event} />
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -2,7 +2,7 @@ import { memo } from 'react';
|
||||
|
||||
export const Placeholder = memo(function Placeholder() {
|
||||
return (
|
||||
<div className="relative z-10 flex h-min animate-pulse select-text flex-col py-4 px-3">
|
||||
<div className="relative z-10 flex h-min animate-pulse select-text flex-col py-5 px-3">
|
||||
<div className="flex items-start gap-4">
|
||||
<div className="relative h-11 w-11 shrink overflow-hidden rounded-full bg-zinc-700" />
|
||||
<div className="flex w-full flex-1 items-start justify-between">
|
||||
|
||||
42
src/components/note/root.tsx
Normal file
42
src/components/note/root.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import { RelayContext } from '@components/contexts/relay';
|
||||
import { Content } from '@components/note/content';
|
||||
|
||||
import useLocalStorage from '@rehooks/local-storage';
|
||||
import { memo, useCallback, useContext, useEffect, useState } from 'react';
|
||||
|
||||
export const RootNote = memo(function RootNote({ id }: { id: string }) {
|
||||
const relayPool: any = useContext(RelayContext);
|
||||
|
||||
const [relays]: any = useLocalStorage('relays');
|
||||
const [event, setEvent] = useState(null);
|
||||
|
||||
const fetchEvent = useCallback(() => {
|
||||
relayPool.subscribe(
|
||||
[
|
||||
{
|
||||
ids: [id],
|
||||
kinds: [1],
|
||||
},
|
||||
],
|
||||
relays,
|
||||
(event: any) => {
|
||||
setEvent(event);
|
||||
}
|
||||
);
|
||||
}, [id, relayPool, relays]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchEvent();
|
||||
}, [fetchEvent]);
|
||||
|
||||
if (event) {
|
||||
return (
|
||||
<div className="relative pb-5">
|
||||
<div className="absolute top-0 left-[21px] h-full w-px bg-zinc-800"></div>
|
||||
<Content data={event} />
|
||||
</div>
|
||||
);
|
||||
} else {
|
||||
return <>Loading...</>;
|
||||
}
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Content } from '@components/note/content';
|
||||
|
||||
import { memo } from 'react';
|
||||
|
||||
export const Single = memo(function Single({ event }: { event: any }) {
|
||||
return (
|
||||
<div className="flex h-min min-h-min w-full cursor-pointer select-text flex-col border-b border-zinc-800 py-4 px-3">
|
||||
<Content data={event} />
|
||||
</div>
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user