From d6bc3b868447ff56ba522de2505e692f00f071b2 Mon Sep 17 00:00:00 2001 From: enes Date: Wed, 13 Nov 2024 16:24:19 +0100 Subject: [PATCH] fix(profile): accept npub as valid profile param Closes #120 --- src/pages/profile/index.tsx | 44 ++++--------------------------------- src/pages/profile/loader.ts | 27 ++++++++++++++++++----- 2 files changed, 26 insertions(+), 45 deletions(-) diff --git a/src/pages/profile/index.tsx b/src/pages/profile/index.tsx index ca942cf..56758a6 100644 --- a/src/pages/profile/index.tsx +++ b/src/pages/profile/index.tsx @@ -14,17 +14,11 @@ import { useNDKContext, useNSFWList } from 'hooks' -import { kinds, nip19, UnsignedEvent } from 'nostr-tools' +import { kinds, UnsignedEvent } from 'nostr-tools' import { useCallback, useEffect, useMemo, useRef, useState } from 'react' -import { - useParams, - Navigate, - Link, - useLoaderData, - useNavigation -} from 'react-router-dom' +import { Link, useLoaderData, useNavigation } from 'react-router-dom' import { toast } from 'react-toastify' -import { appRoutes, getProfilePageRoute } from 'routes' +import { appRoutes } from 'routes' import { BlogCardDetails, FilterOptions, @@ -38,8 +32,6 @@ import { copyTextToClipboard, DEFAULT_FILTER_OPTIONS, extractBlogCardDetails, - log, - LogType, now, npubToHex, scrollIntoView, @@ -52,23 +44,11 @@ import { BlogCard } from 'components/BlogCard' export const ProfilePage = () => { const { + profilePubkey, profile, isBlocked: _isBlocked, isOwnProfile } = useLoaderData() as ProfilePageLoaderResult - // Try to decode nprofile parameter - const { nprofile } = useParams() - let profilePubkey: string | undefined - try { - const value = nprofile - ? nip19.decode(nprofile as `nprofile1${string}`) - : undefined - profilePubkey = value?.data.pubkey - } catch (error) { - // Silently ignore and redirect to home or logged in user - log(true, LogType.Error, 'Failed to decode nprofile.', error) - } - const scrollTargetRef = useRef(null) const { ndk, publish, fetchEventFromUserRelays, fetchMods } = useNDKContext() const userState = useAppSelector((state) => state.user) @@ -292,22 +272,6 @@ export const ProfilePage = () => { profilePubkey ) - // Redirect route - let profileRoute = appRoutes.home - if (!nprofile && userState.auth && userState.user) { - // Redirect to user's profile is no profile is linked - const userHexKey = npubToHex(userState.user.npub as string) - - if (userHexKey) { - profileRoute = getProfilePageRoute( - nip19.nprofileEncode({ - pubkey: userHexKey - }) - ) - } - } - if (!profilePubkey) return - return (
diff --git a/src/pages/profile/loader.ts b/src/pages/profile/loader.ts index aecb15d..f3ac4c0 100644 --- a/src/pages/profile/loader.ts +++ b/src/pages/profile/loader.ts @@ -4,9 +4,10 @@ import { LoaderFunctionArgs, redirect } from 'react-router-dom' import { appRoutes, getProfilePageRoute } from 'routes' import { store } from 'store' import { MuteLists, UserProfile } from 'types' -import { log, LogType } from 'utils' +import { log, LogType, npubToHex } from 'utils' export interface ProfilePageLoaderResult { + profilePubkey: string profile: UserProfile isBlocked: boolean isOwnProfile: boolean @@ -24,10 +25,25 @@ export const profileRouteLoader = const { nprofile } = params let profilePubkey: string | undefined try { - const value = nprofile - ? nip19.decode(nprofile as `nprofile1${string}`) - : undefined - profilePubkey = value?.data.pubkey + // Decode if it starts with nprofile1 + if (nprofile?.startsWith('nprofile1')) { + const value = nprofile + ? nip19.decode(nprofile as `nprofile1${string}`) + : undefined + profilePubkey = value?.data.pubkey + } else if (nprofile?.startsWith('npub1')) { + // Try to get hex from the npub and encode it to nprofile + const value = npubToHex(nprofile) + if (value) { + return redirect( + getProfilePageRoute( + nip19.nprofileEncode({ + pubkey: value + }) + ) + ) + } + } } catch (error) { // Silently ignore and redirect to home or logged in user log(true, LogType.Error, 'Failed to decode nprofile.', error) @@ -57,6 +73,7 @@ export const profileRouteLoader = // Empty result const result: ProfilePageLoaderResult = { + profilePubkey: profilePubkey, profile: {}, isBlocked: false, isOwnProfile: false,