fix lesson parsing on course page, also fetches all lessons in one query now

This commit is contained in:
austinkelsay 2025-04-28 13:28:39 -05:00
parent cc86708b6a
commit fac14d5b77
No known key found for this signature in database
GPG Key ID: 5A763922E5BA08EE
2 changed files with 22 additions and 19 deletions

View File

@ -20,10 +20,6 @@ export default function DesktopCourseDetails({
showCompletedTag
}) {
useEffect(() => {
console.log('menuItems', menuItems);
}, [menuItems]);
return (
<>
{/* Header with course image, title and options */}

View File

@ -90,34 +90,41 @@ const useLessons = (ndk, fetchAuthor, lessonIds, pubkey) => {
const [lessons, setLessons] = useState([]);
const [uniqueLessons, setUniqueLessons] = useState([]);
const { showToast } = useToast();
useEffect(() => {
if (lessonIds.length > 0) {
const fetchLesson = async lessonId => {
if (lessonIds.length > 0 && pubkey) {
const fetchLessons = async () => {
try {
await ndk.connect();
// Create a single filter with all lesson IDs to avoid multiple calls
const filter = {
'#d': [lessonId],
'#d': lessonIds,
kinds: [30023, 30402],
authors: [pubkey],
};
const event = await ndk.fetchEvent(filter);
if (event) {
const events = await ndk.fetchEvents(filter);
const newLessons = [];
// Process events without duplicating
for (const event of events) {
const author = await fetchAuthor(event.pubkey);
const parsedLesson = { ...parseEvent(event), author };
setLessons(prev => {
// Check if the lesson already exists in the array
const exists = prev.some(lesson => lesson.id === parsedLesson.id);
if (!exists) {
return [...prev, parsedLesson];
}
return prev;
});
// Only add if not already in newLessons array
if (!newLessons.some(lesson => lesson.id === parsedLesson.id)) {
newLessons.push(parsedLesson);
}
}
setLessons(newLessons);
} catch (error) {
console.error('Error fetching event:', error);
console.error('Error fetching events:', error);
}
};
lessonIds.forEach(lessonId => fetchLesson(lessonId));
fetchLessons();
}
}, [lessonIds, ndk, fetchAuthor, pubkey]);