From e9aa7bd4ffd491030c8efbf8d69379378b0c8c1f Mon Sep 17 00:00:00 2001
From: Ren Amamiya <123083837+reyamir@users.noreply.github.com>
Date: Fri, 17 Mar 2023 14:15:10 +0700
Subject: [PATCH] fixed sql execute
---
src/components/note/connector.tsx | 21 ++++-----------------
src/components/note/content/index.tsx | 2 +-
src/components/note/form/basic.tsx | 5 ++++-
src/components/user/base.tsx | 7 ++-----
src/components/user/extend.tsx | 7 ++-----
src/components/user/mini.tsx | 7 ++-----
6 files changed, 15 insertions(+), 34 deletions(-)
diff --git a/src/components/note/connector.tsx b/src/components/note/connector.tsx
index 39d979d5..e54effba 100644
--- a/src/components/note/connector.tsx
+++ b/src/components/note/connector.tsx
@@ -7,7 +7,7 @@ import { dateToUnix, hoursAgo } from '@utils/getDate';
import { useLocalStorage } from '@rehooks/local-storage';
import { useSetAtom } from 'jotai';
-import { memo, useCallback, useContext, useEffect, useRef } from 'react';
+import { memo, useCallback, useContext, useMemo, useRef } from 'react';
export const NoteConnector = memo(function NoteConnector() {
const { db }: any = useContext(DatabaseContext);
@@ -23,23 +23,14 @@ export const NoteConnector = memo(function NoteConnector() {
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) VALUES (?, ?, ?, ?, ?, ?);',
+ [event.id, event.pubkey, event.created_at, event.kind, event.content, JSON.stringify(event.tags)]
);
},
[db]
);
- const fetchEvent = useCallback(() => {
+ useMemo(() => {
relayPool.subscribe(
[
{
@@ -60,10 +51,6 @@ export const NoteConnector = memo(function NoteConnector() {
);
}, [relayPool, follows, relays, insertDB, setHasNewerNote]);
- useEffect(() => {
- fetchEvent();
- }, [fetchEvent]);
-
return (
<>
diff --git a/src/components/note/content/index.tsx b/src/components/note/content/index.tsx
index 859ea39f..ab63320e 100644
--- a/src/components/note/content/index.tsx
+++ b/src/components/note/content/index.tsx
@@ -70,7 +70,7 @@ export const Content = memo(function Content({ data }: { data: any }) {
-
diff --git a/src/components/user/base.tsx b/src/components/user/base.tsx
index 51220251..993d124f 100644
--- a/src/components/user/base.tsx
+++ b/src/components/user/base.tsx
@@ -12,13 +12,10 @@ export const UserBase = memo(function UserBase({ pubkey }: { pubkey: string }) {
const cacheProfile = useCallback(
async (event) => {
- const metadata: any = JSON.parse(event.content);
// insert to database
- await db.execute(
- `INSERT OR IGNORE INTO cache_profiles (id, metadata) VALUES ("${pubkey}", '${JSON.stringify(metadata)}')`
- );
+ await db.execute('INSERT OR IGNORE INTO cache_profiles (id, metadata) VALUES (?, ?);', [pubkey, event.content]);
// update state
- setProfile(metadata);
+ setProfile(JSON.parse(event.content));
},
[db, pubkey]
);
diff --git a/src/components/user/extend.tsx b/src/components/user/extend.tsx
index bf516f24..a5c6e7c2 100644
--- a/src/components/user/extend.tsx
+++ b/src/components/user/extend.tsx
@@ -17,13 +17,10 @@ export const UserExtend = memo(function UserExtend({ pubkey, time }: { pubkey: s
const insertCacheProfile = useCallback(
async (event) => {
- const metadata: any = JSON.parse(event.content);
// insert to database
- await db.execute(
- `INSERT OR IGNORE INTO cache_profiles (id, metadata) VALUES ("${pubkey}", '${JSON.stringify(metadata)}')`
- );
+ await db.execute('INSERT OR IGNORE INTO cache_profiles (id, metadata) VALUES (?, ?);', [pubkey, event.content]);
// update state
- setProfile(metadata);
+ setProfile(JSON.parse(event.content));
},
[db, pubkey]
);
diff --git a/src/components/user/mini.tsx b/src/components/user/mini.tsx
index 13140a0b..9a449aa7 100644
--- a/src/components/user/mini.tsx
+++ b/src/components/user/mini.tsx
@@ -12,13 +12,10 @@ export const UserMini = memo(function UserMini({ pubkey }: { pubkey: string }) {
const insertCacheProfile = useCallback(
async (event) => {
- const metadata: any = JSON.parse(event.content);
// insert to database
- await db.execute(
- `INSERT OR IGNORE INTO cache_profiles (id, metadata) VALUES ("${pubkey}", '${JSON.stringify(metadata)}')`
- );
+ await db.execute('INSERT OR IGNORE INTO cache_profiles (id, metadata) VALUES (?, ?);', [pubkey, event.content]);
// update state
- setProfile(metadata);
+ setProfile(JSON.parse(event.content));
},
[db, pubkey]
);