+
@@ -49,8 +32,17 @@ export const ProjectBlogArticles = ({
{[1, 2, 3].map((i) => (
-
-
+
@@ -65,7 +57,11 @@ export const ProjectBlogArticles = ({
}
return (
-
+
+
))}
+
+
+
+
+
+
+
+
+ )
+}
+
export const WikiSideNavigation = ({
className,
lang = "en",
@@ -32,7 +60,10 @@ export const WikiSideNavigation = ({
const [activeSection, setActiveSection] = useState(null)
const observerRef = useRef(null)
- // Extract sections from content
+ const { articles, loading } = useGetProjectRelatedArticles({
+ projectId: project.id,
+ })
+
useEffect(() => {
if (!content) return
const sectionsRegex = /^(#{1,3})\s(.+)/gm
@@ -51,7 +82,6 @@ export const WikiSideNavigation = ({
}
setSections(extractedSections)
- // Set the first section as active by default
if (extractedSections.length > 0) {
setActiveSection(extractedSections[0].id)
}
@@ -85,7 +115,7 @@ export const WikiSideNavigation = ({
observerRef.current.disconnect()
}
}
- }, [sections])
+ }, [sections, loading])
const scrollToSection = (sectionId: string) => {
const element = document.querySelector(`[data-section-id="${sectionId}"]`)
@@ -128,6 +158,8 @@ export const WikiSideNavigation = ({
const { extraLinks = {} } = project
+ const hasRelatedArticles = articles.length > 0 && !loading
+
if (sections.length === 0 || content.length === 0) return null
return (
@@ -138,23 +170,13 @@ export const WikiSideNavigation = ({
diff --git a/hooks/useGetProjectRelatedArticles.ts b/hooks/useGetProjectRelatedArticles.ts
new file mode 100644
index 0000000..62a50d0
--- /dev/null
+++ b/hooks/useGetProjectRelatedArticles.ts
@@ -0,0 +1,39 @@
+import { useState, useEffect } from "react"
+import { Article } from "@/lib/blog"
+
+interface UseGetProjectRelatedArticlesProps {
+ projectId: string
+}
+
+async function fetchArticles(project: string) {
+ const response = await fetch(`/api/articles?project=${project}`)
+ const data = await response.json()
+ return data.articles
+}
+
+export function useGetProjectRelatedArticles({
+ projectId,
+}: UseGetProjectRelatedArticlesProps) {
+ const [articles, setArticles] = useState([])
+ const [loading, setLoading] = useState(true)
+
+ useEffect(() => {
+ const getArticles = async () => {
+ try {
+ const data = await fetchArticles(projectId)
+ setArticles(data)
+ } catch (error) {
+ console.error("Error fetching articles:", error)
+ } finally {
+ setLoading(false)
+ }
+ }
+
+ getArticles()
+ }, [projectId])
+
+ return {
+ articles,
+ loading,
+ }
+}
Related articles diff --git a/components/wiki-side-navigation.tsx b/components/wiki-side-navigation.tsx index cbee494..2a9689a 100644 --- a/components/wiki-side-navigation.tsx +++ b/components/wiki-side-navigation.tsx @@ -7,6 +7,7 @@ import { cn } from "@/lib/utils" import { useTranslation } from "@/app/i18n/client" import { Icons } from "./icons" +import { useGetProjectRelatedArticles } from "@/hooks/useGetProjectRelatedArticles" interface Section { level: number @@ -21,6 +22,33 @@ interface WikiSideNavigationProps { project?: any } +const SideNavigationItem = ({ + text, + id, + activeSection, + onClick, +}: { + text: string + id: string + activeSection: string | null + onClick: () => void +}) => { + return ( +
-
{sections.map((section, index) => (
-
- - - + activeSection={activeSection} + text={section.text} + id={section.id} + onClick={() => scrollToSection(section.id)} + /> ))} {Object.entries(ExtraLinkLabelMapping).map(([key]) => { const links = extraLinks[key as ProjectExtraLinkType] ?? [] @@ -162,34 +184,31 @@ export const WikiSideNavigation = ({ const { label } = ExtraLinkLabelMapping?.[key as any] ?? {} if (!links.length) return null // no links hide the section return ( -
- scrollToSection(key)} - className={cn( - "flex h-8 items-center border-l-2 border-l-anakiwa-200 px-3 duration-200 cursor-pointer", - { - "border-l-anakiwa-500 text-anakiwa-500 font-medium": - activeSection === key, - } - )} - > - {label} - + activeSection={activeSection} + text={label} + id={key} + /> ) })} -
- scrollToSection("related-articles")}
+ activeSection={activeSection}
+ text="Related articles"
+ id="related-articles"
+ />
+ )}
+
scrollToSection("edit-this-page")} - className={cn( - "flex h-8 items-center border-l-2 border-l-anakiwa-200 px-3 duration-200 cursor-pointer", - { - "border-l-anakiwa-500 text-anakiwa-500 font-medium": - activeSection === "edit-this-page", - } - )} - > - Edit this page -
+ activeSection={activeSection}
+ text="Edit this page"
+ id="edit-this-page"
+ />