diff --git a/lib/src/Components/Auth/LoginPage.tsx b/lib/src/Components/Auth/LoginPage.tsx index f3e55ca5..a87bdd01 100644 --- a/lib/src/Components/Auth/LoginPage.tsx +++ b/lib/src/Components/Auth/LoginPage.tsx @@ -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 => { + 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 () => { diff --git a/lib/src/Components/Map/hooks/useMyProfile.ts b/lib/src/Components/Map/hooks/useMyProfile.ts index e4020c87..e94c82ed 100644 --- a/lib/src/Components/Map/hooks/useMyProfile.ts +++ b/lib/src/Components/Map/hooks/useMyProfile.ts @@ -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 } } diff --git a/lib/src/Components/Onboarding/InvitePage.tsx b/lib/src/Components/Onboarding/InvitePage.tsx index c4bc82bf..800766a8 100644 --- a/lib/src/Components/Onboarding/InvitePage.tsx +++ b/lib/src/Components/Onboarding/InvitePage.tsx @@ -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 (