Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,27 +192,50 @@ export default function Home() {

const querySnapshot = await getDocs(q);
const storiesData: Story[] = [];
const storyIds: string[] = [];

for (const doc of querySnapshot.docs) {
const storyData = {
id: doc.id,
...doc.data(),
reactionsCount: 0
} as Story;
storiesData.push(storyData);
storyIds.push(doc.id);
}

const reactionsRef = collection(db, 'reactions');
const reactionsQuery = query(
reactionsRef,
where('story_id', '==', doc.id)
);
// Collect reactions counts in-memory if there are stories
const reactionsCountMap: Record<string, number> = {};
if (storyIds.length > 0) {
const chunks: string[][] = [];
for (let i = 0; i < storyIds.length; i += 30) {
chunks.push(storyIds.slice(i, i + 30));
}

const reactionsSnapshot = await getDocs(reactionsQuery);
const storyReactions = reactionsSnapshot.docs.filter(
reactionDoc => reactionDoc.data().story_id === doc.id
);
for (const chunk of chunks) {
try {
const reactionsRef = collection(db, 'reactions');
const reactionsQuery = query(
reactionsRef,
where('story_id', 'in', chunk)
);
const reactionsSnapshot = await getDocs(reactionsQuery);
reactionsSnapshot.docs.forEach(reactionDoc => {
const data = reactionDoc.data();
const storyId = data.story_id;
if (storyId) {
reactionsCountMap[storyId] = (reactionsCountMap[storyId] || 0) + 1;
}
});
} catch (err) {
console.error('Error batch-fetching reactions chunk:', err);
}
}
}

storyData.reactionsCount = storyReactions.length;
storiesData.push(storyData);
// Merge counts back into stories
for (const story of storiesData) {
story.reactionsCount = reactionsCountMap[story.id] || 0;
}

storiesData.sort((a, b) => (b.reactionsCount ?? 0) - (a.reactionsCount ?? 0));
Expand Down
43 changes: 37 additions & 6 deletions src/pages/Stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,12 @@ export default function Stories() {
const querySnapshot = await getDocs(q);

const fetchedStories: Story[] = [];
const storyIds: string[] = [];

// Process each story document
for (const doc of querySnapshot.docs) {
const storyData = doc.data();

// Get reaction count
const reactionsRef = collection(db, 'reactions');
const reactionsQuery = query(reactionsRef, where('story_id', '==', doc.id));
const reactionsSnapshot = await getDocs(reactionsQuery);

fetchedStories.push({
id: doc.id,
title: storyData.title || '',
Expand All @@ -166,9 +162,44 @@ export default function Stories() {
media_urls: storyData.media_urls || [],
created_at: storyData.created_at,
author_id: storyData.author_id || '',
reactionsCount: reactionsSnapshot.size,
reactionsCount: 0,
risk_level: storyData.risk_level || 'LOW',
});
storyIds.push(doc.id);
}

// Collect reactions counts in-memory if there are stories
const reactionsCountMap: Record<string, number> = {};
if (storyIds.length > 0) {
const chunks: string[][] = [];
for (let i = 0; i < storyIds.length; i += 30) {
chunks.push(storyIds.slice(i, i + 30));
}

for (const chunk of chunks) {
try {
const reactionsRef = collection(db, 'reactions');
const reactionsQuery = query(
reactionsRef,
where('story_id', 'in', chunk)
);
const reactionsSnapshot = await getDocs(reactionsQuery);
reactionsSnapshot.docs.forEach(reactionDoc => {
const data = reactionDoc.data();
const storyId = data.story_id;
if (storyId) {
reactionsCountMap[storyId] = (reactionsCountMap[storyId] || 0) + 1;
}
});
} catch (err) {
console.error('Error batch-fetching reactions chunk:', err);
}
}
}

// Merge counts back into stories
for (const story of fetchedStories) {
story.reactionsCount = reactionsCountMap[story.id] || 0;
}

setStories(fetchedStories);
Expand Down
Loading