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 showCompletedTag
}) { }) {
useEffect(() => {
console.log('menuItems', menuItems);
}, [menuItems]);
return ( return (
<> <>
{/* Header with course image, title and options */} {/* Header with course image, title and options */}

View File

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