fix: add fallback for usersPubkey in loaders
This commit is contained in:
parent
ad3d069ad5
commit
ad68ba8e84
@ -13,6 +13,7 @@ import {
|
|||||||
} from 'types'
|
} from 'types'
|
||||||
import {
|
import {
|
||||||
DEFAULT_FILTER_OPTIONS,
|
DEFAULT_FILTER_OPTIONS,
|
||||||
|
getFallbackPubkey,
|
||||||
getLocalStorageItem,
|
getLocalStorageItem,
|
||||||
log,
|
log,
|
||||||
LogType
|
LogType
|
||||||
@ -41,7 +42,8 @@ export const blogRouteLoader =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const userState = store.getState().user
|
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
|
// Check if editing and the user is the original author
|
||||||
// Redirect if NOT
|
// Redirect if NOT
|
||||||
|
@ -16,6 +16,7 @@ import {
|
|||||||
DEFAULT_FILTER_OPTIONS,
|
DEFAULT_FILTER_OPTIONS,
|
||||||
extractBlogCardDetails,
|
extractBlogCardDetails,
|
||||||
extractModData,
|
extractModData,
|
||||||
|
getFallbackPubkey,
|
||||||
getLocalStorageItem,
|
getLocalStorageItem,
|
||||||
getReportingSet,
|
getReportingSet,
|
||||||
log,
|
log,
|
||||||
@ -46,7 +47,8 @@ export const modRouteLoader =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const userState = store.getState().user
|
const userState = store.getState().user
|
||||||
const loggedInUserPubkey = userState?.user?.pubkey as string | undefined
|
const loggedInUserPubkey =
|
||||||
|
(userState?.user?.pubkey as string | undefined) || getFallbackPubkey()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Set up the filters
|
// Set up the filters
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
import { NDKContextType } from 'contexts/NDKContext'
|
import { NDKContextType } from 'contexts/NDKContext'
|
||||||
import { store } from 'store'
|
import { store } from 'store'
|
||||||
import { MuteLists } from 'types'
|
import { MuteLists } from 'types'
|
||||||
import { getReportingSet, CurationSetIdentifiers, log, LogType } from 'utils'
|
import {
|
||||||
|
getReportingSet,
|
||||||
|
CurationSetIdentifiers,
|
||||||
|
log,
|
||||||
|
LogType,
|
||||||
|
getFallbackPubkey
|
||||||
|
} from 'utils'
|
||||||
|
|
||||||
export interface ModsPageLoaderResult {
|
export interface ModsPageLoaderResult {
|
||||||
muteLists: {
|
muteLists: {
|
||||||
@ -31,15 +37,11 @@ export const modsRouteLoader = (ndkContext: NDKContextType) => async () => {
|
|||||||
|
|
||||||
// Get the current state
|
// Get the current state
|
||||||
const userState = store.getState().user
|
const userState = store.getState().user
|
||||||
|
const loggedInUserPubkey =
|
||||||
// Check if current user is logged in
|
(userState?.user?.pubkey as string | undefined) || getFallbackPubkey()
|
||||||
let userPubkey: string | undefined
|
|
||||||
if (userState.auth && userState.user?.pubkey) {
|
|
||||||
userPubkey = userState.user.pubkey as string
|
|
||||||
}
|
|
||||||
|
|
||||||
const settled = await Promise.allSettled([
|
const settled = await Promise.allSettled([
|
||||||
ndkContext.getMuteLists(userPubkey),
|
ndkContext.getMuteLists(loggedInUserPubkey),
|
||||||
getReportingSet(CurationSetIdentifiers.NSFW, ndkContext),
|
getReportingSet(CurationSetIdentifiers.NSFW, ndkContext),
|
||||||
getReportingSet(CurationSetIdentifiers.Repost, ndkContext)
|
getReportingSet(CurationSetIdentifiers.Repost, ndkContext)
|
||||||
])
|
])
|
||||||
|
@ -6,6 +6,7 @@ import { store } from 'store'
|
|||||||
import { MuteLists, UserProfile } from 'types'
|
import { MuteLists, UserProfile } from 'types'
|
||||||
import {
|
import {
|
||||||
CurationSetIdentifiers,
|
CurationSetIdentifiers,
|
||||||
|
getFallbackPubkey,
|
||||||
getReportingSet,
|
getReportingSet,
|
||||||
log,
|
log,
|
||||||
LogType,
|
LogType,
|
||||||
@ -16,7 +17,6 @@ export interface ProfilePageLoaderResult {
|
|||||||
profilePubkey: string
|
profilePubkey: string
|
||||||
profile: UserProfile
|
profile: UserProfile
|
||||||
isBlocked: boolean
|
isBlocked: boolean
|
||||||
isOwnProfile: boolean
|
|
||||||
muteLists: {
|
muteLists: {
|
||||||
admin: MuteLists
|
admin: MuteLists
|
||||||
user: MuteLists
|
user: MuteLists
|
||||||
@ -58,21 +58,17 @@ export const profileRouteLoader =
|
|||||||
|
|
||||||
// Get the current state
|
// Get the current state
|
||||||
const userState = store.getState().user
|
const userState = store.getState().user
|
||||||
|
const loggedInUserPubkey =
|
||||||
// Check if current user is logged in
|
(userState?.user?.pubkey as string | undefined) || getFallbackPubkey()
|
||||||
let userPubkey: string | undefined
|
|
||||||
if (userState.auth && userState.user?.pubkey) {
|
|
||||||
userPubkey = userState.user.pubkey as string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Redirect if profile naddr is missing
|
// Redirect if profile naddr is missing
|
||||||
// - home if user is not logged
|
// - home if user is not logged
|
||||||
let profileRoute = appRoutes.home
|
let profileRoute = appRoutes.home
|
||||||
if (!profilePubkey && userPubkey) {
|
if (!profilePubkey && loggedInUserPubkey) {
|
||||||
// - own profile
|
// - own profile
|
||||||
profileRoute = getProfilePageRoute(
|
profileRoute = getProfilePageRoute(
|
||||||
nip19.nprofileEncode({
|
nip19.nprofileEncode({
|
||||||
pubkey: userPubkey
|
pubkey: loggedInUserPubkey
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -83,7 +79,6 @@ export const profileRouteLoader =
|
|||||||
profilePubkey: profilePubkey,
|
profilePubkey: profilePubkey,
|
||||||
profile: {},
|
profile: {},
|
||||||
isBlocked: false,
|
isBlocked: false,
|
||||||
isOwnProfile: false,
|
|
||||||
muteLists: {
|
muteLists: {
|
||||||
admin: {
|
admin: {
|
||||||
authors: [],
|
authors: [],
|
||||||
@ -98,14 +93,9 @@ export const profileRouteLoader =
|
|||||||
repostList: []
|
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([
|
const settled = await Promise.allSettled([
|
||||||
ndkContext.findMetadata(profilePubkey),
|
ndkContext.findMetadata(profilePubkey),
|
||||||
ndkContext.getMuteLists(userPubkey),
|
ndkContext.getMuteLists(loggedInUserPubkey),
|
||||||
getReportingSet(CurationSetIdentifiers.NSFW, ndkContext),
|
getReportingSet(CurationSetIdentifiers.NSFW, ndkContext),
|
||||||
getReportingSet(CurationSetIdentifiers.Repost, ndkContext)
|
getReportingSet(CurationSetIdentifiers.Repost, ndkContext)
|
||||||
])
|
])
|
||||||
|
@ -160,3 +160,23 @@ export const parseFormData = <T>(formData: FormData) => {
|
|||||||
export const capitalizeEachWord = (str: string): string => {
|
export const capitalizeEachWord = (str: string): string => {
|
||||||
return str.replace(/\b\w/g, (char) => char.toUpperCase())
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user