wip: ark
This commit is contained in:
@@ -1,45 +1,41 @@
|
||||
import { NDKEvent, NDKSubscriptionCacheUsage, NostrEvent } from '@nostr-dev-kit/ndk';
|
||||
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 { useNDK } from '@libs/ndk/provider';
|
||||
import { useArk } from '@libs/ark';
|
||||
|
||||
export function useEvent(id: undefined | string, embed?: undefined | string) {
|
||||
const { ndk } = useNDK();
|
||||
const { ark } = useArk();
|
||||
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 rEvents = await ndk.fetchEvents({
|
||||
kinds: [naddr.kind],
|
||||
'#d': [naddr.identifier],
|
||||
authors: [naddr.pubkey],
|
||||
const events = await ark.getAllEvents({
|
||||
filter: {
|
||||
kinds: [naddr.kind],
|
||||
'#d': [naddr.identifier],
|
||||
authors: [naddr.pubkey],
|
||||
},
|
||||
});
|
||||
|
||||
const rEvent = [...rEvents].slice(-1)[0];
|
||||
|
||||
if (!rEvent) throw new Error('event not found');
|
||||
return rEvent;
|
||||
event = events.slice(-1)[0];
|
||||
}
|
||||
|
||||
// return embed event (nostr.band api)
|
||||
if (embed) {
|
||||
const embedEvent: NostrEvent = JSON.parse(embed);
|
||||
const ndkEvent = new NDKEvent(ndk, embedEvent);
|
||||
|
||||
return ndkEvent;
|
||||
event = ark.createNDKEvent({ event: embedEvent });
|
||||
}
|
||||
|
||||
// get event from relay
|
||||
const event = await ndk.fetchEvent(id, {
|
||||
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST,
|
||||
});
|
||||
event = await ark.getEventById({ id });
|
||||
|
||||
if (!event)
|
||||
throw new Error(`Cannot get event with ${id}, will be retry after 10 seconds`);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { NDKSubscriptionCacheUsage, NDKUserProfile } from '@nostr-dev-kit/ndk';
|
||||
import { NDKUserProfile } from '@nostr-dev-kit/ndk';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
|
||||
import { useNDK } from '@libs/ndk/provider';
|
||||
import { useArk } from '@libs/ark';
|
||||
|
||||
export function useProfile(pubkey: string, embed?: string) {
|
||||
const { ndk } = useNDK();
|
||||
const { ark } = useArk();
|
||||
const {
|
||||
isLoading,
|
||||
isError,
|
||||
@@ -29,10 +29,7 @@ export function useProfile(pubkey: string, embed?: string) {
|
||||
if (decoded.type === 'npub') hexstring = decoded.data;
|
||||
}
|
||||
|
||||
const user = ndk.getUser({ pubkey: hexstring });
|
||||
const profile = await user.fetchProfile({
|
||||
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST,
|
||||
});
|
||||
const profile = await ark.getUserProfile({ pubkey: hexstring });
|
||||
|
||||
if (!profile)
|
||||
throw new Error(
|
||||
|
||||
@@ -1,46 +1,44 @@
|
||||
import { NDKEvent, NDKKind, NDKRelayUrl, NDKTag } from '@nostr-dev-kit/ndk';
|
||||
import { NDKKind, NDKRelayUrl, NDKTag } from '@nostr-dev-kit/ndk';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { useNDK } from '@libs/ndk/provider';
|
||||
import { useStorage } from '@libs/storage/provider';
|
||||
import { useArk } from '@libs/ark';
|
||||
|
||||
export function useRelay() {
|
||||
const { db } = useStorage();
|
||||
const { ndk } = useNDK();
|
||||
|
||||
const { ark } = useArk();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const connectRelay = useMutation({
|
||||
mutationFn: async (relay: NDKRelayUrl, purpose?: 'read' | 'write' | undefined) => {
|
||||
// Cancel any outgoing refetches
|
||||
await queryClient.cancelQueries({ queryKey: ['relays', db.account.pubkey] });
|
||||
await queryClient.cancelQueries({ queryKey: ['relays', ark.account.pubkey] });
|
||||
|
||||
// Snapshot the previous value
|
||||
const prevRelays: NDKTag[] = queryClient.getQueryData([
|
||||
'relays',
|
||||
db.account.pubkey,
|
||||
ark.account.pubkey,
|
||||
]);
|
||||
|
||||
// create new relay list if not exist
|
||||
if (!prevRelays) {
|
||||
const newListEvent = new NDKEvent(ndk);
|
||||
newListEvent.kind = NDKKind.RelayList;
|
||||
newListEvent.tags = [['r', relay, purpose ?? '']];
|
||||
await newListEvent.publish();
|
||||
await ark.createEvent({
|
||||
kind: NDKKind.RelayList,
|
||||
tags: [['r', relay, purpose ?? '']],
|
||||
publish: true,
|
||||
});
|
||||
}
|
||||
|
||||
// add relay to exist list
|
||||
const index = prevRelays.findIndex((el) => el[1] === relay);
|
||||
if (index > -1) return;
|
||||
|
||||
const event = new NDKEvent(ndk);
|
||||
event.kind = NDKKind.RelayList;
|
||||
event.tags = [...prevRelays, ['r', relay, purpose ?? '']];
|
||||
|
||||
await event.publish();
|
||||
await ark.createEvent({
|
||||
kind: NDKKind.RelayList,
|
||||
tags: [...prevRelays, ['r', relay, purpose ?? '']],
|
||||
publish: true,
|
||||
});
|
||||
|
||||
// Optimistically update to the new value
|
||||
queryClient.setQueryData(['relays', db.account.pubkey], (prev: NDKTag[]) => [
|
||||
queryClient.setQueryData(['relays', ark.account.pubkey], (prev: NDKTag[]) => [
|
||||
...prev,
|
||||
['r', relay, purpose ?? ''],
|
||||
]);
|
||||
@@ -49,19 +47,19 @@ export function useRelay() {
|
||||
return { prevRelays };
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['relays', db.account.pubkey] });
|
||||
queryClient.invalidateQueries({ queryKey: ['relays', ark.account.pubkey] });
|
||||
},
|
||||
});
|
||||
|
||||
const removeRelay = useMutation({
|
||||
mutationFn: async (relay: NDKRelayUrl) => {
|
||||
// Cancel any outgoing refetches
|
||||
await queryClient.cancelQueries({ queryKey: ['relays', db.account.pubkey] });
|
||||
await queryClient.cancelQueries({ queryKey: ['relays', ark.account.pubkey] });
|
||||
|
||||
// Snapshot the previous value
|
||||
const prevRelays: NDKTag[] = queryClient.getQueryData([
|
||||
'relays',
|
||||
db.account.pubkey,
|
||||
ark.account.pubkey,
|
||||
]);
|
||||
|
||||
if (!prevRelays) return;
|
||||
@@ -69,19 +67,20 @@ export function useRelay() {
|
||||
const index = prevRelays.findIndex((el) => el[1] === relay);
|
||||
if (index > -1) prevRelays.splice(index, 1);
|
||||
|
||||
const event = new NDKEvent(ndk);
|
||||
event.kind = NDKKind.RelayList;
|
||||
event.tags = prevRelays;
|
||||
await event.publish();
|
||||
await ark.createEvent({
|
||||
kind: NDKKind.RelayList,
|
||||
tags: prevRelays,
|
||||
publish: true,
|
||||
});
|
||||
|
||||
// Optimistically update to the new value
|
||||
queryClient.setQueryData(['relays', db.account.pubkey], prevRelays);
|
||||
queryClient.setQueryData(['relays', ark.account.pubkey], prevRelays);
|
||||
|
||||
// Return a context object with the snapshotted value
|
||||
return { prevRelays };
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['relays', db.account.pubkey] });
|
||||
queryClient.invalidateQueries({ queryKey: ['relays', ark.account.pubkey] });
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { ReactNode } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import reactStringReplace from 'react-string-replace';
|
||||
|
||||
import { useStorage } from '@libs/storage/provider';
|
||||
import { useArk } from '@libs/ark';
|
||||
|
||||
import {
|
||||
Hashtag,
|
||||
@@ -46,7 +46,7 @@ const VIDEOS = [
|
||||
];
|
||||
|
||||
export function useRichContent(content: string, textmode: boolean = false) {
|
||||
const { db } = useStorage();
|
||||
const { ark } = useArk();
|
||||
|
||||
let parsedContent: string | ReactNode[] = content.replace(/\n+/g, '\n');
|
||||
let linkPreview: string;
|
||||
@@ -58,7 +58,7 @@ export function useRichContent(content: string, textmode: boolean = false) {
|
||||
const words = text.split(/( |\n)/);
|
||||
|
||||
if (!textmode) {
|
||||
if (db.settings.media) {
|
||||
if (ark.settings.media) {
|
||||
images = words.filter((word) => IMAGES.some((el) => word.endsWith(el)));
|
||||
videos = words.filter((word) => VIDEOS.some((el) => word.endsWith(el)));
|
||||
}
|
||||
@@ -90,7 +90,7 @@ export function useRichContent(content: string, textmode: boolean = false) {
|
||||
if (hashtags.length) {
|
||||
hashtags.forEach((hashtag) => {
|
||||
parsedContent = reactStringReplace(parsedContent, hashtag, (match, i) => {
|
||||
if (db.settings.hashtag) return <Hashtag key={match + i} tag={hashtag} />;
|
||||
if (ark.settings.hashtag) return <Hashtag key={match + i} tag={hashtag} />;
|
||||
return null;
|
||||
});
|
||||
});
|
||||
|
||||
6
src/utils/types.d.ts
vendored
6
src/utils/types.d.ts
vendored
@@ -160,3 +160,9 @@ export interface NIP11 {
|
||||
payments_url: string;
|
||||
icon: string[];
|
||||
}
|
||||
|
||||
export interface NIP05 {
|
||||
names: {
|
||||
[key: string]: string;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user