2024-03-26 18:14:32 -05:00
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { Button } from "primereact/button";
|
|
|
|
import MenuTab from "@/components/menutab/MenuTab";
|
2024-08-06 14:50:32 -05:00
|
|
|
import { useCoursesQuery } from "@/hooks/nostrQueries/content/useCoursesQuery";
|
|
|
|
import { useResourcesQuery } from "@/hooks/nostrQueries/content/useResourcesQuery";
|
|
|
|
import { useWorkshopsQuery } from "@/hooks/nostrQueries/content/useWorkshopsQuery";
|
|
|
|
import { useDraftsQuery } from "@/hooks/apiQueries/useDraftsQuery";
|
2024-08-18 17:50:54 -05:00
|
|
|
import { useCourseDraftsQuery } from "@/hooks/apiQueries/useCourseDraftsQuery";
|
2024-08-06 14:50:32 -05:00
|
|
|
import { useContentIdsQuery } from "@/hooks/apiQueries/useContentIdsQuery";
|
2024-08-07 16:02:13 -05:00
|
|
|
import { useSession } from "next-auth/react";
|
2024-08-06 14:50:32 -05:00
|
|
|
import { useToast } from "@/hooks/useToast";
|
2024-03-26 18:14:32 -05:00
|
|
|
import ContentList from "@/components/content/lists/ContentList";
|
2024-03-26 18:54:46 -05:00
|
|
|
import { parseEvent } from "@/utils/nostr";
|
2024-08-06 14:50:32 -05:00
|
|
|
import { useNDKContext } from "@/context/NDKContext";
|
|
|
|
|
|
|
|
const AUTHOR_PUBKEY = process.env.NEXT_PUBLIC_AUTHOR_PUBKEY;
|
2024-03-26 18:14:32 -05:00
|
|
|
|
|
|
|
const UserContent = () => {
|
|
|
|
const [activeIndex, setActiveIndex] = useState(0);
|
2024-08-06 14:50:32 -05:00
|
|
|
const [isClient, setIsClient] = useState(false);
|
|
|
|
const [content, setContent] = useState([]);
|
|
|
|
const [publishedContent, setPublishedContent] = useState([]);
|
|
|
|
|
2024-08-07 16:02:13 -05:00
|
|
|
const { data: session, status } = useSession();
|
|
|
|
const [user, setUser] = useState(null);
|
2024-03-26 18:14:32 -05:00
|
|
|
const router = useRouter();
|
2024-03-26 18:54:46 -05:00
|
|
|
const { showToast } = useToast();
|
2024-08-13 16:28:25 -05:00
|
|
|
const {ndk, addSigner} = useNDKContext();
|
2024-08-06 14:50:32 -05:00
|
|
|
const { courses, coursesLoading, coursesError } = useCoursesQuery();
|
|
|
|
const { resources, resourcesLoading, resourcesError } = useResourcesQuery();
|
|
|
|
const { workshops, workshopsLoading, workshopsError } = useWorkshopsQuery();
|
2024-08-18 17:50:54 -05:00
|
|
|
const { courseDrafts, courseDraftsLoading, courseDraftsError } = useCourseDraftsQuery();
|
2024-08-06 14:50:32 -05:00
|
|
|
const { drafts, draftsLoading, draftsError } = useDraftsQuery();
|
|
|
|
const { contentIds, contentIdsLoading, contentIdsError, refetchContentIds } = useContentIdsQuery();
|
|
|
|
|
2024-08-07 16:02:13 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (session) {
|
|
|
|
setUser(session.user);
|
|
|
|
}
|
|
|
|
}, [session]);
|
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
useEffect(() => {
|
|
|
|
setIsClient(true);
|
|
|
|
}, []);
|
2024-03-26 18:14:32 -05:00
|
|
|
|
2024-03-26 18:54:46 -05:00
|
|
|
const contentItems = [
|
2024-08-06 14:50:32 -05:00
|
|
|
{ label: "Published", icon: "pi pi-verified" },
|
|
|
|
{ label: "Drafts", icon: "pi pi-file-edit" },
|
2024-08-18 17:50:54 -05:00
|
|
|
{ label: "Draft Courses", icon: "pi pi-book" },
|
|
|
|
{ label: "Resources", icon: "pi pi-file" },
|
2024-08-06 14:50:32 -05:00
|
|
|
{ label: "Workshops", icon: "pi pi-video" },
|
|
|
|
{ label: "Courses", icon: "pi pi-desktop" },
|
2024-03-26 18:14:32 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-08-06 14:50:32 -05:00
|
|
|
const fetchAllContentFromNDK = async (ids) => {
|
|
|
|
try {
|
|
|
|
await ndk.connect();
|
|
|
|
const filter = { "#d": ids, authors: [AUTHOR_PUBKEY] };
|
2024-03-26 18:14:32 -05:00
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
const uniqueEvents = new Set();
|
2024-03-26 18:54:46 -05:00
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
const events = await ndk.fetchEvents(filter);
|
|
|
|
|
|
|
|
events.forEach(event => {
|
|
|
|
uniqueEvents.add(event);
|
|
|
|
});
|
2024-03-26 18:54:46 -05:00
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
console.log('uniqueEvents', uniqueEvents)
|
|
|
|
return Array.from(uniqueEvents);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching workshops from NDK:', error);
|
|
|
|
return [];
|
2024-04-23 18:52:55 -05:00
|
|
|
}
|
2024-08-06 14:50:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const fetchContent = async () => {
|
|
|
|
if (contentIds && isClient) {
|
|
|
|
const content = await fetchAllContentFromNDK(contentIds);
|
|
|
|
setPublishedContent(content);
|
2024-04-23 18:52:55 -05:00
|
|
|
}
|
2024-03-26 18:54:46 -05:00
|
|
|
}
|
2024-08-06 14:50:32 -05:00
|
|
|
fetchContent();
|
|
|
|
}, [contentIds, isClient, ndk]);
|
2024-03-26 18:54:46 -05:00
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (isClient) {
|
|
|
|
const getContentByIndex = (index) => {
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
return publishedContent.map(parseEvent) || [];
|
|
|
|
case 1:
|
|
|
|
return drafts || [];
|
|
|
|
case 2:
|
2024-08-18 17:50:54 -05:00
|
|
|
return courseDrafts || [];
|
|
|
|
case 3:
|
2024-08-06 14:50:32 -05:00
|
|
|
return resources?.map(parseEvent) || [];
|
|
|
|
case 3:
|
|
|
|
return workshops?.map(parseEvent) || [];
|
|
|
|
case 4:
|
|
|
|
return courses?.map(parseEvent) || [];
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
setContent(getContentByIndex(activeIndex));
|
2024-03-26 18:54:46 -05:00
|
|
|
}
|
2024-08-18 17:50:54 -05:00
|
|
|
}, [activeIndex, isClient, drafts, resources, workshops, courses, publishedContent, courseDrafts])
|
2024-08-06 14:50:32 -05:00
|
|
|
|
2024-08-18 17:50:54 -05:00
|
|
|
const isLoading = coursesLoading || resourcesLoading || workshopsLoading || draftsLoading || contentIdsLoading || courseDraftsLoading;
|
|
|
|
const isError = coursesError || resourcesError || workshopsError || draftsError || contentIdsError || courseDraftsError;
|
2024-03-26 18:54:46 -05:00
|
|
|
|
2024-03-26 18:14:32 -05:00
|
|
|
return (
|
|
|
|
<div className="w-[90vw] mx-auto max-tab:w-[100vw] max-mob:w-[100vw]">
|
|
|
|
<div className="border-y-2 border-gray-300 mt-12">
|
|
|
|
<h2 className="text-center my-4">Your Content</h2>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-row w-full justify-between px-4">
|
2024-08-06 14:50:32 -05:00
|
|
|
<MenuTab
|
|
|
|
items={contentItems}
|
|
|
|
activeIndex={activeIndex}
|
|
|
|
onTabChange={setActiveIndex}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
onClick={() => router.push("/create")}
|
|
|
|
label="Create"
|
|
|
|
severity="success"
|
|
|
|
outlined
|
|
|
|
className="mt-2"
|
|
|
|
/>
|
2024-03-26 18:14:32 -05:00
|
|
|
</div>
|
|
|
|
<div className="w-full mx-auto my-8">
|
2024-03-26 18:54:46 -05:00
|
|
|
<div className="w-full mx-auto my-8">
|
2024-08-06 14:50:32 -05:00
|
|
|
{isLoading ? (
|
|
|
|
<p>Loading...</p>
|
|
|
|
) : isError ? (
|
|
|
|
<p>Error loading content.</p>
|
|
|
|
) : content.length > 0 ? (
|
|
|
|
<ContentList content={content} />
|
|
|
|
) : (
|
|
|
|
<p>No content available.</p>
|
2024-03-26 18:54:46 -05:00
|
|
|
)}
|
|
|
|
</div>
|
2024-03-26 18:14:32 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-26 18:54:46 -05:00
|
|
|
);
|
|
|
|
};
|
2024-03-26 18:14:32 -05:00
|
|
|
|
|
|
|
export default UserContent;
|