wip: refactor

This commit is contained in:
Ren Amamiya
2023-08-16 11:43:04 +07:00
parent 2d53019c10
commit c05bb54976
33 changed files with 600 additions and 1052 deletions

View File

@@ -13,12 +13,12 @@ export function useEvent(id: string, embed?: string) {
if (embed) {
const event: LumeEvent = JSON.parse(embed);
if (event.kind === 1) embed['content'] = parser(event);
return embed;
return embed as unknown as LumeEvent;
} else {
const event = (await ndk.fetchEvent(id)) as unknown as LumeEvent;
if (!event) throw new Error('Event not found');
if (event.kind === 1) event['content'] = parser(event);
return event as unknown as LumeEvent;
const event = (await ndk.fetchEvent(id)) as LumeEvent;
if (!event) return null;
if (event.kind === 1) event['content'] = parser(event) as unknown as string;
return event as LumeEvent;
}
},
{

View File

@@ -6,10 +6,8 @@ import {
NDKSubscription,
NDKUser,
} from '@nostr-dev-kit/ndk';
import { ndkAdapter } from '@nostr-fetch/adapter-ndk';
import destr from 'destr';
import { destr } from 'destr';
import { LRUCache } from 'lru-cache';
import { NostrFetcher } from 'nostr-fetch';
import { nip19 } from 'nostr-tools';
import { useMemo } from 'react';
@@ -21,11 +19,10 @@ import { useStronghold } from '@stores/stronghold';
import { nHoursAgo } from '@utils/date';
export function useNostr() {
const { ndk, relayUrls } = useNDK();
const { ndk } = useNDK();
const { db } = useStorage();
const privkey = useStronghold((state) => state.privkey);
const fetcher = useMemo(() => NostrFetcher.withCustomPool(ndkAdapter(ndk)), [ndk]);
const subManager = useMemo(
() =>
new LRUCache<string, NDKSubscription, void>({
@@ -85,20 +82,13 @@ export function useNostr() {
try {
if (!ndk) return { status: 'failed', message: 'NDK instance not found' };
const until = since === 24 ? Math.floor(Date.now() / 1000) : nHoursAgo(since / 2);
console.log('fetch events since: ', since);
console.log('fetch events until: ', until);
/*
const events = await ndk.fetchEvents({
kinds: [1],
authors: db.account.network ?? db.account.follows,
since: since,
until: until,
since: nHoursAgo(since),
});
*/
return { status: 'ok', data: [], nextCursor: since * 2 };
return { status: 'ok', data: [...events], nextCursor: since * 2 };
} catch (e) {
console.error('failed get notes, error: ', e);
return { status: 'failed', data: [], message: e };

View File

@@ -1,7 +1,6 @@
import { useQuery } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
import { createMetadata } from '@libs/storage';
export function useProfile(pubkey: string, fallback?: string) {
const { ndk } = useNDK();
@@ -18,7 +17,6 @@ export function useProfile(pubkey: string, fallback?: string) {
await user.fetchProfile();
if (user.profile) {
user.profile.display_name = user.profile.displayName;
await createMetadata(user.npub, pubkey, JSON.stringify(user.profile));
return user.profile;
} else {
throw new Error('User not found');
@@ -29,6 +27,7 @@ export function useProfile(pubkey: string, fallback?: string) {
}
},
{
enabled: !!ndk,
staleTime: Infinity,
refetchOnMount: false,
refetchOnWindowFocus: false,

View File

@@ -1,10 +1,8 @@
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
import { createNote } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { nHoursAgo } from '@utils/date';
import { useAccount } from '@utils/hooks/useAccount';
import { useNostr } from '@utils/hooks/useNostr';
export function useSocial() {
@@ -12,12 +10,12 @@ export function useSocial() {
const { publish } = useNostr();
const { ndk } = useNDK();
const { account } = useAccount();
const { db } = useStorage();
const { status, data: userFollows } = useQuery(
['userFollows', account.pubkey],
['userFollows', db.account.pubkey],
async () => {
const keys = [];
const user = ndk.getUser({ hexpubkey: account.pubkey });
const user = ndk.getUser({ hexpubkey: db.account.pubkey });
const follows = await user.follows();
follows.forEach((item) => {
@@ -27,7 +25,6 @@ export function useSocial() {
return keys;
},
{
enabled: account ? true : false,
refetchOnReconnect: false,
refetchOnMount: false,
refetchOnWindowFocus: false,
@@ -47,7 +44,7 @@ export function useSocial() {
publish({ content: '', kind: 3, tags: tags });
// invalid cache
queryClient.invalidateQueries({
queryKey: ['userFollows', account.pubkey],
queryKey: ['userFollows', db.account.pubkey],
});
};
@@ -64,26 +61,8 @@ export function useSocial() {
publish({ content: '', kind: 3, tags: tags });
// invalid cache
queryClient.invalidateQueries({
queryKey: ['userFollows', account.pubkey],
queryKey: ['userFollows', db.account.pubkey],
});
// fetch events
const events = await ndk.fetchEvents({
authors: [pubkey],
kinds: [1, 6],
since: nHoursAgo(24),
});
for (const event of events) {
await createNote(
event.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at
);
}
};
return { status, userFollows, follow, unfollow };