import React, { useState, useEffect } from 'react'; const API_BASE = 'http://localhost:8000/api'; function MemoryModal({ agentId, agentName, onClose }) { const [activeTab, setActiveTab] = useState('diary'); // 'memory' 或 'diary' const [dates, setDates] = useState([]); const [diaryDates, setDiaryDates] = useState([]); const [selectedDate, setSelectedDate] = useState(''); const [content, setContent] = useState(''); const [currentMonth, setCurrentMonth] = useState(new Date()); const [loading, setLoading] = useState(false); // 加载记忆和日记的日期 useEffect(() => { loadDates(); }, [agentId, activeTab]); const loadDates = async () => { setLoading(true); try { if (activeTab === 'memory') { // 加载记忆日期(每日记忆文件) const response = await fetch(`${API_BASE}/agents/${agentId}/memory/dates/`); const data = await response.json(); setDates(data.dates || []); if (data.dates && data.dates.length > 0) { setSelectedDate(data.dates[0]); } } else { // 加载日记日期(成才之路) const response = await fetch(`${API_BASE}/agents/${agentId}/diary/dates/`); const data = await response.json(); setDiaryDates(data.dates || []); if (data.dates && data.dates.length > 0) { setSelectedDate(data.dates[0]); } } } catch (error) { console.error('加载日期失败:', error); } finally { setLoading(false); } }; // 加载选中日期的内容 useEffect(() => { if (selectedDate) { loadContent(selectedDate); } }, [selectedDate, activeTab, agentId]); const loadContent = async (date) => { setLoading(true); try { if (activeTab === 'memory') { const response = await fetch(`${API_BASE}/agents/${agentId}/memory/${date}/`); const data = await response.json(); setContent(data.content || ''); } else { const response = await fetch(`${API_BASE}/agents/${agentId}/diary/${date}/`); const data = await response.json(); setContent(data.content || ''); } } catch (error) { console.error('加载内容失败:', error); setContent(''); } finally { setLoading(false); } }; // 渲染日历 const renderCalendar = () => { const year = currentMonth.getFullYear(); const month = currentMonth.getMonth(); const firstDay = new Date(year, month, 1); const lastDay = new Date(year, month + 1, 0); const daysInMonth = lastDay.getDate(); const startWeekday = firstDay.getDay(); const days = []; const hasContentDates = activeTab === 'memory' ? dates : diaryDates; // 空白格子 for (let i = 0; i < startWeekday; i++) { days.push(
); } // 日期格子 for (let day = 1; day <= daysInMonth; day++) { const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`; const hasContent = hasContentDates.includes(dateStr); const isSelected = selectedDate === dateStr; days.push(
hasContent && setSelectedDate(dateStr)} > {day}
); } return days; }; const changeMonth = (delta) => { setCurrentMonth(new Date(currentMonth.setMonth(currentMonth.getMonth() + delta))); }; const currentDates = activeTab === 'memory' ? dates : diaryDates; const title = activeTab === 'memory' ? '📔 工作记忆' : '📖 成长之路'; const emptyText = activeTab === 'memory' ? '这一天还没有工作记忆' : '这一天还没有日记'; return (
e.stopPropagation()}>

{title} - {agentName}

{/* 切换标签 */}
{/* 内容区域 */}
{loading ? (
加载中...
) : content ? (

📅 {selectedDate}

{content}
) : (

{emptyText}

)}
{/* 日历 */}
{currentMonth.getFullYear()}年 {currentMonth.getMonth() + 1}月
{['日', '一', '二', '三', '四', '五', '六'].map(day => (
{day}
))} {renderCalendar()}
● 有内容 ○ 无内容
📅 本月 {currentDates.filter(d => d.startsWith(`${currentMonth.getFullYear()}-${String(currentMonth.getMonth() + 1).padStart(2, '0')}`) ).length} 篇
); } export default MemoryModal;