import { useEffect, useLayoutEffect, useRef, useState } from 'react'; import type { DemoState, LifeEvent, Memory } from '../domain/model'; import { publicView } from '../domain/privacy'; import { Button, Icon } from '../shared/ui'; import { delay } from '../adapters/demo'; // HEL-815 page design: a page lying on your side of the table while the room steps back. Only what she has already // told you is ever rendered (AM-001), so there is no "not told yet" label, filter or switch anywhere; any link the // client cannot resolve opens one neutral "can't open" state that reveals nothing about whether the record exists. const KIND = { plan: '打算', now: '正在', done: '已经发生' } as const; const MEMORY_GROUPS = ['我们一起的', '你们的小习惯', '她知道的你', '她的成长']; const phone = () => matchMedia('(max-width:720px)').matches; type Load = 'ok' | 'loading' | 'error'; export function LifePanel({ state, open, onClose, onChat, onSettings, now = '', fail = false, real = false }: { real?: boolean; state: DemoState; open: boolean; now?: string; fail?: boolean; onClose: () => void; onChat: (id: string) => void; onSettings?: () => void; }) { const view = publicView(state), [tab, setTab] = useState('life'), [selection, setSelection] = useState(''), [limit, setLimit] = useState(18), [load, setLoad] = useState('ok'), [more, setMore] = useState(false); const list = useRef(null), detail = useRef(null), scroll = useRef>({}), wasOpen = useRef(false), request = useRef(null); useEffect(() => { if (open) { const params = new URLSearchParams(location.hash.slice(1)); const id = params.get('event'); if (id) { setTab('life'); setSelection(id); } } }, [open]); // Demo only: the failure switch shows how a list that could not be read looks; nothing real is requested. useEffect(() => { if (open && !wasOpen.current && !real) setLoad(fail ? 'error' : 'ok'); wasOpen.current = open; if (!open) { request.current?.abort(); setMore(false); } }, [open, fail, real]); useEffect(() => () => request.current?.abort(), []); async function retry() { request.current?.abort(); const c = new AbortController(); request.current = c; setLoad('loading'); try { await delay(600, c.signal); setLoad(fail ? 'error' : 'ok'); } catch { /* Closed or replaced. */ } } async function earlier() { request.current?.abort(); const c = new AbortController(); request.current = c; setMore(true); try { await delay(real ? 0 : 450, c.signal); setLimit(x => x + 12); } catch { /* Closed. */ } finally { if (!c.signal.aborted) setMore(false); } } // Each tab keeps its own reading position; switching back lands where you left it. // A hidden panel is display:none, which drops its scroll offset; reopening (e.g. "结束交谈 · 回到日子") puts it back. useLayoutEffect(() => { if (list.current && open) list.current.scrollTop = scroll.current[tab] || 0; }, [tab, open]); useLayoutEffect(() => { if (detail.current) detail.current.scrollTop = 0; }, [selection, tab]); const switchTab = (t: string) => { if (list.current) scroll.current[tab] = list.current.scrollTop; setTab(t); setSelection(''); }; const select = (id: string, t = tab) => { if (t !== tab) switchTab(t); setSelection(id); }; // Esc on a phone closes the pushed detail first; the app then closes the panel on the next press. useEffect(() => { if (!open || !selection) return; const key = (e: KeyboardEvent) => { if (e.key !== 'Escape' || !phone() || document.querySelector('.modal.open')) return; e.stopImmediatePropagation(); setSelection(''); }; window.addEventListener('keydown', key, true); return () => window.removeEventListener('keydown', key, true); }, [open, selection]); const ordered = [...view.events.filter(e => e.kind === 'plan'), ...view.events.filter(e => e.kind !== 'plan')], shown = ordered.slice(0, limit); const day = (e: LifeEvent) => e.kind === 'plan' ? '她说过的打算' : e.time.split(' ')[0]; const event = tab === 'life' ? view.events.find(e => e.id === selection) : undefined, memory = tab === 'memory' ? view.memories.find(m => m.id === selection) : undefined; const name = view.identity.name, demoTag = !real && 演示数据; const lines = (e: LifeEvent) => view.messages.filter(m => m.lifeIds.includes(e.id)); const said = (who: string, text: string) =>
{who}{text}
; const linkCard = (key: string, title: string, sub: string, go: () => void) => ; function eventView(e: LifeEvent) { const mems = e.memoryIds.map(id => view.memories.find(m => m.id === id)).filter((m): m is Memory => !!m), chat = lines(e); return <>
{KIND[e.kind]} · {e.time} · {e.place}

{e.title}

{e.told ? `她在 ${e.told}` : '她讲给你听过的事'}{e.kind === 'plan' ? ' · 计划可能会变' : ''}

{said(name, e.text)} {mems.length > 0 && <>
和这件事有关的回忆
{mems.map(m => linkCard(m.id, m.title, m.source, () => select(m.id, 'memory')))}
}
那时候的聊天
{chat.length ?
{chat.map(m =>

{m.role === 'user' ? '你' : name}{m.text}

)}
:

那时候没有留下聊天记录。

}
{demoTag}这是模拟生活里的一条记录,不是真实发生的事。画面只按记录选片,不会反过来改写她的生活。
; } function memoryView(m: Memory) { const from = m.lifeIds.map(id => view.events.find(e => e.id === id)).filter((e): e is LifeEvent => !!e); return <>
{m.group} · {m.source}

{m.title}

她是这样记得的

{said(name, m.text)} {m.group === '她的成长' &&

这是后来慢慢有的。成长单独记录,不改写她最初的底色。{onSettings && }

}
来自这些日子
{from.length ?
{from.map(e => linkCard(e.id, e.title, e.time, () => select(e.id, 'life')))}
:

这条回忆来自你们的日常聊天,没有对应的某一天。

}
{demoTag}回忆由她在相处中整理,只引用她讲过的日子。
; } const failed =

这几天的事一时没翻出来。

不是你的问题,记录也没有丢。

; const skeleton = (n: number) => ; function lifeList() { if (load === 'loading') return <>

正在翻她的日子…

{skeleton(6)}; if (load === 'error') return failed; if (!view.events.length) return
{real ? '还没有可展示的生活记录' : '她今晚刚住进来,还没有发生什么。'}{real ? '生活后台尚未接通,不用示例经历填充。你们说过的话可在交谈的完整记录中查看。' : '等她跟你讲起,日子会一条条留在这里。演示资料可以从「演示场景」载入。'}
; return <>{shown.map((e, i) =>
{(i === 0 || day(shown[i - 1]) !== day(e)) &&
{day(e)}
}
)} {more ?

正在翻更早的日子…

: limit < ordered.length ?
:

再往前,就是她刚住进来的那天了。

}; } function memoryList() { if (load === 'loading') return skeleton(5); if (load === 'error') return failed; if (!real && !view.memories.length) return
还没有共同的回忆。聊着聊着就会有了。
; return MEMORY_GROUPS.map(group => { const items = view.memories.filter(m => m.group === group); return
{group}{!real && {items.length}}
{real &&
暂无可核实的回忆长期记忆尚未接通;文字聊天记录独立保留。
}{items.map(m => )}
; }); } const blank = load !== 'ok' || (tab === 'life' ? !view.events.length : !view.memories.length); return ; }