import { useEffect, useMemo, useRef, useState } from 'react' import { ExhibitFlipOverlay, FlipActionButton, Image, SceneHint } from '@/components' import { cn, getBackgroundImage, getPublicPath, getStaticImage } from '@/lib/utils' import { filterInput } from '@/lib/utils/sensitiveWords' import { useNavigate, useParams } from 'react-router-dom' import { saveZhanglepingStore, useZhanglepingStore } from '@/store/zhanglepingStore' import { type BackgroundKey, type ClothesKey, type MaterialKey, type SidebarBlockKey, STAR_NAME, backgroundToAllowedMaterials, buildCompositeDataUrl, getBackgroundAssetKey, materialConflicts, materialOptions, sidebarBlocks, } from './draw1.config' import { TopMenuPanel } from './TopMenuPanel' import { SanmaoDialogueItem, SanmaoDialogueSteps } from '@/pages/zhangleping/components' const COMPLETION_DIALOGUE = '太好啦!我的记忆都回来啦,还拥有了新时代的模样!爷爷画的我,藏着善良、勇敢与热爱;你画的我,让这些美好品质在今天继续发光!谢谢你,让三毛精神一直传承下去!' const ZhanglepingDraw1 = () => { const navigate = useNavigate() const { moduleName } = useParams<{ moduleName: string }>() const { setDrawArtwork } = useZhanglepingStore() const [activeBlock, setActiveBlock] = useState('衣柜') const [selectedClothes, setSelectedClothes] = useState('默认') const [selectedBackground, setSelectedBackground] = useState('城际高铁') const [selectedMaterials, setSelectedMaterials] = useState>(new Set()) const [showCaptionInput, setShowCaptionInput] = useState(false) const [captionText, setCaptionText] = useState('') const [isSubmitting, setIsSubmitting] = useState(false) const [createdArtworkSrc, setCreatedArtworkSrc] = useState('') const [showCreatedOverlay, setShowCreatedOverlay] = useState(false) const [showCompletionDialogue, setShowCompletionDialogue] = useState(false) const [submitErrorHintKey, setSubmitErrorHintKey] = useState(0) const captionInputRef = useRef(null) const compositionRef = useRef(null) const allowedMaterialKeys = useMemo( () => backgroundToAllowedMaterials[selectedBackground] ?? [], [selectedBackground] ) const backgroundAssetKey = getBackgroundAssetKey(selectedBackground) const scenePreviewSrc = `${STAR_NAME}/reflection/draw/布景/${backgroundAssetKey}` const clothingSrc = `${STAR_NAME}/reflection/draw/衣柜/${selectedClothes}` const boardBgSrc = `${STAR_NAME}/reflection/draw/画板底图` // 切换布景后:移除不允许的素材 useEffect(() => { setSelectedMaterials((prev) => { const next = new Set([...prev].filter((k) => allowedMaterialKeys.includes(k))) return next.size === prev.size ? prev : next }) }, [allowedMaterialKeys]) useEffect(() => { if (showCaptionInput) captionInputRef.current?.focus() }, [showCaptionInput]) const handleToggleMaterial = (key: MaterialKey) => { setSelectedMaterials((prev) => { const next = new Set(prev) if (next.has(key)) { next.delete(key) return next } const conflicts = materialConflicts[key] ?? [] for (const c of conflicts) next.delete(c) if (next.size < 3) next.add(key) return next }) } const handleSubmitArtwork = async () => { if (!showCaptionInput) { setShowCaptionInput(true) return } if (isSubmitting || !compositionRef.current) return const filteredCaptionText = filterInput(captionText.trim()) if (Array.from(filteredCaptionText).length > 15) { setCaptionText(Array.from(filteredCaptionText).slice(0, 15).join('')) setSubmitErrorHintKey((key) => key + 1) return } setIsSubmitting(true) try { const dataUrl = await buildCompositeDataUrl(compositionRef.current) setDrawArtwork(dataUrl, filteredCaptionText) saveZhanglepingStore() setCreatedArtworkSrc(dataUrl) setShowCreatedOverlay(true) } catch (error) { console.error('Failed to submit draw artwork', error) setSubmitErrorHintKey((key) => key + 1) } finally { setIsSubmitting(false) } } const handleCollectArtwork = () => { navigate(`/${moduleName}/${STAR_NAME}/reflection5`) } const isPreviewVisible = activeBlock !== '任务提示' if (!moduleName) return null return (
{/* 中央预览区 */}
{isPreviewVisible && (
衣柜选项
)} {isPreviewVisible && materialOptions .filter((opt) => selectedMaterials.has(opt.key)) .map((opt) => (
{opt.key}
))}
{/* 顶部菜单 */}
{/* 任务提示窗口 */} {activeBlock === '任务提示' && (
①为三毛换装
②搭配1个新时代场景
③融入1-3个新时代元素
④配1句简短正能量文字(不超过15个字)
)} {/* 右侧侧边栏 */}
{sidebarBlocks.map((blk) => ( ))}
{/* 底部输入框 */} {showCaptionInput && (
setCaptionText(Array.from(filterInput(e.target.value)).slice(0, 15).join('')) } className={cn( 'w-260 h-24 bg-transparent', 'text-4xl! text-[#8B6914] leading-none placeholder:text-3xl!' )} autoComplete="off" placeholder="提交之前,请给你创作的我配一句简短的正能量文字吧!(不超过15个字)" />
)} {/* 底部按钮 */}
navigate(`/${moduleName}/${STAR_NAME}/draw`)} />
0} triggerKey={submitErrorHintKey} durationMs={3200}> 作品生成失败,请稍后再试 {createdArtworkSrc && ( { setShowCompletionDialogue(true) }} onClose={() => { setShowCreatedOverlay(false) }} footer={收藏} /> )} {COMPLETION_DIALOGUE}
) } export default ZhanglepingDraw1