Files
rice-pk-xhs/web/components/RiceHome.tsx
T
codex-bot 9615add720
Verify mini program and deploy preview / verify-deploy (push) Successful in 3m57s
feat: redesign typed rice poster editor
2026-09-20 16:37:35 +08:00

25 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { motion } from 'motion/react';
import Image from 'next/image';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import { apiRequest, ensureLogin } from '../lib/api';
import type { TestDetail, TestResult } from '../lib/types';
export default function RiceHome() {
const [test,setTest]=useState<TestDetail|null>(null),[results,setResults]=useState<TestResult[]>([]),[code,setCode]=useState(''),[message,setMessage]=useState(''),[error,setError]=useState('');
useEffect(()=>{Promise.all([apiRequest<TestDetail>('/api/v1/tests/rice'),ensureLogin().then(()=>apiRequest<TestResult[]>('/api/v1/me/results?test_slug=rice'))]).then(([data,history])=>{setTest(data);setResults(history);}).catch((reason:Error)=>setError(reason.message));},[]);
async function bind(){if(code.trim().length!==6){setMessage('请输入 6 位绑定码');return;}try{const data=await apiRequest<{token:string}>('/api/v1/me/link-code/confirm',{method:'POST',body:{code:code.trim().toUpperCase()}});localStorage.setItem('rice_token',data.token);setMessage('档案已连接');setResults(await apiRequest<TestResult[]>('/api/v1/me/results?test_slug=rice'));}catch(reason){setMessage(reason instanceof Error?reason.message:'连接失败');}}
if(error)return <main className="centerState"><h1>暂时没接上服务</h1><p>{error}</p><button onClick={()=>location.reload()}>重新加载</button></main>;
if(!test)return <main className="centerState">正在摆好饭桌…</main>;
return <main className="homeShell">
<header className="topbar"><strong>RICE PK</strong><span>独立饭类版</span></header>
<motion.section className="hero" initial={{opacity:0,y:18}} animate={{opacity:1,y:0}}>
<Image src="/assets/test-covers/rice.jpg" width={900} height={1200} priority sizes="(max-width: 720px) 100vw, 430px" alt="各种各样的饭二选一" />
<div className="heroCopy"><small>ONLY RICE · {test.candidate_count} 强</small><h1>{test.title}</h1><p>{test.subtitle}</p><Link className="primary" href="/test">开始选饭</Link></div>
</motion.section>
<section className="history"><div className="sectionHead"><h2>我的饭类结果</h2><span>{results.length} 次</span></div>{results.length?results.map(result=><Link className="resultRow" key={result.id} href={`/results/${result.id}`}><b>#1</b><span><small>{new Date(result.completed_at).toLocaleDateString()}</small><strong>{result.champion_name}</strong></span><i>→</i></Link>):<p className="empty">还没有冠军,第一轮现在开始。</p>}</section>
<section className="bindPanel"><h2>已有偏爱研究所档案?</h2><p>在综合版生成绑定码,在这里确认后合并两边历史。</p><div><input value={code} onChange={event=>setCode(event.target.value)} maxLength={6} placeholder="6 位绑定码"/><button onClick={bind}>确认连接</button></div>{message?<output>{message}</output>:null}</section>
</main>;
}