import { MoodCheckin, getMoodConfig } from '@/services/moodCheckins'; import dayjs from 'dayjs'; import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; interface MoodHistoryCardProps { moodCheckins: MoodCheckin[]; title?: string; } export function MoodHistoryCard({ moodCheckins, title = '心情记录' }: MoodHistoryCardProps) { // 计算心情统计 const moodStats = React.useMemo(() => { const stats = { total: moodCheckins.length, averageIntensity: 0, moodDistribution: {} as Record, mostFrequentMood: '', }; if (moodCheckins.length === 0) return stats; // 计算平均强度 const totalIntensity = moodCheckins.reduce((sum, checkin) => sum + checkin.intensity, 0); stats.averageIntensity = Math.round(totalIntensity / moodCheckins.length); // 计算心情分布 moodCheckins.forEach(checkin => { const moodLabel = getMoodConfig(checkin.moodType)?.label || checkin.moodType; stats.moodDistribution[moodLabel] = (stats.moodDistribution[moodLabel] || 0) + 1; }); // 找出最频繁的心情 const sortedMoods = Object.entries(stats.moodDistribution) .sort(([, a], [, b]) => b - a); stats.mostFrequentMood = sortedMoods[0]?.[0] || ''; return stats; }, [moodCheckins]); // 获取最近的心情记录 const recentMoods = moodCheckins .sort((a, b) => dayjs(b.createdAt).valueOf() - dayjs(a.createdAt).valueOf()) .slice(0, 5); return ( {title} {moodCheckins.length === 0 ? ( 暂无心情记录 ) : ( <> {/* 统计信息 */} {moodStats.total} 总记录 {moodStats.averageIntensity} 平均强度 {moodStats.mostFrequentMood} 最常见 {/* 最近记录 */} 最近记录 {recentMoods.map((checkin, index) => { const moodConfig = getMoodConfig(checkin.moodType); return ( {moodConfig?.emoji} {moodConfig?.label} {dayjs(checkin.createdAt).format('MM月DD日 HH:mm')} 强度 {checkin.intensity} ); })} )} ); } const styles = StyleSheet.create({ container: { backgroundColor: '#FFFFFF', borderRadius: 16, padding: 16, marginBottom: 16, shadowColor: '#000', shadowOffset: { width: 0, height: 2, }, shadowOpacity: 0.1, shadowRadius: 3.84, elevation: 5, }, title: { fontSize: 18, fontWeight: '600', color: '#192126', marginBottom: 16, }, emptyState: { alignItems: 'center', paddingVertical: 32, }, emptyText: { fontSize: 14, color: '#9CA3AF', fontStyle: 'italic', }, statsContainer: { flexDirection: 'row', justifyContent: 'space-around', marginBottom: 20, paddingVertical: 16, backgroundColor: '#F8F9FA', borderRadius: 12, }, statItem: { alignItems: 'center', }, statValue: { fontSize: 20, fontWeight: '700', color: '#192126', marginBottom: 4, }, statLabel: { fontSize: 12, color: '#6B7280', }, recentContainer: { marginTop: 8, }, sectionTitle: { fontSize: 16, fontWeight: '600', color: '#192126', marginBottom: 12, }, moodItem: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingVertical: 8, borderBottomWidth: 1, borderBottomColor: '#F3F4F6', }, moodInfo: { flexDirection: 'row', alignItems: 'center', flex: 1, }, moodEmoji: { fontSize: 20, marginRight: 12, }, moodDetails: { flex: 1, }, moodLabel: { fontSize: 14, fontWeight: '500', color: '#192126', marginBottom: 2, }, moodDate: { fontSize: 12, color: '#6B7280', }, moodIntensity: { alignItems: 'flex-end', }, intensityText: { fontSize: 12, color: '#6B7280', fontWeight: '500', }, });