'use client'; import { useEffect, useState } from 'react'; import Link from 'next/link'; import { ArrowRight, Info } from 'lucide-react'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from '@/components/ui/tooltip'; import type { SearchResult } from '@/app/api/search/route'; interface RelatedArticlesProps { title: string; currentSlug: string; } export function RelatedArticles({ title, currentSlug }: RelatedArticlesProps) { const [articles, setArticles] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchRelatedArticles = async () => { try { const response = await fetch(`/api/search?query=${encodeURIComponent(title)}`); if (!response.ok) throw new Error('Failed to fetch related articles'); const data = await response.json(); const results = data.results || []; // Filter out the current article and take top 5 const filtered = results .filter((article: SearchResult) => article.title.replace(/ /g, '_').toLowerCase() !== currentSlug.toLowerCase() ) .slice(0, 5); setArticles(filtered); } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); } }; fetchRelatedArticles(); }, [title, currentSlug]); if (loading) { return ( ); } if (error || articles.length === 0) { return null; } return ( ); }