Don't toast error if user profile was not loaded yet

This commit is contained in:
Maximilian Harz 2025-07-09 19:33:40 +02:00
parent 08cf110472
commit 438d590b85
3 changed files with 23 additions and 6 deletions

View File

@ -22,12 +22,14 @@ export function LoginPage({ inviteApi }: Props) {
const { login, loading } = useAuth()
const myProfile = useMyProfile()
const { myProfile, isMyProfileLoaded } = useMyProfile()
const navigate = useNavigate()
const redeemInvite = useCallback(
async (inviteCode: string): Promise<string | null> => {
if (!isMyProfileLoaded) return null
if (!myProfile) {
toast.error('Could not find your profile to redeem the invite.')
return null
@ -37,7 +39,7 @@ export function LoginPage({ inviteApi }: Props) {
localStorage.removeItem('inviteCode') // Clear invite code after redeeming
return invitingProfileId
},
[inviteApi, myProfile],
[inviteApi, isMyProfileLoaded, myProfile],
)
const handleSuccess = useCallback(async () => {

View File

@ -1,15 +1,20 @@
import { useAuth } from '#components/Auth/useAuth'
import { useItems } from './useItems'
import { useItems, useAllItemsLoaded } from './useItems'
export const useMyProfile = () => {
const items = useItems()
const allItemsLoaded = useAllItemsLoaded()
const user = useAuth().user
// allItemsLoaded is not reliable, so we check if items.length > 0
const isMyProfileLoaded = allItemsLoaded && items.length > 0 && !!user
// Find the user's profile item
const myProfile = items.find(
(item) => item.layer?.userProfileLayer && item.user_created?.id === user?.id,
)
return myProfile
return { myProfile, isMyProfileLoaded }
}

View File

@ -20,7 +20,7 @@ export function InvitePage({ inviteApi }: Props) {
const { id } = useParams<{ id: string }>()
const navigate = useNavigate()
const myProfile = useMyProfile()
const { myProfile, isMyProfileLoaded } = useMyProfile()
if (!id) throw new Error('Invite ID is required')
@ -28,6 +28,8 @@ export function InvitePage({ inviteApi }: Props) {
async function redeemInvite() {
if (!id) throw new Error('Invite ID is required')
if (!isMyProfileLoaded) return
if (!myProfile) {
toast.error('Could not find your profile to redeem the invite.')
return
@ -55,7 +57,15 @@ export function InvitePage({ inviteApi }: Props) {
// Redirect to login page
navigate('/login')
}
}, [id, isAuthenticated, inviteApi, navigate, isAuthenticationInitialized, myProfile])
}, [
id,
isAuthenticated,
inviteApi,
navigate,
isAuthenticationInitialized,
myProfile,
isMyProfileLoaded,
])
return (
<MapOverlayPage backdrop className='tw:max-w-xs tw:h-fit'>