import { api, type Color, type RealIdentity } from '../adapters/real'; import { newId } from '../domain/id'; import { useEffect, useRef, useState } from 'react'; import { validateCreation, type Identity } from '../domain/model'; import { freshState } from '../demo/fixtures'; import { delay } from '../adapters/demo'; import { BaseColor, Button, Field, Icon, Status } from '../shared/ui'; import type { DemoState } from '../domain/model'; const HOURS = ['子时', '丑时', '寅时', '卯时', '辰时', '巳时', '午时', '未时', '申时', '酉时', '戌时', '亥时']; // A random birthday she could have: an adult of 19–35, any month, a day every month has, any hour. function randomBirth(now = new Date()) { const pick = (n: number) => Math.floor(Math.random() * n); return { birth: `${now.getFullYear() - 19 - pick(17)}-${String(1 + pick(12)).padStart(2, '0')}-${String(1 + pick(28)).padStart(2, '0')}`, hour: HOURS[pick(12)] }; } // What the left-hand story says at each moment of the first meeting (HEL-815). const STORY: Record = { 0: ['先给她一个名字,和一个属于她的生日。', '名字由你来起。生日可以随机,也可以由你指定。'], 1: ['这是她出生时带来的底色。', '底色只读。她以后的样子,由你们的相处慢慢长出来。'], 2: ['你们从什么关系开始?', '不替你选。以后也可以在设置里调整。'], 3: ['确认之后,她就住进来了。', '这件事只会发生一次。'], run: ['她在收拾东西,马上就好。', '窗外的灯一直亮着。'], fail: ['差一点就好了。', '你填的都还在。'], 4: ['她住进来了。', '去看看她吧。'] }; const RELATIONS: [string, string][] = [['朋友', '像朋友一样相处,慢慢熟起来。'], ['恋人', '从恋人开始,彼此更亲近一些。']]; export function CreationWizard({ onCreate, onEnter, fail, real = false }: { real?: boolean; onCreate: (s: DemoState) => void; onEnter: () => void; fail: boolean; }) { const [initial] = useState(randomBirth); const [step, setStep] = useState(0), [name, setName] = useState(''), [birth, setBirth] = useState(initial.birth), [hour, setHour] = useState(initial.hour), [how, setHow] = useState<'random' | 'pick'>('random'), [relation, setRelation] = useState(''), [nickname, setNickname] = useState(''), [boundary, setBoundary] = useState(''), [agreed, setAgreed] = useState(false), [error, setError] = useState(''), [phase, setPhase] = useState(-1), [busy, setBusy] = useState(false), [back, setBack] = useState(false); const [color, setColor] = useState(); const nameField = useRef(null), birthField = useRef(null), relationGroup = useRef(null); const labels = ['出生资料', '她的底色', '你们的关系', '最终确认']; const field = error.includes('名字') ? 'name' : error.includes('出生') ? 'birth' : error.includes('关系') ? 'relation' : ''; // A rejected step puts the cursor where the problem is, so the next key fixes it. useEffect(() => { if (field === 'name') nameField.current?.focus(); else if (field === 'birth') birthField.current?.focus(); else if (field === 'relation') relationGroup.current?.querySelector('button')?.focus(); }, [field, error]); const go = (to: number) => { setBack(to < step); setStep(to); }; async function next() { const err = validateCreation(name, birth, step === 2 ? relation : '朋友'); if (err) { setError(err); return; } setError(''); if (real && step === 0) { setBusy(true); try { setColor(await api('companion/preview', { method: 'POST', body: JSON.stringify({ name, birth, hour }) })); } catch (e) { setError((e as Error).message); return; } finally { setBusy(false); } } go(step + 1); } async function create() { const err = validateCreation(name, birth, relation); if (err || !agreed) { setError(err || '请先确认你已了解。'); return; } setBusy(true); setError(''); try { if (real) { setPhase(0); const identity = await api('companion', { method: 'POST', body: JSON.stringify({ name, birth, hour, relation, nickname, ...(boundary.trim() ? { boundary: boundary.trim() } : {}) }) }); setPhase(2); onCreate({ ...freshState(identity), messages: [], events: [], memories: [] }); go(4); return; } const c = new AbortController(); for (let p = 0; p < 3; p++) { setPhase(p); await delay(280, c.signal); if (fail && p === 1) throw new Error('这一步没完成,资料都还在。可以重试,或返回修改。'); } const identity: Identity = { id: newId(), name: name.trim(), birth, hour, relation: relation as Identity['relation'], nickname: nickname.trim(), boundary: boundary.trim() || '尊重彼此的节奏' }; onCreate(freshState(identity)); go(4); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } const reroll = () => { const r = randomBirth(); setBirth(r.birth); setHour(r.hour); setHow('random'); setError(''); }; const [y, m, d] = birth.split('-'); const story = STORY[step === 4 ? 4 : step === 3 && phase >= 0 ? (error ? 'fail' : busy ? 'run' : 3) : step]; // One companion only: the server refuses a second (409). Reopening restores the one that exists; nothing is overwritten. const exists = real && error.includes('已经有一位她'); return
初次见面{step < 4 ? ` · 第 ${step + 1} 步,共 4 步` : ' · 第一晚'}

{story[0]}

{story[1]}

{real ? '资料与对话保存在服务器,重开或换设备都能恢复同一位她。最终确认前不会建档。' : '这是本机演示。你的选择只保存在这个浏览器,没有创建服务器上的真实伴侣。'}

首版只有一位。创建后不提供切换或清空重建。
{labels.map((l, i) => )}
{step < 4 ? `${step + 1} / 4 · ${labels[step]}` : '创建完成'}
{step === 0 && <>

先认识她的开始

名字由你起,生辰也可以在确认前修改。

setName(e.target.value)} autoComplete="off"/>
她的生日
{([['random', '随机生成'], ['pick', '我来指定']] as const).map(([k, l]) => )}
{ setBirth(e.target.value); setHow('pick'); }}/>

公历{y || '—'} 年 {Number(m) || '—'} 月 {Number(d) || '—'} 日 · {hour}

农历与四柱{real ? '下一步由排盘计算' : '演示不排盘'}

{how === 'random' ? '已随机生成。可以再随机一次,或换成「我来指定」。' : '确认前都可以改。'}

} {step === 1 && <>

{name.trim()}的底色

这是倾向,不是台词。她会怎样理解自己、长成什么样,由她在相处和生活里慢慢形成,不按固定模板。

} {step === 2 && <>

由你选择关系

没有预选答案。以后可以在设置中确认修改,记忆会保留。

{RELATIONS.map(([r, text]) => )}
setNickname(e.target.value)}/>