migrate to ndk
This commit is contained in:
@@ -1,56 +1,31 @@
|
||||
import { RelayContext } from "@shared/relayProvider";
|
||||
import { useActiveAccount } from "@stores/accounts";
|
||||
import { READONLY_RELAYS } from "@stores/constants";
|
||||
import { createNote, getNoteByID } from "@utils/storage";
|
||||
import { getParentID } from "@utils/transform";
|
||||
import { useContext } from "react";
|
||||
import useSWR from "swr";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
|
||||
const fetcher = ([, id]) => getNoteByID(id);
|
||||
const fetcher = async ([, ndk, id]) => {
|
||||
const result = await getNoteByID(id);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
} else {
|
||||
const event = await ndk.fetchEvent(id);
|
||||
await createNote(
|
||||
event.id,
|
||||
event.pubkey,
|
||||
event.kind,
|
||||
event.tags,
|
||||
event.content,
|
||||
event.created_at,
|
||||
);
|
||||
event["event_id"] = event.id;
|
||||
return event;
|
||||
}
|
||||
};
|
||||
|
||||
export function useEvent(id: string) {
|
||||
const pool: any = useContext(RelayContext);
|
||||
const account = useActiveAccount((state: any) => state.account);
|
||||
const ndk = useContext(RelayContext);
|
||||
const { data } = useSWR(["note", ndk, id], fetcher);
|
||||
|
||||
const { data: cache } = useSWR(["event", id], fetcher);
|
||||
const { data: newest } = useSWRSubscription(
|
||||
!cache ? id : null,
|
||||
(key, { next }) => {
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
ids: [key],
|
||||
},
|
||||
],
|
||||
READONLY_RELAYS,
|
||||
(event: any) => {
|
||||
const parentID = getParentID(event.tags, event.id);
|
||||
// insert event to local database
|
||||
createNote(
|
||||
event.id,
|
||||
account.id,
|
||||
event.pubkey,
|
||||
event.kind,
|
||||
event.tags,
|
||||
event.content,
|
||||
event.created_at,
|
||||
);
|
||||
// update state
|
||||
next(null, event);
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
},
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
return cache ? cache : newest;
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import NDK from "@nostr-dev-kit/ndk";
|
||||
import { RelayContext } from "@shared/relayProvider";
|
||||
import { METADATA_RELAY } from "@stores/constants";
|
||||
import { createPleb, getPleb } from "@utils/storage";
|
||||
import { nip19 } from "nostr-tools";
|
||||
import { useContext } from "react";
|
||||
import useSWR from "swr";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
|
||||
const fetcher = async (key: string) => {
|
||||
const fetcher = async ([, ndk, key]) => {
|
||||
let npub: string;
|
||||
|
||||
if (key.substring(0, 4) === "npub") {
|
||||
@@ -18,59 +17,27 @@ const fetcher = async (key: string) => {
|
||||
const current = Math.floor(Date.now() / 1000);
|
||||
const result = await getPleb(npub);
|
||||
|
||||
if (result && result.created_at + 86400 < current) {
|
||||
if (result && result.created_at + 86400 > current) {
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
const user = ndk.getUser({ npub });
|
||||
await user.fetchProfile();
|
||||
await createPleb(key, user.profile);
|
||||
|
||||
return user.profile;
|
||||
}
|
||||
};
|
||||
|
||||
export function useProfile(key: string) {
|
||||
const pool: any = useContext(RelayContext);
|
||||
|
||||
const {
|
||||
data: cache,
|
||||
error,
|
||||
isLoading,
|
||||
} = useSWR(key, fetcher, {
|
||||
const ndk = useContext(RelayContext);
|
||||
const { data, error, isLoading } = useSWR(["profile", ndk, key], fetcher, {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: true,
|
||||
});
|
||||
|
||||
const { data: newest } = useSWRSubscription(
|
||||
cache ? null : key,
|
||||
(_, { next }) => {
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
authors: [key],
|
||||
kinds: [0],
|
||||
},
|
||||
],
|
||||
METADATA_RELAY,
|
||||
(event: { content: string }) => {
|
||||
const content = JSON.parse(event.content);
|
||||
// update state
|
||||
next(null, content);
|
||||
// save to database
|
||||
createPleb(key, event);
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
},
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
user: newest ? newest : cache,
|
||||
user: data,
|
||||
isLoading,
|
||||
isError: error,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user