refactor note component
This commit is contained in:
@@ -5,18 +5,29 @@ import { createNote, getNoteByID } from '@libs/storage';
|
||||
|
||||
import { parser } from '@utils/parser';
|
||||
|
||||
export function useEvent(id: string) {
|
||||
export function useEvent(id: string, fallback?: string) {
|
||||
const { ndk } = useNDK();
|
||||
const { status, data, error, isFetching } = useQuery(
|
||||
['note', id],
|
||||
async () => {
|
||||
const result = await getNoteByID(id);
|
||||
if (result) {
|
||||
if (result.kind === 1 || result.kind === 1063) {
|
||||
if (result.kind === 1) {
|
||||
result['content'] = parser(result);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
if (fallback) {
|
||||
const embed = JSON.parse(fallback);
|
||||
await createNote(
|
||||
embed.id,
|
||||
embed.pubkey,
|
||||
embed.kind,
|
||||
embed.tags,
|
||||
embed.content,
|
||||
embed.created_at
|
||||
);
|
||||
}
|
||||
const event = await ndk.fetchEvent(id);
|
||||
await createNote(
|
||||
event.id,
|
||||
@@ -27,7 +38,7 @@ export function useEvent(id: string) {
|
||||
event.created_at
|
||||
);
|
||||
event['event_id'] = event.id;
|
||||
if (event.kind === 1 || event.kind === 1063) {
|
||||
if (event.kind === 1) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
event['content'] = parser(event);
|
||||
|
||||
@@ -24,9 +24,6 @@ export function useProfile(pubkey: string, fallback?: string) {
|
||||
if (latest) {
|
||||
await createMetadata(pubkey, pubkey, latest.content);
|
||||
return JSON.parse(latest.content);
|
||||
} else {
|
||||
await createMetadata(pubkey, pubkey, [...events][0].content);
|
||||
return JSON.parse([...events][0].content);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user