diff --git a/src/pages/blog/loader.ts b/src/pages/blog/loader.ts index 0b06b50..f0fc114 100644 --- a/src/pages/blog/loader.ts +++ b/src/pages/blog/loader.ts @@ -13,6 +13,7 @@ import { } from 'types' import { DEFAULT_FILTER_OPTIONS, + getFallbackPubkey, getLocalStorageItem, log, LogType @@ -41,7 +42,8 @@ export const blogRouteLoader = } const userState = store.getState().user - const loggedInUserPubkey = userState?.user?.pubkey as string | undefined + const loggedInUserPubkey = + (userState?.user?.pubkey as string | undefined) || getFallbackPubkey() // Check if editing and the user is the original author // Redirect if NOT diff --git a/src/pages/mod/loader.ts b/src/pages/mod/loader.ts index 032d697..3c7ebc2 100644 --- a/src/pages/mod/loader.ts +++ b/src/pages/mod/loader.ts @@ -16,6 +16,7 @@ import { DEFAULT_FILTER_OPTIONS, extractBlogCardDetails, extractModData, + getFallbackPubkey, getLocalStorageItem, getReportingSet, log, @@ -46,7 +47,8 @@ export const modRouteLoader = } const userState = store.getState().user - const loggedInUserPubkey = userState?.user?.pubkey as string | undefined + const loggedInUserPubkey = + (userState?.user?.pubkey as string | undefined) || getFallbackPubkey() try { // Set up the filters diff --git a/src/pages/mods/loader.ts b/src/pages/mods/loader.ts index 24fc3a4..e7a05ba 100644 --- a/src/pages/mods/loader.ts +++ b/src/pages/mods/loader.ts @@ -1,7 +1,13 @@ import { NDKContextType } from 'contexts/NDKContext' import { store } from 'store' import { MuteLists } from 'types' -import { getReportingSet, CurationSetIdentifiers, log, LogType } from 'utils' +import { + getReportingSet, + CurationSetIdentifiers, + log, + LogType, + getFallbackPubkey +} from 'utils' export interface ModsPageLoaderResult { muteLists: { @@ -31,15 +37,11 @@ export const modsRouteLoader = (ndkContext: NDKContextType) => async () => { // Get the current state const userState = store.getState().user - - // Check if current user is logged in - let userPubkey: string | undefined - if (userState.auth && userState.user?.pubkey) { - userPubkey = userState.user.pubkey as string - } + const loggedInUserPubkey = + (userState?.user?.pubkey as string | undefined) || getFallbackPubkey() const settled = await Promise.allSettled([ - ndkContext.getMuteLists(userPubkey), + ndkContext.getMuteLists(loggedInUserPubkey), getReportingSet(CurationSetIdentifiers.NSFW, ndkContext), getReportingSet(CurationSetIdentifiers.Repost, ndkContext) ]) diff --git a/src/pages/profile/loader.ts b/src/pages/profile/loader.ts index f5bc1b6..13eef6c 100644 --- a/src/pages/profile/loader.ts +++ b/src/pages/profile/loader.ts @@ -6,6 +6,7 @@ import { store } from 'store' import { MuteLists, UserProfile } from 'types' import { CurationSetIdentifiers, + getFallbackPubkey, getReportingSet, log, LogType, @@ -16,7 +17,6 @@ export interface ProfilePageLoaderResult { profilePubkey: string profile: UserProfile isBlocked: boolean - isOwnProfile: boolean muteLists: { admin: MuteLists user: MuteLists @@ -58,21 +58,17 @@ export const profileRouteLoader = // Get the current state const userState = store.getState().user - - // Check if current user is logged in - let userPubkey: string | undefined - if (userState.auth && userState.user?.pubkey) { - userPubkey = userState.user.pubkey as string - } + const loggedInUserPubkey = + (userState?.user?.pubkey as string | undefined) || getFallbackPubkey() // Redirect if profile naddr is missing // - home if user is not logged let profileRoute = appRoutes.home - if (!profilePubkey && userPubkey) { + if (!profilePubkey && loggedInUserPubkey) { // - own profile profileRoute = getProfilePageRoute( nip19.nprofileEncode({ - pubkey: userPubkey + pubkey: loggedInUserPubkey }) ) } @@ -83,7 +79,6 @@ export const profileRouteLoader = profilePubkey: profilePubkey, profile: {}, isBlocked: false, - isOwnProfile: false, muteLists: { admin: { authors: [], @@ -98,14 +93,9 @@ export const profileRouteLoader = repostList: [] } - // Check if user the user is logged in - if (userState.auth && userState.user?.pubkey) { - result.isOwnProfile = userState.user.pubkey === profilePubkey - } - const settled = await Promise.allSettled([ ndkContext.findMetadata(profilePubkey), - ndkContext.getMuteLists(userPubkey), + ndkContext.getMuteLists(loggedInUserPubkey), getReportingSet(CurationSetIdentifiers.NSFW, ndkContext), getReportingSet(CurationSetIdentifiers.Repost, ndkContext) ]) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index bb740e5..e28027d 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -160,3 +160,23 @@ export const parseFormData = (formData: FormData) => { export const capitalizeEachWord = (str: string): string => { return str.replace(/\b\w/g, (char) => char.toUpperCase()) } + +/** + * nostr-login - helper function + * should only be used as the fallback + * user state is not updated before `onAuth` triggers but loaders are faster + */ +export const getFallbackPubkey = () => { + try { + // read nostr-login conf from localStorage + const stored = window.localStorage.getItem('__nostrlogin_nip46') + if (!stored) return + + const info = JSON.parse(stored) + if (info && !info.pubkey) return + + return info.pubkey as string + } catch { + // Silently ignore + } +}