feat: update ark provider

This commit is contained in:
2024-01-27 09:08:41 +07:00
parent bc48391a1a
commit 353c18bb76
3 changed files with 110 additions and 128 deletions

View File

@@ -145,7 +145,6 @@ export class Ark {
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST, cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST,
}); });
if (!profile) return null;
return profile; return profile;
} catch { } catch {
throw new Error("user not found"); throw new Error("user not found");
@@ -167,8 +166,9 @@ export class Ark {
(user) => user.pubkey, (user) => user.pubkey,
); );
if (!pubkey || pubkey === this.account.pubkey) if (!pubkey || pubkey === this.account.pubkey) {
this.account.contacts = contacts; this.account.contacts = contacts;
}
return contacts; return contacts;
} catch (e) { } catch (e) {

View File

@@ -1,8 +1,10 @@
import { useQuery } from "@tanstack/react-query"; import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useArk } from "./useArk"; import { useArk } from "./useArk";
export function useProfile(pubkey: string) { export function useProfile(pubkey: string) {
const ark = useArk(); const ark = useArk();
const queryClient = useQueryClient();
const { const {
isLoading, isLoading,
isError, isError,
@@ -17,6 +19,9 @@ export function useProfile(pubkey: string) {
); );
return profile; return profile;
}, },
initialData: () => {
return queryClient.getQueryData(["user", pubkey]);
},
refetchOnMount: false, refetchOnMount: false,
refetchOnWindowFocus: false, refetchOnWindowFocus: false,
refetchOnReconnect: false, refetchOnReconnect: false,

View File

@@ -23,6 +23,7 @@ import { useSetAtom } from "jotai";
import Linkify from "linkify-react"; import Linkify from "linkify-react";
import { normalizeRelayUrlSet } from "nostr-fetch"; import { normalizeRelayUrlSet } from "nostr-fetch";
import { PropsWithChildren, useEffect, useState } from "react"; import { PropsWithChildren, useEffect, useState } from "react";
import { toast } from "sonner";
import { Ark } from "./ark"; import { Ark } from "./ark";
import { LumeContext } from "./context"; import { LumeContext } from "./context";
@@ -80,24 +81,28 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
return new NDKPrivateKeySigner(userPrivkey); return new NDKPrivateKeySigner(userPrivkey);
} catch (e) { } catch (e) {
console.error(e); toast.error(String(e));
return null; return null;
} }
} }
async function initNDK() { async function initNDK() {
try {
const explicitRelayUrls = normalizeRelayUrlSet([ const explicitRelayUrls = normalizeRelayUrlSet([
"wss://nostr.mutinywallet.com/", "wss://nostr.mutinywallet.com/",
"wss://bostr.nokotaro.com/", "wss://bostr.nokotaro.com/",
]); ]);
const outboxRelayUrls = normalizeRelayUrlSet(["wss://purplepag.es/"]);
const tauriCache = new NDKCacheAdapterTauri(storage); const tauriCache = new NDKCacheAdapterTauri(storage);
const ndk = new NDK({ const ndk = new NDK({
cacheAdapter: tauriCache, cacheAdapter: tauriCache,
explicitRelayUrls, explicitRelayUrls,
outboxRelayUrls,
enableOutboxModel: !storage.settings.lowPower, enableOutboxModel: !storage.settings.lowPower,
autoConnectUserRelays: !storage.settings.lowPower, autoConnectUserRelays: !storage.settings.lowPower,
autoFetchUserMutelist: !storage.settings.lowPower, autoFetchUserMutelist: false, // #TODO: add support mute list
clientName: "Lume", clientName: "Lume",
}); });
@@ -115,7 +120,10 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
await ndk.connect(3000); await ndk.connect(3000);
// auth // auth
ndk.relayAuthDefaultPolicy = async (relay: NDKRelay, challenge: string) => { ndk.relayAuthDefaultPolicy = async (
relay: NDKRelay,
challenge: string,
) => {
const signIn = NDKRelayAuthPolicies.signIn({ ndk }); const signIn = NDKRelayAuthPolicies.signIn({ ndk });
const event = await signIn(relay, challenge).catch((e) => const event = await signIn(relay, challenge).catch((e) =>
console.log("auth failed", e), console.log("auth failed", e),
@@ -129,6 +137,9 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
}; };
setNDK(ndk); setNDK(ndk);
} catch (e) {
toast.error(String(e));
}
} }
async function initArk() { async function initArk() {
@@ -137,12 +148,25 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
// ark utils // ark utils
const ark = new Ark({ ndk, account: storage.currentUser }); const ark = new Ark({ ndk, account: storage.currentUser });
try {
if (ndk && storage.currentUser) { if (ndk && storage.currentUser) {
const user = new NDKUser({ pubkey: storage.currentUser.pubkey }); const user = new NDKUser({ pubkey: storage.currentUser.pubkey });
ndk.activeUser = user; ndk.activeUser = user;
// update contacts // update contacts
await ark.getUserContacts(); const contacts = await ark.getUserContacts();
if (contacts?.length) {
console.log("total contacts: ", contacts.length);
for (const pubkey of ark.account.contacts) {
await queryClient.prefetchQuery({
queryKey: ["user", pubkey],
queryFn: async () => {
return await ark.getUserProfile(pubkey);
},
});
}
}
// subscribe for new activity // subscribe for new activity
const activitySub = ndk.subscribe( const activitySub = ndk.subscribe(
@@ -183,56 +207,9 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
break; break;
} }
}); });
}
// prefetch activty } catch (e) {
await queryClient.prefetchInfiniteQuery({ toast.error(String(e));
queryKey: ["activity"],
initialPageParam: 0,
queryFn: async ({
signal,
pageParam,
}: {
signal: AbortSignal;
pageParam: number;
}) => {
const events = await ark.getInfiniteEvents({
filter: {
kinds: [NDKKind.Text, NDKKind.Repost, NDKKind.Zap],
"#p": [ark.account.pubkey],
},
limit: FETCH_LIMIT,
pageParam,
signal,
});
return events;
},
});
// prefetch timeline
await queryClient.prefetchInfiniteQuery({
queryKey: ["timeline-9999"],
initialPageParam: 0,
queryFn: async ({
signal,
pageParam,
}: {
signal: AbortSignal;
pageParam: number;
}) => {
const events = await ark.getInfiniteEvents({
filter: {
kinds: [NDKKind.Text, NDKKind.Repost],
authors: ark.account.contacts,
},
limit: FETCH_LIMIT,
pageParam,
signal,
});
return events;
},
});
} }
setArk(ark); setArk(ark);