major overhaul (not finished)

This commit is contained in:
Ren Amamiya
2023-03-22 09:22:34 +07:00
parent 3c3ee2fc88
commit 49b7ebf32b
17 changed files with 150 additions and 68 deletions

View File

@@ -1,7 +1,7 @@
import { DatabaseContext } from '@components/contexts/database';
import { RelayContext } from '@components/contexts/relay';
import { atomHasNewerNote } from '@stores/note';
import { hasNewerNoteAtom } from '@stores/note';
import { dateToUnix, hoursAgo } from '@utils/getDate';
@@ -16,7 +16,7 @@ export const NoteConnector = memo(function NoteConnector() {
const [follows]: any = useLocalStorage('follows');
const [relays]: any = useLocalStorage('relays');
const setHasNewerNote = useSetAtom(atomHasNewerNote);
const setHasNewerNote = useSetAtom(hasNewerNoteAtom);
const [isOnline, setIsOnline] = useState(navigator.onLine);
const now = useRef(new Date());
@@ -25,7 +25,7 @@ export const NoteConnector = memo(function NoteConnector() {
// insert to local database
await db.execute(
'INSERT OR IGNORE INTO cache_notes (id, pubkey, created_at, kind, content, tags) VALUES (?, ?, ?, ?, ?, ?);',
[event.id, event.pubkey, event.created_at, event.kind, event.content, JSON.stringify(event.tags)]
[event.id, event.pubkey, event.created_at, event.kind, event.content, String(event.tags)]
);
},
[db]

View File

@@ -9,11 +9,9 @@ import reactStringReplace from 'react-string-replace';
export const Content = memo(function Content({ data }: { data: any }) {
const content = useMemo(() => {
let parsedContent;
let tags;
// get data tags
if (data.tags.length > 1) {
tags = JSON.parse(data.tags);
}
const tags = String(data.tags).replaceAll("'", '"');
const parseTags = JSON.parse(tags);
// remove all image urls
parsedContent = data.content.replace(/(https?:\/\/.*\.(jpg|jpeg|gif|png|webp|mp4|webm)((\?.*)$|$))/gim, '');
// handle urls
@@ -29,10 +27,10 @@ export const Content = memo(function Content({ data }: { data: any }) {
</span>
));
// handle mentions
if (tags.length > 0) {
if (parseTags.length > 0) {
parsedContent = reactStringReplace(parsedContent, /\#\[(\d+)\]/gm, (match, i) => {
if (tags[match][0] === 'p') {
return <UserMention key={match + i} pubkey={tags[match][1]} />;
if (parseTags[match][0] === 'p') {
return <UserMention key={match + i} pubkey={parseTags[match][1]} />;
} else {
// #TODO: handle mention other note
// console.log(tags[match]);
@@ -48,7 +46,7 @@ export const Content = memo(function Content({ data }: { data: any }) {
<UserExtend pubkey={data.pubkey} time={data.created_at} />
<div className="-mt-5 pl-[52px]">
<div className="flex flex-col gap-6">
<div className="flex flex-col">
<div className="flex flex-col gap-2">
<div className="prose prose-zinc max-w-none break-words text-[15px] leading-tight dark:prose-invert prose-headings:mt-3 prose-headings:mb-2 prose-p:m-0 prose-p:text-[15px] prose-p:leading-tight prose-a:font-normal prose-a:text-fuchsia-500 prose-a:no-underline prose-ul:mt-2 prose-li:my-1">
{content}
</div>

View File

@@ -7,7 +7,7 @@ import LikedIcon from '@assets/icons/liked';
import { useLocalStorage } from '@rehooks/local-storage';
import { getEventHash, signEvent } from 'nostr-tools';
import { memo, useContext, useState } from 'react';
import { memo, useContext, useEffect, useState } from 'react';
export const LikesCounter = memo(function LikesCounter({
count,
@@ -24,7 +24,7 @@ export const LikesCounter = memo(function LikesCounter({
const [currentUser]: any = useLocalStorage('current-user');
const [isReact, setIsReact] = useState(false);
const [like, setLike] = useState(count);
const [like, setLike] = useState(0);
const handleLike = (e: any) => {
e.stopPropagation();
@@ -49,6 +49,10 @@ export const LikesCounter = memo(function LikesCounter({
setLike(like + 1);
};
useEffect(() => {
setLike(count);
}, [count]);
return (
<button onClick={(e) => handleLike(e)} className="group flex w-16 items-center gap-1 text-sm text-zinc-500">
<div className="rounded-md p-1 group-hover:bg-zinc-800">

View File

@@ -4,16 +4,17 @@ import { RootNote } from '@components/note/root';
import { memo, useMemo } from 'react';
export const Note = memo(function Note({ event }: { event: any }) {
const tags = JSON.parse(event.tags);
const tags = event.tags.replaceAll("'", '"');
const parseTags = JSON.parse(tags);
const fetchRootEvent = useMemo(() => {
if (tags.length > 0) {
if (tags[0][0] === 'e') {
return <RootNote id={tags[0][1]} />;
if (parseTags.length > 0) {
if (parseTags[0][0] === 'e' || parseTags[0][2] === 'root') {
return <RootNote id={parseTags[0][1]} />;
} else {
tags.every((tag) => {
if (tag[2] === 'root') {
return <RootNote id={tags[1]} />;
parseTags.every((tag) => {
if (tag[0] === 'e' && tag[2] === 'root') {
return <RootNote id={parseTags[1]} />;
}
return <></>;
});
@@ -21,7 +22,7 @@ export const Note = memo(function Note({ event }: { event: any }) {
} else {
return <></>;
}
}, [tags]);
}, [parseTags]);
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 hover:bg-black/20">

View File

@@ -4,7 +4,7 @@ export const Placeholder = memo(function Placeholder() {
return (
<div className="relative z-10 flex h-min animate-pulse select-text flex-col py-5 px-3">
<div className="flex items-start gap-2">
<div className="relative h-11 w-11 shrink overflow-hidden rounded-full bg-zinc-700" />
<div className="relative h-11 w-11 shrink overflow-hidden rounded-md bg-zinc-700" />
<div className="flex w-full flex-1 items-start justify-between">
<div className="flex w-full items-center justify-between">
<div className="flex items-center gap-2 text-sm">

View File

@@ -16,8 +16,8 @@ export const RootNote = memo(function RootNote({ id }: { id: string }) {
async (event: any) => {
// insert to local database
await db.execute(
'INSERT OR IGNORE INTO cache_notes (id, pubkey, created_at, kind, content, tags) VALUES (?, ?, ?, ?, ?, ?);',
[event.id, event.pubkey, event.created_at, event.kind, event.content, JSON.stringify(event.tags)]
'INSERT OR IGNORE INTO cache_notes (id, pubkey, created_at, kind, content, tags, is_root) VALUES (?, ?, ?, ?, ?, ?, ?);',
[event.id, event.pubkey, event.created_at, event.kind, event.content, String(event.tags), 1]
);
},
[db]
@@ -76,7 +76,7 @@ export const RootNote = memo(function RootNote({ id }: { id: string }) {
return (
<div className="relative z-10 flex h-min animate-pulse select-text flex-col pb-5">
<div className="flex items-start gap-2">
<div className="relative h-11 w-11 shrink overflow-hidden rounded-full bg-zinc-700" />
<div className="relative h-11 w-11 shrink overflow-hidden rounded-md bg-zinc-700" />
<div className="flex w-full flex-1 items-start justify-between">
<div className="flex w-full items-center justify-between">
<div className="flex items-center gap-2 text-sm">