From 89f9ca8a2cdf12ed9617fe2bcb713c4544dcc70b Mon Sep 17 00:00:00 2001 From: en Date: Mon, 3 Feb 2025 17:01:51 +0100 Subject: [PATCH 1/6] refactor(ndk): add opts to findMetadata --- src/contexts/NDKContext.tsx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/contexts/NDKContext.tsx b/src/contexts/NDKContext.tsx index dd5e605..b89902c 100644 --- a/src/contexts/NDKContext.tsx +++ b/src/contexts/NDKContext.tsx @@ -6,6 +6,7 @@ import NDK, { NDKList, NDKRelaySet, NDKSubscriptionCacheUsage, + NDKSubscriptionOptions, NDKUser, zapInvoiceFromEvent } from '@nostr-dev-kit/ndk' @@ -48,7 +49,10 @@ export interface NDKContextType { hexKey: string, userRelaysType: UserRelaysType ) => Promise - findMetadata: (pubkey: string) => Promise + findMetadata: ( + pubkey: string, + opts?: NDKSubscriptionOptions + ) => Promise getTotalZapAmount: ( user: string, eTag: string, @@ -111,7 +115,12 @@ export const NDKContextProvider = ({ children }: { children: ReactNode }) => { } const ndk = useMemo(() => { - localStorage.removeItem('debug') + if (import.meta.env.MODE === 'development') { + localStorage.setItem('debug', '*') + } else { + localStorage.removeItem('debug') + } + const dexieAdapter = new NDKCacheAdapterDexie({ dbName: 'degmod-db' }) dexieAdapter.locking = true const ndk = new NDK({ @@ -309,13 +318,16 @@ export const NDKContextProvider = ({ children }: { children: ReactNode }) => { * @param hexKey - The pubkey to search for metadata. * @returns A promise that resolves to the metadata event. */ - const findMetadata = async (pubkey: string): Promise => { + const findMetadata = async ( + pubkey: string, + opts?: NDKSubscriptionOptions + ): Promise => { const npub = hexToNpub(pubkey) const user = new NDKUser({ npub }) user.ndk = ndk - const userProfile = await user.fetchProfile() + const userProfile = await user.fetchProfile(opts) return userProfile } From a5c1f1db7493084610de28d2bc2a32f1b69415a2 Mon Sep 17 00:00:00 2001 From: en Date: Mon, 3 Feb 2025 17:02:21 +0100 Subject: [PATCH 2/6] refactor(useProfile): add opts to hook --- src/hooks/useProfile.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/hooks/useProfile.tsx b/src/hooks/useProfile.tsx index 15f00ff..26508bb 100644 --- a/src/hooks/useProfile.tsx +++ b/src/hooks/useProfile.tsx @@ -1,18 +1,19 @@ +import { NDKSubscriptionOptions } from '@nostr-dev-kit/ndk' import { useNDKContext } from 'hooks' import { useState, useEffect } from 'react' import { UserProfile } from 'types' -export const useProfile = (pubkey?: string) => { +export const useProfile = (pubkey?: string, opts?: NDKSubscriptionOptions) => { const { findMetadata } = useNDKContext() const [profile, setProfile] = useState() useEffect(() => { if (pubkey) { - findMetadata(pubkey).then((res) => { + findMetadata(pubkey, opts).then((res) => { setProfile(res) }) } - }, [findMetadata, pubkey]) + }, [findMetadata, pubkey, opts]) return profile } From 52edaf5eca9fa3cbf68b8364b95e25c99bd685b0 Mon Sep 17 00:00:00 2001 From: en Date: Mon, 3 Feb 2025 17:03:39 +0100 Subject: [PATCH 3/6] fix(profile): on login fetch profile from relays only --- src/layout/header.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/layout/header.tsx b/src/layout/header.tsx index cfbb02a..68d1363 100644 --- a/src/layout/header.tsx +++ b/src/layout/header.tsx @@ -22,7 +22,7 @@ import { npubToHex } from '../utils' import logo from '../assets/img/DEG Mods Logo With Text.svg' import placeholder from '../assets/img/DEG Mods Default PP.png' import { resetUserWot } from 'store/reducers/wot' -import { NDKNip07Signer } from '@nostr-dev-kit/ndk' +import { NDKNip07Signer, NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk' export const Header = () => { const dispatch = useAppDispatch() @@ -66,7 +66,9 @@ export const Header = () => { }) ) ndk.signer = new NDKNip07Signer() - findMetadata(npub).then((userProfile) => { + findMetadata(npub, { + cacheUsage: NDKSubscriptionCacheUsage.ONLY_RELAY + }).then((userProfile) => { if (userProfile) { dispatch( setUser({ From 734f8e6d8cbeb61c8ad9be74e0da9aba1b8221ea Mon Sep 17 00:00:00 2001 From: en Date: Mon, 3 Feb 2025 17:05:13 +0100 Subject: [PATCH 4/6] fix(profile): fix condition, update created_at in content --- src/pages/settings/profile.tsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/pages/settings/profile.tsx b/src/pages/settings/profile.tsx index b553995..4da983c 100644 --- a/src/pages/settings/profile.tsx +++ b/src/pages/settings/profile.tsx @@ -123,12 +123,13 @@ export const ProfileSettings = () => { } const handlePublish = async () => { - if (!userState.auth && !userState.user?.pubkey) return + if (!userState.auth || !userState.user?.pubkey) return setIsPublishing(true) const prevProfile = userState.user as NDKUserProfile - const updatedProfile = { + const createdAt = now() + const updatedProfile: NDKUserProfile = { ...prevProfile, name: formState.name, displayName: formState.displayName, @@ -136,16 +137,16 @@ export const ProfileSettings = () => { picture: formState.picture, banner: formState.banner, nip05: formState.nip05, - lud16: formState.lud16 + lud16: formState.lud16, + created_at: createdAt } - const serializedProfile = serializeProfile(updatedProfile) const unsignedEvent: UnsignedEvent = { kind: kinds.Metadata, tags: [], content: serializedProfile, - created_at: now(), + created_at: createdAt, pubkey: userState.user?.pubkey as string } @@ -176,7 +177,6 @@ export const ProfileSettings = () => { )}` ) - const ndkEvent = new NDKEvent(undefined, signedEvent) const userProfile = profileFromEvent(ndkEvent) dispatch(setUser(userProfile)) } From 4430d1780e42df1cbe155a7ca3464f1c0d9fd0b1 Mon Sep 17 00:00:00 2001 From: en Date: Mon, 3 Feb 2025 17:10:11 +0100 Subject: [PATCH 5/6] fix(profile): fetch parallel on visiting user profile --- src/components/ProfileSection.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/ProfileSection.tsx b/src/components/ProfileSection.tsx index 227a945..16e8b0d 100644 --- a/src/components/ProfileSection.tsx +++ b/src/components/ProfileSection.tsx @@ -26,7 +26,7 @@ import { import { LoadingSpinner } from './LoadingSpinner' import { ZapPopUp } from './Zap' import placeholder from '../assets/img/DEGMods Placeholder Img.png' -import { NDKEvent } from '@nostr-dev-kit/ndk' +import { NDKEvent, NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk' import { useProfile } from 'hooks/useProfile' type Props = { @@ -93,7 +93,9 @@ type ProfileProps = { } export const Profile = ({ pubkey }: ProfileProps) => { - const profile = useProfile(pubkey) + const profile = useProfile(pubkey, { + cacheUsage: NDKSubscriptionCacheUsage.PARALLEL + }) const displayName = profile?.displayName || profile?.name || '[name not set up]' From 3df32e22266d4e8f46a84815facd02419d90ff47 Mon Sep 17 00:00:00 2001 From: en Date: Mon, 3 Feb 2025 17:36:58 +0100 Subject: [PATCH 6/6] fix(profile): fetch only from relays on visiting profile --- src/pages/profile/loader.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/pages/profile/loader.ts b/src/pages/profile/loader.ts index 13eef6c..93cb3dd 100644 --- a/src/pages/profile/loader.ts +++ b/src/pages/profile/loader.ts @@ -1,3 +1,4 @@ +import { NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk' import { NDKContextType } from 'contexts/NDKContext' import { nip19 } from 'nostr-tools' import { LoaderFunctionArgs, redirect } from 'react-router-dom' @@ -94,7 +95,9 @@ export const profileRouteLoader = } const settled = await Promise.allSettled([ - ndkContext.findMetadata(profilePubkey), + ndkContext.findMetadata(profilePubkey, { + cacheUsage: NDKSubscriptionCacheUsage.ONLY_RELAY + }), ndkContext.getMuteLists(loggedInUserPubkey), getReportingSet(CurationSetIdentifiers.NSFW, ndkContext), getReportingSet(CurationSetIdentifiers.Repost, ndkContext)