| // src/components/article/Recommendations.tsx | |
| "use client"; | |
| import { useState, useEffect, useCallback, useMemo } from "react"; | |
| import { Link } from "react-router-dom"; | |
| import api from "@/lib/api"; | |
| import { Article } from "@/types"; | |
| import { Sparkles, Loader2, AlertCircle, RefreshCw } from "lucide-react"; | |
| interface RecommendationsProps { | |
| articleId: number; | |
| maxItems?: number; | |
| } | |
| const RECOMMENDATION_TYPES = [ | |
| { value: "cocitation", label: "Co‑citation", description: "Articles cited together" }, | |
| { value: "bibliographic", label: "Bibliographic coupling", description: "Share references" }, | |
| { value: "pagerank", label: "PageRank", description: "Influential articles" }, | |
| { value: "similar_abstract", label: "Similar abstract", description: "Keyword‑based" }, | |
| { value: "same_author", label: "Same author", description: "Other works by this author" }, | |
| ]; | |
| // Skeleton loader component | |
| const SkeletonItem = () => ( | |
| <div className="flex items-start gap-3 p-3 animate-pulse"> | |
| <div className="w-5 h-5 bg-gray-200 dark:bg-gray-700 rounded-full shrink-0" /> | |
| <div className="flex-1"> | |
| <div className="h-4 w-3/4 bg-gray-200 dark:bg-gray-700 rounded mb-2" /> | |
| <div className="h-3 w-1/2 bg-gray-200 dark:bg-gray-700 rounded" /> | |
| </div> | |
| </div> | |
| ); | |
| export function Recommendations({ articleId, maxItems = 5 }: RecommendationsProps) { | |
| const [recType, setRecType] = useState("cocitation"); | |
| const [recommendations, setRecommendations] = useState<Article[]>([]); | |
| const [loading, setLoading] = useState(true); | |
| const [error, setError] = useState(""); | |
| const [retryCount, setRetryCount] = useState(0); | |
| const fetchRecommendations = useCallback(async () => { | |
| if (!articleId) return; | |
| setLoading(true); | |
| setError(""); | |
| try { | |
| const res = await api.get( | |
| `/articles/${articleId}/graph-recommendations?type=${recType}&limit=${maxItems}` | |
| ); | |
| const data = Array.isArray(res.data) | |
| ? res.data | |
| : res.data?.recommendations || res.data?.result || []; | |
| setRecommendations(data); | |
| } catch (err) { | |
| console.error("Failed to fetch recommendations", err); | |
| setError("Could not load recommendations. Please try again."); | |
| setRecommendations([]); | |
| } finally { | |
| setLoading(false); | |
| } | |
| }, [articleId, recType, maxItems]); | |
| useEffect(() => { | |
| fetchRecommendations(); | |
| }, [fetchRecommendations, retryCount]); | |
| const handleRetry = () => { | |
| setRetryCount(prev => prev + 1); | |
| }; | |
| const currentTypeLabel = useMemo( | |
| () => RECOMMENDATION_TYPES.find(t => t.value === recType)?.label || recType, | |
| [recType] | |
| ); | |
| return ( | |
| <div className="mt-8 bg-white dark:bg-gray-800 rounded-2xl shadow-md p-6 border border-gray-100 dark:border-gray-700"> | |
| <div className="flex flex-wrap items-center justify-between gap-3 mb-4"> | |
| <h3 className="text-lg font-semibold flex items-center gap-2 text-gray-800 dark:text-gray-200"> | |
| <Sparkles className="w-5 h-5 text-primary" /> | |
| You might also like | |
| </h3> | |
| <div className="relative"> | |
| <select | |
| value={recType} | |
| onChange={(e) => setRecType(e.target.value)} | |
| className="px-4 py-2 pr-8 rounded-full border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800 text-sm focus:outline-none focus:ring-2 focus:ring-primary cursor-pointer" | |
| aria-label="Recommendation type" | |
| > | |
| {RECOMMENDATION_TYPES.map((type) => ( | |
| <option key={type.value} value={type.value}> | |
| {type.label} | |
| </option> | |
| ))} | |
| </select> | |
| <div className="absolute right-2 top-1/2 -translate-y-1/2 text-xs text-gray-400"> | |
| ▼ | |
| </div> | |
| </div> | |
| </div> | |
| {loading ? ( | |
| <div className="space-y-2"> | |
| {[...Array(3)].map((_, i) => ( | |
| <SkeletonItem key={i} /> | |
| ))} | |
| </div> | |
| ) : error ? ( | |
| <div className="text-center py-6"> | |
| <div className="flex items-center justify-center gap-2 text-red-500 dark:text-red-400 text-sm mb-2"> | |
| <AlertCircle className="w-4 h-4" /> | |
| {error} | |
| </div> | |
| <button | |
| onClick={handleRetry} | |
| className="inline-flex items-center gap-1 text-primary hover:underline text-sm" | |
| > | |
| <RefreshCw className="w-3 h-3" /> Try again | |
| </button> | |
| </div> | |
| ) : recommendations.length === 0 ? ( | |
| <div className="text-center py-6 text-gray-500 dark:text-gray-400 text-sm"> | |
| No recommendations found for “{currentTypeLabel}”. | |
| </div> | |
| ) : ( | |
| <div className="space-y-1"> | |
| {recommendations.map((rec) => ( | |
| <Link | |
| key={rec.id} | |
| to={`/articles/${rec.id}`} | |
| className="flex items-start gap-3 p-3 rounded-xl hover:bg-gray-50 dark:hover:bg-gray-700/50 transition group" | |
| > | |
| <Sparkles className="w-5 h-5 text-primary mt-0.5 shrink-0 opacity-40 group-hover:opacity-100 transition" /> | |
| <div className="flex-1 min-w-0"> | |
| <div className="font-medium text-gray-800 dark:text-gray-200 group-hover:text-primary transition line-clamp-1"> | |
| {rec.title} | |
| </div> | |
| <div className="text-xs text-gray-500 dark:text-gray-400 mt-0.5"> | |
| by {rec.authorUsername} • {rec.views || 0} views • {rec.likesCount || 0} likes | |
| </div> | |
| {rec.abstract && ( | |
| <p className="text-xs text-gray-400 dark:text-gray-500 line-clamp-1 mt-1"> | |
| {rec.abstract.substring(0, 100)}… | |
| </p> | |
| )} | |
| </div> | |
| </Link> | |
| ))} | |
| {recommendations.length === maxItems && ( | |
| <div className="text-center pt-2"> | |
| <Link | |
| to={`/articles?relatedTo=${articleId}&type=${recType}`} | |
| className="text-xs text-primary hover:underline" | |
| > | |
| More like this → | |
| </Link> | |
| </div> | |
| )} | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |
Xet Storage Details
- Size:
- 6.22 kB
- Xet hash:
- d673845401eb263302775709541e93b3443bffe6b407e63b9ed82af1e61d6b1a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.