added useMetadata and refactor user component

This commit is contained in:
Ren Amamiya
2023-04-06 09:25:18 +07:00
parent 3c63dece46
commit 5437ec5c92
17 changed files with 161 additions and 211 deletions

View File

@@ -44,11 +44,20 @@ export function getNoteById(data: GetNoteByIdData) {
return invoke<Note | null>('get_note_by_id', { data });
}
export type CreatePlebData = { pubkey: string; kind: number; metadata: string; account_id: number };
export type Account = { id: number; pubkey: string; privkey: string; active: boolean; metadata: string };
export type GetLatestNoteData = { date: number };
export type Pleb = { id: number; pubkey: string; kind: number; metadata: string; accountId: number };
export type GetPlebPubkeyData = { pubkey: string };
export type CreateNoteData = {
event_id: string;
pubkey: string;
kind: number;
tags: string;
content: string;
parent_id: string;
parent_comment_id: string;
created_at: number;
account_id: number;
};
export type CreatePlebData = { pleb_id: string; pubkey: string; kind: number; metadata: string; account_id: number };
export type GetNoteByIdData = { event_id: string };
export type Pleb = { id: number; plebId: string; pubkey: string; kind: number; metadata: string; accountId: number };
export type Note = {
id: number;
eventId: string;
@@ -61,18 +70,9 @@ export type Note = {
createdAt: number;
accountId: number;
};
export type Account = { id: number; pubkey: string; privkey: string; active: boolean; metadata: string };
export type GetPlebPubkeyData = { pubkey: string };
export type GetPlebData = { account_id: number };
export type CreateAccountData = { pubkey: string; privkey: string; metadata: string };
export type CreateNoteData = {
event_id: string;
pubkey: string;
kind: number;
tags: string;
content: string;
parent_id: string;
parent_comment_id: string;
created_at: number;
account_id: number;
};
export type GetNoteByIdData = { event_id: string };
export type GetLatestNoteData = { date: number };
export type GetNoteData = { date: number; limit: number; offset: number };

View File

@@ -1,6 +1,39 @@
import { RelayContext } from '@components/relaysProvider';
import { Author } from 'nostr-relaypool';
import { useCallback, useContext, useEffect, useState } from 'react';
export const fetchMetadata = (pubkey: string, pool: any, relays: any) => {
const author = new Author(pool, relays, pubkey);
return new Promise((resolve) => author.metaData(resolve, 0));
};
export const useMetadata = (pubkey) => {
const [pool, relays]: any = useContext(RelayContext);
const [profile, setProfile] = useState(null);
const getCachedMetadata = useCallback(async () => {
const { getPlebByPubkey } = await import('@utils/bindings');
getPlebByPubkey({ pubkey: pubkey })
.then((res) => {
if (res) {
const metadata = JSON.parse(res.metadata);
setProfile(metadata);
} else {
fetchMetadata(pubkey, pool, relays).then((res: any) => {
if (res.content) {
const metadata = JSON.parse(res.content);
setProfile(metadata);
}
});
}
})
.catch(console.error);
}, [pool, relays, pubkey]);
useEffect(() => {
getCachedMetadata().catch(console.error);
}, [getCachedMetadata]);
return profile;
};