Normalize feed load errors, show toast, and add demo fallback on error; refactor mapping function scope
cgen-eeba5c261a774fdeb8fa0fd67b03db1f
This commit is contained in:
parent
97cbf921e5
commit
6fe9b6aded
1 changed files with 32 additions and 20 deletions
|
|
@ -19,6 +19,7 @@ import { useToast } from "@/hooks/use-toast";
|
||||||
import { useAuth } from "@/contexts/AuthContext";
|
import { useAuth } from "@/contexts/AuthContext";
|
||||||
import { aethexSocialService } from "@/lib/aethex-social-service";
|
import { aethexSocialService } from "@/lib/aethex-social-service";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
import { normalizeErrorMessage } from "@/lib/error-utils";
|
||||||
import { communityService, realtimeService } from "@/lib/supabase-service";
|
import { communityService, realtimeService } from "@/lib/supabase-service";
|
||||||
import {
|
import {
|
||||||
ArrowUpRight,
|
ArrowUpRight,
|
||||||
|
|
@ -89,6 +90,26 @@ export default function Feed() {
|
||||||
"all" | "following" | "trending"
|
"all" | "following" | "trending"
|
||||||
>("all");
|
>("all");
|
||||||
|
|
||||||
|
const mapPostsToFeedItems = useCallback(
|
||||||
|
(source: any[]): FeedItem[] =>
|
||||||
|
(Array.isArray(source) ? source : []).map((p: any) => {
|
||||||
|
const meta = parseContent(p.content);
|
||||||
|
const author = p.user_profiles || {};
|
||||||
|
return {
|
||||||
|
id: p.id,
|
||||||
|
authorId: p.author_id,
|
||||||
|
authorName: author.full_name || author.username || "Community member",
|
||||||
|
authorAvatar: author.avatar_url,
|
||||||
|
caption: meta.text,
|
||||||
|
mediaUrl: meta.mediaUrl,
|
||||||
|
mediaType: meta.mediaType,
|
||||||
|
likes: p.likes_count ?? 0,
|
||||||
|
comments: p.comments_count ?? 0,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
const fetchFeed = useCallback(async () => {
|
const fetchFeed = useCallback(async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
|
|
@ -100,24 +121,6 @@ export default function Feed() {
|
||||||
setFollowing([]);
|
setFollowing([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const mapPostsToFeedItems = (source: any[]): FeedItem[] =>
|
|
||||||
(Array.isArray(source) ? source : []).map((p: any) => {
|
|
||||||
const meta = parseContent(p.content);
|
|
||||||
const author = p.user_profiles || {};
|
|
||||||
return {
|
|
||||||
id: p.id,
|
|
||||||
authorId: p.author_id,
|
|
||||||
authorName:
|
|
||||||
author.full_name || author.username || "Community member",
|
|
||||||
authorAvatar: author.avatar_url,
|
|
||||||
caption: meta.text,
|
|
||||||
mediaUrl: meta.mediaUrl,
|
|
||||||
mediaType: meta.mediaType,
|
|
||||||
likes: p.likes_count ?? 0,
|
|
||||||
comments: p.comments_count ?? 0,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
let mapped = mapPostsToFeedItems(posts);
|
let mapped = mapPostsToFeedItems(posts);
|
||||||
|
|
||||||
if (mapped.length === 0) {
|
if (mapped.length === 0) {
|
||||||
|
|
@ -147,11 +150,20 @@ export default function Feed() {
|
||||||
setItems(mapped);
|
setItems(mapped);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to load feed", error);
|
console.error("Failed to load feed", error);
|
||||||
setItems([]);
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: "Failed to load feed",
|
||||||
|
description: normalizeErrorMessage(error),
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
ensureDemoSeed();
|
||||||
|
const demoPosts = getDemoPosts();
|
||||||
|
setItems(mapPostsToFeedItems(demoPosts));
|
||||||
|
} catch {}
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, [user?.id]);
|
}, [aethexSocialService, communityService, mapPostsToFeedItems, toast, user?.id]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchFeed();
|
fetchFeed();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue