From eae708e07c4783e8650d7ef0eec8d896a729edfc Mon Sep 17 00:00:00 2001
From: Ren Amamiya <123083837+reyamir@users.noreply.github.com>
Date: Thu, 23 Feb 2023 09:18:58 +0700
Subject: [PATCH] updated fetch profile function
---
src/components/note/atoms/rootUser.tsx | 36 +++---
src/components/note/atoms/user.tsx | 106 ++++++++----------
.../note/content/preview/imageCard.tsx | 7 +-
3 files changed, 75 insertions(+), 74 deletions(-)
diff --git a/src/components/note/atoms/rootUser.tsx b/src/components/note/atoms/rootUser.tsx
index a0782176..91c29966 100644
--- a/src/components/note/atoms/rootUser.tsx
+++ b/src/components/note/atoms/rootUser.tsx
@@ -2,25 +2,35 @@
import { truncate } from '@utils/truncate';
import { useNostrEvents } from 'nostr-react';
+import { useState } from 'react';
export default function RootUser({ userPubkey, action }: { userPubkey: string; action: string }) {
- const { events } = useNostrEvents({
+ const [profile, setProfile] = useState({ picture: null, name: null });
+
+ const { onEvent } = useNostrEvents({
filter: {
authors: [userPubkey],
kinds: [0],
},
});
- if (events !== undefined && events.length > 0) {
- const userData: any = JSON.parse(events[0].content);
- return (
-
-
- {userData?.name ? userData.name : truncate(userPubkey, 16, ' .... ')} {action}
-
-
- );
- } else {
- return <>>;
- }
+ // #TODO: save response to DB
+ onEvent((rawMetadata) => {
+ try {
+ const metadata: any = JSON.parse(rawMetadata.content);
+ if (metadata) {
+ setProfile(metadata);
+ }
+ } catch (err) {
+ console.error(err, rawMetadata);
+ }
+ });
+
+ return (
+
+
+ {profile.name ? profile.name : truncate(userPubkey, 16, ' .... ')} {action}
+
+
+ );
}
diff --git a/src/components/note/atoms/user.tsx b/src/components/note/atoms/user.tsx
index 1a4f43df..20899f94 100644
--- a/src/components/note/atoms/user.tsx
+++ b/src/components/note/atoms/user.tsx
@@ -7,74 +7,66 @@ import MoreIcon from '@assets/icons/More';
import Avatar from 'boring-avatars';
import { useNostrEvents } from 'nostr-react';
-import { memo } from 'react';
+import { memo, useState } from 'react';
import Moment from 'react-moment';
export const User = memo(function User({ pubkey, time }: { pubkey: string; time: any }) {
- const { events } = useNostrEvents({
+ const [profile, setProfile] = useState({ picture: null, name: null });
+
+ const { onEvent } = useNostrEvents({
filter: {
authors: [pubkey],
kinds: [0],
},
});
- if (events !== undefined && events.length > 0) {
- const userData: any = JSON.parse(events[0].content);
+ // #TODO: save response to DB
+ onEvent((rawMetadata) => {
+ try {
+ const metadata: any = JSON.parse(rawMetadata.content);
+ if (metadata) {
+ setProfile(metadata);
+ }
+ } catch (err) {
+ console.error(err, rawMetadata);
+ }
+ });
- return (
-
-
- {userData?.picture ? (
-
- ) : (
-
- )}
-
-
-
-
-
- {userData?.name ? userData.name : truncate(pubkey, 16, ' .... ')}
-
- ·
-
- {time}
-
-
-
-
-
+ return (
+
+
+ {profile.picture ? (
+
+ ) : (
+
+ )}
+
+
+
+
+
+ {profile.name ? profile.name : truncate(pubkey, 16, ' .... ')}
+
+ ·
+
+ {time}
+
+
+
+
- );
- } else {
- return (
-
- );
- }
+
+ );
});
diff --git a/src/components/note/content/preview/imageCard.tsx b/src/components/note/content/preview/imageCard.tsx
index 9acb7463..55d9f12c 100644
--- a/src/components/note/content/preview/imageCard.tsx
+++ b/src/components/note/content/preview/imageCard.tsx
@@ -3,16 +3,15 @@ import Image from 'next/image';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function ImageCard({ data }: { data: object }) {
return (
-