Update feed fetching to seed Supabase demo data
cgen-a30ed464378d437a8ecb29da34910c17
This commit is contained in:
parent
4078c50f7c
commit
f0427f74ec
1 changed files with 43 additions and 39 deletions
|
|
@ -92,55 +92,59 @@ export default function Feed() {
|
||||||
const fetchFeed = useCallback(async () => {
|
const fetchFeed = useCallback(async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const posts = await communityService.getPosts(30);
|
let posts = await communityService.getPosts(30);
|
||||||
if (user?.id) {
|
if (user?.id) {
|
||||||
const flw = await aethexSocialService.getFollowing(user.id);
|
const flw = await aethexSocialService.getFollowing(user.id);
|
||||||
setFollowing(flw);
|
setFollowing(flw);
|
||||||
} else {
|
} else {
|
||||||
setFollowing([]);
|
setFollowing([]);
|
||||||
}
|
}
|
||||||
const mapped: FeedItem[] = posts.map((p: any) => {
|
|
||||||
const meta = parseContent(p.content);
|
const mapPostsToFeedItems = (source: any[]): FeedItem[] =>
|
||||||
const author = p.user_profiles || {};
|
(Array.isArray(source) ? source : []).map((p: any) => {
|
||||||
return {
|
const meta = parseContent(p.content);
|
||||||
id: p.id,
|
const author = p.user_profiles || {};
|
||||||
authorId: p.author_id,
|
return {
|
||||||
authorName: author.full_name || author.username || "Community member",
|
id: p.id,
|
||||||
authorAvatar: author.avatar_url,
|
authorId: p.author_id,
|
||||||
caption: meta.text,
|
authorName:
|
||||||
mediaUrl: meta.mediaUrl,
|
author.full_name || author.username || "Community member",
|
||||||
mediaType: meta.mediaType,
|
authorAvatar: author.avatar_url,
|
||||||
likes: p.likes_count ?? 0,
|
caption: meta.text,
|
||||||
comments: p.comments_count ?? 0,
|
mediaUrl: meta.mediaUrl,
|
||||||
};
|
mediaType: meta.mediaType,
|
||||||
});
|
likes: p.likes_count ?? 0,
|
||||||
|
comments: p.comments_count ?? 0,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
let mapped = mapPostsToFeedItems(posts);
|
||||||
|
|
||||||
|
if (mapped.length === 0) {
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/community/seed-demo", {
|
||||||
|
method: "POST",
|
||||||
|
});
|
||||||
|
if (response.ok) {
|
||||||
|
posts = await communityService.getPosts(30);
|
||||||
|
mapped = mapPostsToFeedItems(posts);
|
||||||
|
}
|
||||||
|
} catch (seedError) {
|
||||||
|
console.warn("Community demo seed failed", seedError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (mapped.length === 0 && typeof window !== "undefined") {
|
if (mapped.length === 0 && typeof window !== "undefined") {
|
||||||
ensureDemoSeed();
|
try {
|
||||||
const demoPosts = getDemoPosts();
|
ensureDemoSeed();
|
||||||
if (Array.isArray(demoPosts) && demoPosts.length) {
|
const demoPosts = getDemoPosts();
|
||||||
const seeded = demoPosts.map((post: any) => {
|
mapped = mapPostsToFeedItems(demoPosts);
|
||||||
const meta = parseContent(post.content);
|
} catch (fallbackError) {
|
||||||
const author = post.user_profiles || {};
|
console.warn("Local demo feed fallback failed", fallbackError);
|
||||||
return {
|
|
||||||
id: post.id,
|
|
||||||
authorId: post.author_id,
|
|
||||||
authorName: author.full_name || author.username || "Community member",
|
|
||||||
authorAvatar: author.avatar_url,
|
|
||||||
caption: meta.text,
|
|
||||||
mediaUrl: meta.mediaUrl,
|
|
||||||
mediaType: meta.mediaType,
|
|
||||||
likes: post.likes_count ?? 0,
|
|
||||||
comments: post.comments_count ?? 0,
|
|
||||||
} as FeedItem;
|
|
||||||
});
|
|
||||||
setItems(seeded);
|
|
||||||
} else {
|
|
||||||
setItems(mapped);
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
setItems(mapped);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setItems(mapped);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to load feed", error);
|
console.error("Failed to load feed", error);
|
||||||
setItems([]);
|
setItems([]);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue