|
| 1 | +/** |
| 2 | + * WorldRecap — 延时摄影开场(宪法 P2 · 每次打开看见生长) |
| 3 | + * WorldRecap — daily "the world grew while you were away" recap |
| 4 | + * |
| 5 | + * @ai-context: 每日首次打开时展示 6 秒的世界生长摘要(珊瑚/深度/发现 |
| 6 | + * 的日间增量),把"变化"直接递到眼前——留存钩子 4(意外+生长)。 |
| 7 | + * 基线存 localStorage:日期变化才结算,同日多次打开不重复打扰。 |
| 8 | + * 零增长时只留一句温柔文案,无负向表达。 |
| 9 | + * |
| 10 | + * @ai-context: Once per day, shows a 6-second growth recap (coral/depth/ |
| 11 | + * discovery deltas). Baseline settles only on date change. |
| 12 | + */ |
| 13 | +import { useEffect, useState } from 'react'; |
| 14 | +import { createPortal } from 'react-dom'; |
| 15 | +import { AnimatePresence, motion } from 'framer-motion'; |
| 16 | +import { useEcosystemStore } from '../store/useEcosystemStore'; |
| 17 | +import { useDiscoveryStore } from '../store/useDiscoveryStore'; |
| 18 | + |
| 19 | +const BASELINE_KEY = 'ed-recap-baseline'; |
| 20 | +const RECAP_MS = 6000; |
| 21 | + |
| 22 | +interface RecapBaseline { |
| 23 | + date: string; |
| 24 | + corals: number; |
| 25 | + totalDepth: number; |
| 26 | + discoveries: number; |
| 27 | +} |
| 28 | + |
| 29 | +interface RecapDeltas { |
| 30 | + corals: number; |
| 31 | + depth: number; |
| 32 | + discoveries: number; |
| 33 | +} |
| 34 | + |
| 35 | +function today(): string { |
| 36 | + return new Date().toISOString().split('T')[0]; |
| 37 | +} |
| 38 | + |
| 39 | +function readBaseline(): RecapBaseline | null { |
| 40 | + try { |
| 41 | + const raw = localStorage.getItem(BASELINE_KEY); |
| 42 | + return raw ? (JSON.parse(raw) as RecapBaseline) : null; |
| 43 | + } catch { |
| 44 | + return null; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export function WorldRecap() { |
| 49 | + const initialized = useEcosystemStore((s) => s.initialized); |
| 50 | + const corals = useEcosystemStore((s) => s.corals); |
| 51 | + const totalDepth = useEcosystemStore((s) => s.totalDepth); |
| 52 | + const discoveriesCount = useDiscoveryStore((s) => s.totalCount); |
| 53 | + const [deltas, setDeltas] = useState<RecapDeltas | null>(null); |
| 54 | + |
| 55 | + // 结算逻辑:数据就绪后比较基线;跨日才展示,任何情况下都把今天写回基线 |
| 56 | + useEffect(() => { |
| 57 | + if (!initialized) return; |
| 58 | + |
| 59 | + const prev = readBaseline(); |
| 60 | + const now: RecapBaseline = { |
| 61 | + date: today(), |
| 62 | + corals: corals.length, |
| 63 | + totalDepth, |
| 64 | + discoveries: discoveriesCount, |
| 65 | + }; |
| 66 | + |
| 67 | + if (prev && prev.date !== now.date) { |
| 68 | + setDeltas({ |
| 69 | + corals: Math.max(0, now.corals - prev.corals), |
| 70 | + depth: Math.max(0, now.totalDepth - prev.totalDepth), |
| 71 | + discoveries: Math.max(0, now.discoveries - prev.discoveries), |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + localStorage.setItem(BASELINE_KEY, JSON.stringify(now)); |
| 77 | + } catch { /* ignore */ } |
| 78 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 79 | + }, [initialized]); |
| 80 | + |
| 81 | + // 自动消散 |
| 82 | + useEffect(() => { |
| 83 | + if (!deltas) return; |
| 84 | + const t = setTimeout(() => setDeltas(null), RECAP_MS); |
| 85 | + return () => clearTimeout(t); |
| 86 | + }, [deltas]); |
| 87 | + |
| 88 | + if (!deltas) return null; |
| 89 | + |
| 90 | + const lines: string[] = []; |
| 91 | + if (deltas.corals > 0) lines.push(`新种下 ${deltas.corals} 株珊瑚`); |
| 92 | + if (deltas.depth > 0) lines.push(`潜航加深 ${deltas.depth} 米`); |
| 93 | + if (deltas.discoveries > 0) lines.push(`新增 ${deltas.discoveries} 次深海发现`); |
| 94 | + |
| 95 | + return createPortal( |
| 96 | + <AnimatePresence> |
| 97 | + <motion.div |
| 98 | + key={`recap-${deltas.depth}-${deltas.corals}`} |
| 99 | + initial={{ opacity: 0, y: 16 }} |
| 100 | + animate={{ opacity: 1, y: 0 }} |
| 101 | + exit={{ opacity: 0, transition: { duration: 0.3 } }} |
| 102 | + transition={{ type: 'spring', stiffness: 200, damping: 26 }} |
| 103 | + onClick={() => setDeltas(null)} |
| 104 | + style={{ |
| 105 | + position: 'fixed', left: 24, bottom: 24, zIndex: 55, cursor: 'pointer', |
| 106 | + maxWidth: 320, padding: '16px 20px', borderRadius: '16px 10px 14px 12px', |
| 107 | + background: 'rgba(13,24,44,0.78)', border: '1px solid rgba(129,140,248,0.25)', |
| 108 | + backdropFilter: 'blur(14px)', boxShadow: '0 8px 30px rgba(4,10,20,0.5)', |
| 109 | + }} |
| 110 | + > |
| 111 | + <div style={{ fontFamily: "'LXGW WenKai Lite','Noto Serif SC',serif", fontSize: 15, fontWeight: 700, letterSpacing: 2, color: '#E0E6F0' }}> |
| 112 | + 你不在的时候,世界在生长 |
| 113 | + </div> |
| 114 | + {lines.length > 0 ? ( |
| 115 | + <ul style={{ margin: '10px 0 0', padding: 0, listStyle: 'none', display: 'flex', flexDirection: 'column', gap: 5 }}> |
| 116 | + {lines.map((l) => ( |
| 117 | + <li key={l} style={{ fontSize: 12.5, color: '#90A0B8', display: 'flex', alignItems: 'center', gap: 8 }}> |
| 118 | + <span style={{ width: 5, height: 5, borderRadius: '50%', background: '#22D3EE', boxShadow: '0 0 6px #22D3EE' }} /> |
| 119 | + {l} |
| 120 | + </li> |
| 121 | + ))} |
| 122 | + </ul> |
| 123 | + ) : ( |
| 124 | + <div style={{ marginTop: 10, fontSize: 12.5, color: '#90A0B8' }}> |
| 125 | + 海面平静如昨——今天下去看看? |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </motion.div> |
| 129 | + </AnimatePresence>, |
| 130 | + document.body, |
| 131 | + ); |
| 132 | +} |
0 commit comments