Files
pse.dev/hooks/useGetProjectRelatedArticles.ts
Kalidou Diagne 9c9e9c642a feat: homepage updates (#541)
* homepage updates
2025-08-11 20:23:46 +02:00

58 lines
1.5 KiB
TypeScript

import { Article } from "@/lib/content"
import { useState, useEffect } from "react"
interface UseGetProjectRelatedArticlesProps {
projectId: string
excludeIds?: string[]
partialIdMatch?: boolean
}
export async function fetchArticles(
project: string,
excludeIds: string[] = [],
partialIdMatch = false
) {
const response = await fetch(`/api/articles?project=${project}`)
const data = await response.json()
return data.articles.filter((article: Article) => {
if (partialIdMatch) {
return !excludeIds.some((excludeId) => {
const normalizedArticleId = article.id.toLowerCase().replace(/-/g, "")
const normalizedExcludeId = excludeId.toLowerCase().replace(/-/g, "")
return normalizedArticleId.includes(normalizedExcludeId)
})
}
// Exact match check
return !excludeIds.includes(article.id)
})
}
export function useGetProjectRelatedArticles({
projectId,
excludeIds = [],
partialIdMatch = false,
}: UseGetProjectRelatedArticlesProps) {
const [articles, setArticles] = useState<Article[]>([])
const [loading, setLoading] = useState(true)
useEffect(() => {
const getArticles = async () => {
try {
const data = await fetchArticles(projectId, excludeIds, partialIdMatch)
setArticles(data)
} catch (error) {
console.error("Error fetching articles:", error)
} finally {
setLoading(false)
}
}
getArticles()
}, [projectId])
return {
articles,
loading,
}
}