refactor note component

This commit is contained in:
Ren Amamiya
2023-07-15 12:20:09 +07:00
parent 41460436df
commit 1f18d8bb44
38 changed files with 1174 additions and 884 deletions

View File

@@ -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);

View File

@@ -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 {

View File

@@ -1,4 +1,3 @@
import destr from 'destr';
import getUrls from 'get-urls';
import { Event, parseReferences } from 'nostr-tools';
import { ReactNode } from 'react';
@@ -9,20 +8,7 @@ import { MentionUser } from '@shared/notes/mentions/user';
import { LumeEvent } from '@utils/types';
function isJsonString(str: string[][] | string) {
try {
if (typeof str === 'string') JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
export function parser(event: LumeEvent) {
if (isJsonString(event.tags)) {
event['tags'] = destr(event.tags);
}
const references = parseReferences(event as Event);
const urls = getUrls(event.content);