import { useState, memo } from 'react'; import type { Item } from '../types'; import './FeedItem.css'; import { apiFetch } from '../utils'; interface FeedItemProps { item: Item; onToggleStar?: (item: Item) => void; onUpdate?: (item: Item) => void; } const FeedItem = memo(function FeedItem({ item, onToggleStar, onUpdate }: FeedItemProps) { const [loading, setLoading] = useState(false); // We rely on props.item for data. // If we fetch full content, we notify the parent via onUpdate. const handleToggleStar = (e: React.MouseEvent) => { e.stopPropagation(); if (onToggleStar) { onToggleStar(item); } else { // Fallback if no handler passed (backward compat or isolated usage) // But really we should rely on parent. // For now, let's keep the optimistic local update logic if we were standalone, // but since we are optimizing, we assume parent handles it. } }; const loadFullContent = (e: React.MouseEvent) => { e.stopPropagation(); setLoading(true); apiFetch(`/api/item/${item._id}`) .then((res) => { if (!res.ok) throw new Error('Failed to fetch full content'); return res.json(); }) .then((data) => { // Merge the new data (full_content) into the item and notify parent const newItem = { ...item, ...data }; if (onUpdate) { onUpdate(newItem); } setLoading(false); }) .catch((err) => { console.error('Error fetching full content:', err); setLoading(false); }); }; return (