From e40ec068e77c214368cd0635e71be044fb04244b Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Wed, 16 Oct 2024 19:59:07 -0500 Subject: [PATCH] Reset relays again --- .../content/carousels/CoursesCarousel.js | 24 +++++++++++++------ .../carousels/templates/CourseTemplate.js | 5 ++-- src/config/appConfig.js | 18 +++++++------- src/context/NDKContext.js | 4 ++-- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/components/content/carousels/CoursesCarousel.js b/src/components/content/carousels/CoursesCarousel.js index f7b09ce..ae4b2c7 100644 --- a/src/components/content/carousels/CoursesCarousel.js +++ b/src/components/content/carousels/CoursesCarousel.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect, use } from 'react'; +import React, { useState, useEffect, useCallback } from 'react'; import { Carousel } from 'primereact/carousel'; import { parseCourseEvent } from '@/utils/nostr'; import { CourseTemplate } from '@/components/content/carousels/templates/CourseTemplate'; @@ -26,20 +26,21 @@ const responsiveOptions = [ export default function CoursesCarousel() { const [processedCourses, setProcessedCourses] = useState([]); + const [zapAmounts, setZapAmounts] = useState({}); const { courses, coursesLoading, coursesError } = useCourses() const windowWidth = useWindowWidth(); const isMobileView = windowWidth <= 450; + const handleZapAmountChange = useCallback((courseId, amount) => { + setZapAmounts(prev => ({ ...prev, [courseId]: amount })); + }, []); + useEffect(() => { const fetch = async () => { try { if (courses && courses.length > 0) { const processedCourses = courses.map(course => parseCourseEvent(course)); - - // Sort courses by created_at in descending order (most recent first) - const sortedCourses = processedCourses.sort((a, b) => b.created_at - a.created_at); - - setProcessedCourses(sortedCourses); + setProcessedCourses(processedCourses); } else { console.log('No courses fetched or empty array returned'); } @@ -50,6 +51,15 @@ export default function CoursesCarousel() { fetch(); }, [courses]); + useEffect(() => { + if (Object.keys(zapAmounts).length === processedCourses.length) { + const sortedCourses = [...processedCourses].sort((a, b) => + (zapAmounts[b.id] || 0) - (zapAmounts[a.id] || 0) + ); + setProcessedCourses(sortedCourses); + } + }, [zapAmounts, processedCourses]); + if (coursesError) { return
Error: {coursesError.message}
} @@ -73,7 +83,7 @@ export default function CoursesCarousel() { itemTemplate={(item) => !processedCourses.length ? : - + } responsiveOptions={responsiveOptions} /> diff --git a/src/components/content/carousels/templates/CourseTemplate.js b/src/components/content/carousels/templates/CourseTemplate.js index 6acfcf6..3660a7d 100644 --- a/src/components/content/carousels/templates/CourseTemplate.js +++ b/src/components/content/carousels/templates/CourseTemplate.js @@ -15,7 +15,7 @@ import useWindowWidth from "@/hooks/useWindowWidth"; import GenericButton from "@/components/buttons/GenericButton"; import appConfig from "@/config/appConfig"; -export function CourseTemplate({ course }) { +export function CourseTemplate({ course, onZapAmountChange }) { const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: course }); const [zapAmount, setZapAmount] = useState(0); const [lessonCount, setLessonCount] = useState(0); @@ -29,8 +29,9 @@ export function CourseTemplate({ course }) { if (zaps.length > 0) { const total = getTotalFromZaps(zaps, course); setZapAmount(total); + onZapAmountChange(course.id, total); } - }, [zaps, course]); + }, [zaps, course, onZapAmountChange]); useEffect(() => { if (course && course?.tags) { diff --git a/src/config/appConfig.js b/src/config/appConfig.js index 07c6d2c..9b0f49a 100644 --- a/src/config/appConfig.js +++ b/src/config/appConfig.js @@ -1,14 +1,14 @@ const appConfig = { defaultRelayUrls: [ - // "wss://nos.lol/", - // "wss://relay.damus.io/", - // "wss://relay.snort.social/", - // "wss://relay.nostr.band/", - // "wss://relay.mutinywallet.com/", - // "wss://relay.primal.net/", - // "wss://nostr21.com/", - // "wss://nostrue.com/", - // "wss://purplerelay.com/", + "wss://nos.lol/", + "wss://relay.damus.io/", + "wss://relay.snort.social/", + "wss://relay.nostr.band/", + "wss://relay.mutinywallet.com/", + "wss://relay.primal.net/", + "wss://nostr21.com/", + "wss://nostrue.com/", + "wss://purplerelay.com/", "wss://relay.devs.tools/" ], authorPubkeys: ["8cb60e215678879cda0bef4d5b3fc1a5c5925d2adb5d8c4fa7b7d03b5f2deaea", "676c02247668d5b18479be3d1a80933044256f3fbd03640a8c234684e641b6d6", "f33c8a9617cb15f705fc70cd461cfd6eaf22f9e24c33eabad981648e5ec6f741"], diff --git a/src/context/NDKContext.js b/src/context/NDKContext.js index 6dbe847..df41d5c 100644 --- a/src/context/NDKContext.js +++ b/src/context/NDKContext.js @@ -11,8 +11,8 @@ const readOnlyRelays = ["wss://nostr21.com/", "wss://nostr.wine/", "wss://yestr. export const NDKProvider = ({ children }) => { const [ndk, setNdk] = useState(null); // todo: remove this after testing phase - // const [userRelays, setUserRelays] = useLocalStorage("userRelays", appConfig.defaultRelayUrls); - const [userRelays, setUserRelays] = useState([...readOnlyRelays, "wss://relay.devs.tools"]); + const [userRelays, setUserRelays] = useLocalStorage("userRelays", appConfig.defaultRelayUrls); + // const [userRelays, setUserRelays] = useState([...readOnlyRelays, "wss://relay.devs.tools"]); const createNDKInstance = (relays) => { const allRelays = [...new Set([...appConfig.defaultRelayUrls, ...relays])];