import { useState, useEffect, useRef, useCallback } from 'react' import { useNavigate } from 'react-router-dom' import { X } from 'lucide-react' import html2canvas from 'html2canvas' import { finishBackgroundTask, isBackgroundTaskCancelRequested, registerBackgroundTask, updateBackgroundTask } from '../services/backgroundTaskMonitor' import './AnnualReportWindow.scss' interface TopContact { username: string displayName: string avatarUrl?: string messageCount: number sentCount: number receivedCount: number } interface MonthlyTopFriend { month: number displayName: string avatarUrl?: string messageCount: number } interface AnnualReportData { year: number totalMessages: number totalFriends: number coreFriends: TopContact[] monthlyTopFriends: MonthlyTopFriend[] peakDay: { date: string; messageCount: number; topFriend?: string; topFriendCount?: number } | null longestStreak: { friendName: string; days: number; startDate: string; endDate: string } | null activityHeatmap: { data: number[][] } midnightKing: { displayName: string; count: number; percentage: number } | null selfAvatarUrl?: string mutualFriend?: { displayName: string; avatarUrl?: string; sentCount: number; receivedCount: number; ratio: number } | null socialInitiative?: { initiatedChats: number receivedChats: number initiativeRate: number topInitiatedFriend?: string topInitiatedCount?: number } | null responseSpeed?: { avgResponseTime: number; fastestFriend: string; fastestTime: number } | null topPhrases?: { phrase: string; count: number }[] snsStats?: { totalPosts: number typeCounts?: Record topLikers: { username: string; displayName: string; avatarUrl?: string; count: number }[] topLiked: { username: string; displayName: string; avatarUrl?: string; count: number }[] } lostFriend: { username: string displayName: string avatarUrl?: string earlyCount: number lateCount: number periodDesc: string } | null } const DecodeText = ({ value, active }: { value: string | number active: boolean }) => { const strVal = String(value) const [display, setDisplay] = useState(strVal) const decodedRef = useRef(false) useEffect(() => { setDisplay(strVal) }, [strVal]) useEffect(() => { if (!active) { decodedRef.current = false return } if (decodedRef.current) return decodedRef.current = true const chars = '018X-/#*' let iter = 0 const inv = setInterval(() => { setDisplay(strVal.split('').map((c, i) => { if (c === ',' || c === ' ' || c === ':') return c if (i < iter) return strVal[i] return chars[Math.floor(Math.random() * chars.length)] }).join('')) if (iter >= strVal.length) { clearInterval(inv) setDisplay(strVal) } iter += 1 / 3 }, 35) return () => clearInterval(inv) }, [active, strVal]) return <>{display.length > 0 ? display : value} } function AnnualReportWindow() { const navigate = useNavigate() const containerRef = useRef(null) const [reportData, setReportData] = useState(null) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const [loadingProgress, setLoadingProgress] = useState(0) const [loadingStage, setLoadingStage] = useState('正在初始化...') const TOTAL_SCENES = 11 const [currentScene, setCurrentScene] = useState(0) const [isAnimating, setIsAnimating] = useState(false) const p0CanvasRef = useRef(null) const s3LayoutRef = useRef(null) const s3ListRef = useRef(null) const [s3LineVars, setS3LineVars] = useState({}) // 提取长图逻辑变量 const [buttonText, setButtonText] = useState('EXTRACT RECORD') const [isExtracting, setIsExtracting] = useState(false) useEffect(() => { const params = new URLSearchParams(window.location.hash.split('?')[1] || '') const yearParam = params.get('year') const parsedYear = yearParam ? parseInt(yearParam, 10) : new Date().getFullYear() const year = Number.isNaN(parsedYear) ? new Date().getFullYear() : parsedYear generateReport(year) }, []) const generateReport = async (year: number) => { const taskId = registerBackgroundTask({ sourcePage: 'annualReport', title: '年度报告生成', detail: `正在生成 ${year === 0 ? '历史以来' : year + '年'} 年度报告`, progressText: '初始化', cancelable: true }) setIsLoading(true) setError(null) setLoadingProgress(0) const removeProgressListener = window.electronAPI.annualReport.onProgress?.((payload: { status: string; progress: number }) => { setLoadingProgress(payload.progress) setLoadingStage(payload.status) updateBackgroundTask(taskId, { detail: payload.status || '正在生成年度报告', progressText: `${Math.max(0, Math.round(payload.progress || 0))}%` }) }) try { const result = await window.electronAPI.annualReport.generateReport(year) removeProgressListener?.() if (isBackgroundTaskCancelRequested(taskId)) { finishBackgroundTask(taskId, 'canceled', { detail: '已停止后续加载,当前报告结果未继续写入页面' }) setIsLoading(false) return } setLoadingProgress(100) setLoadingStage('完成') if (result.success && result.data) { finishBackgroundTask(taskId, 'completed', { detail: '年度报告生成完成', progressText: '100%' }) setTimeout(() => { setReportData(result.data!) setIsLoading(false) }, 300) } else { finishBackgroundTask(taskId, 'failed', { detail: result.error || '生成年度报告失败' }) setError(result.error || '生成报告失败') setIsLoading(false) } } catch (e) { removeProgressListener?.() finishBackgroundTask(taskId, 'failed', { detail: String(e) }) setError(String(e)) setIsLoading(false) } } // Handle Scroll and touch events const goToScene = useCallback((index: number) => { if (isAnimating || index === currentScene || index < 0 || index >= TOTAL_SCENES) return setIsAnimating(true) setCurrentScene(index) setTimeout(() => { setIsAnimating(false) }, 1500) }, [currentScene, isAnimating, TOTAL_SCENES]) useEffect(() => { if (isLoading || error || !reportData) return let touchStartY = 0 let lastWheelTime = 0 const handleWheel = (e: WheelEvent) => { const now = Date.now() if (now - lastWheelTime < 1000) return // Throttle wheel events if (Math.abs(e.deltaY) > 30) { lastWheelTime = now goToScene(e.deltaY > 0 ? currentScene + 1 : currentScene - 1) } } const handleTouchStart = (e: TouchEvent) => { touchStartY = e.touches[0].clientY } const handleTouchMove = (e: TouchEvent) => { e.preventDefault() // prevent native scroll } const handleTouchEnd = (e: TouchEvent) => { const deltaY = touchStartY - e.changedTouches[0].clientY if (deltaY > 40) goToScene(currentScene + 1) else if (deltaY < -40) goToScene(currentScene - 1) } window.addEventListener('wheel', handleWheel, { passive: false }) window.addEventListener('touchstart', handleTouchStart, { passive: false }) window.addEventListener('touchmove', handleTouchMove, { passive: false }) window.addEventListener('touchend', handleTouchEnd) return () => { window.removeEventListener('wheel', handleWheel) window.removeEventListener('touchstart', handleTouchStart) window.removeEventListener('touchmove', handleTouchMove) window.removeEventListener('touchend', handleTouchEnd) } }, [currentScene, isLoading, error, reportData, goToScene]) useEffect(() => { if (isLoading || error || !reportData || currentScene !== 0) return const canvas = p0CanvasRef.current const ctx = canvas?.getContext('2d') if (!canvas || !ctx) return let rafId = 0 let particles: Array<{ x: number y: number vx: number vy: number size: number alpha: number }> = [] const buildParticle = () => ({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, vx: (Math.random() - 0.5) * 0.3, vy: (Math.random() - 0.5) * 0.3, size: Math.random() * 1.5 + 0.5, alpha: Math.random() * 0.5 + 0.1 }) const initParticles = () => { const count = Math.max(36, Math.floor((canvas.width * canvas.height) / 15000)) particles = Array.from({ length: count }, () => buildParticle()) } const resizeCanvas = () => { canvas.width = window.innerWidth canvas.height = window.innerHeight initParticles() } const animate = () => { ctx.clearRect(0, 0, canvas.width, canvas.height) for (let i = 0; i < particles.length; i++) { const p = particles[i] p.x += p.vx p.y += p.vy if (p.x < 0 || p.x > canvas.width) p.vx *= -1 if (p.y < 0 || p.y > canvas.height) p.vy *= -1 ctx.beginPath() ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2) ctx.fillStyle = `rgba(255, 255, 255, ${p.alpha})` ctx.fill() for (let j = i + 1; j < particles.length; j++) { const q = particles[j] const dx = p.x - q.x const dy = p.y - q.y const distance = Math.sqrt(dx * dx + dy * dy) if (distance < 150) { const lineAlpha = (1 - distance / 150) * 0.15 ctx.beginPath() ctx.moveTo(p.x, p.y) ctx.lineTo(q.x, q.y) ctx.strokeStyle = `rgba(255, 255, 255, ${lineAlpha})` ctx.lineWidth = 0.5 ctx.stroke() } } } rafId = requestAnimationFrame(animate) } resizeCanvas() window.addEventListener('resize', resizeCanvas) animate() return () => { window.removeEventListener('resize', resizeCanvas) cancelAnimationFrame(rafId) } }, [isLoading, error, reportData, currentScene]) useEffect(() => { if (isLoading || error || !reportData) return let rafId = 0 const updateS3Line = () => { cancelAnimationFrame(rafId) rafId = requestAnimationFrame(() => { const root = document.querySelector('.annual-report-window') as HTMLElement | null const layout = s3LayoutRef.current const list = s3ListRef.current if (!root || !layout || !list) return const rootRect = root.getBoundingClientRect() const layoutRect = layout.getBoundingClientRect() const listRect = list.getBoundingClientRect() if (listRect.height <= 0 || layoutRect.width <= 0) return const leftOffset = Math.max(8, Math.min(16, layoutRect.width * 0.018)) const lineLeft = layoutRect.left - rootRect.left + leftOffset const lineCenterTop = listRect.top - rootRect.top + listRect.height / 2 setS3LineVars({ ['--s3-line-left' as '--s3-line-left']: `${lineLeft}px`, ['--s3-line-top' as '--s3-line-top']: `${lineCenterTop}px`, ['--s3-line-height' as '--s3-line-height']: `${listRect.height}px` } as React.CSSProperties) }) } updateS3Line() window.addEventListener('resize', updateS3Line) const resizeObserver = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(() => updateS3Line()) : null if (resizeObserver) { if (s3LayoutRef.current) resizeObserver.observe(s3LayoutRef.current) if (s3ListRef.current) resizeObserver.observe(s3ListRef.current) } return () => { cancelAnimationFrame(rafId) window.removeEventListener('resize', updateS3Line) resizeObserver?.disconnect() } }, [isLoading, error, reportData, currentScene]) const getSceneClass = (index: number) => { if (index === currentScene) return 'scene active' if (index < currentScene) return 'scene prev' return 'scene next' } const handleClose = () => { navigate('/home') } const formatFileYearLabel = (year: number) => (year === 0 ? '历史以来' : String(year)) const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) const handleExtract = async () => { if (isExtracting || !reportData || !containerRef.current) return const dirResult = await window.electronAPI.dialog.openDirectory({ title: '选择导出文件夹', properties: ['openDirectory', 'createDirectory'] }) if (dirResult.canceled || !dirResult.filePaths?.[0]) return const root = containerRef.current const previousScene = currentScene const sceneNames = [ 'THE_ARCHIVE', 'VOLUME', 'NOCTURNE', 'GRAVITY_CENTERS', 'TIME_WAVEFORM', 'MUTUAL_RESONANCE', 'SOCIAL_KINETICS', 'THE_SPARK', 'FADING_SIGNALS', 'LEXICON', 'EXTRACTION' ] setIsExtracting(true) setButtonText('EXTRACTING...') try { const images: Array<{ name: string; dataUrl: string }> = [] root.classList.add('exporting-scenes') for (let i = 0; i < TOTAL_SCENES; i++) { setCurrentScene(i) setButtonText(`EXTRACTING ${i + 1}/${TOTAL_SCENES}`) await wait(2000) const canvas = await html2canvas(root, { backgroundColor: '#050505', scale: 2, useCORS: true, allowTaint: true, logging: false, onclone: (clonedDoc) => { clonedDoc.querySelector('.annual-report-window')?.classList.add('exporting-scenes') } }) images.push({ name: `P${String(i).padStart(2, '0')}_${sceneNames[i] || `SCENE_${i}`}.png`, dataUrl: canvas.toDataURL('image/png') }) } const yearFilePrefix = formatFileYearLabel(reportData.year) const exportResult = await window.electronAPI.annualReport.exportImages({ baseDir: dirResult.filePaths[0], folderName: `${yearFilePrefix}年度报告_分页面`, images }) if (!exportResult.success) { throw new Error(exportResult.error || '导出失败') } setButtonText('SAVED TO DEVICE') } catch (e) { alert(`导出失败: ${String(e)}`) setButtonText('EXTRACT RECORD') } finally { root.classList.remove('exporting-scenes') setCurrentScene(previousScene) await wait(80) setTimeout(() => { setButtonText('EXTRACT RECORD') setIsExtracting(false) }, 2200) } } if (isLoading) { return (
{loadingProgress}%

{loadingStage}

进行中

) } if (error || !reportData) { return (

{error ? `生成报告失败: ${error}` : '暂无数据'}

) } const yearTitle = reportData.year === 0 ? '历史以来' : String(reportData.year) const finalYearLabel = reportData.year === 0 ? 'ALL YEARS' : String(reportData.year) const topFriends = reportData.coreFriends.slice(0, 3) const endingPostCount = reportData.snsStats?.totalPosts ?? 0 const endingReceivedChats = reportData.socialInitiative?.receivedChats ?? 0 const endingTopPhrase = reportData.topPhrases?.[0]?.phrase || '' const endingTopPhraseCount = reportData.topPhrases?.[0]?.count ?? 0 return (
{Array.from({ length: TOTAL_SCENES }).map((_, i) => (
goToScene(i)} /> ))}
向下滑动以继续
{/* S0: THE ARCHIVE */}
一切的起点
{yearTitle}
那些被岁月悄悄掩埋的对话
原来都在这里,等待一个春天。
{/* S1: VOLUME */}
消息报告
这一年,你说出了 {reportData.totalMessages.toLocaleString()} 句话。
无数个日夜的碎碎念,都是为了在茫茫人海中,刻下彼此来过的痕迹。
{/* S2: NOCTURNE */}
深夜
{reportData.midnightKing ? reportData.midnightKing.displayName : '00:00'}

在深夜陪你聊天最多的人
梦境之外,你与{reportData.midnightKing ? reportData.midnightKing.displayName : '00:00'}共同醒着度过了许多个夜晚
“曾有 条消息在那些无人知晓的夜里,代替星光照亮了彼此”
{/* S3: GRAVITY CENTERS */}
聊天排行
漫长的岁月里,是他们,让你的时间有了实实在在的重量。
{topFriends.map((f, i) => (
{f.displayName}
TOP {i + 1}
{f.messageCount.toLocaleString()}
))} {topFriends.length === 0 && (
暂无记录
)}
{/* S4: TIME WAVEFORM (Audio/Heartbeat timeline visual) */}
TIME WAVEFORM
十二个月的更迭,就像走过了一万个冬天
时间在变,但好在总有人陪在身边。
{reportData.monthlyTopFriends.length > 0 ? (
{reportData.monthlyTopFriends.map((m, i) => { const leftPos = (i / 11) * 100; // 0% to 100% const isTop = i % 2 === 0; // Alternate up and down to prevent crowding const isRightSide = i >= 6; // Center-focus alignment logic // Pseudo-random organic height variation for audio-wave feel (from 8vh to 18vh) const heightVariation = 12 + (Math.sin(i * 1.5) * 6); const alignStyle = isRightSide ? { right: '10px', alignItems: 'flex-end', textAlign: 'right' as const } : { left: '10px', alignItems: 'flex-start', textAlign: 'left' as const }; return (
{/* The connecting thread (gradient fades away from center line) */}
{/* Center Glowing Dot */}
{/* Text Payload */}
{m.month.toString().padStart(2, '0')}
{m.displayName}
{m.messageCount.toLocaleString()} M
); })}
) : (
暂无记忆声纹
)}
{/* S5: MUTUAL RESONANCE (Mutual friend) */}
回应的艺术
{reportData.mutualFriend ? ( <>
{reportData.mutualFriend.displayName}
发出
收到
你们之间收发的消息高达 {reportData.mutualFriend.ratio} 的平衡率
“你抛出的每一句话,都落在了对方的心里。
所谓重逢,就是我走向你的时候,你也在走向我。”
) : (
今年似乎独自咽下了很多话。
请相信,分别和孤独总会迎来终结,你终会遇到那个懂你的TA。
)}
{/* S6: SOCIAL KINETICS */}
我的风格
{reportData.socialInitiative || reportData.responseSpeed ? (
{reportData.socialInitiative && (
我的主动性
{reportData.socialInitiative.initiativeRate}%
你的聊天开场大多由你发起。
{reportData.socialInitiative.topInitiatedFriend && (reportData.socialInitiative.topInitiatedCount || 0) > 0 ? (
其中{reportData.socialInitiative.topInitiatedFriend}是你最常联系的人, 有{(reportData.socialInitiative.topInitiatedCount || 0).toLocaleString()}次,是你先忍不住敲响了对方的门
) : (
你主动发起了{reportData.socialInitiative.initiatedChats.toLocaleString()}次联络。
)} 想见一个人的心,总是走在时间的前面。
)} {reportData.responseSpeed && (
回应速度
S
{reportData.responseSpeed.fastestFriend} 回你的消息总是很快。
这世上最让人安心的默契,莫过于一句 "我在"。
)}
) : (
暂无数据。
)}
{/* S7: THE SPARK */}
聊天火花
{reportData.longestStreak ? (
最长连续聊天
{reportData.longestStreak.friendName}
你们曾连续 天,聊到忘记了时间,
那些舍不得说再见的日夜,连成了最漫长的春天。
) : null} {reportData.peakDay ? (
最热烈的一天
{reportData.peakDay.date}
“这一天,你们留下了 {reportData.peakDay.messageCount} 句话。
好像要把积攒了很久的想念,一天全都说完。”
) : null} {!reportData.longestStreak && !reportData.peakDay && (
没有激起过火花。
)}
{/* S8: FADING SIGNALS */}
曾经的好友
{reportData.lostFriend ? (
{reportData.lostFriend.displayName}
后来,你们的交集停留在{reportData.lostFriend.periodDesc}这短短的 句话里。
“我一直相信我们能够再次相见,相信分别的日子总会迎来终结。”
所有的离散,或许都只是一场漫长的越冬。飞鸟要越过一万座雪山,才能带来春天的第一行回信;树木要褪去一万次枯叶,才能记住风的形状。如果时间注定要把我们推向不同的象限,那就在记忆的最深处建一座灯塔。哪怕要熬过几千个无法见面的黄昏,也要相信,总有一次日出的晨光,是为了照亮我们重逢的归途。
) : (
缘分温柔地眷顾着你。
这一年,所有重要的人都在,没有一次无疾而终的告别。
)}
{/* S9: LEXICON & ARCHIVE */}
我的词云
{reportData.topPhrases && reportData.topPhrases.slice(0, 12).map((phrase, i) => { // 12 precisely tuned absolute coordinates for the ultimate organic scatter without overlapping const demoStyles = [ { left: '25vw', top: '25vh', fontSize: 'clamp(3rem, 7vw, 5rem)', color: 'rgba(255,255,255,1)', delay: '0.1s', floatDelay: '0s', targetOp: 1 }, { left: '72vw', top: '30vh', fontSize: 'clamp(2rem, 5vw, 4rem)', color: 'rgba(255,255,255,0.8)', delay: '0.2s', floatDelay: '-1s', targetOp: 0.8 }, { left: '15vw', top: '55vh', fontSize: 'clamp(2.5rem, 6vw, 4.5rem)', color: 'rgba(255,255,255,0.9)', delay: '0.3s', floatDelay: '-2.5s', targetOp: 0.9 }, { left: '78vw', top: '60vh', fontSize: 'clamp(1.5rem, 3.5vw, 3rem)', color: 'rgba(255,255,255,0.6)', delay: '0.4s', floatDelay: '-1.5s', targetOp: 0.6 }, { left: '45vw', top: '75vh', fontSize: 'clamp(1.2rem, 3vw, 2.5rem)', color: 'rgba(255,255,255,0.7)', delay: '0.5s', floatDelay: '-3s', targetOp: 0.7 }, { left: '55vw', top: '15vh', fontSize: 'clamp(1.5rem, 3vw, 2.5rem)', color: 'rgba(255,255,255,0.5)', delay: '0.6s', floatDelay: '-0.5s', targetOp: 0.5 }, { left: '12vw', top: '80vh', fontSize: 'clamp(1rem, 2vw, 1.8rem)', color: 'rgba(255,255,255,0.4)', delay: '0.7s', floatDelay: '-1.2s', targetOp: 0.4 }, { left: '35vw', top: '45vh', fontSize: 'clamp(2.2rem, 5vw, 4rem)', color: 'rgba(255,255,255,0.85)', delay: '0.8s', floatDelay: '-0.8s', targetOp: 0.85 }, { left: '85vw', top: '82vh', fontSize: 'clamp(0.9rem, 1.5vw, 1.5rem)', color: 'rgba(255,255,255,0.3)', delay: '0.9s', floatDelay: '-2.1s', targetOp: 0.3 }, { left: '60vw', top: '50vh', fontSize: 'clamp(1.8rem, 4vw, 3.5rem)', color: 'rgba(255,255,255,0.65)', delay: '1s', floatDelay: '-0.3s', targetOp: 0.65 }, { left: '45vw', top: '35vh', fontSize: 'clamp(1rem, 2vw, 1.8rem)', color: 'rgba(255,255,255,0.35)', delay: '1.1s', floatDelay: '-1.8s', targetOp: 0.35 }, { left: '30vw', top: '65vh', fontSize: 'clamp(1.4rem, 2.5vw, 2.2rem)', color: 'rgba(255,255,255,0.55)', delay: '1.2s', floatDelay: '-2.7s', targetOp: 0.55 }, ]; const st = demoStyles[i]; return (
{phrase.phrase}
) })} {(!reportData.topPhrases || reportData.topPhrases.length === 0) && (
词汇量太少,无法形成星云。
)}
{/* S10: EXTRACTION (白色反色结束页 / Data Receipt) */}
旅程的终点
{/* The Final Summary Receipt / Dashboard */}
{finalYearLabel}
TRANSMISSION COMPLETE
{/* Core Stats Row */}
朋友圈发帖
{endingPostCount.toLocaleString()}
被动开场
{endingReceivedChats.toLocaleString()}
你最爱说
“{endingTopPhrase}”
“故事的最后,我们把这一切悄悄还给岁月
只要这些文字还在,所有的离别,就都只是一场短暂的缺席。”
数据数得清一万句落笔的寒暄,却度量不出一个默契的眼神。
在这片由数字构建的大海里,热烈的回应未必是感情的全部轮廓。
真正的爱与羁绊,从来都不在跳动的屏幕里,而在无法被量化的现实。
) } export default AnnualReportWindow