fix: add fallback for usersPubkey in loaders

This commit is contained in:
enes 2024-12-26 16:46:40 +01:00
parent ad3d069ad5
commit ad68ba8e84
5 changed files with 42 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
])

View File

@ -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)
])

View File

@ -160,3 +160,23 @@ export const parseFormData = <T>(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
}
}