import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Image from 'next/image'; import type { FeedItem } from '@gridpilot/social/domain/entities/FeedItem'; import { getDriverRepository, getImageService, getSocialRepository } from '@/lib/di-container'; function timeAgo(timestamp: Date): string { const diffMs = Date.now() - timestamp.getTime(); const diffMinutes = Math.floor(diffMs / 60000); if (diffMinutes < 1) return 'Just now'; if (diffMinutes < 60) return `${diffMinutes} min ago`; const diffHours = Math.floor(diffMinutes / 60); if (diffHours < 24) return `${diffHours} h ago`; const diffDays = Math.floor(diffHours / 24); return `${diffDays} d ago`; } async function resolveActor(item: FeedItem) { const driverRepo = getDriverRepository(); const imageService = getImageService(); const socialRepo = getSocialRepository(); if (item.actorFriendId) { // Try social graph first (friend display name/avatar) try { const friend = await socialRepo.getFriendByDriverId?.(item.actorFriendId); if (friend) { return { name: friend.displayName ?? friend.driverName ?? `Driver ${item.actorFriendId}`, avatarUrl: friend.avatarUrl ?? imageService.getDriverAvatar(item.actorFriendId), }; } } catch { // fall through to driver lookup } // Fallback to driver entity + image service try { const driver = await driverRepo.findById(item.actorFriendId); if (driver) { return { name: driver.name, avatarUrl: imageService.getDriverAvatar(driver.id), }; } } catch { // ignore and return null below } } return null; } interface FeedItemCardProps { item: FeedItem; } export default function FeedItemCard({ item }: FeedItemCardProps) { const [actor, setActor] = useState<{ name: string; avatarUrl: string } | null>(null); useEffect(() => { let cancelled = false; void (async () => { const resolved = await resolveActor(item); if (!cancelled) { setActor(resolved); } })(); return () => { cancelled = true; }; }, [item]); return (
{item.headline}
{item.body && ({item.body}
)}