feat(ark): refactor

This commit is contained in:
2023-12-16 07:47:00 +07:00
parent ba93bdbb91
commit 17c64ee357
18 changed files with 449 additions and 483 deletions

View File

@@ -1,7 +1,5 @@
import { NDKEvent, NostrEvent } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { nip19 } from 'nostr-tools';
import { AddressPointer } from 'nostr-tools/lib/types/nip19';
import { useArk } from '@libs/ark';
export function useEvent(id: undefined | string, embed?: undefined | string) {
@@ -9,32 +7,14 @@ export function useEvent(id: undefined | string, embed?: undefined | string) {
const { status, isFetching, isError, data } = useQuery({
queryKey: ['event', id],
queryFn: async () => {
let event: NDKEvent = undefined;
const naddr = id.startsWith('naddr')
? (nip19.decode(id).data as AddressPointer)
: null;
// return event refer from naddr
if (naddr) {
const events = await ark.getAllEvents({
filter: {
kinds: [naddr.kind],
'#d': [naddr.identifier],
authors: [naddr.pubkey],
},
});
event = events.slice(-1)[0];
}
// return embed event (nostr.band api)
if (embed) {
const embedEvent: NostrEvent = JSON.parse(embed);
event = ark.createNDKEvent({ event: embedEvent });
return new NDKEvent(ark.ndk, embedEvent);
}
// get event from relay
event = await ark.getEventById({ id });
const event = await ark.getEventById({ id });
if (!event)
throw new Error(`Cannot get event with ${id}, will be retry after 10 seconds`);

View File

@@ -1,6 +1,5 @@
import { NDKUserProfile } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { nip19 } from 'nostr-tools';
import { useArk } from '@libs/ark';
export function useProfile(pubkey: string, embed?: string) {
@@ -19,16 +18,7 @@ export function useProfile(pubkey: string, embed?: string) {
return profile;
}
// get clean pubkey without any special characters
let hexstring = pubkey.replace(/[^a-zA-Z0-9]/g, '');
if (hexstring.startsWith('npub1') || hexstring.startsWith('nprofile1')) {
const decoded = nip19.decode(hexstring);
if (decoded.type === 'nprofile') hexstring = decoded.data.pubkey;
if (decoded.type === 'npub') hexstring = decoded.data;
}
const profile = await ark.getUserProfile({ pubkey: hexstring });
const profile = await ark.getUserProfile({ pubkey });
if (!profile)
throw new Error(

View File

@@ -22,7 +22,6 @@ export function useRelay() {
await ark.createEvent({
kind: NDKKind.RelayList,
tags: [['r', relay, purpose ?? '']],
publish: true,
});
}
@@ -33,7 +32,6 @@ export function useRelay() {
await ark.createEvent({
kind: NDKKind.RelayList,
tags: [...prevRelays, ['r', relay, purpose ?? '']],
publish: true,
});
// Optimistically update to the new value
@@ -69,7 +67,6 @@ export function useRelay() {
await ark.createEvent({
kind: NDKKind.RelayList,
tags: prevRelays,
publish: true,
});
// Optimistically update to the new value

View File

@@ -3,9 +3,13 @@ export function fileType(url: string) {
return 'image';
}
if (url.match(/\.(mp4|mov|webm|wmv|flv|mts|avi|ogv|mkv|mp3|m3u8)$/)) {
if (url.match(/\.(mp4|mov|webm|wmv|flv|mts|avi|ogv|mkv)$/)) {
return 'video';
}
if (url.match(/\.(mp3|ogg|wav)$/)) {
return 'audio';
}
return 'link';
}