|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState, useCallback } from "react"; |
| 4 | +import { Badge } from "@/components/ui/badge"; |
| 5 | +import { Lightbulb, FileVideo, Handshake, Clock } from "lucide-react"; |
| 6 | + |
| 7 | +interface ActivityItem { |
| 8 | + _id: string; |
| 9 | + _type: string; |
| 10 | + _updatedAt: string; |
| 11 | + title?: string; |
| 12 | + companyName?: string; |
| 13 | + status?: string; |
| 14 | +} |
| 15 | + |
| 16 | +const typeConfig: Record< |
| 17 | + string, |
| 18 | + { icon: typeof Lightbulb; label: string; color: string } |
| 19 | +> = { |
| 20 | + contentIdea: { |
| 21 | + icon: Lightbulb, |
| 22 | + label: "Idea", |
| 23 | + color: |
| 24 | + "bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300", |
| 25 | + }, |
| 26 | + automatedVideo: { |
| 27 | + icon: FileVideo, |
| 28 | + label: "Video", |
| 29 | + color: |
| 30 | + "bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300", |
| 31 | + }, |
| 32 | + sponsorLead: { |
| 33 | + icon: Handshake, |
| 34 | + label: "Sponsor", |
| 35 | + color: |
| 36 | + "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300", |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +function formatTimeAgo(dateString: string) { |
| 41 | + const seconds = Math.floor( |
| 42 | + (Date.now() - new Date(dateString).getTime()) / 1000, |
| 43 | + ); |
| 44 | + if (seconds < 60) return "just now"; |
| 45 | + if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`; |
| 46 | + if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`; |
| 47 | + return `${Math.floor(seconds / 86400)}d ago`; |
| 48 | +} |
| 49 | + |
| 50 | +export function RecentActivity() { |
| 51 | + const [items, setItems] = useState<ActivityItem[]>([]); |
| 52 | + |
| 53 | + const fetchActivity = useCallback(async () => { |
| 54 | + try { |
| 55 | + const res = await fetch("/api/dashboard/activity"); |
| 56 | + if (res.ok) { |
| 57 | + const data = await res.json(); |
| 58 | + setItems(data); |
| 59 | + } |
| 60 | + } catch (error) { |
| 61 | + console.error("Failed to fetch activity:", error); |
| 62 | + } |
| 63 | + }, []); |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + fetchActivity(); |
| 67 | + const interval = setInterval(fetchActivity, 30000); |
| 68 | + return () => clearInterval(interval); |
| 69 | + }, [fetchActivity]); |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className="rounded-lg border p-6"> |
| 73 | + <h2 className="text-lg font-semibold">Recent Activity</h2> |
| 74 | + {items.length === 0 ? ( |
| 75 | + <p className="mt-4 text-sm text-muted-foreground"> |
| 76 | + No activity yet \u2014 content will appear here as the pipeline |
| 77 | + runs. |
| 78 | + </p> |
| 79 | + ) : ( |
| 80 | + <div className="mt-4 space-y-3"> |
| 81 | + {items.map((item) => { |
| 82 | + const config = |
| 83 | + typeConfig[item._type] ?? typeConfig.contentIdea; |
| 84 | + const Icon = config.icon; |
| 85 | + const name = |
| 86 | + item.title || item.companyName || "Untitled"; |
| 87 | + return ( |
| 88 | + <div |
| 89 | + key={item._id} |
| 90 | + className="flex items-center gap-3" |
| 91 | + > |
| 92 | + <Icon className="h-4 w-4 shrink-0 text-muted-foreground" /> |
| 93 | + <div className="min-w-0 flex-1"> |
| 94 | + <p className="truncate text-sm font-medium"> |
| 95 | + {name} |
| 96 | + </p> |
| 97 | + <div className="flex items-center gap-2"> |
| 98 | + <Badge |
| 99 | + variant="outline" |
| 100 | + className={`text-xs ${config.color}`} |
| 101 | + > |
| 102 | + {config.label} |
| 103 | + </Badge> |
| 104 | + {item.status && ( |
| 105 | + <span className="text-xs text-muted-foreground"> |
| 106 | + {item.status} |
| 107 | + </span> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + <div className="flex shrink-0 items-center gap-1 text-xs text-muted-foreground"> |
| 112 | + <Clock className="h-3 w-3" /> |
| 113 | + {formatTimeAgo(item._updatedAt)} |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | + })} |
| 118 | + </div> |
| 119 | + )} |
| 120 | + </div> |
| 121 | + ); |
| 122 | +} |
0 commit comments