Reset relays again

This commit is contained in:
austinkelsay 2024-10-16 19:59:07 -05:00
parent 0a39f4f1c3
commit e40ec068e7
4 changed files with 31 additions and 20 deletions
src
components/content/carousels
config
context

@ -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 <div>Error: {coursesError.message}</div>
}
@ -73,7 +83,7 @@ export default function CoursesCarousel() {
itemTemplate={(item) =>
!processedCourses.length ?
<TemplateSkeleton key={Math.random()} /> :
<CourseTemplate key={item.id} course={item} />
<CourseTemplate key={item.id} course={item} onZapAmountChange={handleZapAmountChange} />
}
responsiveOptions={responsiveOptions} />
</div>

@ -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) {

@ -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"],

@ -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])];