Simplify passport data loading and remove local fallbacks

cgen-c30a0808dd3a4b12bc7d2c8dd39c86b8
This commit is contained in:
Builder.io 2025-10-01 00:56:56 +00:00
parent 2195308de9
commit 88d4cd0bc9

View file

@ -44,7 +44,7 @@ const formatDate = (value?: string | null) => {
const ProfilePassport = () => {
const params = useParams<{ id?: string }>();
const navigate = useNavigate();
const { user, linkedProviders, profile: authProfile } = useAuth();
const { user, linkedProviders } = useAuth();
const [profile, setProfile] = useState<
(AethexUserProfile & { email?: string | null }) | null
@ -74,79 +74,47 @@ const ProfilePassport = () => {
}
const loadProfile = async () => {
setLoading(true);
try {
setLoading(true);
const isViewingSelf = params.id === "me" || !params.id;
const profilePromise = isViewingSelf
? aethexUserService.getCurrentUser()
: aethexUserService.getProfileById(targetUserId);
const [profileData, achievementList, interestList, projectList] =
await Promise.all([
params.id === "me" && profile && profile.id === targetUserId
? Promise.resolve(profile)
: aethexUserService.getProfileById(targetUserId),
aethexAchievementService.getUserAchievements(targetUserId),
aethexUserService.getUserInterests(targetUserId),
aethexProjectService.getUserProjects(targetUserId).catch(() => []),
profilePromise,
aethexAchievementService
.getUserAchievements(targetUserId)
.catch((error) => {
console.error("Failed to load achievements", error);
return [] as AethexAchievement[];
}),
aethexUserService.getUserInterests(targetUserId).catch((error) => {
console.error("Failed to load interests", error);
return [] as string[];
}),
aethexProjectService
.getUserProjects(targetUserId)
.catch((error) => {
console.error("Failed to load projects", error);
return [] as ProjectPreview[];
}),
]);
let resolvedProfile = profileData as
| (AethexUserProfile & { email?: string | null })
| null;
const isViewingSelf = params.id === "me" && user?.id === targetUserId;
if (!resolvedProfile && isViewingSelf) {
resolvedProfile = (authProfile as any) ?? null;
}
if (
!resolvedProfile &&
isViewingSelf &&
typeof window !== "undefined"
) {
try {
const stored = localStorage.getItem(`demo_profile_${targetUserId}`);
if (stored) {
resolvedProfile = JSON.parse(stored);
}
} catch {}
}
if (!resolvedProfile && isViewingSelf && user) {
resolvedProfile = {
id: user.id,
username: user.email?.split("@")[0] || `user_${Date.now()}`,
full_name:
(authProfile as any)?.full_name || user.email || "AeThex Creator",
email: user.email,
user_type:
((authProfile as any)?.user_type as any) ||
("community_member" as any),
experience_level:
((authProfile as any)?.experience_level as any) ||
("beginner" as any),
level: (authProfile as any)?.level ?? 1,
total_xp: (authProfile as any)?.total_xp ?? 0,
loyalty_points: (authProfile as any)?.loyalty_points ?? 0,
created_at:
(authProfile as any)?.created_at || new Date().toISOString(),
updated_at:
(authProfile as any)?.updated_at || new Date().toISOString(),
} as AethexUserProfile;
}
if (resolvedProfile && isViewingSelf && typeof window !== "undefined") {
try {
localStorage.setItem(
`demo_profile_${targetUserId}`,
JSON.stringify(resolvedProfile),
);
} catch {}
}
if (!resolvedProfile) {
if (!profileData) {
setNotFound(true);
return;
}
setProfile(resolvedProfile as any);
const resolvedProfile = {
...profileData,
email:
(profileData as any)?.email ?? (isViewingSelf ? user?.email ?? null : null),
} as AethexUserProfile & { email?: string | null };
setProfile(resolvedProfile);
setAchievements(achievementList ?? []);
setInterests(interestList ?? []);
setProjects(
@ -168,9 +136,8 @@ const ProfilePassport = () => {
};
loadProfile();
// We intentionally exclude profile from dependencies to avoid refetch loops when local state updates
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [targetUserId, params.id]);
}, [targetUserId, params.id, user?.email]);
if (loading) {
return <LoadingScreen message="Loading AeThex passport..." />;