'use client' import { useQuery } from '@tanstack/react-query' import { monitorAPI } from '@/lib/api' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Sparkline } from '@/components/sparkline' interface Props { monitorId: string keywords: string[] } export function SEORankingCard({ monitorId, keywords }: Props) { const { data: rankings, isLoading } = useQuery({ queryKey: ['rankings', monitorId], queryFn: async () => { const response = await monitorAPI.rankings(monitorId) return response // { history: [], latest: [] } } }) if (isLoading) { return (
) } const { latest = [], history = [] } = rankings || {} // Group history by keyword for sparklines const historyByKeyword = (history as any[]).reduce((acc, item) => { if (!acc[item.keyword]) acc[item.keyword] = [] acc[item.keyword].push(item) return acc }, {} as Record) return (
{keywords.map(keyword => { const latestRank = latest.find((r: any) => r.keyword === keyword) const keywordHistory = historyByKeyword[keyword] || [] // Sort history by date asc for sparkline const rankHistory = keywordHistory .sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()) .map((item: any) => item.rank || 101) // Use 101 for unranked return ( {keyword} {latestRank?.rank ? ( #{latestRank.rank} ) : ( Not found )}
{latestRank?.urlFound ? ( {new URL(latestRank.urlFound).pathname} ) : ( Not in top 100 )}
{rankHistory.length > 1 && (
{/* Simple visualization if Sparkline component accepts array */}
)}
Last checked: {latestRank ? new Date(latestRank.createdAt).toLocaleDateString() : 'Never'}
) })}
) }