2024-09-05 18:13:17 -05:00
|
|
|
import React, { useEffect, useState, useMemo } from 'react';
|
2024-08-27 17:59:45 -05:00
|
|
|
import GenericCarousel from '@/components/content/carousels/GenericCarousel';
|
|
|
|
import { parseEvent, parseCourseEvent } from '@/utils/nostr';
|
|
|
|
import { useResources } from '@/hooks/nostr/useResources';
|
|
|
|
import { useWorkshops } from '@/hooks/nostr/useWorkshops';
|
|
|
|
import { useCourses } from '@/hooks/nostr/useCourses';
|
|
|
|
import { TabMenu } from 'primereact/tabmenu';
|
|
|
|
import 'primeicons/primeicons.css';
|
|
|
|
import { InputText } from 'primereact/inputtext';
|
2024-09-10 15:44:08 -05:00
|
|
|
import GenericButton from '@/components/buttons/GenericButton';
|
2024-09-05 18:13:17 -05:00
|
|
|
import { useRouter } from 'next/router';
|
2024-08-27 17:59:45 -05:00
|
|
|
|
2024-08-27 20:58:19 -05:00
|
|
|
const MenuTab = ({ items, selectedTopic, onTabChange }) => {
|
2024-09-05 18:13:17 -05:00
|
|
|
const router = useRouter();
|
2024-08-27 20:58:19 -05:00
|
|
|
const allItems = ['All', ...items];
|
|
|
|
|
|
|
|
const menuItems = allItems.map((item, index) => {
|
|
|
|
let icon = 'pi pi-tag';
|
|
|
|
if (item === 'All') icon = 'pi pi-eye';
|
2024-09-05 18:13:17 -05:00
|
|
|
else if (item === 'Resources') icon = 'pi pi-file';
|
|
|
|
else if (item === 'Workshops') icon = 'pi pi-video';
|
|
|
|
else if (item === 'Courses') icon = 'pi pi-desktop';
|
|
|
|
|
2024-09-06 13:11:00 -05:00
|
|
|
const queryParam = item === 'all' ? '' : `?tag=${item.toLowerCase()}`;
|
2024-09-05 18:13:17 -05:00
|
|
|
const isActive = router.asPath === `/content${queryParam}`;
|
2024-08-27 20:58:19 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
label: (
|
2024-09-10 15:44:08 -05:00
|
|
|
<GenericButton
|
2024-09-05 18:13:17 -05:00
|
|
|
className={`${isActive ? 'bg-primary text-white' : ''}`}
|
|
|
|
onClick={() => {
|
|
|
|
onTabChange(item);
|
|
|
|
router.push(`/content${queryParam}`);
|
|
|
|
}}
|
|
|
|
outlined={!isActive}
|
2024-08-27 20:58:19 -05:00
|
|
|
rounded
|
|
|
|
size='small'
|
|
|
|
label={item}
|
|
|
|
icon={icon}
|
|
|
|
/>
|
|
|
|
),
|
2024-09-05 18:13:17 -05:00
|
|
|
command: () => {
|
|
|
|
onTabChange(item);
|
|
|
|
router.push(`/content${queryParam}`);
|
|
|
|
}
|
2024-08-27 20:58:19 -05:00
|
|
|
};
|
|
|
|
});
|
2024-08-27 17:59:45 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="w-full">
|
|
|
|
<TabMenu
|
|
|
|
model={menuItems}
|
2024-08-27 20:58:19 -05:00
|
|
|
activeIndex={allItems.indexOf(selectedTopic)}
|
|
|
|
onTabChange={(e) => onTabChange(allItems[e.index])}
|
2024-08-27 17:59:45 -05:00
|
|
|
pt={{
|
2024-08-27 20:58:19 -05:00
|
|
|
menu: { className: 'bg-transparent border-none ml-2 my-4' },
|
|
|
|
action: ({ context, parent }) => ({
|
|
|
|
className: 'cursor-pointer select-none flex items-center relative no-underline overflow-hidden border-b-2 p-2 font-bold rounded-t-lg',
|
|
|
|
style: { top: '2px' }
|
|
|
|
}),
|
|
|
|
menuitem: { className: 'mr-0' }
|
2024-08-27 17:59:45 -05:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContentPage = () => {
|
2024-09-05 18:13:17 -05:00
|
|
|
const router = useRouter();
|
2024-08-27 17:59:45 -05:00
|
|
|
const { resources, resourcesLoading } = useResources();
|
|
|
|
const { workshops, workshopsLoading } = useWorkshops();
|
|
|
|
const { courses, coursesLoading } = useCourses();
|
|
|
|
|
|
|
|
const [processedResources, setProcessedResources] = useState([]);
|
|
|
|
const [processedWorkshops, setProcessedWorkshops] = useState([]);
|
|
|
|
const [processedCourses, setProcessedCourses] = useState([]);
|
|
|
|
const [allContent, setAllContent] = useState([]);
|
|
|
|
const [allTopics, setAllTopics] = useState([]);
|
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
2024-09-05 18:13:17 -05:00
|
|
|
const [selectedTopic, setSelectedTopic] = useState('All')
|
2024-08-27 20:58:19 -05:00
|
|
|
const [filteredContent, setFilteredContent] = useState([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-09-05 18:13:17 -05:00
|
|
|
const tag = router.query.tag;
|
|
|
|
if (tag) {
|
|
|
|
const topic = tag.charAt(0).toUpperCase() + tag.slice(1);
|
|
|
|
setSelectedTopic(topic);
|
|
|
|
} else {
|
|
|
|
setSelectedTopic('All');
|
|
|
|
}
|
|
|
|
}, [router.query.tag]);
|
2024-08-27 17:59:45 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (resources && !resourcesLoading) {
|
2024-09-05 18:13:17 -05:00
|
|
|
const processedResources = resources.map(resource => ({...parseEvent(resource), type: 'resource'}));
|
2024-08-27 17:59:45 -05:00
|
|
|
setProcessedResources(processedResources);
|
|
|
|
}
|
|
|
|
}, [resources, resourcesLoading]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (workshops && !workshopsLoading) {
|
2024-09-05 18:13:17 -05:00
|
|
|
const processedWorkshops = workshops.map(workshop => ({...parseEvent(workshop), type: 'workshop'}));
|
2024-08-27 17:59:45 -05:00
|
|
|
setProcessedWorkshops(processedWorkshops);
|
|
|
|
}
|
|
|
|
}, [workshops, workshopsLoading]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (courses && !coursesLoading) {
|
2024-09-05 18:13:17 -05:00
|
|
|
const processedCourses = courses.map(course => ({...parseCourseEvent(course), type: 'course'}));
|
2024-08-27 17:59:45 -05:00
|
|
|
setProcessedCourses(processedCourses);
|
|
|
|
}
|
|
|
|
}, [courses, coursesLoading]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-09-05 18:13:17 -05:00
|
|
|
const allContent = [...processedResources, ...processedWorkshops, ...processedCourses];
|
|
|
|
setAllContent(allContent);
|
|
|
|
|
|
|
|
const uniqueTopics = new Set(allContent.map(item => item.topics).flat());
|
|
|
|
const priorityItems = ['All', 'Courses', 'Workshops', 'Resources'];
|
2024-08-27 20:58:19 -05:00
|
|
|
const otherTopics = Array.from(uniqueTopics).filter(topic => !priorityItems.includes(topic));
|
2024-09-05 18:13:17 -05:00
|
|
|
const combinedTopics = [...priorityItems.slice(1), ...otherTopics];
|
|
|
|
setAllTopics(combinedTopics);
|
|
|
|
|
|
|
|
if (selectedTopic) {
|
|
|
|
filterContent(selectedTopic, allContent);
|
|
|
|
}
|
2024-08-27 17:59:45 -05:00
|
|
|
}, [processedResources, processedWorkshops, processedCourses]);
|
|
|
|
|
2024-09-05 18:13:17 -05:00
|
|
|
const filterContent = (topic, content) => {
|
|
|
|
let filtered = content;
|
|
|
|
if (topic !== 'All') {
|
|
|
|
const topicLower = topic.toLowerCase();
|
|
|
|
if (['courses', 'workshops', 'resources'].includes(topicLower)) {
|
|
|
|
filtered = content.filter(item => item.type === topicLower.slice(0, -1));
|
2024-08-27 20:58:19 -05:00
|
|
|
} else {
|
2024-09-05 18:13:17 -05:00
|
|
|
filtered = content.filter(item => item.topics && item.topics.includes(topic.toLowerCase()));
|
2024-08-27 20:58:19 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-05 18:13:17 -05:00
|
|
|
|
2024-08-27 20:58:19 -05:00
|
|
|
setFilteredContent(filtered);
|
2024-09-05 18:13:17 -05:00
|
|
|
};
|
2024-08-27 20:58:19 -05:00
|
|
|
|
|
|
|
const handleTopicChange = (newTopic) => {
|
|
|
|
setSelectedTopic(newTopic);
|
2024-09-05 18:13:17 -05:00
|
|
|
const queryParam = newTopic === 'All' ? '' : `?tag=${newTopic.toLowerCase()}`;
|
|
|
|
router.push(`/content${queryParam}`, undefined, { shallow: true });
|
|
|
|
filterContent(newTopic, allContent);
|
2024-08-27 20:58:19 -05:00
|
|
|
};
|
2024-08-27 17:59:45 -05:00
|
|
|
|
|
|
|
const renderCarousels = () => {
|
2024-08-27 20:58:19 -05:00
|
|
|
return (
|
|
|
|
<GenericCarousel
|
2024-09-05 18:13:17 -05:00
|
|
|
key={selectedTopic} // Add this line
|
2024-08-27 20:58:19 -05:00
|
|
|
items={filteredContent}
|
|
|
|
selectedTopic={selectedTopic}
|
|
|
|
title={`${selectedTopic} Content`}
|
|
|
|
type="all"
|
|
|
|
/>
|
|
|
|
);
|
2024-08-27 17:59:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="w-full px-4">
|
|
|
|
<div className="w-fit mx-4 mt-8 flex flex-col items-start">
|
|
|
|
<h1 className="text-3xl font-bold mb-4 ml-1">All Content</h1>
|
|
|
|
<InputText
|
|
|
|
value={searchQuery}
|
|
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
|
|
placeholder="Search"
|
|
|
|
icon="pi pi-search"
|
2024-08-27 20:58:19 -05:00
|
|
|
className="w-full"
|
2024-08-27 17:59:45 -05:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<MenuTab
|
2024-09-05 18:13:17 -05:00
|
|
|
items={['Courses', 'Workshops', 'Resources', ...allTopics.filter(topic => !['Courses', 'Workshops', 'Resources'].includes(topic))]}
|
2024-08-27 20:58:19 -05:00
|
|
|
selectedTopic={selectedTopic}
|
|
|
|
onTabChange={handleTopicChange}
|
2024-08-27 17:59:45 -05:00
|
|
|
className="max-w-[90%] mx-auto"
|
|
|
|
/>
|
|
|
|
{renderCarousels()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ContentPage;
|