'use client'; import { motion } from 'motion/react'; import { useRouter } from 'next/navigation'; import { useCallback, useEffect, useRef, useState, type ChangeEvent } from 'react'; import { apiRequest, ensureLogin } from '../lib/api'; import { drawPoster } from '../lib/bracketPoster'; import type { TestResult } from '../lib/types'; type ThemeId='clean'|'cream'|'night';type LayoutId='symmetric'|'single';type ViewId='overview'|'left'|'right'|'top16'|'top8'|'final';type ToolId='filter'|'text'|'sticker'|'crop'|'adjust'|'layout'|'brush';type Adjustments={brightness:number;contrast:number;saturate:number}; const TOOLS:readonly [ToolId,string,string][]=[['filter','滤镜','◌'],['text','文字','T'],['sticker','贴纸','▣'],['crop','裁剪','⌗'],['adjust','调整','☷'],['layout','布局','▦'],['brush','画笔','⌁']]; const VIEWS:readonly [ViewId,string][]=[['overview','总览'],['left','左半区'],['right','右半区'],['top16','16 强'],['top8','8 强'],['final','决赛']]; export default function PosterEditor({runId}:{runId:string}){ const router=useRouter(),canvasRef=useRef(null);const[result,setResult]=useState(null),[error,setError]=useState(''),[theme,setTheme]=useState('clean'),[layout,setLayout]=useState('symmetric'),[view,setView]=useState('overview'),[tool,setTool]=useState('filter'),[sticker,setSticker]=useState(true),[caption,setCaption]=useState(''),[background,setBackground]=useState(null),[adjust,setAdjust]=useState({brightness:100,contrast:100,saturate:100}); useEffect(()=>{async function load(){const cached=sessionStorage.getItem('pk_last_result');if(cached){const parsed=JSON.parse(cached) as TestResult;if(parsed.id===runId)return parsed;}await ensureLogin();return apiRequest(`/api/v1/me/results/${runId}`);}load().then(setResult).catch((reason:Error)=>setError(reason.message));},[runId]); const draw=useCallback(()=>{const canvas=canvasRef.current;if(!result||!canvas)return;const ctx=canvas.getContext('2d');if(ctx)drawPoster(ctx,result,{themeId:theme,layout,view,caption,sticker,background,adjust});},[adjust,background,caption,layout,result,sticker,theme,view]);useEffect(()=>draw(),[draw]); function pickBackground(event:ChangeEvent){const file=event.target.files?.[0];if(!file)return;const image=new Image();const url=URL.createObjectURL(file);image.onload=()=>{setBackground(image);URL.revokeObjectURL(url);};image.src=url;} function save(){const canvas=canvasRef.current;if(!canvas||!result)return;const link=document.createElement('a');link.download=`我的PK晋级图-${result.champion_name}.png`;link.href=canvas.toDataURL('image/png');link.click();} if(error)return

结果读取失败

{error}

;if(!result)return
正在打开编辑器…
; return
编辑分享图{result.test_title} · {result.champion_name}
; } function AdjustControls({value,onChange}:{value:Adjustments;onChange:(value:Adjustments)=>void}){return
{Object.entries(value).map(([name,amount])=>)}
;}